fork复制进程__2018.07.24

代码:

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

int main()
{
    pid_t pid;
    pid=fork();
    if(0==pid)
    {
        printf("这是子线程,pid=%d\n",getpid());
        execl("/bin/ls","ls","-l",NULL);
    }
    else if(pid>0)
    {
        printf("这是父线程,pid=%d\n",getpid());
        pid_t ret=wait(NULL);//不管心进程退出状态
        printf("父进程返回\n");
        sleep(1);
        if(ret==pid)
        {
            printf("wait success\n");
        }
        else
        {
            printf("wait failed!\n");
        }
    }
    else
    {
        fprintf(stderr,"ERROR:fork() failed!\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_40316053/article/details/81194513
今日推荐