Week 3a
Week 3a
OPERATING SYSTEMS
Process Creation
Parent process create children processes, which, in turn
create other processes, forming a tree of processes
Generally, process identified and managed via a process
identifier (pid)
Resource sharing
Parent and children share all resources
Children share subset of parent’s resources
Parent and child share no resources
Execution
Parent and children execute concurrently
Parent waits until children terminate
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
Process Creation
int main(){
pid_t pid;
/* fork another process */ pid = fork();
if (pid < 0) { /* error occurred */
cout<<"Fork Failed";
exit(-1); }
Shared data
item nextProduced;
while (true) {
/* Produce an item */
while ( ((in + 1) % BUFFER_SIZE)
== out)
; /* do nothing noop -- no free buffers
*/ buffer[in] = nextProduced;
in = (in + 1) % BUFFER_SIZE;
}
Bounded Buffer – Consumer
item nextConsumed;
while (true) {
while (in == out)
; // do nothing -- nothing to consume
Suppose items produced are integers: 67, 45, 2, 15, 44, 7, 3, 21, 36, 58
Consumer process
9 0
int in = 0;
int out = 0;
8
1
Home work
Previous solution allowed BUFFER_SIZE-1 items in the
buffer at one time.
Write a solution to the producer/consumer problem where
BUFFER_SIZE items can be in the buffer at the same
time.
Interprocess Communication – Message Passing