zoukankan      html  css  js  c++  java
  • fork()函数

    foo.h

    #include <iostream>
    
    class foo
    {
    public:
       
        foo()
        {
            std::cout<<"foo constructor
    ";
        }
        
        ~foo()
        {
            std::cout<<"foo destructor
    ";
        }
        
        void doit()
        {
            std::cout<<"foo doit
    ";
        }
        
    };

    main.cpp

    #include "foo.h"
    
    extern "C"
    {
        #include <unistd.h>
        #include <stdio.h>
    }
    
    int main()
    {
        int count = 0;
        foo f;
        pid_t pid = fork(); //fork是把进程当前的情况拷贝一份,fork只拷贝下一步要执行的代码到新的进程
        if (pid < 0)
            std::cout<<"error in fork"<<std::endl;
        else if (pid == 0)
        {
            std::cout<<"i am child process"<<std::endl;
            ++count;
            std::cout<<"child process id : "<<getpid()<<std::endl;
        }
        else
        {
            std::cout<<"i am parent process"<<std::endl;
            ++count;
            std::cout<<"parent process id : "<<getpid()<<std::endl;
        }
        
        f.doit();
        std::cout<<"count : "<<count<<std::endl;
        
        return 0;
    }
    

      

    输出:

  • 相关阅读:
    010 Editor无限制免费安装注册破解图文教程(破解补丁)
    请求设置
    请求方法
    Session
    Config
    Model
    模板2
    模板语法
    动态路由
    Put和Delete
  • 原文地址:https://www.cnblogs.com/kex1n/p/7064795.html
Copyright © 2011-2022 走看看