Published on

Published in: Technology, Business
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. Chapter 3: Processes
  2. 2. Chapter 3: Processes s Process Concept s Process Scheduling s Operations on Processes s Cooperating Processes s Interprocess Communication s Communication in Client-Server SystemsOperating System Concepts 3.2 Silberschatz, Galvin and Gagne
  3. 3. Process Concept s An operating system executes a variety of programs: q Batch system – jobs q Time-shared systems – user programs or tasks s Textbook uses the terms job and process almost interchangeably s Process – a program in execution; process execution must progress in sequential fashion s A process includes: q program counter which represents current activity q Stack which holds temporary data such as function parameters, local variables and return addresses. q data section which contains global variables q Heap which contains memory that is dynamically allocated during process runtime. q Text includes the applications code, might be shared by a number of processes? q The processs address space.Operating System Concepts 3.3 Silberschatz, Galvin and Gagne
  4. 4. Process in MemoryOperating System Concepts 3.4 Silberschatz, Galvin and Gagne
  5. 5. Process State s As a process executes, it changes state q new: The process is being created q running: Instructions are being executed q waiting: The process is waiting for some event to occur q ready: The process is waiting to be assigned to a processor q terminated: The process has finished executionOperating System Concepts 3.5 Silberschatz, Galvin and Gagne
  6. 6. Diagram of Process StateOperating System Concepts 3.6 Silberschatz, Galvin and Gagne
  7. 7. Process Control Block (PCB) Information associated with each process s Process state s Program counter indicates the address of next instruction. s CPU registers s CPU scheduling information includes process priority and pointers to scheduling queues. s Memory-management information includes value of limit and base registers s Accounting information includes amount of CPU time used, time limits etc. s I/O status information includes the list of I/O devices allocated to processes, list of open filesOperating System Concepts 3.7 Silberschatz, Galvin and Gagne
  8. 8. Process Control Block (PCB)Operating System Concepts 3.8 Silberschatz, Galvin and Gagne
  9. 9. OS Data PCB Process Structures Address Space RAM CPU Kernel User PSW State text IR Memory . Files Accounting PC Priority data User SP CPU register storage heap General Purpose Registers stack 9Operating System Concepts 3.9 Silberschatz, Galvin and Gagne
  10. 10. CPU Switch From Process to ProcessOperating System Concepts 3.10 Silberschatz, Galvin and Gagne
  11. 11. Process Scheduling Queues s Job queue – set of all processes in the system s Ready queue q set of all processes residing in main memory, ready and waiting to execute. q It is stored as a linked list. It contains pointers to the first and final PCB’s. q Each PCB includes a pointer field that points to the next PCB in the ready queue. s Device queues – set of processes waiting for an I/O device s Processes migrate among the various queuesOperating System Concepts 3.11 Silberschatz, Galvin and Gagne
  12. 12. QueuesOperating System Concepts 3.12 Silberschatz, Galvin and Gagne
  13. 13. Representation of Process SchedulingOperating System Concepts 3.13 Silberschatz, Galvin and Gagne
  14. 14. Schedulers s Long-term scheduler (or job scheduler) – selects which processes should be brought into the ready queue s Short-term scheduler (or CPU scheduler) – selects which process should be executed next and allocates CPU s Medium term Scheduler (Swaper)Operating System Concepts 3.14 Silberschatz, Galvin and Gagne
  15. 15. Addition of Medium Term SchedulingOperating System Concepts 3.15 Silberschatz, Galvin and Gagne
  16. 16. Schedulers (Cont.) s Short-term scheduler is invoked very frequently (milliseconds) ⇒ (must be fast) s Long-term scheduler is invoked very infrequently (seconds, minutes) ⇒ (may be slow) s The long-term scheduler controls the degree of multiprogramming s Processes can be described as either: q I/O-bound process – spends more time doing I/O than computations, many short CPU bursts q CPU-bound process – spends more time doing computations; few very long CPU bursts s If all processes are I/O bound, q The ready queue will almost always be empty s If all processes are CPU bound, q The I/O waiting queue will almost always be empty, devices will go unused q System will again be unbalancedOperating System Concepts 3.16 Silberschatz, Galvin and Gagne
  17. 17. Context Switch s 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 s Context-switch time is overhead; the system does no useful work while switching s Time dependent on hardware support s Part of OS responsible for switching the processor among the processes is called DispatcherOperating System Concepts 3.17 Silberschatz, Galvin and Gagne
  18. 18. Two-state process model Processor Queue Enter Dispatch Exit Pause Dispatcher is now redefined: • Moves processes to the waiting queue • Remove completed/aborted processes • Select the next process to run 18Operating System Concepts 3.18 Silberschatz, Galvin and Gagne
  19. 19. How process state changes1 Ready Ready Ready  a := 1  a := 1  a := 1 b := a + 1 read a file b := a + 1 c := b + 1 b := a + 1 c := b + 1 read a file c := b + 1 a := b - c a := b - c a := b - c c := c * b c := c * b c := c * b b := 0 b := 0 b := 0 c := 0 19Operating System Concepts 3.19 Silberschatz, Galvin and Gagne
  20. 20. How process state changes2 Running Ready Ready  a := 1  a := 1  a := 1 b a aa 1 1 :=:=:= 1 + read a file b := a + 1  b := a + 1 c :=bb:= a + 1 +1 b := a + 1 c := b + 1 c :=abfile1 + read := b + 1 c := b + 1 a := b - c  c a file a read - ca file := b a := b - c c := c * b a readb c := * - b c :=ac:= b - c Timeout c := c * b b := 0 b c c0 c c b b :=:=:= * * b := 0 c := 0 b := 0 b := 0 20Operating System Concepts 3.20 Silberschatz, Galvin and Gagne
  21. 21. How process state changes3 Ready Running Ready a := 1  a := 1  a := 1 b := a + 1 read a1file a := b := a + 1 c := b + 1  read + 1 b := a a file c := b + 1  read a file c b := a + 1 := b + 1 a := b - c a := b - c a := b - + c := b c 1 c := c * b c := c * b I/O c a := * b c := c b - b := 0 b := 0 b c := c * b := 0 c := 0 b := 0 21Operating System Concepts 3.21 Silberschatz, Galvin and Gagne
  22. 22. How process state changes4 Ready Blocked Running a := 1 a := 1  a := 1 b := a + 1  read a file ba :=a:= 1 :=a 1 1 + c := b + 1 b := a + 1  b := a + 1 c :=bb:= a + 1 +1  read a file c := b + 1 c := b c 1 + a := b - b + 1  c := - c a := b - c a := b - c c a ac:= b - c :=:= * b b c := c * b Timeout c := c * b b c c0 c c b b :=:=:= * * b := 0 b := 0 c b b0:= 0 :=:= 0 c := 0 c := 0 22Operating System Concepts 3.22 Silberschatz, Galvin and Gagne
  23. 23. How process state changes5 Running Blocked Ready a := 1 a := 1 a := 1 b := a + 1  read a file b := a + 1 c := b + 1 b := a + 1 c := b + 1  read a file c := b + 1  a := b - c a := b - c a := b - c c := c * b c := c * b I/O c := c * b b := 0 b := 0 b := 0 c := 0 23Operating System Concepts 3.23 Silberschatz, Galvin and Gagne
  24. 24. How process state changes6 Blocked Blocked Running The Next Process to Run cannot be simply selected a := 1 a := 1 a := 1 b := a + 1  read a file from the front1 b := a + c := b + 1 b := a + 1 c := b + 1  read a file c := b + 1  a := b - c a := b - c a := b - c c := c * b c := c * b c := c * b b := 0 b := 0 b := 0 c := 0 24Operating System Concepts 3.24 Silberschatz, Galvin and Gagne
  25. 25. How process state changes7 Blocked Blocked Running a := 1 a := 1 a := 1 b := a + 1  read a file b a := 1 1 := a + c := b + 1 b := a + 1 b := a + 1 c := b + 1  read a file c := b + 1  a c := b c 1 := b - + a := b - c a := b - c c a := * b c := c b - c :=Suppose the Green process finishesbI/O0 c * b c*b c := c * b  c := := b := 0 b := 0 b := 0 c := 0 c := 0 25Operating System Concepts 3.25 Silberschatz, Galvin and Gagne
  26. 26. How process state changes8 Blocked Ready Running a := 1 a := 1 a := 1 b := a + 1 read a file b a aa:= 1 :=:= 1 1 + c := b + 1  b := a + 1 b := a + 1 c :=bb:= a + 1 +1  read a file c := b + 1 c := b c 1 - +  a :=cb:= b + 1 a := b - c a := b - c c a ac:= b c c :=:= * b - b- c := c * b Timeout c := c * b  c := c * b b :=c0 c * b := b := 0 b := 0 c b b0:= 0 :=:= 0  := 0 c c := 0 26Operating System Concepts 3.26 Silberschatz, Galvin and Gagne
  27. 27. How process state changes9 Blocked Running Ready a := 1 a := 1 a := 1 b := a + 1 read a1file a := b := a + 1 c := b + 1 read+ 1file a :=a  b :=read 1 file a a c := b + 1  read a file b := a + 1 c :=bb + a1 a := b - c a := b - c  c :=:= c + 1 b+1 a :=cb - b + c := c * b c := c * b Timeout- c 1 := c a ac:= b - c :=:= * b b b := 0  := c * b b := 0 b c c0 c * b :=  c := 0 b :=:= 0 b := 0 27Operating System Concepts 3.27 Silberschatz, Galvin and Gagne
  28. 28. Process Creation s Reasons to create a process  Submit a new batch job/Start program  User logs on to the system  OS creates on behalf of a user (printing)  Spawned by existing process s Parent process create children processes, which, in turn create other processes, forming a tree of processes s Resource sharing q Parent and children share all resources q Children share subset of parent’s resources q Parent and child share no resources s Execution q Parent and children execute concurrently q Parent waits until children terminateOperating System Concepts 3.28 Silberschatz, Galvin and Gagne
  29. 29. Process Creation (Cont.) s Address space q Child duplicate of parent q Child has a program loaded into it s UNIX examples q fork system call creates new process q exec system call used after a fork to replace the process’ memory space with a new programOperating System Concepts 3.29 Silberschatz, Galvin and Gagne
  30. 30. Process CreationOperating System Concepts 3.30 Silberschatz, Galvin and Gagne
  31. 31. 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.31 Silberschatz, Galvin and Gagne
  32. 32. The fork() system call At the end of the system call there is a new process waiting to run once the scheduler chooses it s A new data structure is allocated s The new process is called the child process. s The existing process is called the parent process. s The parent gets the child’s pid returned to it. s The child gets 0 returned to it. s Both parent and child execute at the same point after fork() returns 32Operating System Concepts 3.32 Silberschatz, Galvin and Gagne
  33. 33. Unix Process Control The fork syscall returns a zero to the child and the child process ID to the int pid; int status = 0; parent Parent uses wait to if (pid = fork()) { sleepFork creates an until the /* parent */ childexact copy of the exits; wait …… returns child pid parent process pid = wait(&status); and status. } else { /* child */ Wait variants …… allow wait on a exit(status); specific child, or } Child process of notification passes statusother stops and back to parent on exit, signals to report success/failure 33Operating System Concepts 3.33 Silberschatz, Galvin and Gagne
  34. 34. Process Termination s Batch job issues Halt instruction s User logs off s Process executes a service request to terminate s Parent terminates so child processes terminate s Operating system intervention q such as when deadlock occurs s Error and fault conditions q E.g. memory unavailable, protection error, arithmetic error, I/O failure, invalid instruction 34Operating System Concepts 3.34 Silberschatz, Galvin and Gagne
  35. 35. Process Termination s Process executes last statement and asks the operating system to delete it (exit) q Output data from child to parent (via wait) q Process’ resources are deallocated by operating system s Parent may terminate execution of children processes (abort) q Child has exceeded allocated resources q Task assigned to child is no longer required q If parent is exiting  Some operating system do not allow child to continue if its parent terminates – All children terminated - cascading terminationOperating System Concepts 3.35 Silberschatz, Galvin and Gagne
  36. 36. Inter process Communication Processes executing concurrently in the operating system may be either independent processes or cooperating processes. s Independent process cannot affect or be affected by the execution of another process s Cooperating process can affect or be affected by the execution of another processOperating System Concepts 3.36 Silberschatz, Galvin and Gagne
  37. 37. Advantages of Process Cooperation s Information sharing: Allow concurrent access to same piece of information that several users may be interested in it. s Computation speed-up: break a task to subtasks, each of which will be executing in parallel with the others. (Should be multiple processing elements such as CPUs) s Modularity: divided the system functions into separate processes or threads. s Convenience: for user, user may work on many tasks at the same time.Operating System Concepts 3.37 Silberschatz, Galvin and Gagne
  38. 38. MODELS OF IPC Cooperating processes require an inter process communication mechanism that will allow them to exchange data and information. 2. Shared Memory 3. Message PassingOperating System Concepts 3.38 Silberschatz, Galvin and Gagne
  39. 39. SHARED MEMORY 1. Region of memory that is shared by cooperating processes is established. 2. Processes can then exchange information by reading and writing data to shared regionOperating System Concepts 3.39 Silberschatz, Galvin and Gagne
  40. 40. MESSAGE PASSING Communication takes place by means of messages exchanged between the cooperating processes.Operating System Concepts 3.40 Silberschatz, Galvin and Gagne
  41. 41. Communications ModelsOperating System Concepts 3.41 Silberschatz, Galvin and Gagne
  42. 42. Advantages & Disadvantages of Message Passing & Shared Memory 1. Message passing is useful for exchanging smaller amounts of data 2. Message Passing is easier to implement than shared memory for IPC 3. Shared Memory allows maximum speed and convenience as it can be done at memory speeds within a computer 4. Shared memory is faster than message passing as message passing is implemented using system calls.Operating System Concepts 3.42 Silberschatz, Galvin and Gagne
  43. 43. Contd…. 1. Message passing requires the more time consuming task of kernel intervention. 2. Shared memory system calls are required only to establish shared memory regions, no assistance from the kernel is required.Operating System Concepts 3.43 Silberschatz, Galvin and Gagne
  44. 44. Producer-Consumer Problem s Paradigm for cooperating processes, producer process produces information that is consumed by a consumer process q A compiler may produce assembly code, which is consumed by an assembler. The assembler, in turn, may produce object modules, which are consumed by the loader s A buffer of items that can be filled by the producer and emptied by the consumer. should be available s A producer can produce one item while the consumer is consuming another item. s The producer and consumer must be synchronized q the consumer does not try to consume an item that has not yet been produced. q the consumer must wait until an item is producedOperating System Concepts 3.44 Silberschatz, Galvin and Gagne
  45. 45. Producer-Consumer Problem s unbounded-buffer places no practical limit on the size of the buffer q the consumer may have to wait for new items, but q the producer can always produce new items s bounded-buffer assumes that there is a fixed buffer size q the consumer must wait if the buffer is empty, and q the producer must wait if the buffer is full.Operating System Concepts 3.45 Silberschatz, Galvin and Gagne
  46. 46. Bounded-Buffer – Shared-Memory Solution s Shared data #define BUFFER_SIZE 10 Typedef struct { ... } item; item buffer[BUFFER_SIZE]; int in = 0; % in: the next free position in the buffer int out = 0; % out: the first full position in the buffer s The buffer is empty when in == out ; s The buffer is full when ((in + 1) % BUFFERSIZE) == out s Solution is correct, but can only use BUFFER_SIZE-1 elementsOperating System Concepts 3.46 Silberschatz, Galvin and Gagne
  47. 47. Bounded-Buffer – Insert() Method The producer process has a local variable nextproduced in which the new item to be produced is stored: 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.47 Silberschatz, Galvin and Gagne
  48. 48. Bounded Buffer – Remove() Method The consumer process has a local variable nextconsumed in which the item to be consumed is stored: 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.48 Silberschatz, Galvin and Gagne
  49. 49. Message Passing s Mechanism for processes to communicate and to synchronize their actions s Message system – processes communicate with each other without resorting to shared variables s IPC facility provides two operations: q send(message) – message size fixed or variable q receive(message) s If P and Q wish to communicate, they need to: q establish a communication link between them q exchange messages via send/receive s Implementation of communication link q physical (e.g., shared memory, hardware bus) q logical (e.g., logical properties) e.g. send(), receive()Operating System Concepts 3.49 Silberschatz, Galvin and Gagne
  50. 50. Implementation Questions s How are links established? s Can a link be associated with more than two processes? s How many links can there be between every pair of communicating processes? s What is the capacity of a link? s Is the size of a message that the link can accommodate fixed or variable? s Is a link unidirectional or bi-directional?Operating System Concepts 3.50 Silberschatz, Galvin and Gagne
  51. 51. Methods for logical implementation of link 1. Direct or Indirect Communication 2. Synchronous and Asynchronous Communication 3. Automatic or explicit BufferingOperating System Concepts 3.51 Silberschatz, Galvin and Gagne
  52. 52. Direct Communication s Processes must name each other explicitly: q send (P, message) – send a message to process P q receive(Q, message) – receive a message from process Q s Properties of communication link q Links are established automatically because the processes need to know only the identities to communicate. q A link is associated with exactly one pair of communicating processes q Between each pair there exists exactly one link q The link may be unidirectional, but is usually bi-directionalOperating System Concepts 3.52 Silberschatz, Galvin and Gagne
  53. 53. Indirect Communication s Messages are directed and received from mailboxes (also referred to as ports) q Each mailbox has a unique id q Processes can communicate only if they share a mailbox s Properties of communication link q Link established only if processes share a common mailbox q A link may be associated with many processes q Each pair of processes may share several communication links q Link may be unidirectional or bi-directionalOperating System Concepts 3.53 Silberschatz, Galvin and Gagne
  54. 54. Indirect Communication s Operations q create a new mailbox q send and receive messages through mailbox q destroy a mailbox s Primitives are defined as: send(A, message) – send a message to mailbox A receive(A, message) – receive a message from mailbox AOperating System Concepts 3.54 Silberschatz, Galvin and Gagne
  55. 55. Indirect Communication s Mailbox sharing q P1, P2, and P3 share mailbox A q P1, sends; P2 and P3 receive q Who gets the message? s Solutions q Allow a link to be associated with at most two processes q Allow only one process at a time to execute a receive operation q Allow the system to select arbitrarily the receiver. Sender is notified who the receiver was.Operating System Concepts 3.55 Silberschatz, Galvin and Gagne
  56. 56. Indirect Communication s If the mailbox is owned by a process (that is, the mailbox is part of the address space of the process), q then we distinguish between the owner (who can only receive messages through this mailbox) and the user (who can only send messages to the mailbox). q Since each mailbox has a unique owner, there can be  no confusion about who should receive a message sent to this mailbox. q When a process that owns a mailbox terminates, the mailbox disappears. q Any process that subsequently sends a message to this mailbox must be notified that the mailbox no longer exists.Operating System Concepts 3.56 Silberschatz, Galvin and Gagne
  57. 57. Indirect Communication s If a mailbox owned by the operating system is independent and is not attached to any particular process: q The operating system then must provide a mechanism that allows a process to do the following:  Create a new mailbox.  Send and receive messages through the mailbox.  Delete a mailbox q Note: the ownership and receive privilege may be passed to other processes through appropriate system calls.Operating System Concepts 3.57 Silberschatz, Galvin and Gagne
  58. 58. Synchronization s Message passing may be either blocking or non-blocking s Blocking is considered synchronous q Blocking send has the sender block until the message is received q Blocking receive has the receiver block until a message is available s Non-blocking is considered asynchronous q Non-blocking send has the sender send the message and continue q Non-blocking receive has the receiver receive a valid message or nullOperating System Concepts 3.58 Silberschatz, Galvin and Gagne
  59. 59. Buffering s 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 waitsOperating System Concepts 3.59 Silberschatz, Galvin and Gagne
  60. 60. Client-Server Communication s Sockets s Remote Procedure Calls s Remote Method Invocation (Java)Operating System Concepts 3.60 Silberschatz, Galvin and Gagne
  61. 61. Sockets s A socket is defined as an endpoint for communication s Concatenation of IP address and port s The socket refers to port 1625 on host s Communication consists between a pair of socketsOperating System Concepts 3.61 Silberschatz, Galvin and Gagne
  62. 62. Socket CommunicationOperating System Concepts 3.62 Silberschatz, Galvin and Gagne
  63. 63. Remote Procedure Calls s Remote procedure call (RPC) abstracts procedure calls between processes on networked systems. s Stubs – client-side proxy for the actual procedure on the server. s The client-side stub locates the server and marshalls the parameters. s The server-side stub receives this message, unpacks the marshalled parameters, and performs the procedure on the server. s A stub is a small program routine that substitutes for a longer program, possibly to be loaded later or that is located remotely. For example, a program that uses Remote Procedure Calls ( RPC ) is compiled with stubs that substitute for the program that provides a requested procedure. The stub accepts the request and then forwards it (through another program) to the remote procedure. When that procedure has completed its service, it returns the results or other status to the stub which passes it back to the program that made the request.Operating System Concepts 3.63 Silberschatz, Galvin and Gagne
  64. 64. Execution of RPCOperating System Concepts 3.64 Silberschatz, Galvin and Gagne
  65. 65. End of Chapter 3