100% found this document useful (1 vote)
27 views15 pages

Lecture 4

Uploaded by

jasonqw43
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
100% found this document useful (1 vote)
27 views15 pages

Lecture 4

Uploaded by

jasonqw43
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

CS415: Systems Programming

Process Related System Calls (Cont.)


(wait, waitpid)

Most of the slides in this lecture are either from or adapted from the slides provided by Dr. Ahmad Barghash
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
fork and exec (Create spawn function for Linux)
#include <sys/types.h>
void spawn(char* program, char** arg_list)
{
pid_t child_pid;
child_pid=fork();
if(child_pid!=0)
{
printf("Run\n");
printf(" Diff.\n");
printf(" Things\n");
}
else{
execvp(program,arg_list);
fprintf(stderr,"an error occured in execvp\n");
abort();
}
}

int main()
{
char* arg_list[]={"ls","-l","/“, NULL};
spawn(arg_list[0], arg_list);

printf("Done with the main prgram\n");


return 0;
}
Correct and false runs
Example – Process Creation using fork
• How many processes will be running by the end of the following
snippet of code?
for(int i = 0; i < 3; i++)
{
fork();
}

Before loop when i = 0 when i = 1 when i = 2

P1 P1 P1 P2 P1 P2 P3 P4

P2 P3 P4 P5 P6 P7 P8
Example – Process Creation using fork
• How many processes will be running by the end of the following
snippet of code?
Processes Tree (parent-child relationship)
for(int i = 0; i < 3; i++)
{
fork(); P1
}

P3 P2 P5

P7 P4 P6

P8
Example
The wait System Calls: wait, waitpid
• A process may wait on another process to complete its execution.
• In most systems, a parent process can create an independently executing child
process.
• The parent process may then issue a wait system call, which suspends the
execution of the parent process while the child executes.
• When the child process terminates, it returns an exit status to the operating
system, which is then returned to the waiting parent process.
• The parent process then resumes execution.
Processes - Orphans and Zombies
• A child process whose parent has terminated is referred to as orphan.
• When a child exits while its parent is not currently executing a wait(), a zombie
emerges.
• A zombie or a defunct process is a process that has been completed, but its entry remains in
the process table due to lack of correspondence between the parent and child processes.
• Usually, a parent process keeps a check on the status of its child processes through the wait()
function. When the child process has finished, the wait function signals the parent to
completely exit the process from the memory. However, if the parent fails to call the wait
function for any of its children, the parent process still shows an entry in a process table, so
this process is named a zombie process. These zombie processes might accumulate, in large
numbers, on your system and affect its performance.
The wait System Calls: wait vs. waitpid

• The wait() system call suspends execution of the current process until one
of its children terminates.
• How a process can be terminated?
• It calls exit().
• It returns (an int) from the main function.
• It receives a signal (from the OS or another process) whose default action is to terminate.
• The waitpid() system call suspends execution of the current process until
a child specified by the “pid” argument has a changed state. By default,
waitpid() waits only for terminated children, but this behavior is
modifiable via the options argument, as described later.
The syntax of the “wait” function:
pid_t wait (int *status)
• You can store the location of the status information of the child process
from the *status parameter.
• The function can return:
• Process ID of the terminated process.
• -1 if the process has no child processes at all!

• To return the status code of the child, you have to use the WEXITSTATUS
macro.
• If a process has more than one child processes, then after calling wait(),
the parent process has to be in wait state if no child terminates.
#include <stdio.h>
#include <stdlib.h> “wait” Example
#include <sys/wait.h>
#include <unistd.h>

int main(void) {
pid_t pid;
int status;
int x = 5;
int y = 10; Output:
if(fork() == 0)
{
printf("I am the child, my pid is: %d\n", getpid());
return (x+y);
}
else
{
printf(“parent pid = %d\n", getpid());
pid = wait(&status);
}

printf("Has the child process exited normaly? %d\n", WIFEXITED(status));

if(WIFEXITED(status)){
/* Child process exited normally, through `return` or `exit` */
printf("child pid = %d, return status: %d\n", pid, WEXITSTATUS(status));
printf("child pid = %d, return status: %d\n", pid, status);
}
return 0;
}
“waitpid” Syntax:
pid_t waitpid(pid_t pid, int *status, int options);
pid Description
< -1 meaning wait for any child process whose process group ID is equal to the absolute value of pid.
-1 meaning wait for any child process.
meaning wait for any child process whose process group ID is equal to that of the calling process (i.e.,
0
child and parent in the same process group).
>0 meaning wait for the child whose process ID is equal to the value of pid.
options Description
0 returns when a child has terminated (i.e., similar to wait)
WNOHANG Do not block if no child changed its state.
returns if a child has stopped. Status for traced children which have stopped is provided even if
WUNTRACED
this option is not specified.
WCONTINUED returns if a stopped child has been resumed.
Note:
• wait(&status) is equivalent to waitpid(-1,&status,0)
Waitpid contd. (status) Macro: short for macroinstruction which
means rule or pattern

After the call to waitpid, the status information stored at the location pointed to by statusPtr can be evaluated with
the following macros:

• WIFEXITED(*statusPtr)
evaluates to a nonzero (true) value if the specified process terminated normally.
• WEXITSTATUS(*statusPtr)
if the specified process terminated normally, this macro evaluates the lower 8 bits of the value passed to the exit
or _exit function or returned from main.
• WIFSIGNALED(*statusPtr)
evaluates to a nonzero (true) value if the specified process terminated because of an unhandled signal.
• WTERMSIG(*statusPtr)
if the specified process is ended by an unhandled signal, this macro evaluates to the number of that signal.
• WIFSTOPPED(*statusPtr)
(true) value if the specified process is currently stopped but not terminated.
• WSTOPSIG(*statusPtr)
if the specified process is currently stopped but not terminated, then this macro evaluates to the number of the
signal that caused the process to stop
#include <sys/wait.h> One example
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
int main()
{
Child PID is 3676
pid_t cpid, w;
int status; Exited, status=18
cpid = fork();
if (cpid == 0) { /* Code executed by child */
printf("Child PID is %ld\n", (long) getpid()); The parent process waits for the child
return 18; process until it terminates normally.
}
else { /* Code executed by parent */

w = waitpid(cpid, &status, 0); perror prints a descriptive error message


if (w == -1)
{ perror("waitpid"); exit(EXIT_FAILURE); }
if (WIFEXITED(status))
{ printf("exited, status=%d\n", WEXITSTATUS(status)); }
else if (WIFSIGNALED(status))
{printf("killed by signal %d\n", WTERMSIG(status)); }
else if (WIFSTOPPED(status))
{printf("stopped by signal %d\n", WSTOPSIG(status));}
else if (WIFCONTINUED(status))
{printf("continued\n");}
}
}
Summary about fork, exec, wait
It enables preventing zombies!
• What is the fork-exec-wait pattern?
• A common programming pattern is to call fork followed by exec and wait.
• The original process calls fork, which creates a child process.
• The child process then uses exec to start execution of a new program.
• Meanwhile the parent uses wait (or waitpid) to wait for the child process to
finish.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy