子父进程

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

int main()
{
    
    
        pid_t pid;
        pid_t pid2;

        pid=getpid();
        printf("Before fork pid=%d\n",pid);

        fork();

        pid2=getpid();
        printf("After fork pid =%d\n",pid2);

        if(pid==pid2)
        {
    
    

        printf("This is father !\n");
        }
        else
        {
    
    

                printf("This is child!\n");
        }


        return 0;
}
~   

在这里插入图片描述
说明fork之后创建了子进程

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

int main()
{
    
    
        pid_t pid;

        printf("Father pid is%d\n",getpid());


        pid=fork();
        if(pid>0)
        {
    
    

                printf("This is father!,pid=%d\n",getpid());
        }
        else if(pid==0)
        {
    
    

                printf("This is child!,pid=%d\n",getpid());
        }


        return 0;
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_43482790/article/details/115141116