1. 程式人生 > >多線程初探

多線程初探

amp unistd.h pid_t pipe kpi time main () include

fork()&pipe()多線程

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
    
int main(void){
    
    pid_t P1, P2, P3;
    printf("I am father, My ID is %d.\n", getpid());
    
    P1=fork();
    if(P1==0)
        printf("I am son1, my ID is %d.\n", getpid());
    else
        waitpid(P1,NULL,0);
    
    P2=fork();
    if(P2==0)
        printf("I am son2, my ID is %d.\n", getpid());
    else
        waitpid(P2,NULL,0);
    
    P3=fork();
    if(P3==0)
        printf("I am son3, my ID is %d.\n", getpid());
    else
        waitpid(P3,NULL,0);
    
}

多線程初探