Chapter 2 1 Processes Process concept Process scheduling

  • Slides: 15
Download presentation
Chapter 2. 1 : Processes • • • Process concept Process scheduling Interprocess communication

Chapter 2. 1 : Processes • • • Process concept Process scheduling Interprocess communication Deadlocks Threads Ceng 334 - Operating Systems 1

What is a Process? • A process (task) is the activity resulting from the

What is a Process? • A process (task) is the activity resulting from the execution of a program with its data on a sequential processor • A process is an abstract form of a program Ceng 334 - Operating Systems 2

Sequential Execution Start Program 1 Program 2 Program 3 Terminate • Programs are run

Sequential Execution Start Program 1 Program 2 Program 3 Terminate • Programs are run one after the other (run to completion) Ceng 334 - Operating Systems 3

Concurrent Execution Start Program 1 Terminate Start Program 2 Terminate Start Program 3 Terminate

Concurrent Execution Start Program 1 Terminate Start Program 2 Terminate Start Program 3 Terminate • All programs execute concurrently (share the time of the CPU) Ceng 334 - Operating Systems 4

How to Implement Concurrency? Start Code 1 Data 1 PCB 1 Start Code 2

How to Implement Concurrency? Start Code 1 Data 1 PCB 1 Start Code 2 Data 2 PCB 2 Start Code 3 Data 3 PCB 3 Terminate • Separate a program into code, data and environment (Process table) segments • Process table contains all the necessary data to stop and restart a process at any time Ceng 334 - Operating Systems 5

Process Table (PCB - Process Control Block) • • • Contents of registers Program

Process Table (PCB - Process Control Block) • • • Contents of registers Program counter PSW (Program Status Word) Process state Pointers to code and data Process identifier (PID) Process priority File descriptors Pointer to the parent of the process Pointers to all children of the process The processor it is running on Ceng 334 - Operating Systems 6

Switching Processes • Save the “real world” of the currently active process in process

Switching Processes • Save the “real world” of the currently active process in process table • Restore the “real world” from the process table of the selected process (the one to be executed next) Ceng 334 - Operating Systems 7

System Process Tables Process tables Process Code Data Stack Ceng 334 - Operating Systems

System Process Tables Process tables Process Code Data Stack Ceng 334 - Operating Systems 8

Shared Code PCB 2 PCB 1 Shared Code Data 1 Data 2 • The

Shared Code PCB 2 PCB 1 Shared Code Data 1 Data 2 • The shared code must be re-entrant (ie. , it must not modify itself) Ceng 334 - Operating Systems 9

Process States PREEMPT Create Ready CONTINUE Blocked SCHEDULE Running SUSPEND Terminate Ceng 334 -

Process States PREEMPT Create Ready CONTINUE Blocked SCHEDULE Running SUSPEND Terminate Ceng 334 - Operating Systems 10

Process States • Create – A process is created by an existing process (parent/child);

Process States • Create – A process is created by an existing process (parent/child); a process is created by a service, or as a new batch job, or an interactive logon • Terminate – Normal completion (exit); External completion (forced completion: by operator, by parent); Internal completion (error, time overrun) • Schedule – A process is selected out of the ready queue (is scheduled) and dispatched to the running state • Preempt – The running process is preempted because some other process of higher priority has become ready (or it yields) Ceng 334 - Operating Systems 11

Process States • Suspend – A process starts a time consuming IO operation, or

Process States • Suspend – A process starts a time consuming IO operation, or is swapped out, or makes a spontaneous request to sleep, or is blocked in a synchronization operation. When a process is suspended, it is on some queue, namely the queue of all the processes waiting for the same resource. Processes during their life move from queue to queue, with stays in the running state • Continue – The inverse of Suspension: the resource requested becomes available (timer, IO completes, unblocking) Ceng 334 - Operating Systems 12

How to Create New Processes? Fork & Exec • fork – create a new

How to Create New Processes? Fork & Exec • fork – create a new process that is a copy of current one • exec – change the program that a process is executing • Some systems combine the two into one Ceng 334 - Operating Systems 13

void main() { int child; if ( (child = return( -1 else if (

void main() { int child; if ( (child = return( -1 else if ( child==0 { printf( "This exit(1); } printf( "This } fork())==-1 ) ); ) is the child executingn"); is the parentn" ); Ceng 334 - Operating Systems 14

Parent Child Relationship root p 3 p 1 p 2 p 4 p 5

Parent Child Relationship root p 3 p 1 p 2 p 4 p 5 • root is the parent • p 1, p 2 and p 3 are children of root process • p 4 and p 5 are children of p 3 Ceng 334 - Operating Systems 15