[国嵌攻略][079][多进程程序设计]

fork.c

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

void main(){
    int pid;
    
    pid = fork();
    printf("pid is %d\n", pid);
    
    exit(0);
}

 

vfork.c

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

void main(){
    int pid;
    
    pid = vfork();
    printf("pid is %d\n", pid);
    
    exit(0);
}

 

wait.c

#include <stdio.h>
#include <stdlib.h>

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

void main(){
    int pid;
    
    pid = fork();
    if(pid > 0){
        wait(NULL);
        printf("pid is %d\n", pid);
    }else{
        printf("pid is %d\n", pid);
    }
    
    exit(0);
}

 

excel.c

#include <stdio.h>
#include <stdlib.h>

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

void main(){
    int pid;
    
    pid = fork();
    if(pid > 0){
        wait(NULL);
        printf("pid is %d\n", pid);
    }else{
        execl("/bin/ls", "ls", NULL);
        printf("pid is %d\n", pid);   //不会被执行
    }
    
    exit(0);
}

 

posted @ 2016-02-27 20:58  盛夏夜  阅读(283)  评论(0编辑  收藏  举报