CS238 Lecture 4 Processes Dr. Alan R. Davis. Process Management Processes Chap 4 Threads Chap 5 CPU...

47
CS238 Lecture 4 Processes Dr. Alan R. Davis
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    223
  • download

    1

Transcript of CS238 Lecture 4 Processes Dr. Alan R. Davis. Process Management Processes Chap 4 Threads Chap 5 CPU...

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

CS238 Lecture 4Processes

Dr. Alan R. Davis

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

Process Management

• Processes Chap 4

• Threads Chap 5

• CPU SchedulingChap 6

• Process Synchronization Chap 7

• Deadlocks Chap 8

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

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.

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

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).

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

Processes cont'dProcess 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

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

Process State Diagram

InterruptExitAdmitted

I/O or event wait

New

Ready

Terminated

Running

Waiting

Schedulerdispatch

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

Processes cont'dProcess Control Block

• Process state

• Program counter

• CPU registers

• CPU scheduling information

• Memory-management information

• Accounting information

• I/O Status information

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

Process Control Block (PCB)

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

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.

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

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.

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

Ready Queue And Various I/O Device Queues

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

Representation of Process Scheduling

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

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.

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

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.

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

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.

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

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.

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

Addition of Medium Term Scheduling

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

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.

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

CPU Switch From Process to Process

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

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.

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

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.

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

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.

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

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.

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

#include <stdio.h>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);}

}

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

A Tree of Processes On A Typical UNIX System

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

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.

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

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.

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

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.

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

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.

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

Bounded-Buffer – Shared-Memory Solution

• Shared datavar 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;

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

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.

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

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 methodpublic 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; }

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

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");

elseSystem.out.println ("Producer Entered " + item + " Buffer Size = " + count);

}

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

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;}

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

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.

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

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).

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

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?

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

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

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

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.

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

Indirect Communication

• Messages are sent to and received from mailboxes or ports.

• send (A, message);

• receive (A, message);

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

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

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

Indirect Communication (Continued)

• Mailbox sharing– P1, P2, and P3 share mailbox A.– P1, sends; P2 and 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.

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

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.

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

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.

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

Exception Conditions cont'dA 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.

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

Exception Conditions cont'dLost 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.

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

Exception Conditions cont'dScrambled 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.