免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 860 | 回复: 0
打印 上一主题 下一主题

fork and vfork [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-08-08 16:45 |只看该作者 |倒序浏览
/*this .c file demonstrates how to use fork function to generate a new process*/
/*filename:fork.c
  time:08/06/2009
  author:creatory@mail.com
  when fork success,the process scheduled sequence is unsure.
  Every one can be scheduled by the scheduled process firstly.
  So the result is not sure.
*/
#include
#include   //fork
#include  //exit
int main(void){
    pid_t pid;
    if((pid=fork())==-1){
        printf("fork error.\n");
        exit(1);
    }else if(pid==0){
        //child
        printf("this is child process.\n");
        
    }else{
        //parent
        printf("this is parent process.\n");
    }
    return 0;
}
/* this file demonstrates how to use vfork function
filename:vfork.c
time:08/06/2009
author:creatory@mail.com
Note:
    Call vfork will hang up the parent process until the child process terminate or it call exec function.
So the result is always child process execute firstly,then parent process.It's sure.
*/
/*remain problem:why the end must be exit(0) not "return 0".
if "return 0" then generate "segmentation fault"
*/
#include  //exit
#include  //vfork
#include
#include  //printf
int main(void){
    pid_t pid;
    if((pid=vfork())<0){
        printf("vfork error!\n");
        exit(1);
    }
    else if(pid==0){
        //the child process
        printf("Child process is printing.\n");
        
    }else{
        //the parent process
        printf("the parent is printing.\n");
    }
//    return 0;
    exit(0);
}


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/56374/showart_2021842.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP