linux_c_learn/child_porcess_clean/clear.c

50 lines
857 B
C

#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <sys/time.h>
void process_clean()
{
while (wait(NULL) != -1)
{
printf("child clean\n");
}
return 0;
}
int main(int argc, char *argv[])
{
signal(SIGCHLD, process_clean);
for (int i = 0; i < 10; i++)
{
pid_t pid = fork();
if (pid == 0)
{
sleep(1);
exit(0);
}
else if (pid > 0)
{
int status;
// wait(&status);
// printf("Child %d exited with status %d\n", pid, status);
}
else
{
perror("fork");
}
}
int k = 5;
while (k--)
{
sleep(1);
}
return 0;
}