4

Okay as we know that a single processor can execute one instruction at one time, which means a single processor can execute either the Operating system's instruction or the user program's instruction at one time.

Now how is it possible that an operating system, and a user program can run at the same time in single processor?

Is cpu assigned to a user program when you open the program and when you close the user program the cpu is assigned back to the Operating system ??

Ahti
  • 51

1 Answers1

7

Time-slicing. The CPU switches between tasks every few milliseconds, so it appear to the observer that its doing two or more things at once. There is also the fact that the OS has some control over how threads are granted execution time, so it can stack the deck, but thread scheduling is beyond anything I'm willing to get into with this answer.

CPUs run processes as one or more threads of instructions, and each thread represents its own "context" for the instructions it is performing (register values, etc).

CPUs pipeline the instructions of multiple threads into a queue, and execute them in order. When an instruction from a different thread arrives at the front of the queue, the CPU performs a "context switch" to pause one thread and execute instructions from another. This is necessary so that the CPU can run multiple threads seemingly simultaneously, and for handling interrupts that occur while the CPU is performing other tasks.

You specifically mention the OS and user programs. To the extent that the OS user interface remains responsive to the user while a program is running, that's all about time-slicing, but of equal importance is what happens when the program uses OS functions to do tasks. As @Techie007 pointed out, the distinction between OS and User programs is diminished when OS services are doing half or all of the work. This blurring goes even deeper than that when programs link to system libraries for functionality. When a program uses an OS call to perform a function, the actual machine code of that function is loaded into RAM as part of the thread's stream of instructions, so the user program thread is actually executing the OS's code, not the other way around!

Frank Thomas
  • 36,135