复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44#include<bits/stdc++.h> #include<unistd.h> #include <stdio.h> #include <stdlib.h> using namespace std; #define MAXIMUM 1000 int main() { int fd[2]; pid_t pid; char line[MAXIMUM]; if(pipe(fd) < 0){ printf("Failure to create the pipe.n"); exit(1); } if((pid == fork())< 0){ printf("Failed to create the child process.n"); close(fd[0]); close(fd[1]); exit(2); } if(pid > 0){ close(fd[0]); write(fd[1], "How are you?n", 15); printf("Parent: Successfully!n"); close(fd[1]); sleep(1); } else{ close(fd[1]); read(fd[0], line, MAXIMUM); printf("Child: Read from the pipe:%s",line); close(fd[0]); } return 0; } /* 输出: Parent: Successfully! Child: Read from the pipe:How are you? //代写接单qun:733065427 */
fork系统调用用于创建一个新进程,称为子进程,它与进程(称为系统调用fork的进程)同时运行,此进程称为父进程。创建新的子进程后,两个进程将执行fork()系统调用之后的下一条指令。子进程使用相同的pc(程序计数器),相同的CPU寄存器,在父进程中使用的相同打开文件。
它不需要参数并返回一个整数值。下面是fork()返回的不同值。
负值:创建子进程失败。
零:返回到新创建的子进程。
正值:返回父进程或调用者。该值包含新创建的子进程的进程ID
最后
以上就是雪白裙子最近收集整理的关于c/c++ pipe fork管道 子进程的全部内容,更多相关c/c++内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复