C program on "waipid" system call usage [WIFSTOPPED, WIFEXITED, WIFSIGNALED,WCOREDUMP]
"From the code in the above weblink, "wait" system call is replaced with "waitpid". Changes are made accordingly in the code."
Neelkanth_39$ cat parent_main_2.c
#include <errno.h>
#include <stdio.h>
#include <sys/wait.h>
#include <stdlib.h>
int main()
{
pid_t
child_pid;
int status;
/* now
create new process */
child_pid =
fork();
int ret;
if (child_pid >= 0) /* fork succeeded */
{
if (child_pid == 0) /* fork() returns 0 for the child process */
{
printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
printf("start child process!\n");
printf("child PID = %d, parent pid = %d\n",
getpid(), getppid());
char *cmd[] = {"./child1", "neel", "rockstar", NULL};
return execvp(cmd[0], cmd); // call child1 process
}
else /* parent process */
{
printf("*****************************************************\n");
printf("parent process!\n");
printf("parent PID = %d, child pid = %d\n",
getpid(), child_pid);
/* wait for child to exit, and store child's exit status */
printf("WAIT FOR THE CHILD PROCESS TO TERMINATE\n");
printf("*****************************************************\n");
ret = waitpid(child_pid, &status, WUNTRACED);
if (ret == -1) {
perror("waitpid");
exit(1);
}
/*
WIFSTOPPED(wstatus)
returns true if the child process was stopped by
delivery of a
signal; this is possible only if the call was done
using WUN‐
TRACED or when the child is being traced (see ptrace(2)).
kill -19 [SIGSTOP]
*/
if (WIFSTOPPED(status)) {
printf("stat value: %d Exit status:
%d\n", status, WSTOPSIG(status));
psignal(WSTOPSIG(status), "child received STOP
signal: "
"Exit
signal");
printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
}
/*If child has exited on return from it's main function or exit(0): */
if (WIFEXITED(status)) {
printf("Child process has exited. exit code:
%d\n", WEXITSTATUS(status));
printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
}
/* if child has exited on receiving an exception signal
* e.g. kill -11 (SIGSEGV),
-3 (SIQUIT),
-4 (SIGILL),
-5 (SIGTRAP),
-6 (SIGABRT),
-7 (SIGBUS),
-8 (SIGFPE),
*/
if (WIFSIGNALED(status) && WCOREDUMP(status)) {
printf("stat value: %d Exit status:
%d\n", status, WTERMSIG(status));
psignal(WTERMSIG(status), "child received
exception signal: "
"Exit
signal");
printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
}
/* if child has exited on receiving a termination signal
* e.g. kill -15 (SIGTERM),
-2 (SIGINT),
-9 (SIGKILL),
-1 (SIGHUP),
-13 (SIGPIPE),
-14 (SIGALRM),
-16 (SIGSTKFLT),
*/
else if (WIFSIGNALED(status)) {
printf("stat value: %d Exit status:
%d\n", status, WTERMSIG(status));
psignal(WTERMSIG(status), "Child received
Termination signal: "
"Exit
signal");
printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
}
printf("*****************************************************\n");
printf("Parent exits now!\n\n");
printf("*****************************************************\n");
exit(0); /* parent exits */
}
}
else /*
failure */
{
perror("fork");
exit(0);
}
}
Output:
*****************************************************
parent process!
parent PID = 31974,
child pid = 31975
WAIT FOR THE CHILD PROCESS TO
TERMINATE
*****************************************************
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
start child process!
child PID = 31975,
parent pid = 31974
process name: ./child1
arguments neel rockstar
Child process goes into
infinite while loop
Issue (Kill -15/ Kill -11) to
child process and see what happens
stat value: 4991 Exit status:
19
child received STOP signal:
Exit signal: Stopped (signal)
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
*****************************************************
Parent exits now!
*****************************************************
Neelkanth_39$ ./parent_main_2
*****************************************************
parent process!
parent PID = 31978,
child pid = 31979
WAIT FOR THE CHILD PROCESS TO
TERMINATE
*****************************************************
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
start child process!
child PID = 31979, parent
pid = 31978
process name: ./child1
arguments neel rockstar
Child process goes into
infinite while loop
Issue (Kill -15/ Kill -11) to
child process and see what happens
stat value: 139 Exit status:
11
child received exception
signal: Exit signal: Segmentation fault
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
*****************************************************
Parent exits now!
*****************************************************
Neelkanth_39$ ./parent_main_2
*****************************************************
parent process!
parent PID = 31982,
child pid = 31983
WAIT FOR THE CHILD PROCESS TO
TERMINATE
*****************************************************
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
start child process!
child PID = 31983,
parent pid = 31982
process name: ./child1
arguments neel rockstar
Child process goes into
infinite while loop
Issue (Kill -15/ Kill -11) to
child process and see what happens
stat value: 15 Exit status:
15
Child received Termination
signal: Exit signal: Terminated
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
*****************************************************
Parent exits now!
*****************************************************