#include1.2 运行结果 二、测试,多次执行3fork.c,8次打印的进程顺序是否有规律,分析原因#include #include #include #include int main() { int n = 3; pid_t pid1 = fork(); pid_t pid2 = fork(); pid_t pid3 = fork(); if(pid1 < 0 || pid2 < 0 || pid3 < 0) fprintf(stderr,"错误"); else if(pid1 == 0) { printf("子进程1空间是%d Hellon", (int)getpid()); exit(0); } else if(pid2 == 0) { printf("子进程2空间是%d Hellon", (int)getpid()); return 0; } else if(pid3 == 0) { printf("子进程3空间是%d Hellon", (int)getpid()); return 0; } else { //wait(NULL); printf("父进程空间的pid是%d Hellon", (int)getpid()); } return 0; }
没有规律。
我的一丢丢分析如下:程序只保证了多进程,对进程的完成顺序没有任何限制(某个进程完成了可以直接结束、而不是挂起等待)
三、试想一个办法,让命令提示窗等所有8次打印都结束以后再跳出来 3.1 法一、强制挂起 3.1.1 源码#include3.1.2 解释#include #include #include #include int main() { int n = 3; pid_t pid1 = fork(); pid_t pid2 = fork(); pid_t pid3 = fork(); if(pid1 == 0) { printf("子进程1空间是%d Hellon", (int)getpid()); exit(0); } if(pid2 == 0) { printf("子进程2空间是%d Hellon", (int)getpid()); return 0; } if(pid3 == 0) { printf("子进程3空间是%d Hellon", (int)getpid()); return 0; } if(pid1 && pid2 && pid3) { sleep(1); printf("父进程空间的pid是%d Hellon", (int)getpid()); } return 0; }
- 命令行在父进程结束后刷新挂起父进程,子进程在父进程被挂起之前+被挂起的这段时间内自由跑。给子进程时间
目的:解决挂起时间模糊的问题。
初始想法:wait函数会等待它的子进程,树形结构
3.2.1 子进程也可以是父进程pid的返回值:
| 值 | 状态 |
|---|---|
| > 0 | 父进程 |
| == 0 | 子进程 |
| < 0 | 错误 |
做个小实验验证下小标题:
#include3.2.2 思路#include #include #include #include int main() { pid_t pid1, pid2; printf("进程(pid:%d)开始运行,准备fork;n", (int)getpid()); pid1 = fork(); pid2 = fork(); if(pid1 > 0) printf("子进程1可以作为父进程n"); exit(0); }
思路:
将进程关系抽象成树(虽然这棵树我还不太能画出来QAQ)。子节点可以作为它的子节点的父节点
把所有可能的情况列举出来后用if-else大法暴力写逻辑。只要可能当父节点就添上wait
#include3.2.4 运行结果 四、小结#include #include #include #include int main() { int n = 3; pid_t pid1 = fork(); pid_t pid2 = fork(); pid_t pid3 = fork(); pid_t pid4 = fork(); if(pid1 == 0) { printf("子进程1空间是%d Hellon", (int)getpid()); exit(0); } else //1是父进程 { if(pid2 == 0) { printf("子进程2空间是%d Hellon", (int)getpid()); return 0; } else { wait(NULL); if(pid3 == 0) { printf("子进程3空间是%d Hellon", (int)getpid()); return 0; } else { wait(NULL); if(pid4 == 0) { printf("子进程4空间是%d Hellon", (int)getpid()); return 0; } else { wait(NULL); } } } } if(pid2 == 0) { printf("子进程2空间是%d Hellon", (int)getpid()); return 0; } else { wait(NULL); if(pid1 == 0) { printf("子进程1空间是%d Hellon", (int)getpid()); return 0; } else { wait(NULL); if(pid3 == 0) { printf("子进程3空间是%d Hellon", (int)getpid()); return 0; } else { wait(NULL); if(pid4 == 0) { printf("子进程4空间是%d Hellon", (int)getpid()); return 0; } else { wait(NULL); } } } } if(pid3 == 0) { printf("子进程3空间是%d Hellon", (int)getpid()); return 0; } else { wait(NULL); if(pid1 == 0) { printf("子进程1空间是%d Hellon", (int)getpid()); return 0; } else { wait(NULL); if(pid2 == 0) { printf("子进程2空间是%d Hellon", (int)getpid()); return 0; } else { wait(NULL); if(pid4 == 0) { printf("子进程4空间是%d Hellon", (int)getpid()); return 0; } else { wait(NULL); } } } } if(pid4 == 0) { printf("子进程4空间是%d Hellon", (int)getpid()); return 0; } else { wait(NULL); if(pid1 == 0) { printf("子进程1空间是%d Hellon", (int)getpid()); return 0; } else { wait(NULL); if(pid2 == 0) { printf("子进程2空间是%d Hellon", (int)getpid()); return 0; } else { wait(NULL); if(pid3 == 0) { printf("子进程3空间是%d Hellon", (int)getpid()); return 0; } else { wait(NULL); } } } } if(pid1 && pid2 && pid3 && pid4) { wait(NULL); printf("父进程空间的pid是%d Hellon", (int)getpid()); } return 0; }
- fork的返回状态暴力真香



