2017-04-29 00:33:54 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <signal.h>
|
2017-04-30 16:55:13 +02:00
|
|
|
#include <sys/wait.h>
|
2017-04-29 00:33:54 +02:00
|
|
|
|
2017-05-29 01:42:08 +02:00
|
|
|
int child, cstatus;
|
|
|
|
|
|
|
|
void sighup() {
|
|
|
|
signal(SIGHUP,sighup); /* reset signal */
|
|
|
|
printf("CHILD: I have received a SIGHUP\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void sigint() {
|
|
|
|
signal(SIGINT,sigint); /* reset signal */
|
|
|
|
printf("CHILD: I have received a SIGINT\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void sigquit() {
|
|
|
|
printf("My DADDY has Killed me!!!\n");
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void sigchld() {
|
|
|
|
printf("PARENT: got SIGCHLD\n");
|
|
|
|
#ifdef WITH_SIGCHLD
|
|
|
|
child = wait(&cstatus);
|
|
|
|
if(cstatus == 0)
|
|
|
|
exit(0);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("PARENT: child exited with status %d\n", cstatus);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2017-04-29 00:33:54 +02:00
|
|
|
|
|
|
|
int
|
|
|
|
main() {
|
2017-05-29 01:42:08 +02:00
|
|
|
int pid, p[2];
|
2017-04-29 00:33:54 +02:00
|
|
|
char dummy[1];
|
|
|
|
|
|
|
|
/* get child process */
|
|
|
|
|
|
|
|
if(pipe(p)){
|
|
|
|
perror("pipe");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if ((pid = fork()) < 0) {
|
|
|
|
perror("fork");
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pid == 0) {
|
|
|
|
printf("\nI am the new child!\n\n");
|
|
|
|
signal(SIGHUP,sighup); /* set function calls */
|
|
|
|
signal(SIGINT,sigint);
|
|
|
|
signal(SIGQUIT, sigquit);
|
|
|
|
|
2017-05-02 01:15:10 +02:00
|
|
|
printf("\nChild going to loop...\n\n");
|
2017-09-11 01:01:11 +02:00
|
|
|
write(p[1], "", 1);
|
2017-04-29 00:33:54 +02:00
|
|
|
close(p[1]);
|
|
|
|
close(p[0]);
|
|
|
|
for(;;); /* loop for ever */
|
|
|
|
}
|
|
|
|
else /* parent */
|
|
|
|
{
|
2017-05-29 01:42:08 +02:00
|
|
|
signal(SIGCHLD,sigchld);
|
2017-09-11 01:01:11 +02:00
|
|
|
if(read(p[0], &dummy, 1) != 1){
|
|
|
|
printf("sync read");
|
2017-05-04 00:22:50 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2017-09-11 01:01:11 +02:00
|
|
|
close(p[1]);
|
2017-04-29 00:33:54 +02:00
|
|
|
close(p[0]);
|
|
|
|
printf("\nPARENT: sending SIGHUP\n\n");
|
|
|
|
kill(pid,SIGHUP);
|
|
|
|
sleep(3); /* pause for 3 secs */
|
|
|
|
printf("\nPARENT: sending SIGINT\n\n");
|
|
|
|
kill(pid,SIGINT);
|
|
|
|
sleep(3); /* pause for 3 secs */
|
|
|
|
printf("\nPARENT: sending SIGQUIT\n\n");
|
|
|
|
kill(pid,SIGQUIT);
|
2017-05-29 01:42:08 +02:00
|
|
|
|
|
|
|
#ifdef WITH_SIGCHLD
|
|
|
|
sleep(10000);
|
|
|
|
exit(1);
|
|
|
|
#else
|
2017-04-30 16:55:13 +02:00
|
|
|
child = wait(&cstatus);
|
|
|
|
if(child == pid && cstatus == 0)
|
|
|
|
exit(0);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("PARENT: child exited with status %d\n", cstatus);
|
|
|
|
exit(1);
|
|
|
|
}
|
2017-05-29 01:42:08 +02:00
|
|
|
#endif
|
2017-04-29 00:33:54 +02:00
|
|
|
}
|
|
|
|
}
|