Process concept Concept of Multiprogramming When there is a single program running in the CPU, it leads to the degradation of the CPU utilization. Example: When a running program initiates an I/O operation, the CPU remain idle until the I/O operation is completed. Solution to this problem is provided by Multiprogramming. Multiprogramming Continued.. Definition: A mode of operation that provides for the interleaved execution of two or more programs by a single processor. Multiprogramming Continued.. Improving CPU utilization By allowing several programs to reside in main memory at the “same time” the CPU might be shared, such that when one program initiates an I/O operation, another program can be assigned to the CPU, thus the improvement of the CPU utilization. Multiprogramming Continued.. Implementation The concept of process need to be introduced in order to understand multiprogramming and the concurrent execution of sequential processes in a computer system. What is a process? Process Definition: A program in execution An asynchronous activity The “locus of control” of a procedure in execution It is manifested by the existence of a process control block (PCB) in the operating system. Process States A state of a process describes the activity that the process is doing at a certain moment in time. New : A newly created process, not in the ready queue. Ready : It can use the CPU , if available. Running : If it is currently in the CPU. Waiting : Waiting for some event ex: I/O Abend : Stops executing due to an error. End : Finished executing properly. Suspended: Stops temporarily while the OS uses the CPU States of processes SUSPENDED END NEW READY RUNNING WAITING ABEND Causes of state change When a process executes, it changes states and interrupts cause process to change states. Current State New state Interrupt Running End EOP ( End of Program) Running (Abnormal end) Running (Start I/O) ABEND Trap Blocked for I/O System Call (SIO) Depiction of state change EOP END TRAP ABEND RUNNING SIO BLOCKED Process Continued… The activity of a process is controlled by a data structure called Process Control Block(PCB). A PCB is created every time a program is loaded to be executed. So, a process is defined by PCB-Program couple. Structure of PCB PCB contains information about processes, for instance: the current state of a process Unique identification of process Process priority Contents of some CPU registers Instruction Pointer (IP), also known as PC Base and limit registers Time limits and I/O status information Structure of PCB Contd… PROGRAM S T A C K Pointer to next PCB Process name or ID Base code Limit…. Code IP or PC Stack Pointer Registers State MODE Interrupt Flags .…. ….. Thread Control Block (TCB) Process Continued… A thread is known as “the unit of execution” of a process and it is represented by the Thread Control Block (TCB). The TCB consists of four fields: PC, stack pointer, the contents of some registers, and the state. Process Continued… We can now observe how each stage of a process takes place by the aid of a state diagrams. Process creation : An OS can create one or more processes, via a create-process system call. During the course of execution an user process may create new processes or threads as well. In this case, the creating process (thread) is called the parent and the created (new) process (thread) is named the child. Process Creation New Create Ready The process is created and then inserted at the back of the ready queue, it moves to the head of the queue according to a scheduling policy. Process Creation Contd… Program Load PCB OS code Create stack Process Creation (closer view) Cont… Create OS stack PCB heap bss Load Program file header data data code code Process working space Memory symbol table Disk Process working space (run-time environment) stack Dynamic link, return address, Local variables, function Parameters, … heap int z; bss int y = 7; data Dynamically allocated variables Global and static variables Constants / initialized data code Program text (write protected) malloc(n); x = 4; Process working space Process working space (run-time environment) Process working space sum Local bss: means “block started by symbol” and has that name for historical reasons. stack Local Local void sub(float total, int part ) { int List[5]; float sum; … Local } Local Local Parameter heap Parameter int z; bss Dynamic Link int y = 7; data malloc(n); x = 4; code Return Address Thread working space (run-time environment) Multithreading: Each thread is a unit of execution. stack-T1 stack-T2 stack-main Multithreaded processes need a stack per thread. All threads shared the same address space. Each thread has is own TCB. heap bss data code Thread working space (run-time environment) Multithreading: All threads share the CPU-PC and CPU-SP. In this picture thread-T1 is using the CPU. PCB stack-T1 stack-T2 CPU sp stack-main pc regs heap bss data code Process ID SP PC regs state TCB-T1 SP PC regs state TCB-T2 SP PC regs state TCB-main . . . Open files Other resources . . . Memory snap shot of two processes in Ready State INTERRUPT HANDLER DISPATCHER RUNTIME LIBRARY LOADER RUNNING READY PCB1 1 st a c k PCB2 2 NULL st a c k The PCB is stored in the OS memory area in a linked list. Ready to Running Dispatcher RUNNING READY Timer Interrupt When a process reaches the head of the queue and the CPU is available, the process is dispatched which means that the CPU is assigned to the process. This cause a transition from the ready state to the running state. When the time slice of the running process expires it goes back to the ready state. Ready to Running INTERRUPT HANDLER DISPATCHER RUNTIME LIBRARY LOADER RUNNING READY PCB1 1 st a c k PCB2 2 st a c k INTERRUPT FLAGS IP OV MP PI Accumulator TI I/O SVC MASK TO BE DEFINED LATER Mode CPU Ready to Running INTERRUPT HANDLER DISPATCHER RUNTIME LIBRARY LOADER RUNNING READY PCB1 1 st a c k PCB2 2 st a c k INTERRUPT FLAGS IP OV MP PI Accumulator TI I/O SVC PCB2 2 st a c k MASK TO BE DEFINED LATER Mode CPU Running to Ready LOADER INTERRUPT HANDLER DISPATCHER RUNTIME LIBRARY RUNNING READY PCB2 PCB1 1 st a c k INTERRUPT FLAGS IP OV MP PI Accumulator TI I/O SVC 2 st a c k MASK TO BE DEFINED LATER Mode CPU Running to Ready INTERRUPT HANDLER DISPATCHER RUNTIME LIBRARY LOADER RUNNING READY PCB2 2 st a c k st a c k 1 INTERRUPT FLAGS IP OV MP PCB2 PCB1 PI Accumulator TI I/O SVC 2 PCB1 st a c k 1 st a c k MASK TO BE DEFINED LATER Mode CPU Process Continued… As the OS switches the allocation of CPU among processes it uses the PCB to store the CPU information or context, which represents the state of the process. In the previous example we have seen how the OS performed a context switch between processes P2(from Running to Ready) and P1(from Ready to Running). When a context switch occurs we need to save the state of the running process in its PCB and load the state of the new process in the CPU. Case of Timer interrupt P2 P1 Dispatch RUNNING READY Timer Interrupt After a timer interrupt, the OS move P1 back to the ready state and the CPU Is assigned to P2. Context switching EXECUTING P1 OS P2 WAITING TIMER Save state into PCB1 WAITING WAITING Reload state from PCB2 DISPATCH WAITING EXECUTING Save state into PCB2 TIMER WAITING WAITING Reload state from PCB1 DISPATCH EXECUTING WAITING Case of I/O interrupt RUNNING READY Start I/O (System call) I/O Interrupt WAITING Case of I/O interrupt Contd.. LOADER INTERRUPT HANDLER DISPATCHER RUNTIME LIBRARY RUNNING READY PCB2 PCB1 PCB3 SIO I/O INTERRUPT FLAGS IP OV MP Accumulator PI TI I/O SVC MASK TO BE DEFINED LATER I/O DEVICE Case of I/O interrupt Contd.. LOADER INTERRUPT HANDLER DISPATCHER RUNTIME LIBRARY RUNNING READY PCB1 PCB2 PCB2 PCB3 PCB3 I/O INTERRUPT FLAGS IP OV MP Accumulator PI TI I/O SVC MASK TO BE DEFINED LATER I/O DEVICE Case of I/O interrupt Contd.. LOADER INTERRUPT HANDLER DISPATCHER RUNTIME LIBRARY READY OS PCB1 RUNNING PCB2 PCB3 I/O SUSPEND INTERRUPT FLAGS IP MASK I/O DEVICE OV MP Accumulator PI TI I/O SVC TO BE DEFINED LATER Case of I/O interrupt Contd.. LOADER INTERRUPT HANDLER DISPATCHER RUNTIME LIBRARY READY OS PCB1 PCB3 RUNNING PCB2 PCB3 I/O SUSPEND INTERRUPT FLAGS IP MASK I/O DEVICE OV MP Accumulator PI TI I/O SVC TO BE DEFINED LATER Context switching EXECUTING P3 OS P2 SIO Save state into PCB3 Put P3 into the I/O queue WAITING IN I/O QUEUE WAITING EXECUTING Reload state from PCB2 IN I/O STATE WAITING Suspend P2 READY STATE DISPATCH EXECUTING I/O INTERRUPT HANDLE I/O SUSPENDED Put P3 in Ready state Resume P2 READY STATE DISPATCH EXECUTING WAITING Handling I/O: Here the process waiting in the I/O queue is moved back to the ready state after the I/O request is completed. End of Process END EOP RUNNING TRAP ABEND PCB prog stack TRAP 1. ABORT PROGRAM 2. ERROR MESSAGE 3. FREE RESOURSES 4. DELETE PCB OS EOP 1. PROGRAM RUNS SUCCESSFULLY 2. FREE RESOURSES 3. DELETE PCB