Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

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

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

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

5 3.5 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition 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

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

7 3.7 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition 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

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

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

10 3.10 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition 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

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

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

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

14 3.14 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Process Creation Parent process create children processes. process identified via a process identifier (pid) Options in resource sharing Parent and children share all resources Children share subset of parent’s resources Parent and child share no resources Execution Parent and children execute concurrently Parent waits until children terminate

15 3.15 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Process Creation (Cont.) Options in address space Child duplicate of parent Child has another program loaded UNIX examples fork system call creates new process exec system call used after a fork to replace the process’ memory space with a new program

16 3.16 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Unix Fork/Exec/Exit/Wait Example int pid = fork(); Create a new process that is a clone of its parent. 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. int pid = wait*(&status); Wait for exit (or other status change) of a child. fork parentfork child wait exit exec initialize child context

17 3.17 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Example: Process Creation in Unix int pid; int status = 0; if (pid = fork()) { /* parent */ ….. pid = wait(&status); } else { /* child */ ….. exit(status); } 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. The fork syscall returns twice: it returns a zero to the child and the child process ID (pid) to the parent.

18 3.18 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition 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); }

19 3.19 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition A tree of processes on a typical Solaris

20 3.20 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Process Termination Process executes last statement and asks the operating system to delete it (exit) Output data from child to parent (via wait) Process’ resources are deallocated Parent may terminate execution of children processes Child has exceeded allocated resources Task assigned to child is no longer required If parent is exiting  Some operating system terminate all children - cascading termination

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

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

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

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

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

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

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

28 3.28 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition 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;

29 3.29 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Interprocess Communication – Message Passing Two operations: send(message) – message size fixed or variable receive(message) If P and Q wish to communicate, they need to: establish a communication link between them exchange messages via send/receive

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

31 3.31 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Direct vs. Indirect Messages Direct Communication: Processes must name each other explicitly: send (P, message) – send a message to process P receive(Q, message) – receive a message from process Q Indirect Communication: 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

32 3.32 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Blocking vs. non-blocking message passing 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

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

34 3.34 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition POSIX Shared Memory n Write process l Create shared memory segment 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;

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

36 3.36 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Sockets in Client-server systems 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

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

38 3.38 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Example: Client connection in Java try { Socket sock = new Socket("127.0.0.1",6013); InputStream in = sock.getInputStream(); BufferedReader bin = new BufferedReader(new InputStreamReader(in)); String line; while( (line = bin.readLine()) != null) System.out.println(line); sock.close(); } Read data sent from server and print Make a connection to server

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


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

Similar presentations


Ads by Google