#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. MVC简介

    全名是Model View Controller,模型-视图-控制器,一种软件设计典范,用一种业务逻辑.数据.界面显示分离的方法组织代码,将业务逻辑聚集到一个部件里面,在改进和个性化定制界面及用户交互 ...

  2. 使用multi curl进行http并发访问

    curl是一款利用URL语法进行文件传输的工具,它支持多种协议,包括FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET等,我们既可以在命令行上使用它,也可以利用 libcur ...

  3. IOS基础之 (十五)知识点

    一 SEL 1. 方法的存储位置 每个类的方法地址列表都存储在类对象中. 每个方法都有一个与之对应的SEL类型的对象. 根据一个SEL对象就可以找到方法的地址,进而调用方法. Person.h #im ...

  4. UVa OJ 175 - Keywords (关键字)

    Time limit: 3.000 seconds限时3.000秒 Problem问题 Many researchers are faced with an ever increasing numbe ...

  5. --hdu 2124 Repair the Wall(贪心)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2124 Ac code : #include<stdio.h> #include<st ...

  6. json中loads的用法

    python中json中的loads()和dumps()它们的作用经常弄换了,这里记录下,loads方法是把json对象转化为python对象,dumps方法是把pyhon对象转化为json对象,我是 ...

  7. Apache配置默认首页

    操作系统:CentOS 6.5 Apache默认主页为index.html,如果要修改为index.php或其它,需要修改httpd.conf文件 用vim或其它编辑器打开httpd.conf 在上图 ...

  8. DAO,Service,Controller各层之间的关系

    DAO层:DAO层主要是做数据持久层的工作,负责与数据库进行联络的一些任务都封装在此,DAO层的设计首先是设计DAO的接口,然后在Spring的配置文件中定义此接口的实现类,然后就可在模块中调用此接口 ...

  9. app 后端技术

    app 后端技术 一直以来工作的方向是web server,对app server没有什么了解.虽然没有接触过移动app开发,但对app后端技术还是挺有探索欲望的,app应用和web应用在前端的用户习 ...

  10. jQuery插件 -- Form表单插件jquery.form.js

    http://blog.csdn.net/zzq58157383/article/details/7718956 http://my.oschina.net/i33/blog/77250