1 / 69

CHAPTER 4 PROCESSES

CHAPTER 4 PROCESSES. Operating Systems. Processes. Current-day computer systems allow multiple programs to be loaded into memory and to be executed concurrently. This resulted in process , which is a program in execution. Process Concept Process Scheduling Operation on Processes

sean-fox
Download Presentation

CHAPTER 4 PROCESSES

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CHAPTER 4PROCESSES Operating Systems

  2. Processes Current-day computer systems allow multiple programs to be loaded into memory and to be executed concurrently. This resulted in process, which is a program in execution. • Process Concept • Process Scheduling • Operation on Processes • Cooperating Processes • Interprocess Communication

  3. Process Concept §4.1 • An operating system executes a variety of programs: • Batch system – jobs • Time-shared systems – user programs or tasks • Even on a single-user system, such as Windows or Mac, a user may be able to run several programs at one time. • Textbook uses the terms job and process almost interchangeably. • Process – a program in execution; process execution must progress in sequential fashion.

  4. Process Concept §4.1.1 • A process is more than the program code (known as the text section), it also includes the current activity represented by: • program counter • Stack– contains temporary data (such as method parameters, return addresses, and local variables) • data section– contains global variables. • A program is not a process; a program is a passive entity, such as the contents of a file stored on disk, whereas a process is an active entity, with a program counter specifying the next instruction to execute.

  5. Process Concept • Two processes associated with same program are considered two separate execution sequences. • Ex: several users running different copies of the mail program or same user may invoke many copies of the editor program. • Each of above is a separate process, and, although the text sections are equivalent, the data sections vary.

  6. Process State §4.1.2 • 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 process. • terminated: The process has finished execution. • Only one process can be running on any processor at any instant. Many processes may be ready and waiting, however.

  7. Diagram of Process State

  8. §4.1.3 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

  9. Process Control Block (PCB)

  10. CPU Switch From Process to Process

  11. Process Scheduling Queues §4.2 • Job queue – set of all processes in the system. • Ready queue – set of all processes residing in main memory, ready and waiting to execute. This queue is generally stored as a linked list. • Device queues – Since there are many processes in the system, the device may be busy with other processes’ requests. The list of processes waiting for a particular I/O device is called a device queue.

  12. Ready Queue and Various I/O Device Queues

  13. Process Migration • A common representation for process migration is a queueing diagram. • A new process is initially put in the ready queue. It waits in the ready queue until it is selected for execution or is dispatched. Once the process is allocated the CPU and is executing, one of several events could occur: • Issue an I/O request and be placed in an I/O queue. • Create a new subprocess and wait for its termination. • Be removed forcibly from the CPU, as a result of an interrupt, and be put back in the ready queue.

  14. Queueing Diagram

  15. Schedulers §4.2.2 • Scheduler select processes from scheduling queues for their migration within the system. • Long-term scheduler (or job scheduler) – selects which processes should be brought into the ready queue (loaded into memory for execution). • Short-term scheduler (or CPU scheduler) – selects which process should be executed next and allocates CPU. • Primary difference between them: the frequency of execution. Invoked very infrequently (seconds, minutes) may be slow Select frequently (miliseconds)  must be fast

  16. Long-term scheduler • The long-term scheduler controls the degree of multiprogramming (the number of processes in memory). • It is important that the long-term scheduler select a good process mix of I/O-bound and CPU-bound processes to reach the best performance. • If all processes are I/O bound … • If all processes are CPU bound … • If no long-term scheduler… (some time-sharing system such as UNIX) THINK THINK THINK

  17. Medium-term scheduler • Some OS may introduce an additional, intermediate level of scheduling. • Key idea: sometimes it can be advantageous to remove processes from memory (and from active contention for the CPU), and thus to reduce the degree of multiprogramming………swapping. • Swapping may be necessary to improve the process mix, or because a change in memory requirements has overcommitted available memory, requiring memory to be freed up.

  18. Medium-term scheduler

  19. Context Switch §4.2.3 • 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. • The context of a process is represented in the PCB of a process. • When a context switch occurs, the kernel saves the context of the old process in its PCB and loads the saved context of the new process scheduled to run. • Context-switch time is overhead; the system does no useful work while switching. • Time dependent on hardware support.

  20. Process Creation §4.3.1 • Parent process creates children processes, which, in turn create other processes, forming a tree of processes. (Fig. 4.7) • Resource sharing • A subprocess may obtain its resources directly from the OS, or • may be constrained to a subset of the resources of the parent process. prevents any process from overloading the system by creating too many subprocesses

  21. A Tree of Processes On a Typical UNIX System Back

  22. Process Creation • Execution possibilities • Parent and children execute concurrently. • Parent waits until some or all children have terminated. • Address space • Child is a duplicate of the parent. • Child has a program loaded into it. • UNIX examples • Each process is identified by its process identifier. • A new process is created by the fork system call. • Both the parent and the child continue execution at the instruction after the fork with different returning code: 0 : child, nonzero (process id): parent • execlp system call used after a fork to replace the process’ memory space with a new program.

  23. UNIX Example #include <stdio.h> Void main(int argc, char *argv[]) {int pid; pid = fork(); /* fork another process */ 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 wait for the child to complete*/ wait(NULL); printf(“Child Complete”); exit(0); } }

  24. Process Termination §4.3.2 • 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 by operating system. • Parent may terminate execution of children processes (abort) for following reasons: • Child has exceeded allocated resources. • Task assigned to the child is no longer required. • Parent is exiting. • Operating system does not allow child to continue if its parent terminates… called cascading termination.

  25. #include <stdio.h> Void main(int argc, char *argv[]) {int pid; pid = fork(); /* fork another process */ 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 wait for the child to complete*/ wait(NULL); printf(“Child Complete”); exit(0); } } Parent Child (pid > 0 ) (pid = 0) #include <stdio.h> Void main(int argc, char *argv[]) {int pid; pid = fork(); /* fork another process */ 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 wait for the child to complete*/ wait(NULL); printf(“Child Complete”); exit(0); } }

  26. Parent Child (pid > 0 ) (pid = 0) #include <stdio.h> Void main(int argc, char *argv[]) {int pid; pid = fork(); /* fork another process */ 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 wait for the child to complete*/ wait(NULL); printf(“Child Complete”); exit(0); } } #include <stdio.h> Void main(int argc, char *argv[]) {int pid; pid = fork(); /* fork another process */ 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 wait for the child to complete*/ wait(NULL); printf(“Child Complete”); exit(0); } }

  27. Parent Child (pid > 0 ) (pid = 0) #include <stdio.h> Void main(int argc, char *argv[]) {int pid; pid = fork(); /* fork another process */ 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 wait for the child to complete*/ wait(NULL); printf(“Child Complete”); exit(0); } } #include <stdio.h> Void main(int argc, char *argv[]) {int pid; pid = fork(); /* fork another process */ 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 wait for the child to complete*/ wait(NULL); printf(“Child Complete”); exit(0); } }

  28. Parent Child (pid > 0 ) (pid = 0) #include <stdio.h> Void main(int argc, char *argv[]) {int pid; pid = fork(); /* fork another process */ 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 wait for the child to complete*/ wait(NULL); printf(“Child Complete”); exit(0); } } #include <stdio.h> Void main(int argc, char *argv[]) {int pid; pid = fork(); /* fork another process */ 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 wait for the child to complete*/ wait(NULL); printf(“Child Complete”); exit(0); } }

  29. Parent Child (pid > 0 ) (pid = 0) #include <stdio.h> Void main(int argc, char *argv[]) {int pid; pid = fork(); /* fork another process */ 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 wait for the child to complete*/ wait(NULL); printf(“Child Complete”); exit(0); } } #include <stdio.h> Void main(int argc, char *argv[]) {int pid; pid = fork(); /* fork another process */ 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 wait for the child to complete*/ wait(NULL); printf(“Child Complete”); exit(0); } }

  30. Parent Child (pid > 0 ) (pid = 0) #include <stdio.h> Void main(int argc, char *argv[]) {int pid; pid = fork(); /* fork another process */ 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 wait for the child to complete*/ wait(NULL); printf(“Child Complete”); exit(0); } } #include <stdio.h> Void main(int argc, char *argv[]) {int pid; pid = fork(); /* fork another process */ 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 wait for the child to complete*/ wait(NULL); printf(“Child Complete”); exit(0); } } /bin/ls

  31. Cooperating Processes §4.4 • 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: if multiple processor exists. • Modularity: divide the system function into separate processes or threads. • Convenience: a user may have many tasks on which to work at one time.

  32. Producer-Consumer Problem • A common paradigm for cooperating processes, producer process produces information that is consumed by a consumer process. • Require a buffer that can be filled by the producer and emptied by the consumer. • The producer and consumer must be synchronized, so that the consumer does not try to consume an item that has not yet been produced. • unbounded-buffer places no practical limit on the size of the buffer. • bounded-buffer assumes that there is a fixed buffer size.

  33. Bounded-Buffer – Shared-Memory Solution • Shared data • #define BUFFER_SIZE 10 • Typedef struct { • . . . • } item; • item buffer[BUFFER_SIZE]; • int in = 0; • int out = 0; • Solution is correct, but can only use BUFFER_SIZE-1 elements (Fig 4.9)

  34. Bounded-Buffer – Shared-Memory Solution Consumer (Consumer reads contents of last full buffer. The value of out is incremented by 1.) Producer (Producer stores data in buffer(in). The value of in is increased by 1) Bounded (Circular) Buffer in out

  35. Bounded-Buffer – Shared-Memory Solution item nextProduced; while (1) { while (((in + 1) % BUFFER_SIZE) == out) ; /* do nothing */ buffer[in] = nextProduced; in = (in + 1) % BUFFER_SIZE; } Producer process

  36. Bounded-Buffer – Shared-Memory Solution item nextConsumed; while (1) { while (in == out) ; /* do nothing */ nextConsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; } Consumer process

  37. §4.5 Interprocess Communication (IPC) • Message-passing system for processes to communicate and to synchronize their actions without sharing the same address space. • Useful in distributed environment. Ex: chat program • IPC facility provides at least two operations: • send(message) – message size fixed or variable • receive(message) • If P and Q wish to communicate, they need to: • establish a communicationlink between them • exchange messages via send/receive • Implementation of communication link • physical (e.g., shared memory, hardware bus, network) • logical (e.g., logical properties)

  38. Naming §4.5.2 • Communicating processes must have a way to refer to each other, directly or indirectly. • Direct communication • Indirect communication

  39. Direct Communication §4.5.2.1 • Processes must name each other explicitly: • send(P, message)– send a message to process P • receive(Q, message)– receive a message from process Q • Properties of communication link • Links are established automatically. The processes need to know only each other’s identity to communicate. • A link is associated with exactly two processes. • Between each pair there exists exactly one link.

  40. Direct Communication • A variant of symmetry in addressing is asymmetry addressing: only the sender names the recipient; the recipient is not required to name the sender. • send(P, message)– send a message to process P • receive(id, message)– receive a message from any process;idis set to the name the process with which communication has taken place. • Both symmetry and asymmetry communication has limited modularity of the resulting process definitions. Changing the name of a process may necessitate examining all other process definition.

  41. §4.5.2.2 Indirect Communication • Messages are sent to and received from mailboxes (also referred to as ports). Each mailbox has a unique id. Processes can communicate only if they share a mailbox. • send(A, message)– send a message to mailbox A • receive(A,message)– receive a message from mailbox A • Properties of communication link • Link established only if processes share a common mailbox • A link may be associated with many processes. • Each pair of processes may share several communication links.

  42. Indirect Communication • A mailbox may be owned either by a process or by the operating system. • Mailbox sharing • P1, P2, and P3 share mailbox A. • P1, sends; P2and P3 receive. • Who gets the message? • Solutions • Allow a link to be associated with at most two processes. • Allow only one process at a time to execute a receive operation. • Allow the system to select arbitrarily the receiver. Sender is notified who the receiver was.

  43. Synchronization §4.5.3 • Message passing may be either blocking or nonblocking – also known as synchronous and asynchronous. • Blocking send: The sending process is blocked until the message is received. • Nonblocking send: The sending process resumes operation. • Blocking receive: The receiver blocks until a message is available. • Nonblocking receive: The receiver retrieves either a valid message or a null. • When both the send and receive are blocking, we have a rendezvous between sender and receiver.

  44. Buffering §4.5.4 • Message exchanged by communicating processes reside in a temporary queue implemented in one of three ways. 1. Zero capacity – 0 messagesSender must wait for receiver (rendezvous). 2. Bounded capacity – finite length of n messagesSender must wait if link full. 3. Unbounded capacity – infinite length Sender never waits.

  45. An Example: Mach (略)

  46. Example: Windows 2000 • Windows 2000 supports multiple operating environments or subsystems, with which application programs (i.e. clients) communicate via a message-passing mechanism. • In Windows 2000, the message-passing between two processes that are on the same machine is called the local procedure-call facility (LPC). • NT uses a port object to establish and maintain a connection between two processes.

  47. Example: Windows 2000 • Communication works as follows: • The client opens a handle to the subsystem’s connection port object. • The client sends a connection request. • The server creates two private communication ports, and returns the handle to one of them to the client • The client and server use the corresponding port handle to send messages or callbacks and to listen for replies

  48. Example: Windows 2000 • NT uses three types of message-passing techniques over a port that the client specifies when it establishes the channel: • For small message, uses the port’s message queue as intermediate storage and copies the message from one process to the other. • For larger message, it passes the message through a section object (shared memory). • For better performance, in quick LPC, the server sets up a dedicated server thread to handle requests.

  49. Communication in Client-Server Systems §4.6.1 • A Socket is defined as an endpoint for communication. • A pair of processes (or threads) communicating over a network employs a pair of sockets – one for each process. • A socket is identified by an IP address concatenated with a port number.

  50. Sockets • The server waits for incoming client requests by listening to a specified port. Once a request is received, the server accepts a connection from the client socket to complete the connection. • Servers implementing specific services by listening to well-known ports. All ports below 1024 are considered well known. Telnet server listens to port 23. Ftp server listens to port 21. Web server (http) listens to port 80.

More Related