You are on page 1of 14

Lab 3 : Process Wait and Signal

Wait Vs waitpid
• The wait function can block the caller until a
child process terminates, whereas waitpid has
an option that prevents it from blocking.

• The waitpid function doesn't wait for the child


that terminates first; it has a number of
options that control which process it waits for.
Continued…
• For both functions, the argument statloc is a
pointer to an integer.
Example

Void pr_exit(int status)


{
if (WIFEXITED(status))
printf("normal termination:%d”,WEXITSTATUS(status));
else if (WIFSIGNALED(status))
{printf("abnormal termination:%d”, WTERMSIG(status));
else if (WIFSTOPPED(status))
printf("child stopped:%d\n", WSTOPSIG(status));
}
Continued…
#include <sys/wait.h>
Int main(void)
{
pid_t pid;
int status;
if ((pid = fork()) < 0)
printf("fork error");
else if (pid == 0) /* child */
exit(0);
Continued…
if (wait(&status) != pid) //waitpid(pid,&status,0)
printf("wait error");
pr_exit(status);
if ((pid = fork()) < 0)
printf("fork error");
else if (pid == 0)
abort(); //Generates SIGABRT
Continued…
if (wait(&status) != pid)
printf("wait error");
pr_exit(status);
}
Macro Description
WIFEXITED(status) True if status was returned for a child that terminated
normally. In this case, we can execute
WEXITSTATUS (status)
to fetch the low-order 8 bits of the argument that the child
passed to exit, _exit,or _Exit.

WIFSIGNALED (status) True if status was returned for a child that terminated
abnormally, by receipt of a signal that it didn't catch. In this
case, we can execute
WTERMSIG (status)
to fetch the signal number that caused the termination.
Additionally, some implementations (but not the Single
UNIX Specification) define the macro
WCOREDUMP (status)
that returns true if a core file of the terminated process
was generated.

WIFSTOPPED (status) True if status was returned for a child that is currently
stopped. In this case, we can execute
WSTOPSIG (status)
to fetch the signal number that caused the child to stop.

WIFCONTINUED (status) True if status was returned for a child that has been
continued after a job control stop (XSI extension to POSIX.1;
waitpid only).
Waitpid
• Waits for a specific process
• pid == 1
– Waits for any child process. In this respect, waitpid
is equivalent to wait.
• pid > 0
– Waits for the child whose process ID equals pid
• pid == 0
– Waits for any child whose process group ID equals
that of the calling process
Self Suspension
• A process must obtain its process ID and then pass
the ID as an argument to suspend.
• Pause function
– int pause(void); 
– Causes the invoking process (or thread) to sleep until a
signal is received that either terminates it or causes it
to call a signal-catching function.  
– Since pause() suspends thread execution indefinitely
unless interrupted by a signal, there is no successful
completion return value.
Continued…

• Kill
– int kill(pid_t pid, int sig);
– If pid is greater than 0, sig shall be sent to the process
whose process ID is equal to pid.
– If pid is 0, sig shall be sent to all processes (excluding
an unspecified set of system processes)
– If pid is -1, sig shall be sent to all processes (excluding
an unspecified set of system processes) for which the
process has permission to send that signal.
Continued..
• Sigaction()
–  #include <signal.h>
– int sigaction(int sig, const struct sigaction
*restrict act, struct sigaction *restrict oact); 
• The sigaction() function allows the calling
process to examine and/or specify the action
to be associated with a specific signal.
Continued…
• Alarm Function:
– The alarm function allows us to set a timer that
will expire at a specified time in the future
– When the timer expires, the SIGALRM signal is
generated
– If we ignore or don't catch this signal, its default
action is to terminate the process.
Continued…
• #include <unistd.h>
unsigned int alarm(unsigned int seconds);
• The seconds value is the number of clock
seconds in the future when the signal should
be generated.
• There is only one of these alarm clocks per
process

You might also like