Chapter 3: Processes: Silberschatz, Galvin and Gagne ©2018 Operating System Concepts - 10 Edition
Chapter 3: Processes: Silberschatz, Galvin and Gagne ©2018 Operating System Concepts - 10 Edition
Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
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
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 interprocess communication using shared
memory and message passing.
Design programs that uses pipes and POSIX shared memory to
perform interprocess communication.
Describe client-server communication using sockets and remote
procedure calls.
Design kernel modules that interact with the Linux operating system.
Process Concept
An operating system executes a variety of programs that run as a
process.
Process – a program in execution; process execution must progress
in sequential fashion. No parallel execution of instructions of a single
process
Multiple parts
• The program code, also called text section
• Current activity including program counter, processor registers
• Stack containing temporary data
Function parameters, return addresses, local variables
• Data section containing global variables
• Heap containing memory dynamically allocated during run time
Process Concept (Cont.)
Program is passive entity stored on disk (executable file);
process is active
• Program becomes process when an executable file is
loaded into memory
Execution of program started via GUI mouse clicks, command
line entry of its name, etc.
One program can be several processes
• Consider multiple users executing the same program
Process in Memory
Memory Layout of a C Program
Process State
Timeout
I/O
Multitasking in Mobile Systems
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
Operations on Processes
B C
B
Code inside process A:
C
int cid1=fork();
Code inside process A: int cid2=fork();
int cid=fork();
If(cid==0) Fork flood causes exponential
int cid=fork(); number of processes
While(1){
fork();
}
Creating a Separate Process via Windows API
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
Android Process Importance Hierarchy
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;
}
Consumer Process – Shared Memory
item next_consumed;
while (true) {
while (in == out)
; /* do nothing */
next_consumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
while (true) {
/* produce an item in next produced */
while (true) {
while (counter == 0)
; /* do nothing */
next_consumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
counter--;
/* consume the item in next consumed */
}
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
Physical:
• Shared memory
• Hardware bus
• Network
Logical:
• Direct or indirect
• Synchronous or asynchronous
• Automatic or explicit buffering
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 bi-directional
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
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
Message passing may be either blocking or non-blocking
Producer
message next_produced;
while (true) {
/* produce an item in next_produced */
send(next_produced);
}
Consumer
message next_consumed;
while (true) {
receive(next_consumed)
#include<mach/mach.h>
struct message {
mach_msg_header_t header;
int data;
};
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
Socket Communication
Sockets in Java
Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Producer-Consumer Problem
Paradigm for cooperating processes:
• producer process produces information that is consumed
by a consumer process
Two variations:
• unbounded-buffer places no practical limit on the size of
the buffer:
Producer never waits
Consumer waits if there is no buffer to consume
• bounded-buffer assumes that there is a fixed buffer size
Producer must wait if all buffers are full
Consumer waits if there is no buffer to consume
Cooperating Processes