Unit 1
Unit 1
• Soft real-time
– Limited utility in industrial control of robotics
– Useful in applications (multimedia, virtual reality)
requiring advanced operating-system features.
Handheld Systems
• Personal Digital Assistants (PDAs)
• Cellular telephones
• Issues:
– Limited memory
– Slow processors
– Small display screens.
Migration of Operating-System Concepts and Features
Computing Environments
• Traditional computing
• Web-Based Computing
• Embedded Computing
Operating-System Structures
• System Components
• Operating System Services
• System Calls
• System Programs
• System Structure
• Virtual Machines
• System Design and Implementation
• System Generation
Common System Components
• Process Management
• Main Memory Management
• File Management
• I/O System Management
• Secondary Management
• Networking
• Protection System
• Command-Interpreter System
Process Management
• A process is a program in execution. A
process needs certain resources, including
CPU time, memory, files, and I/O devices, to
accomplish its task.
• The operating system is responsible for the
following activities in connection with process
management.
– Process creation and deletion.
– process suspension and resumption.
– Provision of mechanisms for:
• process synchronization
• process communication
Main-Memory Management
• Memory is a large array of words or bytes, each with its
own address. It is a repository of quickly accessible data
shared by the CPU and I/O devices.
• Main memory is a volatile storage device. It loses its
contents in the case of system failure.
• The operating system is responsible for the following
activities in connections with memory management:
– Keep track of which parts of memory are currently being
used and by whom.
– Decide which processes to load when memory space
becomes available.
– Allocate and deallocate memory space as needed.
File Management
– command-line interpreter
– shell (in UNIX)
void producer(void)
{
int item;
while (TRUE) {
produce item(&item);
produce_if (count == N) sleep();
enter_item(item);
count = count + 1;
if (count == 1) wakeup(consumer);
}
}
Bounded-Buffer – Consumer Process
void consumer(void) {
int item;
while (TRUE) {
if (count == 0) sleep();
remove_item(&item);
count = count - 1;
if (count == N-1) wakeup(producer);
consume_item(item);
}
}
Interprocess Communication (IPC)
• Mechanism for processes to communicate and to
synchronize their actions.
• Message system – 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)
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
• Messages are directed and received from
mailboxes (also referred to as ports).
– 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.
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
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.
Synchronization
• Message passing may be either blocking or
non-blocking.
• Blocking is considered synchronous
• Non-blocking is considered asynchronous
• send and receive primitives may be either
blocking or non-blocking.
Buffering
• Queue of messages attached to the link;
implemented in one of three ways.
1.Zero capacity – 0 messages
Sender must wait for receiver (rendezvous).
2.Bounded capacity – finite length of n messages
Sender must wait if link full.
3.Unbounded capacity – infinite length
Sender never waits.
Client-Server Communication
• Sockets
• Remote Procedure Calls
• Remote Method Invocation (Java)
Sockets
• A socket is defined as an endpoint for
communication.
• 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.
Socket Communication
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.
Execution of RPC
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.
Marshalling Parameters
Race Condition
• Race condition: The situation where several
processes access – and manipulate shared data
concurrently. The final value of the shared data
depends upon which process finishes last.
signal (S):
S++;
Critical Section of n Processes
• Shared data:
semaphore mutex; //initially mutex = 1
• Process Pi:
do {
wait(mutex);
critical section
signal(mutex);
remainder section
} while (1);
Semaphore Implementation
• Define a semaphore as a record
typedef struct {
int value;
struct process *L;
} semaphore;
signal(S):
S.value++;
if (S.value <= 0) {
remove a process P from S.L;
wakeup(P);
}
Deadlock and Starvation
• Deadlock – two or more processes are waiting indefinitely for
an event that can be caused by only one of the waiting processes.
• Let S and Q be two semaphores initialized to 1
P0 P1
wait(S); wait(Q);
wait(Q); wait(S);
signal(S); signal(Q);
signal(Q) signal(S);
• Starvation – indefinite blocking. A process may never be
removed from the semaphore queue in which it is suspended.
Two Types of Semaphores
• Counting semaphore – integer value can
range over an unrestricted domain.
• Binary semaphore – integer value can range
only between 0 and 1; can be simpler to
implement.
• Can implement a counting semaphore S as a
binary semaphore.
Implementing S as a Binary Semaphore
• Data structures:
binary-semaphore S1, S2;
int C:
• Initialization:
S1 = 1
S2 = 0
C = initial value of semaphore S
Implementing S
• wait operation
wait(S1);
C--;
if (C < 0) {
signal(S1);
wait(S2);
}
signal(S1);
• signal operation
wait(S1);
C ++;
if (C <= 0)
signal(S2);
else
signal(S1);
Classical Problems of
Synchronization
• Bounded-Buffer Problem
• Dining-Philosophers Problem
Bounded-Buffer Problem
• Shared data
Initially:
do {
…
produce an item in nextp
…
wait(empty);
wait(mutex);
…
add nextp to buffer
…
signal(mutex);
signal(full);
} while (1);
Bounded-Buffer Problem Consumer Process
do {
wait(full)
wait(mutex);
…
remove an item from buffer to nextc
…
signal(mutex);
signal(empty);
…
consume the item in nextc
…
} while (1);
Readers-Writers Problem
• Shared data
Initially
wait(wrt);
…
writing is performed
…
signal(wrt);
Readers-Writers Problem Reader Process
wait(mutex);
readcount++;
if (readcount == 1)
wait(wrt);
signal(mutex);
…
reading is performed
…
wait(mutex);
readcount--;
if (readcount == 0)
signal(wrt);
signal(mutex):
Dining-Philosophers Problem
• Shared data
semaphore chopstick[5];
Initially all values are 1
Dining-Philosophers Problem
• Philosopher i:
do {
wait(chopstick[i])
wait(chopstick[(i+1) % 5])
…
eat
…
signal(chopstick[i]);
signal(chopstick[(i+1) % 5]);
…
think
…
} while (1);
Example – Bounded Buffer
• Shared data:
struct buffer {
int pool[n];
int count, in, out;
}
Bounded Buffer Producer Process
• Producer process inserts nextp into the shared
buffer
monitor monitor-name
{
shared variable declarations
procedure body P1 (…) {
...
}
procedure body P2 (…) {
...
}
procedure body Pn (…) {
...
}
{
initialization code
}
}
Monitors
• To allow a process to wait within the monitor,
a condition variable must be declared, as
condition x, y;
• Condition variable can only be used with the
operations wait and signal.
– The operation
x.wait();
means that the process invoking this operation is
suspended until another process invokes
x.signal();
– The x.signal operation resumes exactly one
suspended process. If no process is suspended,
then the signal operation has no effect.
Schematic View of a Monitor
Monitor With Condition Variables
Dining Philosophers Example
monitor dp
{
enum {thinking, hungry, eating} state[5];
condition self[5];
void pickup(int i) // following slides
void putdown(int i) // following slides
void test(int i) // following slides
void init() {
for (int i = 0; i < 5; i++)
state[i] = thinking;
}
}
Dining Philosophers
void pickup(int i) {
state[i] = hungry;
test[i];
if (state[i] != eating)
self[i].wait();
}
void putdown(int i) {
state[i] = thinking;
// test left and right neighbors
test((i+4) % 5);
test((i+1) % 5);
}
Dining Philosophers
void test(int i) {
if ( (state[(I + 4) % 5] != eating) &&
(state[i] == hungry) &&
(state[(i + 1) % 5] != eating)) {
state[i] = eating;
self[i].signal();
}
}