Chapter 3 Processes Chapter 3 Processes n Process

  • Slides: 34
Download presentation
Chapter 3: Processes

Chapter 3: Processes

Chapter 3: Processes n Process Concept n Process Scheduling n Operations on Processes n

Chapter 3: Processes n Process Concept n Process Scheduling n Operations on Processes n Cooperating Processes n Interprocess Communication Operating System Concepts 3. 2 Silberschatz, Galvin and Gagne © 2005

Process Concept n n n A process is a program in execution. It is

Process Concept n n n A process is a program in execution. It is a unit of work within the system. Program is a passive entity, process is an active entity. Process needs resources to accomplish its task l CPU, memory, I/O, files l Initialization data Process termination requires reclaim of any reusable resources Single-threaded process has one program counter specifying location of next instruction to execute l Process executes instructions sequentially, one at a time, until completion Multi-threaded process has one program counter per thread Typically system has many processes, some user, some operating system running concurrently on one or more CPUs l Concurrency by multiplexing the CPUs among the processes / threads Operating System Concepts 3. 3 Silberschatz, Galvin and Gagne © 2005

Process Concept n An operating system executes a variety of programs: Batch system –

Process Concept n An operating system executes a variety of programs: Batch system – jobs l Time-shared systems – user programs or tasks n Textbook uses the terms job and process almost interchangeably n Process – a program in execution; process execution must progress in sequential fashion n A process includes: l program counter l stack l l data section Operating System Concepts 3. 4 Silberschatz, Galvin and Gagne © 2005

Multiprogramming needed for efficiency Single user cannot keep CPU and I/O devices busy at

Multiprogramming needed for efficiency Single user cannot keep CPU and I/O devices busy at all times l Multiprogramming organizes jobs (code and data) so CPU always has one to execute l A subset of total jobs in system is kept in memory l One job selected and run via job scheduling l When it has to wait (for I/O for example), OS switches to another job n Timesharing (multitasking) is logical extension in which CPU switches jobs so frequently that users can interact with each job while it is running, creating interactive computing l Response time should be < 1 second l Each user has at least one program executing in memory process l If several jobs ready to run at the same time CPU scheduling l If processes don’t fit in memory, swapping moves them in and out to run l Virtual memory allows execution of processes not completely in memory l Operating System Concepts 3. 5 Silberschatz, Galvin and Gagne © 2005

Memory Layout for Multiprogrammed System Operating System Concepts 3. 6 Silberschatz, Galvin and Gagne

Memory Layout for Multiprogrammed System Operating System Concepts 3. 6 Silberschatz, Galvin and Gagne © 2005

Process Management Activities The operating system is responsible for the following activities in connection

Process Management Activities The operating system is responsible for the following activities in connection with process management: n Creating and deleting both user and system processes n Suspending and resuming processes n Providing mechanisms for process synchronization n Providing mechanisms for process communication n Providing mechanisms for deadlock handling Operating System Concepts 3. 7 Silberschatz, Galvin and Gagne © 2005

Process State n As a process executes, it changes state l new: The process

Process State n As a process executes, it changes state l new: The process is being created l running: Instructions are being executed l waiting: The process is waiting for some event to occur l ready: The process is waiting to be assigned to a process l terminated: The process has finished execution Operating System Concepts 3. 8 Silberschatz, Galvin and Gagne © 2005

Diagram of Process State Operating System Concepts 3. 9 Silberschatz, Galvin and Gagne ©

Diagram of Process State Operating System Concepts 3. 9 Silberschatz, Galvin and Gagne © 2005

Process Control Block (PCB) Each process has a process control block which includes: n

Process Control Block (PCB) Each process has a process control block which includes: n Process state n Program counter (PC) n CPU registers n CPU scheduling information n Memory-management information n Accounting information n I/O status information Operating System Concepts 3. 10 Silberschatz, Galvin and Gagne © 2005

Process Control Block (PCB) Operating System Concepts 3. 11 Silberschatz, Galvin and Gagne ©

Process Control Block (PCB) Operating System Concepts 3. 11 Silberschatz, Galvin and Gagne © 2005

CPU Switch From Process to Process Operating System Concepts 3. 12 Silberschatz, Galvin and

CPU Switch From Process to Process Operating System Concepts 3. 12 Silberschatz, Galvin and Gagne © 2005

Context Switch n When CPU switches to another process, the system must save the

Context Switch n When CPU switches to another process, the system must save the state of the old process and load the saved state for the new process n Context-switch time is overhead; the system does no useful work while switching n Time dependent on hardware support Operating System Concepts 3. 13 Silberschatz, Galvin and Gagne © 2005

Process Creation n A parent process creates children processes by system calls, which, in

Process Creation n A parent process creates children processes by system calls, which, in turn create other processes, forming a tree of processes n Resource sharing l Parent and children share all resources l Children share subset of parent’s resources l Parent and child share no resources n Execution l Parent and children execute concurrently l Parent waits until children terminate Operating System Concepts 3. 14 Silberschatz, Galvin and Gagne © 2005

Process Creation (Cont. ) n Address space l Child duplicate of parent l Child

Process Creation (Cont. ) n Address space l Child duplicate of parent l Child has a program loaded into it n UNIX examples l fork system call creates new process l exec system call used after a fork to replace the process’ memory space with a new program Operating System Concepts 3. 15 Silberschatz, Galvin and Gagne © 2005

Process Creation Operating System Concepts 3. 16 Silberschatz, Galvin and Gagne © 2005

Process Creation Operating System Concepts 3. 16 Silberschatz, Galvin and Gagne © 2005

C Program Forking Separate Process int main() { Pid_t pid; /* fork another process

C Program Forking Separate Process int main() { Pid_t pid; /* fork another process */ pid = fork(); if (pid < 0) { /* error occurred */ fprintf(stderr, "Fork Failed"); exit(-1); } else if (pid == 0) { /* child process */ execlp("/bin/ls", "ls", NULL); } else { /* parent process */ /* parent will wait for the child to complete */ wait (NULL); printf ("Child Complete"); exit(0); } } Operating System Concepts 3. 17 Silberschatz, Galvin and Gagne © 2005

Process Termination n Process executes last statement and asks the operating system to delete

Process Termination n Process executes last statement and asks the operating system to delete it (exit) l Output data from child to parent (via wait) l Process’ resources are deallocated by operating system n Parent may terminate execution of children processes (abort) l Child has exceeded allocated resources l Task assigned to child is no longer required l If parent is exiting 4 Some operating system do not allow child to continue if its parent terminates – Operating System Concepts All children terminated - cascading termination 3. 18 Silberschatz, Galvin and Gagne © 2005

Cooperating Processes n Independent process cannot affect or be affected by the execution of

Cooperating Processes n Independent process cannot affect or be affected by the execution of another process n Cooperating process can affect or be affected by the execution of another process n Advantages of process cooperation l Information sharing l Computation speed-up l Modularity l Convenience n Two interprocess communication models l Shared-memory model l Message-passing model Operating System Concepts 3. 19 Silberschatz, Galvin and Gagne © 2005

Two Interprocesss Communications Models a. message-passing model Operating System Concepts b. shared-memory model 3.

Two Interprocesss Communications Models a. message-passing model Operating System Concepts b. shared-memory model 3. 20 Silberschatz, Galvin and Gagne © 2005

Shared-Memory Model for IPC n Two or more processes share a common memory region.

Shared-Memory Model for IPC n Two or more processes share a common memory region. Processes can exchange information by reading and writing data to the shared region. n Advantages l Allow maximum spend l No assistance from the kernel is required n Disadvantages l Difficult to implement l No protection from one process accessing another process’s memory Operating System Concepts 3. 21 Silberschatz, Galvin and Gagne © 2005

Producer-Consumer Problem n Paradigm for cooperating processes, producer process produces information that is consumed

Producer-Consumer Problem n Paradigm for cooperating processes, producer process produces information that is consumed by a consumer process l unbounded-buffer places no practical limit on the size of the buffer l bounded-buffer assumes that there is a fixed buffer size Operating System Concepts 3. 22 Silberschatz, Galvin and Gagne © 2005

Bounded-Buffer – Shared-Memory Solution n Shared data #define BUFFER_SIZE 10 Typedef struct {. .

Bounded-Buffer – Shared-Memory Solution n Shared data #define BUFFER_SIZE 10 Typedef struct {. . . } item; item buffer[BUFFER_SIZE]; int in = 0; int out = 0; In points the next free position in the buffer l Out points the first full position in the buffer l The buffer is empty when in==out l The buffer is full when in+1 mod BUFFER_SIZE==out. l Operating System Concepts 3. 23 Silberschatz, Galvin and Gagne © 2005

Bounded-Buffer – Insert() Method while (true) { /* Produce an item */ while (((in

Bounded-Buffer – Insert() Method while (true) { /* Produce an item */ while (((in = (in + 1) % BUFFER SIZE count) == out) ; /* do nothing -- no free buffers */ buffer[in] = item; in = (in + 1) % BUFFER SIZE; { Operating System Concepts 3. 24 Silberschatz, Galvin and Gagne © 2005

Bounded Buffer – Remove() Method while (true) { while (in == out) ; //

Bounded Buffer – Remove() Method while (true) { while (in == out) ; // do nothing -- nothing to consume // remove an item from the buffer item = buffer[out]; out = (out + 1) % BUFFER SIZE; return item; { Operating System Concepts 3. 25 Silberschatz, Galvin and Gagne © 2005

Message-Passing Model for IPC n Communication takes place by message exchanged between the cooperating

Message-Passing Model for IPC n Communication takes place by message exchanged between the cooperating processes n It is easy to implement, but low speed, and needs kernel intervention. n IPC facility provides two operations: l send(message) – message size fixed or variable l receive(message) n If P and Q wish to communicate, they need to: l establish a communication link between them l exchange messages via send/receive n Implementation of communication link physical (e. g. , shared memory, hardware bus) l logical (e. g. , logical properties) l Operating System Concepts 3. 26 Silberschatz, Galvin and Gagne © 2005

Implementation Methods n Direct or indirect communication. n Synchronous or asynchronous communication n Automatic

Implementation Methods n Direct or indirect communication. n Synchronous or asynchronous communication n Automatic or explicit buffering Operating System Concepts 3. 27 Silberschatz, Galvin and Gagne © 2005

Direct Communication n Processes must name each other explicitly: l Send (P, message) –

Direct Communication n Processes must name each other explicitly: l Send (P, message) – send a message to process P l Receive (Q, message) – receive a message from process Q n Properties of communication link l Links are established automatically l A link is associated with exactly one pair of communicating processes l Between each pair there exists exactly one link Operating System Concepts 3. 28 Silberschatz, Galvin and Gagne © 2005

Indirect Communication n Messages are directed and received from mailboxes (also referred to as

Indirect Communication n Messages are directed and received from mailboxes (also referred to as ports) l Each mailbox has a unique id l Processes can communicate only if they share a mailbox n Properties of communication link l Link established only if processes share a common mailbox l A link may be associated with many processes l Each pair of processes may share several communication links, with each link corresponding to one mailbox Operating System Concepts 3. 29 Silberschatz, Galvin and Gagne © 2005

Indirect Communication n Mailboxes are owned by OS n Operations l create a new

Indirect Communication n Mailboxes are owned by OS n Operations l create a new mailbox l send and receive messages through mailbox l destroy a mailbox n Primitives are defined as: send(A, message) – send a message to mailbox A receive(A, message) – receive a message from mailbox A Operating System Concepts 3. 30 Silberschatz, Galvin and Gagne © 2005

Indirect Communication n Mailbox sharing l P 1, P 2, and P 3 share

Indirect Communication n Mailbox sharing l P 1, P 2, and P 3 share mailbox A l P 1, sends; P 2 and P 3 receive l Who gets the message? n Solutions l Allow a link to be associated with at most two processes l Allow only one process at a time to execute a receive operation l Allow the system to select arbitrarily the receiver. Sender is notified who the receiver was. Operating System Concepts 3. 31 Silberschatz, Galvin and Gagne © 2005

Synchronization n Message passing may be either blocking or non-blocking n Blocking is considered

Synchronization n Message passing may be either blocking or non-blocking n Blocking is considered synchronous n l Blocking send has the sender block until the message is received l Blocking receive has the receiver block until a message is available Non-blocking is considered asynchronous l Non-blocking send has the sender send the message and continue l Non-blocking receive has the receiver receive a valid message or null Operating System Concepts 3. 32 Silberschatz, Galvin and Gagne © 2005

Buffering n Queue of messages attached to the link; implemented in one of three

Buffering n Queue of messages attached to the link; implemented in one of three ways 1. Zero capacity – 0 messages Sender must wait for receiver (rendezvous) 2. Bounded capacity – finite length of n messages Sender must wait if link full 3. Unbounded capacity – infinite length Sender never waits Operating System Concepts 3. 33 Silberschatz, Galvin and Gagne © 2005

End of Chapter 3

End of Chapter 3