0% found this document useful (0 votes)
60 views

AOS Lect 08 Process

The document discusses process management in operating systems. It describes the wait() and waitpid() system calls which allow a parent process to wait for a child process to terminate and retrieve its exit status. Context switching is explained as the process of saving the state of the current process and loading the saved state for the next process. The key events that can trigger a process switch are also summarized.

Uploaded by

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

AOS Lect 08 Process

The document discusses process management in operating systems. It describes the wait() and waitpid() system calls which allow a parent process to wait for a child process to terminate and retrieve its exit status. Context switching is explained as the process of saving the state of the current process and loading the saved state for the next process. The key events that can trigger a process switch are also summarized.

Uploaded by

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

ADVANCED OPERATING SYSTEMS

(CS G623)
LECTURE 8: PROCESS

wait( ) and waitpid( )





pid_t wait (int *status)


pid_t waitpid (pid_t pid, int *status, int options)


If we would like to find out when a child process


has finished then we use wait( ) system call.
It will pause / suspends the parent process until one
of its child processes is exited or until a signal is
delivered whose action is to terminate the parent or
child process or to call a signal handling function.

30 August 2014

Biju K Raveendran@BITS Pilani.

If a child has already exited by the time of the


call, the function returns immediately and system
resources used by the child are freed.
The call returns the PID of the child process
(when child process terminates) on success.
The status information allows the parent process
to determine the exit status of the child process.
i.e the value returned from main or passed by exit.
If status is not a null pointer, the status
information will be written to the location to
which it points.

30 August 2014

Biju K Raveendran@BITS Pilani.

In waitpid() the value of pid can be

< -1 which means to wait for any child process whose process
group ID is equal to the absolute value of pid.
-1 which means to wait for any child process; this is the
same behavior which wait exhibits.
0 which means to wait for any child process whose process
group ID is equal to that of the calling process.
> 0 which means to wait for the child whose process ID is
equal to the value of pid.

30 August 2014

Biju K Raveendran@BITS Pilani.

The value of options is an OR of zero or more of the


following constants:


WNOHANG
 return immediately if no child has exited
WUNTRACED
 return for children which are stopped, and whose status has
not been reported
WCONTINUED
 Returns if a stopped child has been resumed by delivery of
SIGCONT

If status is not NULL, wait or waitpid store status


information in the location pointed to by status.
Return value


The process ID of the child which exited, or zero if WNOHANG


was used and no child was available, or -1 on error

30 August 2014

Biju K Raveendran@BITS Pilani.

else if (pid == 0) {
printf( This is from child
process I am exiting\n);
exit(2);
}
else {
printf(This is from parent
process\n);
}
wait(&status);
// waitpid(pid, &status,0);
// Child is no more and this part is
only for Parent
printf(Child exited already now
with exit status %d\n,status);
return 0;

#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
int main()
{
pid_t pid;
int status;
printf(fork program starting \n);
pid = fork();
if (pid < 0 ){
perror(fork failed\n);
exit(1);
}
}
30 August 2014

Biju K Raveendran@BITS Pilani.

exec() family


int execl(const char *path, const


char *arg0, ..., (char *) 0);
int execlp(const char *file, const
char *arg0, ..., (char *) 0);
int execle(const char *path, const
char *arg0, ..., (char *) 0, const
char *envp[]);
int execv(const char *path, const
char *argv[]);
int execvp(const char *file, const
char *argv[]);
int execve(const char *path, const
char *argv[],const char *envp[]);

30 August 2014

Biju K Raveendran@BITS Pilani.

C Program Forking Separate Process


int main()
{ pid_t pid;
/* fork another process */
pid = fork();
if (pid < 0) { /* error occurred */
fprintf(stderr, "Fork Failed");
exit(-1);
}
else if (pid == 0) { /* child process */
execl("/bin/ls", "ls", NULL);
}
else { /* parent process */
/* parent will wait for the child to complete */
wait (NULL);
printf ("Child Complete");
exit(0);
}
}

30 August 2014

Biju K Raveendran@BITS Pilani.

Replaces the current process with a new process


specified by the path or file arguments.
These functions belong to two types.


execl, execlp, and execle take a variable number of


arguments ending with null pointer.
execv, execvp, and execve have as their second argument
an array of strings.
The functions with name suffixed with a p different in
that they will search the PATH environment variable to
find the new program executable file.
An additional argument to the functions execle and execve
is available for passing an array of strings to be used as the
new program environment.

The function will return a value only if it fails (-1).

30 August 2014

Biju K Raveendran@BITS Pilani.

#include <stdio.h>
#include <unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
int main(){ pid_t pid;
int status;
const char *ps_argv[] =
{ps, -ax, 0};
const char *ps_envp[] =
{PATH=/bin:/usr/bin,
TERM=console,0};
printf(fork program starting \n);
pid = fork();
if (pid < 0 ){
printf(fork failed\n);
exit(1);
}

30 August 2014

else if (pid == 0){


printf( From child process, about to
execute exec\n);
execl(/bin/ps,ps,-ax,0);
//execlp(ps,ps,-ax,0);
//execle(/bin/ps,ps,ax,0,ps_envp);
//execv(/bin/ps,ps_argv);
//execvp(ps,ps_argv);
//execve(/bin/ps,ps_argv, ps_envp);
printf(Exec failed\n);
exit(2);
}
else { printf(From parent process\n); }
wait(&status); // waitpid(pid, &status,0);
printf(Child exited already now I am
exiting\n);
return 0;}

Biju K Raveendran@BITS Pilani.

10

When to Switch a Process




Clock interrupt





I/O interrupt
Memory fault


memory address is in virtual memory so it must be brought


into main memory

Trap



process has executed for the maximum allowable time slice

error or exception occurred


may cause process to be moved to Exit state

Supervisor call


such as file open

30 August 2014

Biju K Raveendran@BITS Pilani.

11

Context Switch





When CPU switches to another process, the


system must save the state of the old process
and load the saved state for the new process
via a context switch
Context of a process represented in the PCB
Context-switch time is overhead; the system
does no useful work while switching
Time dependent on hardware support

30 August 2014

Biju K Raveendran@BITS Pilani.

12

CPU Switch From Process to Process

30 August 2014

Biju K Raveendran@BITS Pilani.

13

Change of Process State










Save context of processor including program counter


and other registers
Update the process control block of the process that
is currently in the Running state
Move process control block to appropriate queue
ready; blocked; ready/suspend
Select another process for execution
Update the process control block of the process
selected
Update memory-management data structures
Restore context of the selected process

30 August 2014

Biju K Raveendran@BITS Pilani.

14

Process termination








Normal completion
Time limit expired
Errors
Failures
Operator/OS intervention
Parent terminated
Parent request

30 August 2014

Biju K Raveendran@BITS Pilani.

15

You might also like