Chinaunix首页 | 论坛 | 博客
  • 博客访问: 117110
  • 博文数量: 61
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 230
  • 用 户 组: 普通用户
  • 注册时间: 2014-04-26 11:35
个人简介

实践Linux的理论

文章存档

2015年(1)

2014年(60)

我的朋友

分类: C/C++

2014-04-29 21:28:31

在Linux下面,创建进程是一件十分有意思的事情。我们都知道,进程是操作系统下面享有资源的基本单位。那么,在Linux下面应该怎么创建进程呢?其实非常简单,一个fork函数就可以搞定了。但是,我们需要清楚的是子进程与父进程之间除了代码是共享的之外,堆栈数据和全局数据均是独立的。
[cpp] view plaincopy
#include  
#include  
#include  
#include  
#include  
#include  
#include  
  
int main()  
{  
    pid_t pid;  
  
    if(-1 == (pid = fork()))  
    {  
        printf("Error happened in fork function!\n");  
        return 0;  
    }  
  
    if(0 == pid)  
    {  
        printf("This is child process: %d\n", getpid());  
    }  
    else  
    {  
        printf("This is parent process: %d\n", getpid());  
    }  
  
    return 0;  
}  
     
阅读(701) | 评论(0) | 转发(0) |
0

上一篇:进程等待

下一篇:C之定时器

给主人留下些什么吧!~~