码迷,mamicode.com
首页 > 系统相关 > 详细

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

时间:2016-02-27 21:56:37      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:

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);
}

 

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

标签:

原文地址:http://www.cnblogs.com/d442130165/p/5223552.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!