Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS238 Lecture 4 Processes Dr. Alan R. Davis. Process Management Processes Chap 4 Threads Chap 5 CPU SchedulingChap 6 Process SynchronizationChap 7 DeadlocksChap.

Similar presentations


Presentation on theme: "CS238 Lecture 4 Processes Dr. Alan R. Davis. Process Management Processes Chap 4 Threads Chap 5 CPU SchedulingChap 6 Process SynchronizationChap 7 DeadlocksChap."— Presentation transcript:

1 CS238 Lecture 4 Processes Dr. Alan R. Davis

2 Process Management Processes Chap 4 Threads Chap 5 CPU SchedulingChap 6 Process SynchronizationChap 7 DeadlocksChap 8

3 Processes A process can be considered a program in execution. It is a basic unit of work on most systems, which execute instructions one at a time. A process is a subset of attributes of a computer system in operation. The process includes the program instructions and program data (the address space), the stack, the contents of the CPU registers including the program counter, and associated resources (CPU time, memory, files, I/O devices). A program is passive. A process is active.

4 Processes cont'd Kernel processes or operating system processes execute system code. User processes execute user code. One program may be associated with several processes (several users may be running the c++ compiler at the same time).

5 Processes cont'd Process State The process state is... New if it is being created Running if its instructions are being executed Waiting if it is waiting for some event to occur Ready if it can be assigned to a processor Terminated if it has finished execution

6 Process State Diagram

7 Processes cont'd Process Control Block Process state Program counter CPU registers CPU scheduling information Memory-management information Accounting information I/O Status information

8 Process Control Block (PCB)

9 Process Scheduling Since system resources are finite, processes will often need to "take a number" in order to use them. Several processes could be in the ready state at the same time, and will need to be placed in the ready queue to wait their turn to use the CPU. Similarly, several processes could need to get input from a disk drive at the same time. They will be placed in the device queue associated with that disk drive.

10 Process Scheduling cont’d Implementation of these queues can be done in any algorithmically appropriate manner. Linked lists of Process Control Blocks is one method. A simulation of this material is a good exercise.

11 Ready Queue And Various I/O Device Queues

12 Representation of Process Scheduling

13 Process Scheduling cont’d A process "moves around" through various queues during its lifetime. The scheduler is the part of the OS that maintains these queues and selects the next process. The long term scheduler selects jobs from the job queue in secondary storage and moves them to the ready queue in main memory. The short term scheduler selects a process from the ready queue in main memory and gives it the CPU.

14 Process Scheduling cont’d The short term scheduler (CPU scheduler) must make a decision every time the executing process changes to a wait state. Since this happens frequently, the CPU scheduler must be very fast. This limits the complexity of the algorithm used to implement the scheduler.

15 Process Scheduling cont’d The long term scheduler determines the degree of multiprogramming. It needs to be invoked every time a process leaves the system (is terminated). Since this happens much less frequently than a process going into a wait state, the long term scheduler has much more time to make decisions. However, these decisions need to be good ones in order to choose between (for example) I/O bound processes and CPU bound processes.

16 Process Scheduling cont'd Some systems may benefit from a medium-term scheduler. Its function is to change the mix of ready processes, or the number of ready processes, based on the current demands on the system. Time-sharing systems could benefit from this feature to keep users satisfied. In this context, we call the action swapping.

17 Addition of Medium Term Scheduling

18 Context Switch Switching the CPU from one process to another requires saving the state of the old process and loading the saved state for the new process. This is a context switch. A context switch is a very costly operation and is pure overhead. The total cost depends on how complex the state of a process is, and on what hardware support exists on a given machine.

19 CPU Switch From Process to Process

20 Preemptive Scheduling Scheduling decisions may take place when a process... –1. switches from running to waiting. –2. switches from running to ready. –3. switches from waiting to ready. –4. terminates. Scheduling is nonpreemptive if only 1 and 4 occur. Scheduling is preemptive otherwise.

21 Dispatcher The dispatcher is the part of the OS given the responsibility of removing a process from the front of the ready queue and giving it the CPU resource. The dispatcher gives control of the CPU to the process selected by the short term scheduler. It switches context. It switches to user mode. It jumps to the proper location in the user program to restart that program.

22 Process Creation The OS must provide a mechanism, such as a CreateProcess system call, for process creation. A process may create other processes (subprocesses) during the course of its execution, forming a hierarchy of processes. The creating process is called the parent process and the subprocess is called a child process.

23 Process Creation cont’d Let a subprocess inherit all or some of the parent's resources or let each subprocess obtain its own resources. Allow the parent to continue executing concurrently with its children, or wait until some or all of the children have terminated. Let the address space of the subprocess be a duplicate of the parent's, or have a program loaded into it. Unix uses fork and execve system calls when creating processes.

24 #include void main(int argc, char* argv[]) {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 waits for the child to complete */ wait(NULL); printf("Child complete"); exit(0);} }

25 A Tree of Processes On A Typical UNIX System

26 Process Termination The OS must provide a mechanism, such as ExitProcess( ), for process termination. A process terminates when it finishes executing its last statement and asks the OS to delete it by a system call. Let the process return data (output) to its parent process. Deallocate all of the associated resources, including termination of subprocesses. Subprocesses can be terminated by their parents if they exceed usage of some resource allocation, its task is no longer required, its parent is exiting.

27 Cooperating Processes A process is cooperating if it can affect or be affected by the other processes executing in the system, otherwise it is independent. Cooperation requires sharing of data, which can be implemented by shared memory or by message passing. Cooperating processes allow information sharing, computation speedup, modularity, and convenience.

28 Producer-Consumer A common paradigm for cooperating processes is the producer-consumer model. One process produces information that is consumed by another process, such as a print program producing characters for a printer driver. This paradigm requires a buffer in which to place the information. It requires synchronization so that the consumer doesn't try to consume an item which has not yet been produced.

29 Producer-Consumer cont’d The buffer can either be bounded or unbounded. In an unbounded-buffer model, the producer can always produce new items. The consumer may have to wait for an item. In a bounded-buffer model, the producer must wait if the buffer is full, and the consumer must wait if the buffer is empty. The buffer may either be provided by the OS through the use of interprocess communication IPC, or explicitly coded by the application programmer with the use of shared memory.

30 Bounded-Buffer – Shared-Memory Solution Shared data var n; type item = … ; var buffer. array [0..n–1] of item; in, out: 0..n–1; Producer process repeat … produce an item in nextp … while in+1 mod n = out do no-op; buffer [in] :=nextp; in :=in+1 mod n; until false;

31 Bounded-Buffer (Cont.) Consumer process repeat while in = out do no-op; nextc := buffer [out]; out := out+1 mod n; … consume the item in nextc … until false; Solution is correct, but can only fill up n–1 buffer.

32 import java.util.*; public class BoundedBuffer {public BoundedBuffer() {//buffer is initially empty count = 0;in = 0;out = 0; buffer = new Object[BUFFER_SIZE]; } //producer calls this public void enter(Object item) {//Fig 4.10 } //consumer calls this method public Object remove() {//Fig 4.11} public static final int NAP_TIME = 5; private static final int BUFFER_SIZE = 5; private volatile int count; private int in;private int out; private Object[] buffer; }

33 public void enter(Object item) {while (count == BUFFER_SIZE); //do nothing //add an item to the buffer ++count; buffer[in] = item; in = (in + 1) % BUFFER_SIZE; if (count == BUFFER_SIZE) System.out.println ("Producer Entered " + item + " Buffer FULL"); else System.out.println ("Producer Entered " +item + " Buffer Size = " + count); }

34 public Object remove() { Object item; while (count == 0) ; //do nothing //remove an item from the buffer --count; item = buffer[out]; out = (out + 1) % BUFFER_SIZE; if (count == 0) System.out.println("Consumer Consumed " + item + "Buffer EMPTY"); else System.out.println("Consumer Consumed " + item + "Buffer Size = " + count); return item; }

35 Interprocess Communication An OS can provide the means for processes to communicate via an interprocess-communication (IPC) facility. It can be implemented as a message passing system. Message passing allows processes to communicate without the need for shared variables.

36 Interprocess Communication cont'd An IPC facility provides two primitive operations: send(message) and receive(message). In order for processes P and Q to send and receive messages they must establish a communication link. The communication link could be implemented by shared memory (same as before?), hardware bus, or network. A message system is particularly useful in a distributed environment, where processes may reside at different machines (use different memory).

37 Interprocess Communication cont'd How are links established? Can a link be associated with more than two processes? How many links can exist between a pair of processes? What is the capacity of the link? What is the message size? Is the link unidirectional or bidirectional?

38 Interprocess Communication cont'd Logical Implementation Options –Direct or indirect communication –Symmetric or asymmetric communication –Automatic or explicit buffering –Send by copy or send by reference –Fixed-sized or variable-sized messages

39 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 Properties of communication link –Lilnks are established automatically. –A link is associated with exactly one pair of communicating processes. –Between each pair there exists exactly one link. –The link may be unidirectional, but is usually bi-directional.

40 Indirect Communication Messages are sent to and received from mailboxes or ports. send (A, message); receive (A, message);

41 Indirect Communication Messages are directed and received from mailboxes (also referred to as ports). –send(A, message); and receive(A,message); –Each mailbox has a unique id. –Processes can communicate only if they share a mailbox. 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. –Link may be unidirectional or bi-directional. Operations –create a new mailbox –send and receive messages through mailbox –destroy a mailbox

42 Indirect Communication (Continued) Mailbox sharing –P 1, P 2, and P 3 share mailbox A. –P 1, sends; P 2 and P 3 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 Buffering A queue of messages can be attached to the link. The queue can have zero, bounded, or unbounded capacity. Bounded and unbounded capacity queues provide automatic buffering.

44 Exception Conditions In a distributed environment it is more likely that something will go wrong during communication between processes, than in a single processor environment. That error may affect only a portion of the distributed system, leaving the remainder functioning. Therefore the OS must anticipate and handle such errors.

45 Exception Conditions cont'd A process, P or Q could terminate. If P is waiting for a message from Q, and Q terminates, P will be left waiting and be blocked forever. The OS must either terminate P or notify P that Q has terminated. P sends a message to Q but Q has terminated. P can request acknowledgment of receipt. With automatic buffering P just continues execution. With no buffering P is blocked forever.

46 Exception Conditions cont'd Lost messages The OS must detect this event and resend the message. The sending process must detect this event and resend the message if it wants to. The OS must detect this event and notify the sending process that the message is lost. The sending process can choose how to proceed.

47 Exception Conditions cont'd Scrambled Messages This error can be detected by error checking codes such as checksum, parity, and CRC. Can be handled similar to the case of a lost message.


Download ppt "CS238 Lecture 4 Processes Dr. Alan R. Davis. Process Management Processes Chap 4 Threads Chap 5 CPU SchedulingChap 6 Process SynchronizationChap 7 DeadlocksChap."

Similar presentations


Ads by Google