© DEEDS – OS Course WS11/12 Lecture 2 - Processes Administrative Issues Exercise office hour...

37
© DEEDS – OS Course WS11/12 Lecture 2 - Processes Administrative Issues Exercise office hour rescheduled: Wed 2-3 PM (Lab office hour remains the same: Wed 3-4 PM) Doodle poll outcome: do the lecture in one shot & finish 10 min earlier (14:5) Lab dates online: First lab issued next week (Nov 3 rd ) Topic: Linux Inter-Process Communication (IPC) Submission deadline: Nov 23 rd Testing: TBA

Transcript of © DEEDS – OS Course WS11/12 Lecture 2 - Processes Administrative Issues Exercise office hour...

© DEEDS – OS Course WS11/12Lecture 2 - Processes

Administrative Issues

Exercise office hour rescheduled: Wed 2-3 PM(Lab office hour remains the same: Wed 3-4 PM)

Doodle poll outcome: do the lecture in one shot& finish 10 min earlier (14:5)

Lab dates online: First lab issued next week (Nov 3rd) Topic: Linux Inter-Process Communication (IPC) Submission deadline: Nov 23rd

Testing: TBA

Processes (and Threads)• Features, management, scheduling…• Inter-process communication• Threads• Classical IPC/Thread Problems

Silberschatz, Ch.3/101-141Tannenbaum Ch 2.1/81-93

© DEEDS – OS Course WS11/12Lecture 2 - Processes 3

Process

Process: A program in “execution”

Program passive entity (specification)Process active entity (execution of the specification)

…with requisite data and resources

Processes creation & execution need resources CPU, memory, files,… Initialization data, I/O,…

Processes need management Multiple processes (user, kernel) concurrently on one

or more CPUs/cores (“multi-programming”)

© DEEDS – OS Course WS11/12Lecture 2 - Processes 4

© DEEDS – OS Course WS11/12Lecture 2 - Processes 5

OS Process Management

Creating and deleting both user and system processes

Running, suspending and resuming processes Providing mechanisms for

process resource allocation, handling, reclaiming process communication -------------------------- process scheduling process synchronization process deadlock handling

© DEEDS – OS Course WS11/12Lecture 2 - Processes 6

The Process States

Processes Termination Conditions- Normal exit (voluntary)- Error exit (voluntary)- Fatal error (involuntary)- Killed by another process (involuntary)

blocked

As a process executes, it changes state: new: Process (parameters) initialized ready: Waiting to be assigned to CPU running: Instructions are being executed waiting: Waiting for data/I/O or events terminated: Finished execution

© DEEDS – OS Course WS11/12Lecture 2 - Processes 7

1. Multiprogramming/Concurrent Processes

CPU slice time

process #

P1

P2

P3

P4

Each process’ local execution is sequential!

© DEEDS – OS Course WS11/12Lecture 2 - Processes 8

Process Info Tables (1 entry per process)

© DEEDS – OS Course WS11/12Lecture 2 - Processes 9

PCB Usage: Switching Across Processes

Memory

Proc 1

Proc 0

global var

OS

Each process’ local execution is sequential!

Context-switch time is “overhead” (on OS + HW)

the system does no useful work while switching

© DEEDS – OS Course WS11/12Lecture 2 - Processes 10

Core Process Issues

Management Creation Forking Termination

Communication Co-operating processes Inter-process communication

© DEEDS – OS Course WS11/12Lecture 2 - Processes 11

2. Process Creation Parent process creates children processes, which, in turn create

other processes, forming a tree of processes (inter-process naming & ordering considered later)

Issue/Options: Resource sharing Parent and children share all resources Children share subset of parent’s resources Parent and child share no resources

Issue/Options: Execution Parent and children execute concurrently Parent waits until some/all children terminate

Issue/Option: Address space Child is a duplicate of parent Child has another program loaded into it

UNIX fork system call creates new (clone) process exec system call used after a fork to replace the process’ memory

space with a new program

© DEEDS – OS Course WS11/12Lecture 2 - Processes 12

Tree of Processes: Solaris

Each process has a unique ID called PID

© DEEDS – OS Course WS11/12Lecture 2 - Processes 13

Process Hierarchies

Parent creates a child process; child processes can become a standalone process (differing in address space, state w.r.t. local changes, possibly sharing memory/files)

Parent/child relation results in hierarchy UNIX calls this a “process group”

• Parent – child relation cannot be dropped• Parent – child maintain distinct address spaces; initially

child inherits/shares parent’s address space contents Windows has a different concept of process hierarchy

• processes can be created sans implicit heritage relations (though a parent can control a child using a “handle”)

• distinct address space from start

© DEEDS – OS Course WS11/12Lecture 2 - Processes 14

Process Creation (UNIX)

fork system call creates new (clone) process

exec system call used after a fork to replace the process’ memory space with a new program

parent “waits” till child finishes execution

© DEEDS – OS Course WS11/12Lecture 2 - Processes 15

C Program: Forking Separate Process (UNIX)

int main(){

pid_t pid;pid = fork(); /* fork a child 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 (NULL); /* parent will wait for the child to complete */

printf ("Child Complete");exit(0);

}}

© DEEDS – OS Course WS11/12Lecture 2 - Processes 16

3. Process Termination

Process executes last statement and asks OS to delete it exit() Return status value to parent (via wait) Process’ resources are de-allocated by OS

Parent may terminate execution of children processes (kill(), TerminateProcess()) if Child has exceeded allocated resources Task assigned to child is no longer required Parent is exiting

• Some operating system do not allow child to continue if its parent terminates (zombie control)

*All children terminated - cascading termination

© DEEDS – OS Course WS11/12Lecture 2 - Processes 17

4. Cooperating Processes

Independent processes cannot (a) affect or (b) be affected by each other’s execution

Cooperating processes can (a) affect or (b) be affected by each other’s execution

Process cooperation issues+ Information sharing + Speed-up/Convenience: subtask computation model for

distributed systems; multiprogramming basis+ Modularity- Ordering, precedence relations, deadlocks… (later lectures)

© DEEDS – OS Course WS11/12Lecture 2 - Processes 18

5. Inter-process Communication (IPC) Models

Message Passing Shared Memory

© DEEDS – OS Course WS11/12Lecture 2 - Processes 19

5.1 Shared-Memory: Producer-Consumer Models

Cooperating processes: Producer process produces information (eg. printer program) that is consumed by a Consumer process (eg. printer)

unbounded-buffer - no practical limit on buffer size bounded-buffer - assumes fixed buffer size

buffer

© DEEDS – OS Course WS11/12Lecture 2 - Processes 20

Bounded-Buffer: Shared-Memory Solution

Shared data

#define BUFFER_SIZE 10typedef struct {

. . .} item;

item buffer[BUFFER_SIZE]; // a circular array

int in = 0; // next free position

int out = 0; // first full position

in == out ? ((in + 1) % BUFFER_SIZE) == out ?

© DEEDS – OS Course WS11/12Lecture 2 - Processes 21

Bounded-Buffer

while (true) { // do nothing -- no free bufferswhile (((in + 1) % BUFFER_SIZE)

== out) ;

// produce an itembuffer[in] = item;in = (in + 1) % BUFFER_SIZE;

}

Insert() Producer Remove() Consumer

while (true) {// do nothing -- nothing to consumewhile (in == out) ;

// remove an item from the bufferitem = buffer[out];out = (out + 1) % BUFFER_SIZE;process(item);

}

Concurrent consumer & producer access?

© DEEDS – OS Course WS11/12Lecture 2 - Processes 22

5.2 Msg Based Communication

IPC: Mechanism for processes to communicate and synchronize their actions [shared memory; msg passing]

Message passing – processes communicate with each other without resorting to shared variables

IPC facility provides 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

Implementation/design of the communication link physical (shared memory, hardware bus) logical (logical properties)

© DEEDS – OS Course WS11/12Lecture 2 - Processes 23

Link Implementation Issues How to establish the links? 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 that a link can

accommodate be fixed or variable? Is a link unidirectional or bi-directional?

Direct Communication Indirect Communication

Synchronization Aspects

© DEEDS – OS Course WS11/12Lecture 2 - Processes 24

A. Direct Communication

Processes must name each other explicitly: A: send (P, message) – A sends a message to

process P B: receive(Q, message) – B receives a message

from process Qo Symmetric: send(P,msg); receive(Q, msg)o Asymmetric: send(P, msg); receive(pid, msg)

Properties of communication link Links are established automatically Link 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 Queue models (zero, bounded, unbounded)

© DEEDS – OS Course WS11/12Lecture 2 - Processes 25

B. Indirect Communication (Producer/Consumer?)

Messages are directed and received from mailboxes (Unix) … also referred to as ports Each mailbox (X) has a unique id send(X,msg),

receive(X,msg) Processes can communicate only if they share a

mailbox Unix pipes: cat xyz | lpr ; implicit FIFO 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

© DEEDS – OS Course WS11/12Lecture 2 - Processes 26

Indirect Communication

Operations create a new mailbox (buffer) send and receive messages through mailbox destroy a mailbox

Primitives are defined as:send(A, message) – send a message to mailbox A

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

© DEEDS – OS Course WS11/12Lecture 2 - Processes 27

Indirect Communication

Mailbox sharing P1, P2, and P3 share mailbox A P1, sends; P2 and P3 execute receive Who gets the message?

Solution choices based on whether we 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.

© DEEDS – OS Course WS11/12Lecture 2 - Processes 28

Synchronization Issues

Message passing may be either blocking or non-blocking

Blocking(B) is synchronous/lockstep B.Send: has the sender block until the message is received B.Receive: has the receiver block until a message is available “Rendezvous” across blocking send and receive; no buffers Is this similar to shared memory communication?

Non-blocking(NB) is asynchronous NB.Send: has the sender send the message and continue NB.Receive: has the receiver receive a valid message or null

© DEEDS – OS Course WS11/12Lecture 2 - Processes 29

Issues: Buffering (Direct or Indirect)

Messages of communicating processes reside in temporary (linked) queues & implemented as:

1. Zero capacity – 0 messages ie. no msg. waiting possible Sender must wait for receiver (Blocking/Lockstep/Rendezvous)

2. Bounded capacity – Finite buffer length of n messagesSender must wait if link full (blocking only on wait)

3. Unbounded capacity – Infinite length queueSender never waits (no blocking)

© DEEDS – OS Course WS11/12Lecture 2 - Processes 30

Windows IPC Implem. (Local Procedure Call: LPC)

Client opens a handle to subsystem connection port

Client sends a conn. req. Server creates 2 private

conn. ports and returns handle to one of them to the client

Client and server use the corresponding port handle to send msgs. or callbacks (and to listen to replies)

Client Server

Conn.Port

handle

handle

handle

conn. req

conn. port: client

conn. port: server

shared object

© DEEDS – OS Course WS11/12Lecture 2 - Processes 31

6. Client-Server Communication

(Shared Memory Communication) (Message Passing) Sockets Remote Procedure Calls

© DEEDS – OS Course WS11/12Lecture 2 - Processes 32

Sockets

Socket: an endpoint for communication. A pair of communicating processes employ sockets at each end (1 per process)

Concatenation of IP address and port The socket 161.25.19.8:1625 refers to port

1625 on host 161.25.19.8 Communication occurs between socket pairs

© DEEDS – OS Course WS11/12Lecture 2 - Processes 33

Socket Communication

© DEEDS – OS Course WS11/12Lecture 2 - Processes 34

Remote Procedure Calls

Remote procedure call (RPC) abstracts procedure calls between processes on networked systems.

Stubs – client-side proxy for the actual procedure on the server.

The client-side stub locates the server and marshalls the parameters.

The server-side stub receives this message, unpacks the marshalled parameters, and performs the procedure on the server.

© DEEDS – OS Course WS11/12Lecture 2 - Processes 35

Client-Server Models Behind RPC

client

server

name server

1. Register

2. Inquire

4. Send Req

3. Send Right

5. Send Reply

© DEEDS – OS Course WS11/12Lecture 2 - Processes 36

Execution of RPC

© DEEDS – OS Course WS11/12Lecture 2 - Processes 37

Summary

Process Definition, PCB States (& changing states) Creation, forking, termination

Communication Shared memory Message passing

• Links• Naming, synchronization, buffering

Client-Server • Sockets• RPC