使用fork()系统调用创建两个子进程,子进程1输出child1 Running....,子进程2输出child2 Running....的代码
时间: 2023-06-08 16:06:49 浏览: 44
好的,以下是您需要的代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
pid_t pid1, pid2;
pid1 = fork(); // 创建子进程1
if (pid1 == -1) {
perror("fork error");
exit(1);
}
if (pid1 == 0) { // 子进程1
printf("child1 Running....\n");
exit(0);
}
pid2 = fork(); // 创建子进程2
if (pid2 == -1) {
perror("fork error");
exit(1);
}
if (pid2 == 0) { // 子进程2
printf("child2 Running....\n");
exit(0);
}
// 父进程等待子进程的结束
waitpid(pid1, NULL, 0);
waitpid(pid2, NULL, 0);
return 0;
}
```
以上是使用fork()系统调用创建两个子进程,子进程1输出child1 Running....,子进程2输出child2 Running....的完整代码,您可以按照这个思路扩展并发控制程序。
相关问题
1.编写一段程序,使用系统调用fork()创建两个子进程,再用系统 调用signal()让父进程捕捉组合键的中断信号(即按ctrl+c键),当 捕捉到中断信号后,父进程用系统调用kill()向两个子进程发出信号, 子进程捕捉到父进程发来的信号后,分别输出下列信息后终止: Child process 1 is killed by parent! Child process 2 is killed by parent! 父进程等待两个子进程终止后,输出以下信息后终止: Parent process exit!
好的,以下是实现上述功能的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
void child_process_1();
void child_process_2();
void signal_handler(int signal);
int main() {
pid_t pid1, pid2;
int status;
// 创建子进程1
pid1 = fork();
if (pid1 < 0) {
perror("fork 1 failed");
exit(EXIT_FAILURE);
} else if (pid1 == 0) {
child_process_1();
exit(EXIT_SUCCESS);
}
// 创建子进程2
pid2 = fork();
if (pid2 < 0) {
perror("fork 2 failed");
exit(EXIT_FAILURE);
} else if (pid2 == 0) {
child_process_2();
exit(EXIT_SUCCESS);
}
// 等待父进程捕捉中断信号
signal(SIGINT, signal_handler);
printf("Parent process is running. Press CTRL+C to terminate child processes.\n");
// 等待两个子进程终止
waitpid(pid1, &status, 0);
waitpid(pid2, &status, 0);
printf("Parent process exit!\n");
exit(EXIT_SUCCESS);
}
void child_process_1() {
printf("Child process 1 is running.\n");
sleep(10);
printf("Child process 1 is killed by parent!\n");
}
void child_process_2() {
printf("Child process 2 is running.\n");
sleep(10);
printf("Child process 2 is killed by parent!\n");
}
void signal_handler(int signal) {
if (signal == SIGINT) {
printf("CTRL+C is pressed. Killing child processes...\n");
kill(0, SIGKILL);
exit(EXIT_SUCCESS);
}
}
```
该程序通过调用 `fork()` 系统调用创建了两个子进程,并在父进程中注册了一个中断信号处理函数 `signal_handler()`,用于捕捉用户按下 CTRL+C 的中断信号。
当父进程捕捉到中断信号后,会向两个子进程发送信号 `SIGKILL`,子进程在收到该信号后会输出相应的信息并终止。
当两个子进程都终止后,父进程会输出相应的信息并终止。
编制一段程序,使用系统调用fork()创建两个子进程,再用系统调用signal()让父进程捕捉键盘上来的中断信号(即按ctrl+c键),当捕捉到中断信号后,父进程用系统调用kill()向两个子进程发出信号,子进程捕捉到信号后,分别输出下列信息后终止: child process1 is killed by parent! child process2 is killed by parent! 父进程等待两个子进程终止后,输出以下信息后终止: parent process is killed!
以下是示例代码:
```c
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
void child_process1();
void child_process2();
void signal_handler(int signum);
int main() {
pid_t pid1, pid2;
signal(SIGINT, signal_handler);
pid1 = fork();
if (pid1 < 0) {
printf("Error: Failed to fork child process 1.\n");
exit(1);
} else if (pid1 == 0) {
child_process1();
}
pid2 = fork();
if (pid2 < 0) {
printf("Error: Failed to fork child process 2.\n");
exit(1);
} else if (pid2 == 0) {
child_process2();
}
// 等待子进程终止
waitpid(pid1, NULL, 0);
waitpid(pid2, NULL, 0);
printf("parent process is killed!\n");
return 0;
}
void child_process1() {
while (1) {
printf("child process1 is running...\n");
sleep(1);
}
}
void child_process2() {
while (1) {
printf("child process2 is running...\n");
sleep(1);
}
}
void signal_handler(int signum) {
printf("Received signal %d, killing child processes...\n", signum);
kill(0, SIGTERM);
exit(0);
}
```
在上面的代码中,我们在主函数中使用 `fork()` 系统调用创建了两个子进程,并使用 `signal()` 系统调用让父进程捕捉键盘上来的中断信号 `SIGINT`。当捕捉到中断信号后,父进程使用 `kill()` 向两个子进程发出信号 `SIGTERM`,子进程捕捉到信号后,分别输出指定的信息并终止。父进程等待两个子进程终止后,输出指定的信息并终止。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)