C语言实现linux系统调用多个fork()时命令提示行等待程序运行结束才刷新出来
一、 根据参考代码fork.c,改写3fork.c,每次打印hello时,输出正在打印的进程编号1.1 源码#include <stdio.h>#include <unistd.h>#include <stdlib.h>#include <sys/types.h>#include <sys/wait.h>int main(){int n
·
一、 根据参考代码fork.c,改写3fork.c,每次打印hello时,输出正在打印的进程编号
1.1 源码
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
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 Hello\n", (int)getpid());
exit(0);
}
else if(pid2 == 0)
{
printf("子进程2空间是%d Hello\n", (int)getpid());
return 0;
}
else if(pid3 == 0)
{
printf("子进程3空间是%d Hello\n", (int)getpid());
return 0;
}
else
{
//wait(NULL);
printf("父进程空间的pid是%d Hello\n", (int)getpid());
}
return 0;
}
1.2 运行结果
二、测试,多次执行3fork.c,8次打印的进程顺序是否有规律,分析原因
没有规律。
我的一丢丢分析如下:程序只保证了多进程,对进程的完成顺序没有任何限制(某个进程完成了可以直接结束、而不是挂起等待)
三、试想一个办法,让命令提示窗等所有8次打印都结束以后再跳出来
3.1 法一、强制挂起
3.1.1 源码
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
int main()
{
int n = 3;
pid_t pid1 = fork();
pid_t pid2 = fork();
pid_t pid3 = fork();
if(pid1 == 0)
{
printf("子进程1空间是%d Hello\n", (int)getpid());
exit(0);
}
if(pid2 == 0)
{
printf("子进程2空间是%d Hello\n", (int)getpid());
return 0;
}
if(pid3 == 0)
{
printf("子进程3空间是%d Hello\n", (int)getpid());
return 0;
}
if(pid1 && pid2 && pid3)
{
sleep(1);
printf("父进程空间的pid是%d Hello\n", (int)getpid());
}
return 0;
}
3.1.2 解释
- 命令行在父进程结束后刷新
- 挂起父进程,子进程在父进程被挂起之前+被挂起的这段时间内自由跑。给子进程时间
3.1.3 结果截图
3.2 基于暴力的一丢丢优化
目的:解决挂起时间模糊的问题。
初始想法:wait
函数会等待它的子进程,树形结构
3.2.1 子进程也可以是父进程
pid
的返回值:
值 | 状态 |
---|---|
> 0 | 父进程 |
== 0 | 子进程 |
< 0 | 错误 |
做个小实验验证下小标题:
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
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);
}
3.2.2 思路
思路:
-
将进程关系抽象成树(虽然这棵树我还不太能画出来QAQ)。子节点可以作为它的子节点的父节点
-
把所有可能的情况列举出来后用
if-else
大法暴力写逻辑。只要可能当父节点就添上wait
3.2.3 代码(超长预警🤣)
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
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 Hello\n", (int)getpid());
exit(0);
}
else //1是父进程
{
if(pid2 == 0)
{
printf("子进程2空间是%d Hello\n", (int)getpid());
return 0;
}
else
{
wait(NULL);
if(pid3 == 0)
{
printf("子进程3空间是%d Hello\n", (int)getpid());
return 0;
}
else
{
wait(NULL);
if(pid4 == 0)
{
printf("子进程4空间是%d Hello\n", (int)getpid());
return 0;
}
else
{
wait(NULL);
}
}
}
}
if(pid2 == 0)
{
printf("子进程2空间是%d Hello\n", (int)getpid());
return 0;
}
else
{
wait(NULL);
if(pid1 == 0)
{
printf("子进程1空间是%d Hello\n", (int)getpid());
return 0;
}
else
{
wait(NULL);
if(pid3 == 0)
{
printf("子进程3空间是%d Hello\n", (int)getpid());
return 0;
}
else
{
wait(NULL);
if(pid4 == 0)
{
printf("子进程4空间是%d Hello\n", (int)getpid());
return 0;
}
else
{
wait(NULL);
}
}
}
}
if(pid3 == 0)
{
printf("子进程3空间是%d Hello\n", (int)getpid());
return 0;
}
else
{
wait(NULL);
if(pid1 == 0)
{
printf("子进程1空间是%d Hello\n", (int)getpid());
return 0;
}
else
{
wait(NULL);
if(pid2 == 0)
{
printf("子进程2空间是%d Hello\n", (int)getpid());
return 0;
}
else
{
wait(NULL);
if(pid4 == 0)
{
printf("子进程4空间是%d Hello\n", (int)getpid());
return 0;
}
else
{
wait(NULL);
}
}
}
}
if(pid4 == 0)
{
printf("子进程4空间是%d Hello\n", (int)getpid());
return 0;
}
else
{
wait(NULL);
if(pid1 == 0)
{
printf("子进程1空间是%d Hello\n", (int)getpid());
return 0;
}
else
{
wait(NULL);
if(pid2 == 0)
{
printf("子进程2空间是%d Hello\n", (int)getpid());
return 0;
}
else
{
wait(NULL);
if(pid3 == 0)
{
printf("子进程3空间是%d Hello\n", (int)getpid());
return 0;
}
else
{
wait(NULL);
}
}
}
}
if(pid1 && pid2 && pid3 && pid4)
{
wait(NULL);
printf("父进程空间的pid是%d Hello\n", (int)getpid());
}
return 0;
}
3.2.4 运行结果
四、小结
- fork的返回状态
- 暴力真香
更多推荐
已为社区贡献1条内容
所有评论(0)