SIGCHLD, . signal() (man signal). child_died() , SIGCHLD ( ). , , child_died() , (man pipe). :
#include <signal.h>
#include <strings.h>
#include <stdio.h>
#define MAX_SIZE 256
void child_died(int);
int fd[2];
int main()
{
int pid;
extern int fd[];
char *str;
pipe(fd);
signal(SIGCHLD, child_died);
if ((pid = fork()) < 0) {
printf("Error\n");
} else if (pid == 0) {
printf("In child process\n");
close(fd[0]);
str = strdup("Message from child process.\n");
write(fd[1], str, strlen(str) + 1);
} else {
wait();
printf("Now in parent\n");
}
return 0;
}
void child_died(int s)
{
char *str;
close(fd[1]);
read(fd[0], str, MAX_SIZE);
printf("%s", str);
}
sigchld.c : gcc -Wall sigchld.c -o sigchld
int (, pid) - , , .
SIGCHLD, child_died(). if. , , , , . , , child_died() , .
, . , . , .