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 = 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 解释
  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 思路

思路:

  1. 将进程关系抽象成树(虽然这棵树我还不太能画出来QAQ)。子节点可以作为它的子节点的父节点

  2. 把所有可能的情况列举出来后用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 运行结果

四、小结

  1. fork的返回状态
  2. 暴力真香
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值