Process concept Process concept Run time environment Process

  • Slides: 57
Download presentation
Process concept • Process concept. • Run time environment. • Process implementation. • Thread

Process concept • Process concept. • Run time environment. • Process implementation. • Thread concept and implementation. • Process Synchronization

Concept of Multiprogramming n When there is a single program running in the CPU,

Concept of Multiprogramming n When there is a single program running in the CPU, it leads to the degradation of the CPU utilization. n 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

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

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

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 n Definition: Ø Ø A program in execution An asynchronous activity The “locus

Process n 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

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 (Please label the arrows naming the event) SUSPENDED END NEW READY

States of processes (Please label the arrows naming the event) SUSPENDED END NEW READY RUNNING WAITING I/O ABEND

Causes of state change When a process executes, it changes states and interrupts cause

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 Waiting for I/O System Call (SIO)

Depiction of state change EOP END TRAP ABEND RUNNING SIO WAITING FOR I/O

Depiction of state change EOP END TRAP ABEND RUNNING SIO WAITING FOR I/O

Process Continued… n The activity of a process is controlled by a data structure

Process Continued… n The activity of a process is controlled by a data structure called Process Control Block(PCB). n A PCB is created every time a program is loaded to be executed. n So, a process is defined by PCB-Program couple.

Structure of PCB contains information about processes, for instance: Ø the current state of

Structure of 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… RUN TIME ENVIRONMENT STACK CODE Pointer to next PCB Process

Structure of PCB Contd… RUN TIME ENVIRONMENT STACK CODE 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

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

Process Continued… We can now observe how each stage of a process takes place by the aid of a state diagrams. Process creation : n An OS can create one or more processes, via a create-process system call. n 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

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 Contd… Program Load PCB OS code Create stack

Process Creation (closer view) Cont… Create PCB stack heap bss OS Load Program file

Process Creation (closer view) Cont… Create PCB stack heap bss OS Load Program file header data code Process working space Memory symbol table Disk

Process working space (run-time environment) stack Dynamic link, return address, Local variables, function Parameters,

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

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 void sub(float total, int part ) { int List[5]; float sum; … 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-T

Thread working space (run-time environment) Multithreading: Each thread is a unit of execution. stack-T 1 stack-T 2 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

Thread working space (run-time environment) Multithreading: All threads share the CPU-PC and CPU-SP. In this picture thread-T 2 is using the CPU. PCB stack-T 1 stack-T 2 CPU sp stack-main pc regs heap bss data code Process ID SP PC regs state TCB-T 1 SP PC regs state TCB-T 2 SP PC regs state TCB-main . . . Open files Other resources. . .

Thread working space (run-time environment) Multithreading: All threads share the CPU-PC and CPU-SP. In

Thread working space (run-time environment) Multithreading: All threads share the CPU-PC and CPU-SP. In this picture thread-T 1 is using the CPU. PCB stack-T 1 stack-T 2 CPU sp stack-main pc regs heap bss data code Process ID SP PC regs state TCB-T 1 SP PC regs state TCB-T 2 SP PC regs state TCB-main . . . Open files Other resources. . .

Ready to Running Dispatcher RUNNING READY Timer Interrupt When a process reaches the head

Ready to Running Dispatcher RUNNING READY Timer Interrupt When a process reaches the head of the ready 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.

Process Continued… As the OS switches the allocation of CPU among processes it uses

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 P 2(from Running to Ready) and P 1(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 P 2 P 1 Dispatch RUNNING READY Timer Interrupt After

Case of Timer interrupt P 2 P 1 Dispatch RUNNING READY Timer Interrupt After a timer interrupt, the OS move P 1 back to the ready state and the CPU Is assigned to P 2.

Context switching EXECUTING P 1 OS P 2 READY TIMER Save state into PCB

Context switching EXECUTING P 1 OS P 2 READY TIMER Save state into PCB 1 READY Reload state from PCB 2 DISPATCH READY EXECUTING Save state into PCB 2 READY TIMER READY Reload state from PCB 1 EXECUTING DISPATCH READY

Case of I/O interrupt RUNNING READY Start I/O (System call) I/O Interrupt WAITING

Case of I/O interrupt RUNNING READY Start I/O (System call) I/O Interrupt WAITING

Context switching EXECUTING P 3 OS Save state into PCB 3 Put P 3

Context switching EXECUTING P 3 OS Save state into PCB 3 Put P 3 into the I/O queue WAITING IN I/O QUEUE WAITING Suspend P 2 READY STATE SIO READY EXECUTING Reload state from PCB 2 IN I/O STATE P 2 DISPATCH EXECUTING I/O INTERRUPT HANDLE I/O Put P 3 in Ready state Resume P 2 READY STATE SUSPENDED RESUME EXECUTING WAITING

n Handling I/O: Here the process waiting in the I/O queue is moved back

n 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.

Process Synchronization

Process Synchronization

Concurrency n Definition: Two or more processes execute concurrently when they execute different activities

Concurrency n Definition: Two or more processes execute concurrently when they execute different activities on different devices at the same time.

Concurrency Contd. . RUNNING READY Process 3 WAIT ON DEVICE 1 WAIT ON DEVICE

Concurrency Contd. . RUNNING READY Process 3 WAIT ON DEVICE 1 WAIT ON DEVICE 2 Process 1

Concurrency Contd. . n In a multiprogramming system CPU time is multiplexed among a

Concurrency Contd. . n In a multiprogramming system CPU time is multiplexed among a set of processes. n Users like to share their programs and data and the OS must keep information integrity. n Processes are allowed to use shared data through threads. The concurrent access to shared data may result in data inconsistency.

Concurrency Contd. . Example : Consider a shared variable X =4 and two programs

Concurrency Contd. . Example : Consider a shared variable X =4 and two programs running concurrently. Let us assume that P 1 is running and a timer interrupt occurs after executing “Load X”. P 1 P 2 { { X X+10 Store X X X+2 Store X Load X Timer interrupt } }

Concurrency Contd. . Once P 1 is interrupted, The OS gathers the context and

Concurrency Contd. . Once P 1 is interrupted, The OS gathers the context and saves it in the PCB associated to P 1, say PCB 1. The value “ 4” is loaded from one of the CPU registers into PCB 1. As the CPU is idle, The OS assigns it to process two and P 2 starts execution, enters the critical section and loads the value “ 4” from the shared variable “X”. In its critical section P 2 executes “X = X + 2” and run to completion leaving behind variable “X = 6”. After process P 2 finishes execution, P 1 resumes execution restoring the value “ 4” in a CPU register, and the value of X becomes X = 4 + 10 =14. The final result will be “ 14” (which is wrong)!

Concurrency Contd. . n Consider the case when P 1 executes completely. The value

Concurrency Contd. . n Consider the case when P 1 executes completely. The value of X will become: X = 4 + 10 = 14 The process P 2 executes and the value of X will be changed to: X = 14 + 2 = 16 (the correct answer)

Concurrency Contd. . n Here there are two different values for the same variable

Concurrency Contd. . n Here there are two different values for the same variable X depending on the order of execution of the instructions. n This is called a Race Condition. n It occurs when processes access shared variables without using an appropriate synchronization mechanism.

Race Condition n Definition: A race condition is an undesirable situation that occurs when

Race Condition n Definition: A race condition is an undesirable situation that occurs when two or more operations manipulate shared data concurrently and the outcome depends on the particular order the operations occur. In order to avoid a race condition, it is to necessary to ensured that only one process, at a time, has exclusive access to the shared data.

Race Condition Contd. . The prevention of other process from accessing a shared variable,

Race Condition Contd. . The prevention of other process from accessing a shared variable, while one process is accessing it, is called mutual exclusion In order to guarantee mutual exclusion we need some kind of synchronization mechanism. In most synchronization schemes a physical entity must be used to represent a resource. This entity is often called Lock Byte or Semaphore.

Process Synchronization n Concept of Critical Section: A Critical Section is the segment of

Process Synchronization n Concept of Critical Section: A Critical Section is the segment of code where a shared variable is used. If several processes are accessing a shared variable when one process is in its critical section, no other process is allowed to enter its critical section.

Process Synchronization contd. . n n Each process must request permission to enter the

Process Synchronization contd. . n n Each process must request permission to enter the critical section (CS). A solution to CS problem must satisfy the following requirements 1. Mutual exclusion 2. Progress

Process Synchronization contd. . n n Mutual exclusion: When a process is executing in

Process Synchronization contd. . n n Mutual exclusion: When a process is executing in the critical section other processes can not execute their critical sections. Progress: If no process is executing in its critical section and there are processes that wish to enter the critical section, only one of them can enter the critical section.

Process Synchronization contd. . n Test and Set Before entering the critical section we

Process Synchronization contd. . n Test and Set Before entering the critical section we need to execute a Lock(x) operation and an Unlock(x) operation before leaving the CS. P 1 P 2. . Lock(x) { { CS CS } } Unlock(x)

Process Synchronization contd. . n If a system implements Test and Set as a

Process Synchronization contd. . n If a system implements Test and Set as a hardware instruction, we can implement mutual exclusion with the help of a Boolean variable, TS, that is initialized to “ 0” and two operations. Lock Unlock Label: If TS = 1 then goto Label TS 0 else TS 1 This is implemented in hardware

Process Synchronization contd. . n The main disadvantage here is that when one process

Process Synchronization contd. . n The main disadvantage here is that when one process is in the critical section all other processes only used the CPU to execute Test and Set. n This is called busy waiting. n To overcome this problem the concept of Semaphores was proposed by Dijkstra in his work E. W. Dijkstra. Cooperating Sequential Processes. Programming Languages (F. Genuys, ed. ), Academic Press, New York, 1968

Concept of Semaphores n Semaphores: A semaphore S combines an integer variable and a

Concept of Semaphores n Semaphores: A semaphore S combines an integer variable and a pointer that can be is accessed only through two standard “atomic” operations. ØP(s) ØV(s)

Concept of Semaphores n When a process executes a P(s) operation and finds that

Concept of Semaphores n When a process executes a P(s) operation and finds that the semaphore value is not positive the process blocks itself, and the OS places the process in the semaphore waiting queue. n The process will be restarted when some other process executed the V(s) operation, which changes the process state from waiting to ready.

Semaphores contd. . n The operations were originally named as: P means Wait V

Semaphores contd. . n The operations were originally named as: P means Wait V means Signal S value Semaphore queue P C B

Semaphores contd. . n The semaphore operations can be defined as follows P(S) :

Semaphores contd. . n The semaphore operations can be defined as follows P(S) : inhibit interrupts S. value : = S. value -1 if S. value < 0 then { add this process to S. queue } end; enable interrupts

Semaphores contd. . V(S): inhibit interrupts S. value : =S. value+1 if S. value<=0

Semaphores contd. . V(S): inhibit interrupts S. value : =S. value+1 if S. value<=0 then { remove a process from S. queue add process to Ready queue } end; enable interrupts

Masking Interrupts… n We need to mask the interrupts while the P(s) or the

Masking Interrupts… n We need to mask the interrupts while the P(s) or the V(s) operations are executed. n Thus the current sequence of instructions would be allowed to execute without preemption.

Semaphores contd. . n Algorithms for P and V operations n P(S) 1. Decrement

Semaphores contd. . n Algorithms for P and V operations n P(S) 1. Decrement value of S by 1 2. If S<0 then - Find current process descriptor - Remove from processor queue - Add to semaphore queue 3. Call Dispatcher

Semaphores contd. . n V(S) 1. Increment value of S by 1 2. If

Semaphores contd. . n V(S) 1. Increment value of S by 1 2. If S<=0 then -Dequeue some process descriptor from semaphore queue - Add the process to ready queue 3. Call Dispatcher

Semaphores contd. . n P 1 P 2 P(S) CS V(S) Mutual exclusion implementation

Semaphores contd. . n P 1 P 2 P(S) CS V(S) Mutual exclusion implementation with semaphores

Bounded Buffer Producer/Consumer While (TRUE){ Produce item P(full) P(mutex) Put item in Buffer V(mutex)

Bounded Buffer Producer/Consumer While (TRUE){ Produce item P(full) P(mutex) Put item in Buffer V(mutex) V(empty) } While (TRUE){ P(empty) P(mutex) Get item from Buffer V(mutex) V(full) Consume Item } Initial semaphore values: full = buffer size; empty = 0; mutex = 1;

Process concept THE END

Process concept THE END