exec函数族的用法

#include <stdio.h>
#include"common.h"

int main()
{
    
    
    pid_t pid = fork();
    int status;
    if(pid>0)
        {
    
    
        sleep(2);
        printf("parent process\n");
        wait(&status);
        switch (WEXITSTATUS(status)) {
    
     //返回子进程的结束状态

        case 0:
            printf("子进程执行成功\n");
            break;
         default:
            printf("不明原因失败\n");
            break;
        }
    }
    if(pid==0)
        {
    
    
        printf("-----------%d------------\n",__LINE__);
        execlp("ls","ls",NULL);             //exec族函数  让进程加载一段新的代码,覆盖原来的代码
                                            //下面的代码就不执行了
        printf("------------%d----------\n",__LINE__);
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_50188452/article/details/110283189
今日推荐