Chapter 3 Processes Modified from the text book

Chapter 3: Processes Modified from the text book Slides. TY, Sept 2011. Operating System Concepts – 8 th Edition Silberschatz, Galvin and Gagne © 2009

Chapter 3: Processes n Process Concept n Process Scheduling n Operations on Processes n Interprocess Communication Operating System Concepts – 8 th Edition 3. 2 Silberschatz, Galvin and Gagne © 2009

Process Concept n Textbook uses the terms job and process almost interchangeably n Process – a program in execution; l progress in sequential fashion n A process in memory includes: program counter l Stack/heap l Data/instruction (text) section l Operating System Concepts – 8 th Edition 3. 3 Silberschatz, Galvin and Gagne © 2009

Load an Executable File to Process Header “magic number” indicates type of image. Section table an array of (offset, len, start. VA) Program/data sections Used by linker; may be removed after final link step header text data idata writable global/static data j, s symbol j, s , p, sbuf table relocation records Operating System Concepts – 8 th Edition program instructions p immutable data (constants) “hellon” 3. 4 int j = 327; char* s = “hellon”; char sbuf[512]; int p() { int k = 0; j = write(1, s, 6); return(j); } Silberschatz, Galvin and Gagne © 2009

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 processor l terminated: The process has finished execution Operating System Concepts – 8 th Edition 3. 5 Silberschatz, Galvin and Gagne © 2009

Diagram of Process State Operating System Concepts – 8 th Edition 3. 6 Silberschatz, Galvin and Gagne © 2009

Process Control Block (PCB) Information associated with each process n Process state n Program counter n CPU registers n CPU scheduling information n Memory-management information n Accounting information n I/O status information Operating System Concepts – 8 th Edition 3. 7 Silberschatz, Galvin and Gagne © 2009

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 via a context switch. n Context of a process represented in the PCB n Context-switch time is overhead; the system does no useful work while switching Operating System Concepts – 8 th Edition 3. 8 Silberschatz, Galvin and Gagne © 2009

CPU Switch From Process to Process Operating System Concepts – 8 th Edition 3. 9 Silberschatz, Galvin and Gagne © 2009

Process Scheduling Queues n Job queue – set of all processes in the system n Ready queue – set of all processes residing in main memory, ready and waiting to execute n Device queues – set of processes waiting for an I/O device n Processes migrate among the various queues Operating System Concepts – 8 th Edition 3. 10 Silberschatz, Galvin and Gagne © 2009

Ready Queue And Various I/O Device Queues Operating System Concepts – 8 th Edition 3. 11 Silberschatz, Galvin and Gagne © 2009

Representation of Process Scheduling Operating System Concepts – 8 th Edition 3. 12 Silberschatz, Galvin and Gagne © 2009

Schedulers n Long-term scheduler l Selects which processes should be brought into the ready queue l invoked very infrequently (seconds, minutes) l controls the degree of multiprogramming n Short-term scheduler l – selects which process should be executed next and allocates CPU l is invoked very frequently (milliseconds) (must be fast) Operating System Concepts – 8 th Edition 3. 13 Silberschatz, Galvin and Gagne © 2009

Process Creation n Parent process create children processes. l process identified via a process identifier (pid) n Options in 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 – 8 th Edition 3. 14 Silberschatz, Galvin and Gagne © 2009

Process Creation (Cont. ) n Options in address space l Child duplicate of parent l Child has another program loaded 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 – 8 th Edition 3. 15 Silberschatz, Galvin and Gagne © 2009

Unix Fork/Exec/Exit/Wait Example int pid = fork(); Create a new process that is a clone of its parent. fork parent initialize child context exec*(“program” [, argvp, envp]); Overlay the calling process virtual memory with a new program, and transfer control to it. exit(status); Exit with status, destroying the process. fork child wait exec exit int pid = wait*(&status); Wait for exit (or other status change) of a child. Operating System Concepts – 8 th Edition 3. 16 Silberschatz, Galvin and Gagne © 2009

Example: Process Creation in Unix The fork syscall returns twice: it returns a zero to the child and the child process ID (pid) to the parent. int pid; int status = 0; if (pid = fork()) { /* parent */ …. . pid = wait(&status); } else { /* child */ …. . exit(status); } Operating System Concepts – 8 th Edition 3. 17 Parent uses wait to sleep until the child exits; wait returns child pid and status. Wait variants allow wait on a specific child, or notification of stops and other signals. Silberschatz, Galvin and Gagne © 2009

C Program Forking Separate Process int main() { int 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 – 8 th Edition 3. 18 Silberschatz, Galvin and Gagne © 2009

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 n Parent may terminate execution of children processes l Child has exceeded allocated resources l Task assigned to child is no longer required l If parent is exiting 4 Some operating system terminate all children cascading termination Operating System Concepts – 8 th Edition 3. 20 Silberschatz, Galvin and Gagne © 2009

Interprocess Communication n Processes within a system may l independent l or cooperating with information sharing n Cooperating processes need interprocess communication (IPC) l Shared memory l Message Operating System Concepts – 8 th Edition passing 3. 21 Silberschatz, Galvin and Gagne © 2009

Communications Models Operating System Concepts – 8 th Edition 3. 22 Silberschatz, Galvin and Gagne © 2009

Example: Producer-Consumer Problem n Producer process produces information that is consumed by a consumer process l E. g. Print utility places data and printer fetches data to print. n Synchronize through a buffer. l bounded-buffer assumes that there is a fixed buffer size Operating System Concepts – 8 th Edition 3. 23 Silberschatz, Galvin and Gagne © 2009

Multiple Producers and Consumers Operating System Concepts – 8 th Edition A. Frank - P. 3. 24 Weisberg Silberschatz, Galvin and Gagne © 2009

Circular array for Producer/Consumer Solution n The bounded buffer is implemented as a circular array with 2 logical pointers: in and out. n in the next free position in the buffer for data writing (producer) n Out he first filled position in the buffer for data reading (consumer) Operating System Concepts – 8 th Edition 3. 25 Silberschatz, Galvin and Gagne © 2009

Bounded-Buffer – Shared-Memory Solution n Shared data buffer #define BUFFER_SIZE 10 typedef struct {. . . } item; item buffer[BUFFER_SIZE]; int in = 0; int out = 0; Operating System Concepts – 8 th Edition 3. 26 Silberschatz, Galvin and Gagne © 2009

Producer: Produce an item while (((in + 1) % BUFFER SIZE) == out) ; /* do nothing -- no free buffers */ buffer[in] = item; in = (in + 1) % BUFFER SIZE; Operating System Concepts – 8 th Edition 3. 27 Silberschatz, Galvin and Gagne © 2009

Consumer: consume an item 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 – 8 th Edition 3. 28 Silberschatz, Galvin and Gagne © 2009

Interprocess Communication – Message Passing n 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 Operating System Concepts – 8 th Edition 3. 29 Silberschatz, Galvin and Gagne © 2009

Implementation Questions n How are links established? n Can a link be associated with more than two processes? n How many links can there be between every pair of communicating processes? n What is the capacity of a link? n Is the size of a message fixed or variable? n Is a link unidirectional or bi-directional? Operating System Concepts – 8 th Edition 3. 30 Silberschatz, Galvin and Gagne © 2009

Direct vs. Indirect Messages n Direct Communication: 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 Indirect Communication: 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 Operating System Concepts – 8 th Edition 3. 31 Silberschatz, Galvin and Gagne © 2009

Blocking vs. non-blocking message passing n Blocking is considered synchronous l Blocking send has the sender block until the message is received l Blocking receive has the receiver block until a message is available n 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 – 8 th Edition 3. 32 Silberschatz, Galvin and Gagne © 2009

Examples of Cooperative Communications n Shared memory IPC in Posix l POSIX is the name of a family of related standard specified by IEEE to define API in Unix. n Unix pipe n Sockets n Remote Procedure Calls (RPC) Operating System Concepts – 8 th Edition 3. 33 Silberschatz, Galvin and Gagne © 2009

POSIX Shared Memory n Write process l Create shared memory segment id = shmget(key, size, IPC_CREAT); l Attach shared memory to its address space addr= (char *) shmat(id, NULL, 0); l write to the shared memory *addr = 1; n Read process segment id = shmget(key, size, 0666); addr= (char *) shmat(id, NULL, 0); c= *addr; Operating System Concepts – 8 th Edition 3. 34 Silberschatz, Galvin and Gagne © 2009

Unix Pipe n Allow parent-child communication a producer- consumer style Operating System Concepts – 8 th Edition 3. 35 Silberschatz, Galvin and Gagne © 2009

Sockets in Client-server systems n A socket is defined as an endpoint for communication l Concatenation of IP address and port l The socket 161. 25. 19. 8: 1625 refers to port 1625 on host 161. 25. 19. 8 n Communication consists between a pair of sockets Operating System Concepts – 8 th Edition 3. 36 Silberschatz, Galvin and Gagne © 2009

Socket Communication Operating System Concepts – 8 th Edition 3. 37 Silberschatz, Galvin and Gagne © 2009

Example: Client connection in Java Make a connection to server try { Socket sock = new Socket("127. 0. 0. 1", 6013); Input. Stream in = sock. get. Input. Stream(); Read data sent from Buffered. Reader bin = new Buffered. Reader(new server and print Input. Stream. Reader(in)); String line; while( (line = bin. read. Line()) != null) System. out. println(line); } sock. close(); Operating System Concepts – 8 th Edition 3. 38 Silberschatz, Galvin and Gagne © 2009

Server code: handling client requests one by one Create a socket to listen Server. Socket sock = new Server. Socket(6013); Listen for connections while (true) { Socket client = sock. accept(); // we have a connection Write date to the socket Print. Writer pout = new Print. Writer(client. get. Output. Stream(), true); pout. println(new java. util. Date(). to. String()); } Close the socket and resume listening for more connections client. close(); Operating System Concepts – 8 th Edition 3. 39 Silberschatz, Galvin and Gagne © 2009
- Slides: 38