Process_acs
Process_acs
Concept
Chapter 3: Processes
Process Concept
Process Scheduling
Operations on Processes
Interprocess Communication
Examples of IPC Systems
Communication in Client-Server Systems
Process Concept
Execution options
Parent and children execute concurrently
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
Process Termination
Computation speedup
Modularity
Convenience
Message passing
Communications Models
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;
}
Bounded Buffer – Consumer
item next_consumed;
while (true) {
while (in == out)
; /* do nothing */
next_consumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
Implementation issues:
How are links established?
Operations
create a new mailbox (port)
send and receive messages through mailbox
destroy a mailbox
message next_produced;
while (true) {
/* produce an item in next produced */
send(next_produced);
}
message next_consumed;
while (true) {
receive(next_consumed);
port_allocate()
Send and receive are flexible, for example four options if mailbox
full:
Wait indefinitely
Wait at most n milliseconds
Return immediately
Temporarily cache a message
Examples of IPC Systems – Windows
Sockets
Remote Procedure Calls
Pipes
Remote Method Invocation (Java)
Ordinary Pipes
SOLUTION:
STATE MINIMUM MAXIMUM
READY 0/1 ? m
RUNNING 0 n
BLOCK 0 m
Threads
Motivation
Types of parallelism
Data parallelism – distributes subsets of the same data across
multiple cores, same operation on each
Task parallelism – distributing threads across cores, each thread
performing unique operation
As # of threads grows, so does architectural support
for threading
CPUs have cores as well as hardware threads
Consider Oracle SPARC T4 with 8 cores, and 8 hardware threads
per core
Concurrency vs. Parallelism
Many-to-One
One-to-One
Many-to-Many
Many-to-One
Race condition
Two or more processes are reading or writing some shared
data and the final result depends on who runs precisely when
Critical Section Problem
Critical region
Part of the program where the shared memory is accessed
Mutual exclusion
Prohibit more than one process from reading and writing the
shared data at the same time
Critical Section Problem
entry section
critical section
exit section
remainder
section
} while (TRUE);
2. Progress –
If no process is executing in its critical section
and there exist some processes that wish to enter their
critical section
then only the processes outside remainder section (i.e. the
processes competing for critical section, or exit section)
can participate in deciding which process will enter CS next
3. Bounded Waiting - A bound must exist on the number of times that other
processes are allowed to enter their critical sections after a process has
made a request to enter its critical section and before that request is
granted
Assume that each process executes at a nonzero speed
No assumption concerning relative speed of the n processes
Critical Section Problem
0 X out Consum
1 A er
Buffer empty=>
in=out 2 B
3 C
Buffer full=>
4 in Producer
(in+1)%size=out
5
Shared data
#define BUFFER_SIZE 10
typedef struct {
...
} item;
item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
Producer
Int count=0;
Void producer(void)
{ int next_produced;
while (true) {
/* produce an item in next produced */
Void consumer(void)
{
int next_consumed;
while (true) {
while (counter == 0)
; /* do nothing Buffer Empty */
next_consumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
counter--;
/* consume the item in next consumed */
}
}
Mutual Exclusion
Disable interrupt
After entering critical region, disable all interrupts
Since clock is just an interrupt, no CPU preemption can occur
Disabling interrupt is useful for OS itself, but not for users…
Mutual Exclusion with busy waiting
Lock variable
A software solution
A single, shared variable (lock)
before entering critical region, programs test the variable,
if 0, enter CS;
if 1, the critical region is occupied
Busy waiting
Continuously testing a variable until some value appears
Spin lock
A lock using busy waiting is call a spin lock
do {
flag[i] = true;
turn = j;
while (flag[j] && turn = = j);
critical section
flag[i] = false;
remainder section
} while (true);
Peterson’s Solution (Cont.)
do {
acquire lock
critical section
release lock
remainder section
} while (TRUE);
test_and_set Instruction
Definition:
boolean test_and_set (boolean *target)
{
boolean rv = *target;
*target = TRUE;
return rv:
}
1. Executed atomically
2. Returns the original value of passed
parameter
3. Set the new value of passed parameter to
“TRUE”.
Solution using test_and_set()
} while (true);
compare_and_swap Instruction
Definition:
int compare _and_swap(int *value, int expected, int new_value) {
int temp = *value;
if (*value == expected)
*value = new_value;
return temp;
}
1. Executed atomically
2. Returns the original value of passed parameter
“value”
3. Set the variable “value” the value of the passed
parameter “new_value” but only if “value”
==“expected”. That is, the swap takes place only
under this condition.
Solution using compare_and_swap
do {
waiting[i] = true;
key = true;
while (waiting[i] && key)
key = test_and_set(&lock);
waiting[i] = false;
/* critical section */
j = (i + 1) % n;
while ((j != i) && !waiting[j])
j = (j + 1) % n;
if (j == i)
lock = false;
else
waiting[j] = false;
/* remainder section */
} while (true);
Mutex Locks
wait(semaphore *S) {
S->value--;
if (S->value < 0) {
add this process to S->list;
block();
}
}
signal(semaphore *S) {
S->value++;
if (S->value <= 0) {
remove a process P from S->list;
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);
do {
...
/* produce an item in next_produced */
...
wait(empty);
wait(mutex);
...
/* add next produced to the buffer */
...
signal(mutex);
signal(full);
} while (true);
Bounded Buffer Problem (Cont.)
The structure of the consumer process
Do {
wait(full);
wait(mutex);
...
/* remove an item from buffer to next_consumed */
...
signal(mutex);
signal(empty);
...
/* consume the item in next consumed */
...
} while (true);
Readers-Writers Problem
do {
wait(rw_mutex);
...
/* writing is performed */
...
signal(rw_mutex);
} while (true);
Readers-Writers Problem (Cont.)
// eat
signal (chopstick[i] );
signal (chopstick[ (i + 1) % 5] );
// think
} while (TRUE);
What is the problem with this algorithm?
Dining-Philosophers Problem Algorithm
// eat
signal (chopstick[i] );
signal (chopstick[ (i + 1) % 5] );
// think
} while (TRUE);
What is the problem with this algorithm?
Dining-Philosophers Problem Algorithm (Cont.)
Deadlock handling
Allow at most 4 philosophers to be sitting simultaneously at the
table.
Allow a philosopher to pick up the forks only if both are available
(picking must be done in a critical section.
Use an asymmetric solution -- an odd-numbered philosopher
picks up first the left chopstick and then the right chopstick.
Even-numbered philosopher picks up first the right chopstick
and then the left chopstick.
QUESTION ON DINING PHILOSOPHER
PROBLEM
A solution to the Dining Philosophers Problem which
avoids deadlock is
(a) Ensure that all philosophers pick up the left fork
before the right fork
(b) Ensure that all philosophers pick up the right fork
before the left fork
(c) Ensure that all philosophers pick up the left fork
before the right fork , and all other philosophers
pick up the right fork before the left fork
(d) None of the above
SOLUTION