Operating Systems · Unit 2
Process ManagementA computer runs many programs at once, but a processor core can execute only one instruction stream at a time. Process management is the part of the operating system that creates the illusion of everything running together, and decides who gets the CPU next. 1. What is a process?A program is a file stored on disk: a passive list of instructions. A process is that program in execution. When you open the same program twice, the disk still holds one program, but the operating system now manages two separate processes, each with its own memory and its own position in the code. DefinitionA process is a program in execution, together with its current activity: the program counter, the CPU register values, the stack, the data section and the heap.
In memory a process is laid out in four parts:
2. Process statesA process does not run from start to finish without stopping. It moves between five states as it waits for the CPU or for input and output.
Exam tipA waiting process never goes straight back to Running. It always returns to Ready first and waits to be dispatched again. This is a frequent one-mark question.
3. Process control block (PCB)The operating system keeps one record for every process, called the process control block. When the CPU is taken from one process and given to another, the first process's details are saved in its PCB and the second process's details are loaded from its PCB. This is a context switch. A PCB holds:
A context switch does no useful work for the user, so its time is pure overhead. A scheduler that switches too often wastes the CPU on saving and loading PCBs. 4. Process vs threadA thread is a single path of execution inside a process. Threads of the same process share its code, data and open files, but each thread has its own stack and registers.
5. FCFS scheduling, solvedFirst Come First Served gives the CPU to processes in the order they arrive. It is non-preemptive: once a process starts, it runs until it finishes. FormulasTurnaround time = Completion time − Arrival time
Waiting time = Turnaround time − Burst time ExampleThree processes arrive at time 0 in the order P1, P2, P3 with burst times 5, 3 and 8 ms. Find the average waiting time and the average turnaround time.
Average waiting time = (0 + 5 + 8) ÷ 3 = 4.33 ms If the short process P2 had run first, both averages would be lower. Short processes stuck behind a long one is called the convoy effect, and it is the main weakness of FCFS. 6. Quick revision
7. Practice questionsQ1. Which part of a process's memory holds local variables of a function? Answer: The stack. Q2. Processes P1, P2, P3 arrive at time 0 with burst times 6, 2 and 4 ms. Find the average waiting time under FCFS. Answer: Waiting times are 0, 6 and 8, so the average is 14 ÷ 3 = 4.67 ms. Q3. Why is creating a thread faster than creating a process? Answer: A new thread reuses the address space, code and open files of its process; only a stack and registers are created. |