3 Process Management
3 Process Management
Outline
• Process Concept
• Process Scheduling
• Operations on Processes
• Interprocess Communication
• IPC in Shared-Memory Systems
• IPC in Message-Passing Systems
• Examples of IPC Systems
• Communication in Client-Server Systems
1
Objectives
• Identify the separate components of a process and illustrate how
they are represented and scheduled in an operating system.
• Describe how processes are created and terminated in an operating
system, including developing programs using the appropriate
system calls that perform these operations.
• Describe and contrast inter-process communication using shared
memory and message passing.
• Design programs that uses pipes and POSIX shared memory to
perform inter-process communication.
• Describe client-server communication using sockets and remote
procedure calls.
• Design kernel modules that interact with the Linux operating
system.
Process Concept
2
Process Concept (Cont.)
Process in Memory
3
Memory Structure
4
Process State
5
Diagram of Process State
6
Threads
• So far, process has a single thread of execution
• Consider having multiple program counters per process
• Multiple locations can execute at once
• Multiple threads of control -> threads
• Must then have storage for thread details, multiple program
counters in PCB
• Explore in detail in Chapter 4
7
Process Scheduling
• Process scheduler selects among available processes for
next execution on CPU core
• Goal -- Maximize CPU use, quickly switch processes onto
CPU core
• Maintains scheduling queues of processes
• Ready queue – set of all processes residing in main memory,
ready and waiting to execute
• Wait queues – set of processes waiting for an event (i.e., I/O)
• Processes migrate among the various queues
8
Representation of Process Scheduling
9
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 via a context switch
• Context of a process represented in the PCB
• Context-switch time is pure overhead; the system does no
useful work while switching
• The more complex the OS and the PCB the longer the
context switch
• Time dependent on hardware support
• Some hardware provides multiple sets of registers per CPU
multiple contexts loaded at once
• Some mobile systems (e.g., early version of iOS) allow only one
process to run, others suspended
• Due to screen real estate, user interface limits iOS provides for a
• Single foreground process- controlled via user interface
• Multiple background processes– in memory, running, but not on the
display, and with limits
• Limits include single, short task, receiving notification of events,
specific long-running tasks like audio playback
• Android runs foreground and background, with fewer limits
• Background process uses a service to perform tasks
• Service can keep running even if background process is suspended
• Service has no user interface, small memory use
10
Operations on Processes
Process Creation
11
Process Creation (Cont.)
• Address space
• Child duplicate of parent
• Child has a program loaded into it
• UNIX examples
• fork() system call creates new process
• exec() system call used after a fork() to replace the process’
memory space with a new program
• Parent process calls wait()waiting for the child to terminate
12
C Program Forking Separate Process
13
Process Termination
• Process executes last statement and then asks the operating
system to delete it using the exit() system call.
• Returns status data from child to parent (via wait())
• Process’ resources are deallocated by operating system
• Parent may terminate the execution of children processes using
the abort() system call. Some reasons for doing so:
• Child has exceeded allocated resources
• Task assigned to child is no longer required
• The parent is exiting, and the operating systems does not allow a child
to continue if its parent terminates
Process Termination
• Some operating systems do not allow child to exists if its
parent has terminated. If a process terminates, then all its
children must also be terminated.
• cascading termination. All children, grandchildren, etc., are
terminated.
• The termination is initiated by the operating system.
• The parent process may wait for termination of a child
process by using the wait()system call. The call returns
status information and the pid of the terminated process
pid = wait(&status);
• If no parent waiting (did not invoke wait()) process is a
zombie
• If parent terminated without invoking wait(), process is an
orphan
14
Android Process Importance Hierarchy
15
Interprocess Communication
Communications Models
(a) Shared memory. (b) Message passing.
16
Producer-Consumer Problem
17
Bounded-Buffer – Shared-Memory Solution
• Shared data
#define BUFFER_SIZE 10
typedef struct {
. . .
} item;
item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
item next_produced;
while (true) {
/* produce an item in next produced */
while (((in + 1) % BUFFER_SIZE) == out)
; /* do nothing */
buffer[in] = next_produced;
in = (in + 1) % BUFFER_SIZE;
}
18
Consumer Process – Shared Memory
item next_consumed;
while (true) {
while (in == out)
; /* do nothing */
next_consumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
19
Producer
while (true) {
/* produce an item in next produced */
Consumer
while (true) {
while (counter == 0)
; /* do nothing */
next_consumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
counter--;
/* consume the item in next consumed */
}
20
Race Condition
• counter++ could be implemented as
register1 = counter
register1 = register1 + 1
counter = register1
• counter-- could be implemented as
register2 = counter
register2 = register2 - 1
counter = register2
21
IPC – Message Passing
22
Implementation of Communication Link
• Physical:
• Shared memory
• Hardware bus
• Network
• Logical:
• Direct or indirect
• Synchronous or asynchronous
• Automatic or explicit buffering
Direct Communication
23
Indirect Communication
• Operations
• Create a new mailbox (port)
• Send and receive messages through mailbox
• Delete a mailbox
• Primitives are defined as:
• send(A, message) – send a message to mailbox A
• receive(A, message) – receive a message from mailbox A
24
Indirect Communication (Cont.)
• 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.
Synchronization
25
Producer-Consumer: Message Passing
• Producer
message next_produced;
while (true) {
/* produce an item in next_produced */
send(next_produced);
}
• Consumer
message next_consumed;
while (true) {
receive(next_consumed)
Buffering
26
Examples of IPC Systems - POSIX
27
IPC POSIX Consumer
28
Mach Messages
#include<mach/mach.h>
struct message {
mach_msg_header_t header;
int data;
};
29
Mach Message Passing - Server
30
Local Procedure Calls in Windows
Pipes
31
Ordinary Pipes
• Ordinary Pipes allow communication in standard producer-
consumer style
• Producer writes to one end (the write-end of the pipe)
• Consumer reads from the other end (the read-end of the pipe)
• Ordinary pipes are therefore unidirectional
• Require parent-child relationship between communicating
processes
Named Pipes
32
Communications in Client-Server Systems
• Sockets
• Remote Procedure Calls
Sockets
• A socket is defined as an endpoint for communication
• Concatenation of IP address and port – a number included at start
of message packet to differentiate network services on a host
• The socket 161.25.19.8:1625 refers to port 1625 on host
161.25.19.8
• Communication consists between a pair of sockets
• All ports below 1024 are well known, used for standard services
• Special IP address 127.0.0.1 (loopback) to refer to system on which
process is running
33
Socket Communication
Sockets in Java
34
Sockets in Java
The equivalent Date client
35
Remote Procedure Calls (Cont.)
Execution of RPC
36
End of Chapter 3
37