linux 创建两个进程
时间: 2023-08-24 14:07:19 浏览: 30
可以使用fork()函数来创建两个进程,其中一个是父进程,另一个是子进程。具体实现代码如下:
```
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
pid_t pid; // 定义一个 pid_t 类型变量
pid = fork(); // 调用 fork() 函数创建子进程
if (pid < 0) { // 如果创建子进程失败
fprintf(stderr, "Fork failed");
exit(-1);
} else if (pid == 0) { // 如果是子进程
printf("This is child process\n");
} else { // 如果是父进程
printf("This is parent process\n");
}
return 0;
}
```
运行该程序,可以看到输出结果为:
```
This is parent process
This is child process
```
说明成功创建了一个父进程和一个子进程。
相关问题
linux创建两个进程,实现两个进程轮流数数的功能
好的,我可以为您解答。以下是一个简单的示例程序,其中创建了两个进程,它们交替打印数字,从而实现了轮流数数的功能:
```c
#include <stdio.h>
#include <unistd.h>
int main() {
int num = 0;
pid_t pid;
pid = fork(); // 创建第一个子进程
if (pid < 0) {
printf("Fork failed.\n");
return 1;
} else if (pid == 0) { // 第一个子进程
while (num < 100) {
printf("Child process 1: %d\n", num);
num += 2;
sleep(1);
}
} else { // 父进程
pid = fork(); // 创建第二个子进程
if (pid < 0) {
printf("Fork failed.\n");
return 1;
} else if (pid == 0) { // 第二个子进程
while (num < 100) {
printf("Child process 2: %d\n", num + 1);
num += 2;
sleep(1);
}
} else { // 父进程等待子进程结束
wait(NULL);
wait(NULL);
}
}
return 0;
}
```
以上程序创建了一个父进程和两个子进程,其中一个子进程打印偶数,另一个子进程打印奇数。每个子进程都使用一个循环来打印数字,并使用 `sleep` 函数来暂停一秒钟,以便观察交替打印的效果。父进程等待两个子进程结束后退出。
Linux怎么创建两个进程
Linux可以使用fork()系统调用来创建子进程。fork()会将当前进程复制一份,生成一个新的子进程。新的子进程与原进程完全一致,包括进程的代码、数据、堆栈等。在新的子进程中,fork()返回值为0,表示该进程是子进程;在原进程中,fork()返回值为子进程的进程ID,表示该进程是父进程。
以下是一个创建两个进程的例子:
```
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
pid_t pid1, pid2;
pid1 = fork(); // 创建第一个子进程
if (pid1 < 0)
{
printf("fork error\n");
exit(1);
}
else if (pid1 == 0) // 子进程1
{
// do something
printf("Child process 1\n");
exit(0);
}
else // 父进程
{
pid2 = fork(); // 创建第二个子进程
if (pid2 < 0)
{
printf("fork error\n");
exit(1);
}
else if (pid2 == 0) // 子进程2
{
// do something
printf("Child process 2\n");
exit(0);
}
else // 父进程
{
printf("Parent process\n");
// wait for child process to exit
waitpid(pid1, NULL, 0);
waitpid(pid2, NULL, 0);
}
}
return 0;
}
```
这个例子中,父进程创建了两个子进程,分别执行不同的任务。等待两个子进程执行完毕后,父进程才会继续执行。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)