Operating Systems - ch4-F06
Operating Systems - ch4-F06
Chapter 4: Processes
Process Concept Process Scheduling Operations on Processes Cooperating Processes Interprocess Communication Communication in Client-Server Systems
Chapter 4
Operating Systems
Process Concept
An operating system executes a variety of programs: Batch system jobs Time-shared systems user programs or tasks Textbook uses the terms job and process almost interchangeably Process a program in execution; process execution must progress in sequential fashion A process includes: program counter stack data section
Chapter 4 Operating Systems 3
Process State
As a process executes, it changes state new: The process is being created running: Instructions are being executed waiting: The process is waiting for some event to occur ready: The process is waiting to be assigned to a process terminated: The process has finished execution
Chapter 4 Operating Systems 4
Chapter 4
Operating Systems
Chapter 4
Operating Systems
Chapter 4
Operating Systems
Chapter 4
Operating Systems
Chapter 4
Operating Systems
Schedulers
Long-term scheduler (or job scheduler) selects which processes should be brought into the ready queue Short-term scheduler (or CPU scheduler) selects which process should be executed next and allocates CPU Medium-term scheduler removes partially executed (incomplete) processes in and out of memory
Chapter 4
Operating Systems
10
Chapter 4
Operating Systems
11
Schedulers (Cont.)
Short-term scheduler is invoked very frequently (milliseconds) (must be fast) Long-term scheduler is invoked very infrequently (seconds, minutes) (may be slow) The long-term scheduler controls the degree of multiprogramming Processes can be described as either: I/O-bound process spends more time doing I/O than computations, many short CPU bursts CPU-bound process spends more time doing computations; few very long CPU bursts 12 Chapter 4 Operating Systems
Context Switch
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 Context-switch time is overhead; the system does no useful work while switching Time dependent on hardware support
Chapter 4
Operating Systems
13
Process Creation
Parent process create children processes, which, in turn create other processes, forming a tree of processes Resource sharing Parent and children share all resources Children share subset of parents resources Parent and child share no resources Execution Parent and children execute concurrently Parent waits until children terminate
Chapter 4
Operating Systems
14
Chapter 4
Operating Systems
15
Chapter 4
16
Chapter 4
Operating Systems
17
Process Termination
Process executes last statement and asks the operating system to delete it (exit) Output data from child to parent (via wait) Process resources are deallocated by operating system Parent may terminate execution of children processes (abort) Case 1: Child has exceeded allocated resources Case 2: Task assigned to child is no longer required Case 3: parent is exiting Some operating system do not allow child to continue if its parent terminates All children terminated - cascading termination
Chapter 4 Operating Systems 18
Cooperating Processes
Independent process cannot affect or be affected by the execution of another process Cooperating process can affect or be affected by the execution of another process (parallel programming) Advantages of process cooperation
Information sharing (concurrent users interested in the same information). Computation speed-up Modularity Convenience (single user can issues several task in the same time)
Chapter 4
Operating Systems
19
Shared memory : Producer-Consumer Problem Paradigm for cooperating processes, producer process produces information that is consumed by a consumer process unbounded-buffer places no practical limit on the size of the buffer bounded-buffer assumes that there is a fixed buffer size
Chapter 4
Operating Systems
21
Bounded-Buffer Shared-Memory Solution Shared data #define BUFFER_SIZE 10 Typedef struct { ... } item;
item buffer[BUFFER_SIZE]; int in = 0;// points to next free location int out = 0;// points to the first full location
Buffer is FULL if (in+1 % BUFFER_SIZE ==out) Buffer is Empty if in == out Solution is correct, but can only use BUFFER_SIZE-1 elements
Chapter 4 Operating Systems 22
while (true) { /* Produce an item */ while ( (in + 1) % BUFFER_SIZE_count) == out) ; /* do nothing -- no free buffers */ buffer[in] = item; in = (in + 1) % BUFFER SIZE; {
Chapter 4
Operating Systems
23
Chapter 4
Operating Systems
24
Message passing
Message system processes communicate with each other without resorting to shared variables Useful in distributed environments. 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 features: Direct/Indirect synchronous/asynchronous )
Chapter 4
Operating Systems
25
Implementation Questions
How are links established? 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 the link can accommodate fixed or variable? Is a link unidirectional or bi-directional?
Chapter 4
Operating Systems
26
Communications Models
Chapter 4
Operating Systems
27
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
Links 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 bidirectional
Indirect Communication
Messages are directed and received from mailboxes (also referred to as ports) Mailbox is an object in which messages are placed into 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
Chapter 4 Operating Systems 29
Indirect Communication
Operations create a new mailbox 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
Chapter 4
Operating Systems
30
Indirect Communication
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.
Chapter 4 Operating Systems 31
Chapter 4
Operating Systems
32
Synchronization
Message passing may be either blocking or non-blocking Blocking is considered synchronous Blocking send has the sender block until the message is received Blocking receive has the receiver block until a message is available Non-blocking is considered asynchronous Non-blocking send has the sender send the message and continue Non-blocking receive has the receiver receive a valid message or null
Chapter 4 Operating Systems 33
Buffering
Regardless whether the communication is direct or indirect the messages must reside in temporary queue. Queue of messages attached to the link; implemented in one of three ways 1. Zero capacity the queue has 0 length Sender must block for receiver 2. Bounded capacity finite length of n messages -If queue is not full sender send and continue If queue is full Sender must block 3. Unbounded capacity infinite length Sender never waits
Chapter 4 Operating Systems 34
Client-Server Communication
Sockets Remote Procedure Calls Remote Method Invocation (Java)
Chapter 4
Operating Systems
35
Sockets
A socket is defined as an endpoint for communication Any pair of processes wish to communicates must employ a pair of sockets. One on each side. Concatenation of IP address and port The socket 161.25.19.8:1625 refers to port 1625 on host 161.25.19.8 Communication consists between a pair of sockets Sockets are client-server architecture, where server listen to well-known ports. Once message is received on these ports it accepts the connection from client socket.
Chapter 4 Operating Systems 36
Socket Communication
* How a process in different computer can establish a connection with the web server ? * How could another process in the samer computer establish a connection with the web server ?
Chapter 4 Operating Systems 37
Chapter 4
Operating Systems
38
Execution of RPC
RPC steps: 1-Client invokes RPC.
2-The suitable stub on the client is called with the procedure parameters.
3-The stub locates procedure port on the server, marshalls the parameters and sends the request. 4-The server stub receives the request, unmarshalls the parameters, performs the required procedure and returns the result.
Chapter 4
Operating Systems
39
Chapter 4
Operating Systems
40
Marshalling Parameters
Stub: Client side proxy that is called when the client invokes a remote object. Skeleton: server side proxy that is responsible for unmarshalling the parameters and calling the desired remote method.
Chapter 4
Operating Systems
41