zoukankan      html  css  js  c++  java
  • fork()初级知识

    来自:http://blog.csdn.net/jason314/article/details/5640969

    #include <unistd.h>   
    #include <stdio.h>    
    int main ()    
    {    
        pid_t fpid; //fpid表示fork函数返回的值   
        int count=0;   
        fpid=fork();    
        if (fpid < 0)    
            printf("error in fork!");    
        else if (fpid == 0) {   
            printf("i am the child process, my process id is %d\n",getpid());    
            printf("我是爹的儿子");//对某些人来说中文看着更直白。   
            count++;   
        }   
        else {   
            printf("i am the parent process, my process id is %d\n",getpid());    
            printf("我是孩子他爹");   
            count++;   
        }   
        printf("统计结果是: %d\n",count);   
        return 0;   
    }   
    //打印结果如下:fork()函数返回三种可能的值
    //第一个:在父进程中返回子进程的id,
    //第二个:在子进程中返回0。
    //第三个:错误时返回-1。
    //fork()出新的进程后如何调用那个进程要看系统的进程调用策略,进程标识符PID,getpid();
    //fork()出错的原因,有几个1:当前的进程数已经达到系统的上限,这时errno的值被设置为EAGAIN。
    //系统内存不足

  • 相关阅读:
    输入输出、基本运算符、流程控制
    Node学习6-fs模块
    Node学习5-events模块
    Node学习4-Buffer模块
    Node学习3-Path模块
    Node学习2-基础知识/HelloWorld/模块调用/global/process
    Node学习1-基本概念
    gulp/bower/less知识
    AngularJS学习1-基础知识
    JavaScript学习-类/原型链/class
  • 原文地址:https://www.cnblogs.com/fenglongyu/p/7531549.html
Copyright © 2011-2022 走看看