Linux进程创建方法(fork/exec/system)

system 创建进程

在一个程序内部启动一个程序,从而创建一个新进程

运行以字符串参数的形式传递给他的命令并等待该命令的完成

使用头文件和函数参数

#include<stdlib.h>

int system(const char* string);
#include<stdlib.h>
#include<stdio.h>

int main()
{

        printf("Running ps with system\n");
        system("ps ax");
        printf("Done.\n");
        exit(0);
}     

在这里插入图片描述

execl 替换进程

#include<unistd.h>
execl
execlp
execle

execv
execvp
execve

6种写了常见的2种

#include<stdio.h>
#include<string.h>
#include<unistd.h>

int main(int argc,char *argv[],char *envp[])
{

        if(strcmp(argv[1],"execl"))
        {
                printf(" argv = %s\n",argv[1]);
                printf("execl!\n");
                execl("/usr/bin/ps","ps",0);
                printf("this is word next execl")

                
        }
        else
        {
                printf(" argv = %s\n",argv[1]);
                printf("execlp!\n");
                execlp("ps","ps",0);
                printf("this is word next execlp")
        }
        return 0;
}

替换进程后原进程的代码不会执行 比如execl/execlp后面的printf
在这里插入图片描述

fork复制进程

这个系统调用复制当前进程 在进程表中创建一个新的表项,新的进程拥有自己的数据空间,环境和文件描述符

#include<sys/types.h>
#include<unistd.h>

pid_t fork(void);

父进程中fork调用返回了新的子进程的PID
新进程继续执行 但是fork调用返回值为0 由此判断父子进程
失败返回-1
1.子进程数目超过CHILD_MAX errno EAGAIN
2.进程表没有足够空间创建新的表单/虚拟内存不足 errno ENOMEM

#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<stdlib.h>

int main()
{
        pid_t pid;
        char *message;
        int n;


        printf("fork program starting~\n");
        pid = fork();

        switch(pid)
        {
                case -1:
                        perror("fork failed");
                        exit(1);
                case 0:
                        message = "This is the child";
                        n = 1;
                        break;
                default:
                        message = "This is the parents";
                        n = 3;
                        break;
        }

        for(;n > 0;n--)
        {
                puts(message);
                sleep(1);
        }

        exit(0);
}

在这里插入图片描述

wait等待进程

fork启动一个子进程,子进程有了自己的生命周期独立运行
父进程在子进程之前结束 子进程还在继续运行
通过在父进程中调用wait让父进程中调用wait函数让父进程等待子进程结束

#include<sys/types.h>
#include<sys/wait.h>

pid_t wait(int *stat_loc);

wait系统调用将暂停父进程直到他的子进程结束为止,这个调用返回子进程的PID,已经结束运行的子进程的PID。
状态信息允许父进程了解子进程的退出状态,子进程main函数返回的值/或exit中的退出码,如果stat_loc不是空指针,状态信息将被写入他所在的位置

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>

int main()
{

        pid_t cpid;
        char* message;
        int num;
        int exit_code;

        printf("fork program starting\n");
        cpid = fork();

        switch(cpid)
        {
                case -1:
                        perror("forl failed");
                        exit(1);
                case 0:
                        message = "This is child";
                        num = 9;
                        exit_code = 10;
                        break;
                default:
                        message = "This is the parents";
                        num = 3;
                        exit_code = 0;
                        break;
        }
        for(;num>0;num--)
        {
                puts(message);
                sleep(1);
        }

        if(cpid != 0)

        {
                int stat_val;
                pid_t child_pid;
       
                child_pid = wait(&stat_val);

                printf("Children has finished : PID = %d\n",child_pid);

                //normal exit
                if(WIFEXITED(stat_val))
                {
                        printf("child exit with code %d\n",WEXITSTATUS(stat_val));
                }
                else
                {
                        printf("Child terminated abnormally\n");
                }
        }

        exit(exit_code);
}
                                          

在这里插入图片描述

waitpid

等待某个特定进程结束
和wait一样 会挂起调用进程

#include<sys/types.h>
#include<sys/wait.h>

pid_t waitpid(pid_t pid,int * stat_loc, int options);
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>

int main()
{

        pid_t cpid;

        printf("programing fork is begining!~\n");
        cpid = fork();

        switch(cpid)
        {

                case -1:
                        perror("fork failed~!\n");
                        exit(0);
                case 0 :
                        sleep(10);
                        break;
                default:
                        printf("im parent\n");
                        pid_t wpid;
                        wpid = waitpid(cpid, NULL, WNOHANG);
                        printf("child end pid = %s\n",wpid);

                        break;

        }

        exit(0);

}

在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值