实现线程A和B,C,实现线程的子进程,父进程杀死子进程,子进程杀死父进程,C++代码实现
时间: 2024-05-30 22:16:05 浏览: 124
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
void child_process() {
printf("Child process started\n");
sleep(5);
printf("Child process finished\n");
}
void parent_process(pid_t pid) {
printf("Parent process started\n");
sleep(2);
printf("Killing child process with id %d\n", pid);
kill(pid, SIGTERM);
printf("Parent process finished\n");
}
int main() {
pid_t pid = fork();
if (pid == -1) {
printf("Error: Failed to create child process\n");
exit(1);
} else if (pid == 0) {
child_process();
} else {
parent_process(pid);
}
return 0;
}
阅读全文