#include <iostream>
#include <vector>
#include <cstdint>
#include <cstring>
#include <error.h>
#include <unistd.h>

using namespace std;

int main()
{
    //system("ps aux &");//run on background
    char *const ps_argv[]={"ps", "ax", 0};

//eg env
    char *const ps_envp[]={"PATH=/bin:/usr/bin","TERM=console",0};

//possible exec
    //execl("/bin/ps","ps","ax",0);//assume ps in the bin
    //execlp("ps","ps","ax",0);//assume /bin is in the PATH
    execle("/bin/ps","ps","ax",0,ps_envp);//pass own env

//execv("/bin/ps",ps_argv);
//    execvp("ps",ps_argv);
//    execve("/bin/ps",ps_argv,ps_envp);

//    execlp("ps","ps","ax",0);

pid_t new_pid;

new_pid = fork();
    new_pid = getpid();
    cout << new_pid << endl;
    switch (new_pid) {
    case -1:
        break;
    case 0:
        break;
    default:
        break;
    }
    cout << "errx" << endl;
    return 0;
}

#include <iostream>
#include <vector>

#include <cstdint>
#include <cstring>
#include <error.h>

#include <unistd.h>
#include <sys/types.h>

using namespace std;

int main()
{
    pid_t pid;
    string message;
    int n;

pid = fork();
    //pid = getpid();

//cout << pid << endl;

switch (pid) {
    case -1:
        perror("fork failed");
        exit(1);
    case 0:
        message = "this is the child";
        n=5;
        break;
    default:
        message = "this is the parent";
        n=3;
        break;
    }

for(; n > 0;n--)
    {
        cout<<message<<endl;
        sleep(1);
    }
    //cout << "errx" << endl;
    return 0;
}

this is the parent
this is the child
this is the parent
this is the child
this is the parent
this is the child
$ this is the child
this is the child

#include <iostream>
#include <vector>

#include <cstdint>
#include <cstring>
#include <error.h>

#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

using namespace std;

int main()
{
    pid_t pid;
    string message;
    int n;
    int exit_code;

pid = fork();
    //pid = getpid();

//cout << pid << endl;

switch (pid) {
    case -1:
        perror("fork failed");
        exit(1);
    case 0:
        message = "this is the child";

n=5;
        exit_code = 37;//casual set
        break;
    default:
        message = "this is the parent";
        exit_code = 0;
        n=3;
        break;
    }

for(; n > 0;n--)
    {
        cout << message << ",which pid:" << getpid() << endl;

sleep(1);
    }

if(pid != 0)
    {
        int stat_val;
        pid_t child_pid;

child_pid = wait(&stat_val);

printf("child has finished: PID = %d\n",child_pid);
        if(WIFEXITED(stat_val))
            printf("child exited with code %d\n",WEXITSTATUS(stat_val));
        else {
            printf("child terminated abnormally\n");
        }
    }
    //cout << "errx" << endl;
    exit(exit_code);
}

unix/linux进程详解——代码的更多相关文章

  1. unix/linux进程详解

    技术分享 启动新进程 stdlib.hintsystem(const char *string)whichequals to "sh -c string" 替换进程映像unistd ...

  2. Linux 进程详解

    Linux内核的七大区间 .进程管理(进程创建,进程的三种状态,进程间的调度,调度算法...) .内存管理(段式管理(Linux所有段都从0开始),页式管理--地址偏移量) .系统调用(C语言库函数的 ...

  3. Linux学习之守护进程详解

    Linux系统守护进程详解                                                              ---转自:http://yuanbin.blog ...

  4. Linux 系统结构详解

    Linux 系统结构详解 Linux系统一般有4个主要部分: 内核.shell.文件系统和应用程序.内核.shell和文件系统一起形成了基本的操作系统结构,它们使得用户可以运行程序.管理文件并使用系统 ...

  5. PHP 进程详解

    .note-content { font-family: "Helvetica Neue", Arial, "Hiragino Sans GB", STHeit ...

  6. linux syslog详解

    linux syslog详解 分三部分 一.syslog协议介绍 二.syslog函数 三.linux syslog配置   一.syslog协议介绍 1.介绍 在Unix类操作系统上,syslog广 ...

  7. Linux init详解(转)

    Linux init详解 一.什么是INIT: init是Linux系统操作中不可缺少的程序之一. 所谓的init进程,它是一个由内核启动的用户级进程. 内核自行启动(已经被载入内存,开始运行,并已初 ...

  8. Linux内存详解

    --Linux内存详解 -----------------2014/05/24 Linux的内存上表现的不像windows那么直观,本文准备详细的介绍一下Linux的内存. 请看这下有linux命令f ...

  9. Linux权限详解 命令之 chmod:修改权限

    权限简介 Linux系统上对文件的权限有着严格的控制,用于如果相对某个文件执行某种操作,必须具有对应的权限方可执行成功. Linux下文件的权限类型一般包括读,写,执行.对应字母为 r.w.x. Li ...

随机推荐

  1. appium-车友会欢迎界面向右滑动4次点击‘立即体验’进入首屏

    代码如下: driver.swipe(610, 2452, 658, 2452, 200) 只是示例滑动1页,可以使用循环,下一页比上一页x坐标大48

  2. Nagios安装部署和介绍(一)

    一.软件版本下载 Nagios版本下载地址: http://prdownloads.sourceforge.net/sourceforge/nagios/ http://sourceforge.net ...

  3. CentOS 6.5(6.4)安装过程图文教程

    CentOS 6.4是最新的出的系统,这里分享下安装教程,有些设置大部分教程没出现过,特分享下,方便需要的朋友 1.首先,要有一张CentOS 6.4的安装介质,使用介质启动电脑出现如下界面 界面说明 ...

  4. Memcache和Redis

  5. Laravel教程 八:queryScope 和 setAttribute

    Laravel教程 八:queryScope 和 setAttribute 此文章为原创文章,未经同意,禁止转载. Laravel Eloquent Database 直接就是按照上一节所说的那样,我 ...

  6. Substance风格实例大全javaswing皮肤风格大全(原)

    转载请注明来路:http://www.cnblogs.com/langtianya/ 下图是所支持的设置 下面把上面的每一个选项的子选项进行试验 substance皮肤: AutumnSkin Bus ...

  7. sudo: unable to resolve host ubuntu提示的解决

    http://blog.sina.com.cn/s/blog_6c9d65a1010180mg.html

  8. 《PHP程序设计》读书笔记

    好久没有更新过我的博客了,主要前一阵子去了实习,现在实习进入尾声,终于有机会看看书了. 在前一阵子的实习中,用到最多就是PHP的CI框架和Jquery,所以现在再看一本有关PHP的书籍来深刻认识一下P ...

  9. 一个Java对象到底占用多大内存?

    最近在读<深入理解Java虚拟机>,对Java对象的内存布局有了进一步的认识,于是脑子里自然而然就有一个很普通的问题,就是一个Java对象到底占用多大内存? 在网上搜到了一篇博客讲的非常好 ...

  10. [Effective JavaScript 笔记] 第11条:熟练掌握闭包

    理解闭包三个基本的事实 第一个事实:js允许你引用在当前函数以外定义的变量. function makeSandwich(){ var magicIngredient=”peanut butter”; ...