0% found this document useful (0 votes)
20 views24 pages

Week 3a

Os Notes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views24 pages

Week 3a

Os Notes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Lecture 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

There will be a complete lab on fork() system call


C++ Program Forking Separate Process

int main(){
pid_t pid;
/* fork another process */ pid = fork();
if (pid < 0) { /* error occurred */
cout<<"Fork Failed";
exit(-1); }

else if (pid == 0) { /* child process*/


cout<<“Child”<,endl; }

else { /* parent process */


/* parent will wait for the child to complete */ wait (NULL);
cout<<“Parent”<<endl;
exit(0); }
}
Fork Example
int main()
{
fork();
fork();
fork();
printf("hello\n");
}
Output:
hello
hello
hello
hello
hello
hello
hello
hello
Fork Example

Total Number of Processes = 2n


n = 3, 23 = 8
Process Termination
 Process executes last statement and asks the operating
system to delete it (exit)
 Return status value to parent process (via wait)
 Process’ resources are deallocated by operating system
 Parent may terminate execution of children processes
(abort)
 Child has exceeded allocated resources
 Task assigned to child is no longer required
 If parent is exiting
 Some operating system do not allow child to continue if
its parent terminates
– All children terminated - cascading
termination
Cooperating Processes
 Independent process cannot affect or be affected by the
execution of another process
 Cooperating process can affect or be affected by the
execution of another process
 Advantages of process cooperation
 Information sharing
 Computation speed-up
 Modularity
 Convenience
Interprocess Communication
 Processes within a system may be independent or cooperating
 Cooperating process can affect or be affected by other processes,
including sharing data

 Cooperating processes need interprocess communication


(IPC)
 Two models of IPC
 Shared memory
 Message passing
Communications Models

Message passing Shared Memory


Message Passing vs. Shared Memory
 Message passing
 is useful for exchanging smaller amounts of data, because conflicts
don’t need to be avoided
 is easier to implement
 is implemented using system calls requiring kernel intervention
 Shared memory
 allows maximum speed and convenience of communication
 is faster, since a system call is only required to establish the shared
memory regions
 all other accesses are routine memory access, not requiring kernel
assistance
Producer-Consumer Problem
 Paradigm for cooperating processes, producer process
produces information that is consumed by a consumer
process
 unbounded-buffer places no practical limit on the size of
the buffer
 bounded-buffer assumes that there is a fixed buffer size
 Shared buffer is implemented as a circular array with two
pointers (in and out)
 In points to next free position; out points to first full
position
 Buffer is empty when in ==out
 Buffer is full when ((in+1)%buffersize) == out
Bounded-Buffer – Shared-Memory Solution

 Shared data

#define BUFFER_SIZE 10 typedef struct {


...
} item;

item buffer[BUFFER_SIZE]; int in = 0;


int out = 0;
 Solution is correct, but can only use BUFFER_SIZE-1
elements
Bounded-Buffer – Producer
The Producer Process

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

The Consumer Process

item nextConsumed;
while (true) {
while (in == out)
; // do nothing -- nothing to consume

// remove an item from the buffer


nextConsumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
return item;
}
Producer process
9 0
int in = 0;
8 int out = 0;
1
item nextProduced;
while (true) {
7 /* Produce an item */
2 while ( ((in + 1) % BUFFER_
SIZE) == out)
; /* do nothing noop -- no free
6 buffers */
buffer[in] = nextProduced;
3
in = (in + 1) % BUFFER_SIZE;
5
4 }

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

item nextConsumed; while (true) {


while (in == out)
7 ; // do nothing -- nothing
2 to consume

6 // remove an item from the buffer


nextConsumed = buffer[out];
3
out = (out + 1) % BUFFER_SIZE;
5 return item;
4
}
 We did not discuss situations where Producer and
Consumer try to access shared memory concurrently….
 These issues will be covered when we study Process
Synchronization

 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

 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)
Implementation Questions
 How are links established?
 Can a link be associated with more than two processes?
 How many links can there be between every pair of
communicating processes?
 What is the capacity of a link?
 Is the size of a message that the link can accommodate
fixed or variable?
 Is a link unidirectional or bi-directional?
 How do direct and indirect communication differ?
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
 One disadvantage of this scheme is limited modularity due to
the need for hard-coding identifiers
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

You might also like