Chapter 3: Processes. Operations on Processes

Chapter 3: Processes Operations on Processes Operations on Processes  Multiple Processes can execute concurrently in a Computer System   Proc...
Author: Gervase Little
18 downloads 0 Views 96KB Size
Chapter 3: Processes Operations on Processes

Operations on Processes



Multiple Processes can execute concurrently in a Computer System 



Processes may be Created and Deleted dynamically

How do you manually create and delete a Process?

Our Focus is: How to dynamically create and delete Processes

Operating Systems CS 33211

2

Operations on Processes Process Creation 

We use system call to create a Process 

Unix based system call: 





fork ()

The Creating Process 

Parent Process



Needs Resources (memory, CPU Time, I/O Devices) to perform Tasks

The New Process 

Child Process (of the Parent Process) 

The Child Process can create one or more processes



Needs Resources (memory, CPU Time, I/O Devices) to perform Tasks

Operating Systems CS 33211

3

Operations on Processes Process Creation 





Resource Sharing Options 

Parent and Child Share no Resources



Child Shares a subset of Parent’s Resources



Parent & Child Share All Resources

Process Tasks 

Parent and Child may execute tasks concurrently



Parent suspends execution (waits) until Child terminates

Address Space 

Child has Program Loaded into it



Child is duplicate of Parent 

Parent and Child have Separate Address Space

Operating Systems CS 33211

4

Operations on Processes Tree Process on a Typical Solaris System ( Go to Slide 21)



Process Tasks Summary   

pageout: Memory Management fsflush – File System Management init – parent for user processes

Operating Systems CS 33211

5

Operations on Processes C Program forking Separate Process ( )

 

Use the fork () system call to create a new process (Child Process) When the Child Process is Created, the Parent and Child will execute the trailing instruction after fork () 





Use the return value from fork() to make a distinction between Parent and Child Process If fork ( ) returns:   



Two different processes running a copy of the same program

Negative value  Process Creation Failed (No Child Process) 0  To the Child Process Positive Value  The Process ID of the Child Process to the Parent

Always perform a test after a fork ( ) system call

Operating Systems CS 33211

6

Operations on Processes C Program forking Separate Process ( ) [ Go to slide 20] Parent Process makes fork () system call

int main () { Pid_t pid;

pid = fork (); if (pid < 0) {

….. }

…….. }

Operating Systems CS 33211

7

Operations on Processes C Program forking Separate Process ( ) [ Go to slide 20] Suppose fork () system call is successful Parent Process int main () { Pid_t pid; pid = fork (); if (pid < 0) { ….. } …….. }

Child Process int main () { Pid_t pid; pid = fork (); if (pid < 0) { ….. } …….. }

Both Process resume execution after the system call if (pid < 0) • Both Processes have identical variables and values but different address spaces •Variable changes in a Process will not affect the other Process Operating Systems CS 33211

8

Operations on Processes C Program forking Separate Process ( ) [ Go to slide 20] Suppose fork () system call is successful Parent Process int main () { Pid_t pid; pid = fork (); pid  123 if (pid < 0) { ….. } else if (pid == 0){ execlp (“/bin/ls”, “ls, NULL); } else{ wait (NULL); printf (“Child Complete”); exit (0); } …….. }

Parent calls wait () Operating Systems CS 33211

Child Process int main () { Pid_t pid; pid = fork (); pid  0 if (pid < 0) { ….. } else if (pid == 0){ execlp (“/bin/ls”, “ls, NULL); } else{ wait (NULL); printf (“Child Complete”); exit (0); } …….. }

Child calls execlp () 9

Process Termination System Call exit( ) [ Go to slide 22] 

Process Terminates itself by issuing System Call 

exit(status) 

Calling Process Resources are deallocated by OS 

File Descriptors etc..

Operating Systems CS 33211

10

Process Termination Scenarios 

Suppose Child Process executes system call exit (stats), while Parent is executing system call wait(int *stat_loc): 

OS will notify Parent about Terminated Child Process 



Suppose Child Process executes system call exit (stats); but Parent is not executing wait( ) 

Child process becomes a zombie process  



wait( ) returns the pid of the Terminated Child process

No user or kernel space/resources Only has entry in the process table

Suppose Parent terminates a child process using system call abort (pid) [ Child exceeds its resources] 

All children processes of the child are also terminated (Cascading Termination)

Operating Systems CS 33211

11

Inter-process Communication Cooperating Processes 



Processes may run Independently  Process A can not affect or be affected by Process B’s execution It is advantageous for several Processes to Cooperate (i.e work together):  Information Sharing – Several users may want concurrent access to the same information  Computational Speed-up - Divide complex tasks to smaller tractable subtasks and assign sub-tasks to PEs for concurrent executions  Modularity – Divide system functions into separate processes (why? – efficiency, code maintenance, teamwork..)  Convenience – A User may choose to run several tasks at the same time

Operating Systems CS 33211

12

Inter-Process Communication (IPC) Cooperating Processes 

Basic Approach for IPC 

Shared Memory – Processes exchange information by reading and writing data to a shared region in memory  

 

Useful for large data Systems calls mainly to create and attach to memory followed by routine memory access calls Maximum speed-up

Message Passing – Processes exchange information by means of messages (packets)  

Advantageous for small data Disadvantage – Overhead, several system calls, kernel interventions

Operating Systems CS 33211

13

Inter-Process Communication Shared Memory 

“Producer” Process and “Consumer” Process use a buffer in shared memory   

“Producer” Process fills buffer “Consumer” Process empties the buffer Buffer is a circular array 

access based on FIFO

Bounded Buffer: fixed size 4th

Producer waits if buffer is full ((in + 1) % BUFFER_SIZE)) == out

in (Next free position)

3rd

2nd 1st

Consumer waits if buffer is empty in == out

out (first full position)

So what is the unbounded buffer? Operating Systems CS 33211

14

Inter-Process Communication Message Passing 

OS provides a set of system utilities (primitives) to facilitate packet exchange amongst Processes      

How links are established between two Processes Link capacity Message size Bi-directional and uni-directional links How to recover lost messages What happens if sender or receiver process dies

Operating Systems CS 33211

15

Inter-Process Communication Message Passing: Direct Communication Process P to Q:  



send(Q, message) P sends message to Q, then OS blocks processing in P until Q receives the message [Blocking send] receive (P, message) 

Q is blocked by the OS until a message is received from P [Blocking receive]

Operating Systems CS 33211

16

Inter-Process Communication Message Passing: Indirect Communication 

Messages are sent to and received from mailboxes (ports):  Sender/Receiver Processes must share the same mailbox – Sender/Receiver Link  Communication Link may be established with more than two Processes  Communication Link may be bi-directional or uni-directional



OS provides primitives to:  Create a new mailbox  Send and Receive Messages through the Mailbox  Delete a Mailbox

Operating Systems CS 33211

17

Inter-Process Communication Indirect Communication: Three or More Processes Assume P1, P2 and P3 share Mailbox A  Let P1 send a message to mailbox A; assume P2 and P3 execute the receive( )  Who gets the message from A?  It depends on method used to create the links:   

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 Os to select arbitrarily the receiver process

Operating Systems CS 33211

18

Client/Server Model



Server 

Process (or set of processes) that provides one or more services 



Client  



Mail server

Process that requests the services from a server Netscape, IE Explorer, Outlook

Client/Server Protocol:  

Client sends request message to the server Server performs requested service and sends reply message to Client

Operating Systems CS 33211

19

Client/Server Model End to End Communication [Go to Slide 39] 

Socket:  Endpoint for Communication 



IP Address : Port Number

Server Process listens to client request on designated port  Well known ports 

Ports assigned number < 1024 fro Specific service  



Client Process initiates request  Host assigns client a port number > 1024 



http: 80 ftp: 23

Client Socket is Host IP + assigned port Number

Each Client Request has a different socket

Operating Systems CS 33211

20