Cooperative Processes - Introduction to Operating System - Lecture Notes, Study notes of Operating Systems

Cooperative processes, Producer problem, Interprocess communication, Consumer problem, Process Synchronization, The wait () system call, Zombie process, Code structure . Above mentioned are key points of this lecture handout. Virtual University handout for introduction to operating system are in detail and explanatory.

Typology: Study notes

2011/2012

Uploaded on 11/06/2012

ahsen
ahsen 🇵🇰

4.6

(88)

84 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
39
Operating Systems--[CS-604] Lecture No. 7
Operating Systems
Lecture No. 7
Reading Material
Operating Systems Concepts, Chapter 4
UNIX/Linux manual pages for execlp(), exit(), and wait() system calls
Summary
The execlp(), wait(), and exec() system calls and sample code
Cooperating processes
Producer-consumer problem
Interprocess communication (IPC) and process synchronization
The wait() system call
The wait system call suspends the calling process until one of the immediate children
terminate, or until a child that is being traced stops because it has hit an event of interest.
The wait will return prematurely if a signal is received. If all child processes stopped or
terminated prior to the call on wait, return is immediate. If the call is successful, the
process ID of a child is returned. If the parent terminates however all its children have
assigned as their new parent, the init process. Thus the children still have a parent to
collect their status and execution statistics. The synopsis of the wait system call is as
follows:
#include <sys/types.h>
#include <sys/wait.h>
pid_t wait(int *stat_loc);
A zombie process is a process that has terminated but whose exit status has not yet been
received by its parent process or by init. Sample code showing the use of fork() and
wait() system calls is given in Figure 7.1 below.
#include <stdio.h>
void main(){
int pid, status;
pid = fork();
if(pid == -1) {
printf(“fork failed\n”);
exit(1);
}
if(pid == 0) { /* Child */
printf(“Child here!\n”);
exit(0);
}
else { /* Parent */
wait(&status);
pf3
pf4

Partial preview of the text

Download Cooperative Processes - Introduction to Operating System - Lecture Notes and more Study notes Operating Systems in PDF only on Docsity!

Operating Systems--[CS-604] Lecture No. 7

Operating Systems

Lecture No. 7

Reading Material

ƒ Operating Systems Concepts, Chapter 4 ƒ UNIX/Linux manual pages for execlp(), exit(), and wait() system calls

Summary

ƒ The execlp(), wait(), and exec() system calls and sample code ƒ Cooperating processes ƒ Producer-consumer problem ƒ Interprocess communication (IPC) and process synchronization

The wait() system call

The wait system call suspends the calling process until one of the immediate children terminate, or until a child that is being traced stops because it has hit an event of interest. The wait will return prematurely if a signal is received. If all child processes stopped or terminated prior to the call on wait, return is immediate. If the call is successful, the process ID of a child is returned. If the parent terminates however all its children have assigned as their new parent, the init process. Thus the children still have a parent to collect their status and execution statistics. The synopsis of the wait system call is as follows: #include <sys/types.h> #include <sys/wait.h> pid_t wait(int *stat_loc); A zombie process is a process that has terminated but whose exit status has not yet been received by its parent process or by init. Sample code showing the use of fork() and wait() system calls is given in Figure 7.1 below.

#include <stdio.h> void main(){ int pid, status; pid = fork(); if(pid == -1) { printf(“fork failed\n”); exit(1); } if(pid == 0) { /* Child / printf(“Child here!\n”); exit(0); } else { / Parent */ wait(&status);

printf(“Well done kid!\n”); exit(0); } } Figure 7.1 Sample code showing use of the fork() and wait() system calls

The execlp() system call

Typically, the execlp() system call is used after a fork() system call by one of the two processes to replace the process’ memory space with a new program. The new process image is constructed from an ordinary, executable file. This file is either an executable object file, or a file of data for an interpreter. There can be no return from a successful exec because the calling process image is overlaid by the new process image. In this manner, the two processes are able to communicate and then go their separate ways. The synopsis of the execlp() system call is given below:

#include <unistd.h> int execlp (const char *file, const,char *arg0, ..., const char *argn,(char *)0); Sample code showing the use of fork() and execlp() system calls is given in Figure 7.2 below.

#include <stdio.h> void main() { int pid, status;

pid = fork(); if(pid == -1) { printf(“fork failed\n”); exit(1); } if(pid == 0) { /* Child / if (execlp(“/bin/ls”, “ls”, NULL)< 0) { printf(“exec failed\n”); exit(1); } } else { / Parent */ wait(&status); printf(“Well done kid!\n”); exit(0); } } Figure 7.2 Sample code showing use of fork(), execlp(), wait(), and exit()

Figure 7.4 The producer-consumer problem

Figure 7.5 shows the shared buffer and other variables used by the producer and consumer processes.

#define BUFFER_SIZE 10 typedef struct { … } item; item buffer[BUFFER_SIZE]; int in=0; int out=0;

Figure 7.5 Shared buffer and variables used by the producer and consumer processes

The shared buffer is implemented as a circular array with two logical pointers: in an out. The ‘in’ variable points to the next free position in the buffer; ‘out’ points to the first full position in the buffer. The buffer is empty when in==out, the buffer is full when ((in+1)%BUFFER_SIZE)==out. The code structures for the producer and consumer processes are shown in Figure 7.6.

Producer Process while(1) { /Produce an item in nextProduced/ while(((in+1)%BUFFER_SIZE)==out); /do nothing/ buffer[in]=nextProduced; in=(in+1)%BUFFER_SIZE; } Consumer Process while(1) { while(in == out); //do nothing nextConsumed=buffer[out]; out=(out+1)%BUFFER_SIZE; /Consume the item in nextConsumed/ }

Figure 7.6 Code structures for the producer and consumer processes

Producer Consumer

Empty Pool

Full Pool