Chapter 4: Processes. Process Concept

Chapter 4: Processes n Process Concept n Process Scheduling n Operations on Processes n Cooperating Processes n Interprocess Communication n Communica...
Author: Harold McDowell
2 downloads 0 Views 854KB Size
Chapter 4: Processes n Process Concept n Process Scheduling n Operations on Processes n Cooperating Processes n Interprocess Communication n Communication in Client-Server Systems

Operating System Concepts with Java

4.1

Silberschatz, Galvin and Gagne ©2003

Process Concept n An operating system executes a variety of programs: l Batch system – jobs l Time-shared systems – user programs or tasks

n Textbook uses the terms job and process almost

interchangeably n Process – a program in execution; process execution

must progress in sequential fashion n A process includes: l program counter l stack l data section

Operating System Concepts with Java

4.2

Silberschatz, Galvin and Gagne ©2003

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 process l terminated: The process has finished execution

Operating System Concepts with Java

4.3

Silberschatz, Galvin and Gagne ©2003

Diagram of Process State

Operating System Concepts with Java

4.4

Silberschatz, Galvin and Gagne ©2003

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 with Java

4.5

Silberschatz, Galvin and Gagne ©2003

Process Control Block (PCB)

Operating System Concepts with Java

4.6

Silberschatz, Galvin and Gagne ©2003

CPU Switch From Process to Process

Operating System Concepts with Java

4.7

Silberschatz, Galvin and Gagne ©2003

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 Process migration between the various queues

Operating System Concepts with Java

4.8

Silberschatz, Galvin and Gagne ©2003

Ready Queue And Various I/O Device Queues

Operating System Concepts with Java

4.9

Silberschatz, Galvin and Gagne ©2003

Representation of Process Scheduling

Operating System Concepts with Java

4.10

Silberschatz, Galvin and Gagne ©2003

Schedulers n Long-term scheduler (or job scheduler) – selects which

processes should be brought into the ready queue n Short-term scheduler (or CPU scheduler) – selects which

process should be executed next and allocates CPU

Operating System Concepts with Java

4.11

Silberschatz, Galvin and Gagne ©2003

Addition of Medium Term Scheduling

Operating System Concepts with Java

4.12

Silberschatz, Galvin and Gagne ©2003

Schedulers (Cont.) n Short-term scheduler is invoked very frequently (milliseconds) ⇒

(must be fast) n Long-term scheduler is invoked very infrequently (seconds,

minutes) ⇒ (may be slow) n The long-term scheduler controls the degree of

multiprogramming n Processes can be described as either: l I/O-bound process – spends more time doing I/O than

computations, many short CPU bursts l CPU-bound process – spends more time doing computations; few

very long CPU bursts

Operating System Concepts with Java

4.13

Silberschatz, Galvin and Gagne ©2003

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 n Context-switch time is overhead; the system does no useful work

while switching n Time dependent on hardware support

Operating System Concepts with Java

4.14

Silberschatz, Galvin and Gagne ©2003

Process Creation n Parent process create children processes, which, in turn create

other processes, forming a tree of processes n 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 with Java

4.15

Silberschatz, Galvin and Gagne ©2003

Process Creation (Cont.) n Address space l Child duplicate of parent l Child has a program loaded into it

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 with Java

4.16

Silberschatz, Galvin and Gagne ©2003

Creating Processes in UNIX

• Use

fork system call; • Fork creates a new process child; • The child inherits the code of the parent process and receives a copy of the data of the parent; • The fork system call return the pid (process id) of the child to the parent and returns zero to the child. Operating System Concepts with Java

4.17

Silberschatz, and Gagne ©2003 © 2004 D.A. Galvin Menascé

Creating Processes in UNIX pid = 25

a = 10 P this is a copy of the P’s data

P1 pid = 124

Operating System Concepts with Java

P2

a = 10

pid = 57

4.18

Silberschatz, and Gagne ©2003 © 2004 D.A. Galvin Menascé

Creating Processes in UNIX pid = fork ( ); if pid = = 0 { /* child code * / ------exit (0); } else { /* parent code */ ------}

pid = 25 P

P1 pid = 124

Operating System Concepts with Java

P2 pid = 57

4.19

Silberschatz, and Gagne ©2003 © 2004 D.A. Galvin Menascé

C Program Forking Separate Process #include #include int 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 will wait for the child to complete */ wait(NULL); printf("Child Complete"); exit(0); } }

Operating System Concepts with Java

4.20

Silberschatz, Galvin and Gagne ©2003

Processes Tree on a UNIX System

Operating System Concepts with Java

4.21

Silberschatz, Galvin and Gagne ©2003

Process Termination n Process executes last statement and asks the operating system

to decide it (exit) l Output data from child to parent (via wait) l Process’ resources are deallocated by operating system

n Parent may terminate execution of children processes (abort) l Child has exceeded allocated resources l Task assigned to child is no longer required l If parent is exiting 4 Some operating system do not allow child to continue if its parent

terminates – All children terminated - cascading termination

Operating System Concepts with Java

4.22

Silberschatz, Galvin and Gagne ©2003

Cooperating Processes n Independent process cannot affect or be affected by the

execution of another process n Cooperating process can affect or be affected by the execution

of another process n Advantages of process cooperation l Information sharing l Computation speed-up l Modularity l Convenience

Operating System Concepts with Java

4.23

Silberschatz, Galvin and Gagne ©2003

Producer-Consumer Problem n Paradigm for cooperating processes, producer process produces

information that is consumed by a consumer process l unbounded-buffer places no practical limit on the size of the buffer l bounded-buffer assumes that there is a fixed buffer size

Operating System Concepts with Java

4.24

Silberschatz, Galvin and Gagne ©2003

Bounded-Buffer – Shared-Memory Solution

public interface Buffer { // producers call this method public abstract void insert(Object item); // consumers call this method public abstract Object remove(); }

Operating System Concepts with Java

4.25

Silberschatz, Galvin and Gagne ©2003

Bounded-Buffer – Shared Memory Solution import java.util.*; public class BoundedBuffer implements Buffer { private static final int BUFFER SIZE = 5; private int count; // number of items in the buffer private int in; // points to the next free position private int out; // points to the next full position private Object[] buffer; public BoundedBuffer () { // buffer is initially empty count = 0; in = 0; out = 0; buffer = new Object[BUFFER SIZE];

} // producers calls this method public void insert(Object item) { // Slide 4.24 } // consumers calls this method public Object remove() { // Figure 4.25 }

} Operating System Concepts with Java

4.26

Silberschatz, Galvin and Gagne ©2003

Bounded-Buffer – Insert() Method public void insert(Object item) { while (count == BUFFER SIZE) ; // do nothing -- no free buffers // add an item to the buffer ++count; buffer[in] = item; in = (in + 1) % BUFFER SIZE; }

Operating System Concepts with Java

4.27

Silberschatz, Galvin and Gagne ©2003

Bounded Buffer – Remove() Method public Object remove() { Object item; while (count == 0) ; // do nothing -- nothing to consume // remove an item from the buffer --count; item = buffer[out]; out = (out + 1) % BUFFER SIZE; return item; }

Operating System Concepts with Java

4.28

Silberschatz, Galvin and Gagne ©2003

Interprocess Communication (IPC) n Mechanism for processes to communicate and to synchronize

their actions n Message system – processes communicate with each other

without resorting to shared variables n IPC facility provides 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 n Implementation of communication link l physical (e.g., shared memory, hardware bus) l logical (e.g., logical properties)

Operating System Concepts with Java

4.29

Silberschatz, Galvin and Gagne ©2003

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 that the link can accommodate fixed or

variable? n Is a link unidirectional or bi-directional?

Operating System Concepts with Java

4.30

Silberschatz, Galvin and Gagne ©2003

Direct Communication n 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 Properties of communication link l Links are established automatically l A link is associated with exactly one pair of communicating

processes l Between each pair there exists exactly one link l The link may be unidirectional, but is usually bi-directional

Operating System Concepts with Java

4.31

Silberschatz, Galvin and Gagne ©2003

Direct Communication n 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 Properties of communication link l Links are established automatically l A link is associated with exactly one pair of communicating

processes l Between each pair there exists exactly one link l The link may be unidirectional, but is usually bi-directional

Disadvantages?

Operating System Concepts with Java

4.32

Silberschatz, Galvin and Gagne ©2003

Indirect Communication n 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

n Properties of communication link l Link established only if processes share a common mailbox l A link may be associated with many processes l Each pair of processes may share several communication

links l Link may be unidirectional or bi-directional

Operating System Concepts with Java

4.33

Silberschatz, Galvin and Gagne ©2003

Indirect Communication n Operations l create a new mailbox l send and receive messages through mailbox l destroy a mailbox

n Primitives are defined as:

send(A, message) – send a message to mailbox A receive(A, message) – receive a message from mailbox A

Operating System Concepts with Java

4.34

Silberschatz, Galvin and Gagne ©2003

Indirect Communication n Mailbox sharing l P1, P 2, and P3 share mailbox A l P1, sends; P2 and P3 receive l Who gets the message?

n Solutions l Allow a link to be associated with at most two processes l Allow only one process at a time to execute a receive operation l Allow the system to select arbitrarily the receiver. Sender is notified

who the receiver was.

Operating System Concepts with Java

4.35

Silberschatz, Galvin and Gagne ©2003

Synchronization n Message passing may be either blocking or non-blocking 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 with Java

4.36

Silberschatz, Galvin and Gagne ©2003

Blocked Send: message in process address space Process Address Space

buffer Kernel Address Space

Operating System Concepts with Java

1. Message is placed in buffer 2. Send is invoked. Process blocks. 3. Message is transmitted from buffer. 4. Process unblocks.

4.37

Silberschatz, and Gagne ©2003 © 2004 D.A. Galvin Menascé

Blocked Send: message in kernel address space Process Address Space

buffer Kernel Address Space

Operating System Concepts with Java

1. Message is placed in buffer 2. Send is invoked. Process blocks 3. Message is copied into kernel address space. Process unblocks. 4. Message is transmitted.

4.38

Silberschatz, and Gagne ©2003 © 2004 D.A. Galvin Menascé

Unblocked Send Process Address Space

buffer Kernel Address Space

Operating System Concepts with Java

1. Message is placed in buffer 2. Send is invoked. Process continues to execute. 3. Message is transmitted. 4. An interrupt is generated to the process to indicate that the buffer can be reused.

4.39

Silberschatz, and Gagne ©2003 © 2004 D.A. Galvin Menascé

Unblocked Receive Process Address Space

buffer Kernel Address Space

Operating System Concepts with Java

1. Receive is invoked. 2. If there is a message, it is copied into processes’ address space. 3. If there is no message, process continues execution. 4. The process needs to check for message arrival.

4.40

Silberschatz, and Gagne ©2003 © 2004 D.A. Galvin Menascé

Blocked Receive Process Address Space

buffer Kernel Address Space

Operating System Concepts with Java

1. Receive is invoked. 2. If there is a message, it is copied into processes’ address space. 3. If there is no message, the process is blocked until a message arrives.

4.41

Silberschatz, and Gagne ©2003 © 2004 D.A. Galvin Menascé

Blocked vs. Non-Blocked Primitives n Send: l blocked without message copying: l blocked with message copying 4 extra message copying may degrade performance l unblocked: 4 faster but awkward to program.

n Receive: l blocked: 4 preferred: avoids checking for message arrival l unblocked

Operating System Concepts with Java

4.42

Silberschatz, and Gagne ©2003 © 2004 D.A. Galvin Menascé

Buffering n Queue of messages attached to the link; implemented in one of

three ways 1. Zero capacity – 0 messages Sender must wait for receiver (rendezvous) 2. Bounded capacity – finite length of n messages Sender must wait if link full 3. Unbounded capacity – infinite length Sender never waits

Operating System Concepts with Java

4.43

Silberschatz, Galvin and Gagne ©2003

Client-Server Communication n Sockets n Remote Procedure Calls n Remote Method Invocation (Java)

Operating System Concepts with Java

4.44

Silberschatz, Galvin and Gagne ©2003

Sockets n A socket is defined as an endpoint for communication n Concatenation of IP address and port n 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 with Java

4.45

Silberschatz, Galvin and Gagne ©2003

Addressing in the C/S Model

• Machine-process addressing • Process addressing with broadcasting • Address lookup via a name server

Operating System Concepts with Java

4.46

Silberschatz, and Gagne ©2003 © 2004 D.A. Galvin Menascé

Machine Process Addressing in the C/S Model 243.23.45.6

199.28.1.5

1

Client

Server

2

12

0

Kernel

Kernel

1: Request to 243.23.45.6:0 (machine:process) 2: Reply to 199.28.1.5:12

Operating System Concepts with Java

Silberschatz, and Gagne ©2003 © 2004 D.A. Galvin Menascé

4.47

Broadcast Addressing in the C/S Model 243.23.45.6

199.28.1.5

3

Client

Server

4

12

0

Kernel 1

Kernel 1

2

1: Broadcast “Where is Server X?” 2: Server X address is 243.23.45.6:0 3: Request to 243.23.45.6:0 (machine.process) 4: Reply to 199.28.1.5:12 Operating System Concepts with Java

4.48

Silberschatz, and Gagne ©2003 © 2004 D.A. Galvin Menascé

Name Server Based Addressing in the C/S Model 199.28.1.5

1

3

S

C

4

0 Kernel

115.34.1.6

243.23.45.6

12 Kernel

NS

2

0 Kernel

1: Request Name Server to Lookup for Server X 2: Server X address is 243.23.45.6:0 3: Request to 243.23.45.6:0 (machine.process) 4: Reply to 199.28.1.5:12 Operating System Concepts with Java

4.49

Silberschatz, and Gagne ©2003 © 2004 D.A. Galvin Menascé

Addressing in the C/S Model • Machine-process addressing • not transparent • Process addressing with broadcasting • generates extra load on the system (caching can help) • Address lookup via a name server • requires centralized component (name server replication can help) Operating System Concepts with Java

4.50

Silberschatz, and Gagne ©2003 © 2004 D.A. Galvin Menascé

Client/Server Interaction REQ

SERVER

CLIENT REP

CLIENT

Operating System Concepts with Java

REQ ACK AYA? IAA REP ACK 4.51

SERVER

Silberschatz, and Gagne ©2003 © 2004 D.A. Galvin Menascé

Socket Communication

Operating System Concepts with Java

4.52

Silberschatz, Galvin and Gagne ©2003

Remote Procedure Calls n Remote procedure call (RPC) abstracts procedure calls between

processes on networked systems. n Stubs – client-side proxy for the actual procedure on the server. n The client-side stub locates the server and marshalls the

parameters. n The server-side stub receives this message, unpacks the

marshalled parameters, and peforms the procedure on the server.

Operating System Concepts with Java

4.53

Silberschatz, Galvin and Gagne ©2003

Remote Procedure Call (RPC) machine A

machine B

read (f, &buff) procedure execution

(process suspended)

Operating System Concepts with Java

4.54

Silberschatz, and Gagne ©2003 © 2004 D.A. Galvin Menascé

Remote Procedure Call (RPC) - it should be transparent to the caller that the called procedure is being executed in a remote machine. - transparency is achieved by inserting a client stub in the client side and a server stub at the server side.

Operating System Concepts with Java

Silberschatz, and Gagne ©2003 © 2004 D.A. Galvin Menascé

4.55

Remote Procedure Call Client

Server

R Client Stub

U

C

C

P

R

U P

Kernel

Server Stub

Kernel

P: pack parameters (parameter marshaling) U: unpack parameters Operating System Concepts with Java

4.56

Silberschatz, and Gagne ©2003 © 2004 D.A. Galvin Menascé

RPC: Parameter Passing n Format conversions have to be made by the client and server

stubs (e.g., integer and floating point representations). n Parameters passed by value: copied into the message. n Parameters passed by reference or pointers: copy data structure

(e.g., array) into message and restore at client stub. n Pointer passing to general data structures is a problem.

Operating System Concepts with Java

4.57

Silberschatz, and Gagne ©2003 © 2004 D.A. Galvin Menascé

RPC: Parameter Passing and Stub Generation

n Server Formal Specification: describes the server procedures,

its parameters, and parameter types, and whether the parameter is an input, output, or input/output parameter. n Compilers can generate client and server stubs automatically

from the server formal specification.

Operating System Concepts with Java

4.58

Silberschatz, and Gagne ©2003 © 2004 D.A. Galvin Menascé

RPC Semantics under Failures Server cannot be located

Client

Binder

exception

R

Client Stub

U

C P

Kernel

Kernel server not found

Operating System Concepts with Java

Silberschatz, and Gagne ©2003 © 2004 D.A. Galvin Menascé

4.59

RPC Semantics under Failures Lost Request Message

Client

Server

R Client Stub

U

C

C

P

R

U P

Kernel

Server Stub

Kernel

Client stub uses timeout and retransmission. Operating System Concepts with Java

4.60

Silberschatz, and Gagne ©2003 © 2004 D.A. Galvin Menascé

RPC Semantics under Failures Lost Reply Message Client

Server

R Client Stub

U

C

C

P

R

U P

Kernel

Server Stub

Kernel

Request retransmission may cause problem if request is not idempotent. Operating System Concepts with Java

Silberschatz, and Gagne ©2003 © 2004 D.A. Galvin Menascé

4.61

RPC Semantics under Failures Server Crashes Server

C

R U P

Server

C Server Stub

R U P

Kernel

Normal

Server

C Server Stub

U P Server

Kernel

Kernel

Crash After Execution

Stub

Crash Before Execution

= crash Operating System Concepts with Java

4.62

Silberschatz, and Gagne ©2003 © 2004 D.A. Galvin Menascé

RPC Semantics under Failures Server Crashes Server

C

R U P Server Stub

Kernel

At least once semantics: keep trying until RPC is carried out at least once. Need to watch for non-idempotent requests! At most once semantics: gives up immediately and report RPC failure. Works fine in this case.

Crash After Execution

Operating System Concepts with Java

4.63

Silberschatz, and Gagne ©2003 © 2004 D.A. Galvin Menascé

RPC Semantics under Failures Server Crashes Server

C U P Server Stub

Kernel

At least once semantics: keep trying until RPC is carried out at least once. Works fine in this case. At most once semantics: gives up immediately and report RPC failure. RPC needs to be retried by client.

Crash Before Execution

Operating System Concepts with Java

4.64

Silberschatz, and Gagne ©2003 © 2004 D.A. Galvin Menascé

RPC Semantics under Failures Client Crash Client

Server

R Client Stub

C U

C

P

R

U P

Kernel

Server Stub

Kernel

Computation at server are left orphan. Files may be left locked at the server. Operating System Concepts with Java

4.65

Silberschatz, and Gagne ©2003 © 2004 D.A. Galvin Menascé

RPC Semantics under Failures Client Crash

Reincarnation: client divides time into epochs. When it reboots it starts a new epoch and broadcast the epoch number to all machines. Orphans belonging to old epochs are killed. Gentle reincarnation: similar to reincarnation except that orphan is only killed if owner cannot be found.

Operating System Concepts with Java

4.66

Silberschatz, and Gagne ©2003 © 2004 D.A. Galvin Menascé

Execution of RPC

Operating System Concepts with Java

4.67

Silberschatz, Galvin and Gagne ©2003

Remote Method Invocation n Remote Method Invocation (RMI) is a Java mechanism similar to

RPCs. n RMI allows a Java program on one machine to invoke a method

on a remote object.

Operating System Concepts with Java

4.68

Silberschatz, Galvin and Gagne ©2003

Marshalling Parameters

Operating System Concepts with Java

4.69

Silberschatz, Galvin and Gagne ©2003

Threads n A thread (or lightweight process) is a basic unit of CPU

utilization; it consists of: l program counter l register set l stack space

n A thread shares with its peer threads its: l code section l data section l operating-system resources

collectively know as a task . n A traditional or heavyweight process is equal to a task with one

thread

Operating System Concepts with Java

4.70

Silberschatz, Galvin and Gagne ©2003

Threads (Cont.) n In a multiple threaded task, while one server thread is blocked

and waiting, a second thread in the same task can run. l Cooperation of multiple threads in same job confers higher

throughput and improved performance. l Applications that require sharing a common buffer (i.e., producer-

consumer) benefit from thread utilization. n Threads provide a mechanism that allows sequential

processes to make blocking system calls while also achieving parallelism. n Kernel-supported threads (Mach and OS/2). n User-level threads; supported above the kernel, via a set of

library calls at the user level (Project Andrew from CMU). n Hybrid approach implements both user-level and kernel-

supported threads (Solaris 2).

Operating System Concepts with Java

4.71

Silberschatz, Galvin and Gagne ©2003

Multiple Threads within a Task

Operating System Concepts with Java

4.72

Silberschatz, Galvin and Gagne ©2003

Threads Support in Solaris 2 n Solaris 2 is a version of UNIX with support for threads at the

kernel and user levels, symmetric multiprocessing, and real-time scheduling. n LWP – intermediate level between user-level threads and

kernel-level threads. n Resource needs of thread types: l Kernel thread: small data structure and a stack; thread switching does not require changing memory access information – relatively fast. l LWP: PCB with register data, accounting and memory information,; switching between LWPs is relatively slow. l User-level thread: only need stack and program counter; no kernel involvement means fast switching. Kernel only sees the LWPs that support user-level threads.

Operating System Concepts with Java

4.73

Silberschatz, Galvin and Gagne ©2003

Solaris 2 Threads

Operating System Concepts with Java

4.74

Silberschatz, Galvin and Gagne ©2003