#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. Asp.Net MVC3 简单入门详解过滤器Filter

    http://www.cnblogs.com/boruipower/archive/2012/11/18/2775924.html 前言 在开发大项目的时候总会有相关的AOP面向切面编程的组件,而MV ...

  2. matlab学习笔记 bsxfun函数

    matlab学习笔记 bsxfun函数 最近总是遇到 bsxfun这个函数,前几次因为无关紧要只是大概看了一下函数体去对比结果,今天再一次遇见了这个函数,想想还是有必要掌握的,遂查了些资料总结如下. ...

  3. 去除DedeCms 5.7后台版权广告链接的方法

    织梦DedeCms 5.7后台有很多的织梦官方的广告链接,下面我们来将这些广告去掉吧. 一.去处后台登陆页login.php广告链. 1.找到登录界面模板文件/dede/templets/login. ...

  4. Javascript检测用户注册信息

    <html> <head> <title>用户注册</title> <meta http-equiv="content-type&quo ...

  5. Android手机 Fildder真机抓包

    Fiddler是一个http调试代理,它能 够记录所有的你电脑和互联网之间的http通讯,Fiddler 可以也可以让你检查所有的http通讯,设置断点,以及Fiddle 所有的“进出”的数据(指co ...

  6. 新浪微博客户端(23)-计算Cell内控件的frame

    DJStatusCellFrame.m #import "DJStatusCellFrame.h" #import "DJStatus.h" #import & ...

  7. 【原创】ui.router源码解析

    Angular系列文章之angular路由 路由(route),几乎所有的MVC(VM)框架都应该具有的特性,因为它是前端构建单页面应用(SPA)必不可少的组成部分. 那么,对于angular而言,它 ...

  8. Hadoop Streaming例子(python)

    以前总是用java写一些MapReduce程序现举一个例子使用Python通过Hadoop Streaming来实现Mapreduce. 任务描述: HDFS上有两个目录/a和/b,里面数据均有3列, ...

  9. 繁华模拟赛 Vincent的城堡

    #include<iostream> #include<cstdio> #include<string> #include<cstring> #incl ...

  10. 没玩过这些微信小游戏你就out了

    你确定没玩过下面这些微信小游戏?是不是有点out了?赶紧添加微信号kangfuyk,回复H5马上畅玩! 当然了,扫一下二维码关注后回复H5更快捷噢! 微信小游戏列表,持续更新中 辨色大比拼!心理游戏 ...