Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3: Processes. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 7, 2006 Process Concept Process – a program.

Similar presentations


Presentation on theme: "Chapter 3: Processes. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 7, 2006 Process Concept Process – a program."— Presentation transcript:

1 Chapter 3: Processes

2 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 7, 2006 Process Concept Process – a program in execution; process execution must progress in sequential fashion A process includes: Executable Code (text) Stack Data section Heap Review MAR memory address register MDR memory data register IR current instruction PC next instruction Pseudo Code for OS Simulator //~ load pc into mar //~ increment pc //~ check to ensure that pc is legal //~ load mdr based upon mar //~ move mdr to ir //~ execute ir

3 3.3 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 7, 2006 Process State As a process executes, it changes state new: The process is being created running: Instructions are being executed waiting: The process is waiting for some event to occur ready: The process is waiting to be assigned to a processor terminated: The process has finished execution

4 3.4 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 7, 2006 Process Control Block (PCB) Information associated with each process Process state Program counter CPU registers CPU scheduling information Memory-management information Accounting information I/O status information

5 3.5 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 7, 2006 Context Switch 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 Context-switch time is overhead; the system does no useful work while switching Time dependent on hardware support

6 3.6 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 7, 2006 Process Scheduling Queues Job queue – set of all processes in the system Ready queue – set of all processes residing in main memory, ready and waiting to execute Device queues – set of processes waiting for an I/O device Processes migrate among the various queues

7 3.7 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 7, 2006 Schedulers Long-term scheduler (or job scheduler) – selects which processes should be brought into the ready queue Medium-term scheduler– selects which process should be removed from ready Q Short-term scheduler (or CPU scheduler) – selects which process should be executed next and allocates CPU There is an entire chapter on scheduling

8 3.8 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 7, 2006 Ability to spawn a new process Why might an operating system want to be able to do this? Why might an operating system want to be able to support/provide this service? Process A Spawn New process

9 3.9 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 7, 2006 Process Creation Parent process create children processes, which, in turn create other processes, forming a tree of processes Execution Parent and children execute concurrently Parent waits until children terminate

10 3.10 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 7, 2006 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); }

11 3.11 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 7, 2006 Cooperating Processes Independent process cannot affect or be affected by the execution of another process Cooperating process can affect or be affected by the execution of another process Advantages of process cooperation Information sharing Computation speed-up Modularity Convenience

12 3.12 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 7, 2006 Interprocess Communication (IPC) Why might an operating system want to provide this service? Mechanism for processes to communicate and to synchronize their actions Shared memory Message-passing Process A Process B

13 3.13 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 7, 2006 Shared Memory Models Producer- Consumer Problem Shared memory models Must keep from stepping on one another  Don’t write to a location that has not been read yet Known as the producer-consumer problem producer process produces information that is consumed by a consumer process

14 3.14 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 7, 2006 A Solution #define BUFFER_SIZE 10 typedef struct {... } item; item buffer[BUFFER_SIZE]; int in = 0; int out = 0; in points to the next free position out points to the first full position Empty when in == out Both producer and consumer can access in, out, and buffer

15 3.15 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 7, 2006 Producer while (true) { /* Produce an item */ while (( (in + 1) % BUFFER_SIZE) == out) ; /* do nothing -- no free buffers */ buffer[in] = item; in = (in + 1) % BUFFER SIZE; }

16 3.16 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 7, 2006 Consumer while (true) { while (in == out) ; // do nothing -- nothing to consume // remove an item from the buffer item = buffer[out]; // could set buffer[out] to some val // indicating that it is empty out = (out + 1) % BUFFER SIZE; // consume (do something with) item; }

17 3.17 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 7, 2006 Messaging-passing Direct Process AProcess B while (TRUE) {while (TRUE) { produce an item receive ( A, item ) send ( B, item ) consume item }} Indirect Messages are directed and received from mailboxes (also referred to as ports)  Each mailbox has a unique id  Processes can communicate only if they share a mailbox

18 3.18 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 7, 2006 Synchronization When Communicating Blocking is considered synchronous Blocking send has the sender block until the message is received Blocking receive has the receiver block until a message is available Non-blocking is considered asynchronous Non-blocking send has the sender send the message and continue Non-blocking receive has the receiver receive a valid message or null Example: checking an indirect message-passing mailbox to see if any messages; if none, continue execution Blocking or non-blocking?

19 3.19 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 7, 2006 Remote Procedure Calls Message-passing Blocking Similar to local procedure call To the calling process it appears as if the process “blocks” while waiting for the procedure to complete Process A Process B Call Result

20 3.20 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 7, 2006 Buffering Allows for speed mismatch 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 Buffer Process AProcess B

21 3.21 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 7, 2006 Long distance IPC Sockets TCP/IP A socket is defined as an endpoint for communication Concatenation of IP address and port The socket 161.25.19.8:1625 refers to port 1625 on host 161.25.19.8 Communication consists between a pair of sockets

22 End of Chapter 3


Download ppt "Chapter 3: Processes. 3.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts - 7 th Edition, Feb 7, 2006 Process Concept Process – a program."

Similar presentations


Ads by Google