2017-05-02 01:15:10 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
|
|
|
void sighup(); /* routines child will call upon sigtrap */
|
|
|
|
void sigint();
|
|
|
|
void sigquit();
|
|
|
|
|
|
|
|
int
|
|
|
|
main() {
|
|
|
|
int pid, p[2], w, status, sig = 0;
|
|
|
|
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");
|
2017-09-11 01:01:11 +02:00
|
|
|
write(p[1], "", 1);
|
2020-02-17 01:07:15 +01:00
|
|
|
close(p[1]);
|
|
|
|
close(p[0]);
|
2017-08-12 23:33:38 +02:00
|
|
|
for(;;){
|
|
|
|
/* loop for ever */
|
|
|
|
printf(".");
|
|
|
|
usleep(500);
|
|
|
|
}
|
2017-05-02 01:15:10 +02:00
|
|
|
}
|
|
|
|
else /* parent */
|
|
|
|
{
|
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);
|
|
|
|
}
|
2020-02-17 01:07:15 +01:00
|
|
|
close(p[1]);
|
|
|
|
close(p[0]);
|
2017-05-29 23:45:00 +02:00
|
|
|
printf("\nPARENT: sending SIGINT\n\n");
|
|
|
|
kill(pid,SIGINT);
|
2017-05-02 01:15:10 +02:00
|
|
|
do {
|
|
|
|
w = waitpid(pid, &status, WUNTRACED);
|
|
|
|
if (w == -1) {
|
|
|
|
perror("waitpid");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (WIFEXITED(status)) {
|
|
|
|
printf("exited, status=%d\n", WEXITSTATUS(status));
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
} else if (WIFSIGNALED(status)) {
|
|
|
|
sig = WTERMSIG(status);
|
2017-05-29 23:45:00 +02:00
|
|
|
if(sig == SIGINT){
|
|
|
|
printf("killed by SIGINT\n");
|
2017-05-02 01:15:10 +02:00
|
|
|
}else{
|
|
|
|
printf("killed by signal %d\n", sig);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
} else if (WIFSTOPPED(status)) {
|
|
|
|
printf("stopped by signal %d\n", WSTOPSIG(status));
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
}
|