48
© DEEDS – OS Course WS10/11 Lecture 2 - Processes 1 Application Application- Process Application User- Process User- Process Utilities Shells, Editor, Compiler Libraries e.g. libx.a Hardware (centralized or distributed) Login Application Programming Interface (API) e.g. open, close, read, write, fork, exec, kill File System Access Control Device Drivers I/O-System Interrupt- System Networking TCP/IP, PPP, ... Process/Thread Management Memory- Management Authentication Passwords OS kernel

© DEEDS – OS Course WS10/11 Lecture 2 - Processes 1 Application Application- Process Application User- Process User- Process Utilities Shells, Editor,

Embed Size (px)

Citation preview

Page 1: © DEEDS – OS Course WS10/11 Lecture 2 - Processes 1 Application Application- Process Application User- Process User- Process Utilities Shells, Editor,

© DEEDS – OS Course WS10/11Lecture 2 - Processes 1

Application

Application-Process

Application

User-Process User-

Process

UtilitiesShells, Editor, Compiler

Librariese.g. libx.a

Hardware (centralized or distributed)

LoginApplication Programming Interface (API)e.g. open, close, read, write, fork, exec, kill

File SystemAccess Control

DeviceDrivers

I/O-SystemInterrupt-System

NetworkingTCP/IP, PPP, ...

Process/ThreadManagement

Memory-Management

AuthenticationPasswords

OS kernel

Page 2: © DEEDS – OS Course WS10/11 Lecture 2 - Processes 1 Application Application- Process Application User- Process User- Process Utilities Shells, Editor,

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

Silberschatz, Ch.3/81-116Tannenbaum Ch 2

Page 3: © DEEDS – OS Course WS10/11 Lecture 2 - Processes 1 Application Application- Process Application User- Process User- Process Utilities Shells, Editor,

© DEEDS – OS Course WS10/11Lecture 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

Page 4: © DEEDS – OS Course WS10/11 Lecture 2 - Processes 1 Application Application- Process Application User- Process User- Process Utilities Shells, Editor,

© DEEDS – OS Course WS10/11Lecture 2 - Processes 4

Page 5: © DEEDS – OS Course WS10/11 Lecture 2 - Processes 1 Application Application- Process Application User- Process User- Process Utilities Shells, Editor,

© DEEDS – OS Course WS10/11Lecture 2 - Processes 5

OS Process Management

Creating and deleting both user and system processes

Running, suspending and resuming processes Providing mechanisms for

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

Page 6: © DEEDS – OS Course WS10/11 Lecture 2 - Processes 1 Application Application- Process Application User- Process User- Process Utilities Shells, Editor,

© DEEDS – OS Course WS10/11Lecture 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

Page 7: © DEEDS – OS Course WS10/11 Lecture 2 - Processes 1 Application Application- Process Application User- Process User- Process Utilities Shells, Editor,

© DEEDS – OS Course WS10/11Lecture 2 - Processes 7

1. Multiprogramming/Concurrent Processes

CPU slice time

process #

P1

P2

P3

P4

Each process’ local execution is sequential!

Page 8: © DEEDS – OS Course WS10/11 Lecture 2 - Processes 1 Application Application- Process Application User- Process User- Process Utilities Shells, Editor,

© DEEDS – OS Course WS10/11Lecture 2 - Processes 8

Process Info Tables (1 entry per process)

Page 9: © DEEDS – OS Course WS10/11 Lecture 2 - Processes 1 Application Application- Process Application User- Process User- Process Utilities Shells, Editor,

© DEEDS – OS Course WS10/11Lecture 2 - Processes 9

Process Control Block (PCB)

Information associated with each process:

Process state Program counter/Stack Pointer CPU registers CPU scheduling information Memory-management information Resource accounting information I/O status information

Page 10: © DEEDS – OS Course WS10/11 Lecture 2 - Processes 1 Application Application- Process Application User- Process User- Process Utilities Shells, Editor,

© DEEDS – OS Course WS10/11Lecture 2 - Processes 10

PCB Usage: Switching Across Processes

Memory

Proc 1

Proc 0

global var

OS

Each process’ local execution is sequential!

Page 11: © DEEDS – OS Course WS10/11 Lecture 2 - Processes 1 Application Application- Process Application User- Process User- Process Utilities Shells, Editor,

© DEEDS – OS Course WS10/11Lecture 2 - Processes 11

Process Ordering: Scheduler

Lowest layer of process-structured OS handles interrupts, scheduling

Above that layer are sequential processes

Page 12: © DEEDS – OS Course WS10/11 Lecture 2 - Processes 1 Application Application- Process Application User- Process User- Process Utilities Shells, Editor,

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

Process Queues for Scheduling: Ready, I/O…

Job queue – set of *all* processes in the system

Ready queue – set of all processes residing in main memory, ready and waiting to execute

Device queues – set of processes waiting for an I/O device

Processes migrate among the various queues

Page 13: © DEEDS – OS Course WS10/11 Lecture 2 - Processes 1 Application Application- Process Application User- Process User- Process Utilities Shells, Editor,

© DEEDS – OS Course WS10/11Lecture 2 - Processes 13

Representation of Process Scheduling

Page 14: © DEEDS – OS Course WS10/11 Lecture 2 - Processes 1 Application Application- Process Application User- Process User- Process Utilities Shells, Editor,

© DEEDS – OS Course WS10/11Lecture 2 - Processes 14

Schedulers

Long-term scheduler (or job scheduler) – selects which processes should be brought into the ready queue from mass-storage repository

Short-term scheduler (or CPU scheduler) – selects which process from ready queue should be executed next and allocates CPU

Windows/Unix skip “long term scheduler” and put all new processes in memory for short term scheduler

* simple but stability of load handling gets affected!

Page 15: © DEEDS – OS Course WS10/11 Lecture 2 - Processes 1 Application Application- Process Application User- Process User- Process Utilities Shells, Editor,

© DEEDS – OS Course WS10/11Lecture 2 - Processes 15

Schedulers: Concurrency Control

Short-term scheduler is invoked very frequently (milliseconds) must be fast

Long-term scheduler is invoked very infrequently (seconds, minutes) can be slow CPU-bound process – mostly computations, only short

(or infrequent) I/O bursts I/O-bound process – mostly I/O than computations,

only short (or infrequent) CPU bursts

Page 16: © DEEDS – OS Course WS10/11 Lecture 2 - Processes 1 Application Application- Process Application User- Process User- Process Utilities Shells, Editor,

© DEEDS – OS Course WS10/11Lecture 2 - Processes 16

Addition of Medium Term Scheduling

Provision for swap-in/swap-out of processes tocontrol degree of multi-programming (swapping)

Page 17: © DEEDS – OS Course WS10/11 Lecture 2 - Processes 1 Application Application- Process Application User- Process User- Process Utilities Shells, Editor,

© DEEDS – OS Course WS10/11Lecture 2 - Processes 17

Concurrency, Scheduling Context Switches

Context-switch time is “overhead” (on OS + HW) the system does no useful work while switching

Page 18: © DEEDS – OS Course WS10/11 Lecture 2 - Processes 1 Application Application- Process Application User- Process User- Process Utilities Shells, Editor,

© DEEDS – OS Course WS10/11Lecture 2 - Processes 18

Core Process Issues

Management Creation Forking Termination

Communication Co-operating processes Inter-process communication

Page 19: © DEEDS – OS Course WS10/11 Lecture 2 - Processes 1 Application Application- Process Application User- Process User- Process Utilities Shells, Editor,

© DEEDS – OS Course WS10/11Lecture 2 - Processes 19

2. Process Creation Parent process create 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

Page 20: © DEEDS – OS Course WS10/11 Lecture 2 - Processes 1 Application Application- Process Application User- Process User- Process Utilities Shells, Editor,

© DEEDS – OS Course WS10/11Lecture 2 - Processes 20

Tree of Processes: Solaris

Each process has a unique ID called PID (in all OS’s)

Page 21: © DEEDS – OS Course WS10/11 Lecture 2 - Processes 1 Application Application- Process Application User- Process User- Process Utilities Shells, Editor,

© DEEDS – OS Course WS10/11Lecture 2 - Processes 21

Process Hierarchies

Parent creates a child process; child processes can create its own process (differing in address spaces, different identity w.r.t local changes, sharing mem. image/files)

Forms a 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 Windows has no concept of process hierarchy

• all processes are created equal sans heritage relations (though a parent can control a child using a “handle”)

• distinct address space from start

Page 22: © DEEDS – OS Course WS10/11 Lecture 2 - Processes 1 Application Application- Process Application User- Process User- Process Utilities Shells, Editor,

© DEEDS – OS Course WS10/11Lecture 2 - Processes 22

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

Page 23: © DEEDS – OS Course WS10/11 Lecture 2 - Processes 1 Application Application- Process Application User- Process User- Process Utilities Shells, Editor,

© DEEDS – OS Course WS10/11Lecture 2 - Processes 23

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

}}

Page 24: © DEEDS – OS Course WS10/11 Lecture 2 - Processes 1 Application Application- Process Application User- Process User- Process Utilities Shells, Editor,

© DEEDS – OS Course WS10/11Lecture 2 - Processes 24

3. Process Termination

Process executes last statement and asks OS to delete it exit() Output data from child to parent (via wait) Process’ resources are de-allocated by OS

Parent may terminate execution of children processes (abort(), 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

Page 25: © DEEDS – OS Course WS10/11 Lecture 2 - Processes 1 Application Application- Process Application User- Process User- Process Utilities Shells, Editor,

© DEEDS – OS Course WS10/11Lecture 2 - Processes 25

4. Cooperating Processes

Independent process cannot (a) affect or (b) be affected by the execution of another process

Cooperating process can (a) affect or (b) be affected by the execution of another process

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

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

Page 26: © DEEDS – OS Course WS10/11 Lecture 2 - Processes 1 Application Application- Process Application User- Process User- Process Utilities Shells, Editor,

© DEEDS – OS Course WS10/11Lecture 2 - Processes 26

5. Inter-process Communication (IPC) Models

Message Passing Shared Memory

Page 27: © DEEDS – OS Course WS10/11 Lecture 2 - Processes 1 Application Application- Process Application User- Process User- Process Utilities Shells, Editor,

© DEEDS – OS Course WS10/11Lecture 2 - Processes 27

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

Page 28: © DEEDS – OS Course WS10/11 Lecture 2 - Processes 1 Application Application- Process Application User- Process User- Process Utilities Shells, Editor,

© DEEDS – OS Course WS10/11Lecture 2 - Processes 28

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 ?

Page 29: © DEEDS – OS Course WS10/11 Lecture 2 - Processes 1 Application Application- Process Application User- Process User- Process Utilities Shells, Editor,

© DEEDS – OS Course WS10/11Lecture 2 - Processes 29

Bounded-Buffer

while (true) { /* Produce an item */

while ((in = (in + 1) % BUFFER_SIZE count) == out); /* do nothing -- no free buffers */ buffer[in] = item; in = (in + 1) % BUFFER_SIZE;

{

Insert() Producer) Remove() Consumer

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

nothing to consume /* remove an item from the buffer item = buffer[out]; out = (out + 1) % BUFFER SIZE;return item;

{

Page 30: © DEEDS – OS Course WS10/11 Lecture 2 - Processes 1 Application Application- Process Application User- Process User- Process Utilities Shells, Editor,

© DEEDS – OS Course WS10/11Lecture 2 - Processes 30

Process Ordering?

Concurrent consumer & producer access? Serialization: RAW models! Precedence sequences

Page 31: © DEEDS – OS Course WS10/11 Lecture 2 - Processes 1 Application Application- Process Application User- Process User- Process Utilities Shells, Editor,

© DEEDS – OS Course WS10/11Lecture 2 - Processes 31

5.2 Msg Based Communication

IPC: Mechanism for processes to communication 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 of communication link physical (e.g., shared memory, hardware bus) logical (e.g., logical properties)

Page 32: © DEEDS – OS Course WS10/11 Lecture 2 - Processes 1 Application Application- Process Application User- Process User- Process Utilities Shells, Editor,

© DEEDS – OS Course WS10/11Lecture 2 - Processes 32

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

Page 33: © DEEDS – OS Course WS10/11 Lecture 2 - Processes 1 Application Application- Process Application User- Process User- Process Utilities Shells, Editor,

© DEEDS – OS Course WS10/11Lecture 2 - Processes 33

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)

Page 34: © DEEDS – OS Course WS10/11 Lecture 2 - Processes 1 Application Application- Process Application User- Process User- Process Utilities Shells, Editor,

© DEEDS – OS Course WS10/11Lecture 2 - Processes 34

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

Page 35: © DEEDS – OS Course WS10/11 Lecture 2 - Processes 1 Application Application- Process Application User- Process User- Process Utilities Shells, Editor,

© DEEDS – OS Course WS10/11Lecture 2 - Processes 35

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

Page 36: © DEEDS – OS Course WS10/11 Lecture 2 - Processes 1 Application Application- Process Application User- Process User- Process Utilities Shells, Editor,

© DEEDS – OS Course WS10/11Lecture 2 - Processes 36

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.

Page 37: © DEEDS – OS Course WS10/11 Lecture 2 - Processes 1 Application Application- Process Application User- Process User- Process Utilities Shells, Editor,

© DEEDS – OS Course WS10/11Lecture 2 - Processes 37

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

Page 38: © DEEDS – OS Course WS10/11 Lecture 2 - Processes 1 Application Application- Process Application User- Process User- Process Utilities Shells, Editor,

© DEEDS – OS Course WS10/11Lecture 2 - Processes 38

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 queue Sender never waits (no blocking)

Page 39: © DEEDS – OS Course WS10/11 Lecture 2 - Processes 1 Application Application- Process Application User- Process User- Process Utilities Shells, Editor,

© DEEDS – OS Course WS10/11Lecture 2 - Processes 39

WinXP IPC Implem. (Local Call Procedure: LCP)

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 Object

handle

handle

handle

conn. req

conn. port: client

conn. port: server

shared object

Page 40: © DEEDS – OS Course WS10/11 Lecture 2 - Processes 1 Application Application- Process Application User- Process User- Process Utilities Shells, Editor,

© DEEDS – OS Course WS10/11Lecture 2 - Processes 40

6. Client-Server Communication

(Shared Memory Communication) (Message Passing) Sockets Remote Procedure Calls Remote Method Invocation (Java)

Page 41: © DEEDS – OS Course WS10/11 Lecture 2 - Processes 1 Application Application- Process Application User- Process User- Process Utilities Shells, Editor,

© DEEDS – OS Course WS10/11Lecture 2 - Processes 41

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

Page 42: © DEEDS – OS Course WS10/11 Lecture 2 - Processes 1 Application Application- Process Application User- Process User- Process Utilities Shells, Editor,

© DEEDS – OS Course WS10/11Lecture 2 - Processes 42

Socket Communication

Page 43: © DEEDS – OS Course WS10/11 Lecture 2 - Processes 1 Application Application- Process Application User- Process User- Process Utilities Shells, Editor,

© DEEDS – OS Course WS10/11Lecture 2 - Processes 43

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 peforms the procedure on the server.

Page 44: © DEEDS – OS Course WS10/11 Lecture 2 - Processes 1 Application Application- Process Application User- Process User- Process Utilities Shells, Editor,

© DEEDS – OS Course WS10/11Lecture 2 - Processes 44

Client-Server Models Behind RPC

client

server

name server

1. Register

2. Inquire

4. Send Req

3. Send Right

5. Send Reply

Page 45: © DEEDS – OS Course WS10/11 Lecture 2 - Processes 1 Application Application- Process Application User- Process User- Process Utilities Shells, Editor,

© DEEDS – OS Course WS10/11Lecture 2 - Processes 45

Execution of RPC

Page 46: © DEEDS – OS Course WS10/11 Lecture 2 - Processes 1 Application Application- Process Application User- Process User- Process Utilities Shells, Editor,

© DEEDS – OS Course WS10/11Lecture 2 - Processes 46

Remote Method Invocation

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

RMI allows a Java program on one machine to invoke a method on a remote object.

Page 47: © DEEDS – OS Course WS10/11 Lecture 2 - Processes 1 Application Application- Process Application User- Process User- Process Utilities Shells, Editor,

© DEEDS – OS Course WS10/11Lecture 2 - Processes 47

RMI: Marshalling Parameters

Page 48: © DEEDS – OS Course WS10/11 Lecture 2 - Processes 1 Application Application- Process Application User- Process User- Process Utilities Shells, Editor,

© DEEDS – OS Course WS10/11Lecture 2 - Processes 48

Summary Process

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

Scheduling Long-term Short-term (Medium-term)

Communication Shared memory Message passing

• Links• Naming, synchronization, buffering

Client-Server • Sockets• RPC• RMI (Java)