unix/linux进程详解——代码
#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进程详解——代码的更多相关文章
- unix/linux进程详解
技术分享 启动新进程 stdlib.hintsystem(const char *string)whichequals to "sh -c string" 替换进程映像unistd ...
- Linux 进程详解
Linux内核的七大区间 .进程管理(进程创建,进程的三种状态,进程间的调度,调度算法...) .内存管理(段式管理(Linux所有段都从0开始),页式管理--地址偏移量) .系统调用(C语言库函数的 ...
- Linux学习之守护进程详解
Linux系统守护进程详解 ---转自:http://yuanbin.blog ...
- Linux 系统结构详解
Linux 系统结构详解 Linux系统一般有4个主要部分: 内核.shell.文件系统和应用程序.内核.shell和文件系统一起形成了基本的操作系统结构,它们使得用户可以运行程序.管理文件并使用系统 ...
- PHP 进程详解
.note-content { font-family: "Helvetica Neue", Arial, "Hiragino Sans GB", STHeit ...
- linux syslog详解
linux syslog详解 分三部分 一.syslog协议介绍 二.syslog函数 三.linux syslog配置 一.syslog协议介绍 1.介绍 在Unix类操作系统上,syslog广 ...
- Linux init详解(转)
Linux init详解 一.什么是INIT: init是Linux系统操作中不可缺少的程序之一. 所谓的init进程,它是一个由内核启动的用户级进程. 内核自行启动(已经被载入内存,开始运行,并已初 ...
- Linux内存详解
--Linux内存详解 -----------------2014/05/24 Linux的内存上表现的不像windows那么直观,本文准备详细的介绍一下Linux的内存. 请看这下有linux命令f ...
- Linux权限详解 命令之 chmod:修改权限
权限简介 Linux系统上对文件的权限有着严格的控制,用于如果相对某个文件执行某种操作,必须具有对应的权限方可执行成功. Linux下文件的权限类型一般包括读,写,执行.对应字母为 r.w.x. Li ...
随机推荐
- 一个完整的编译器前端-A.1 源语言
这个语言的一个程序由一个块组成,该块中包含可选的声明和语句.语法符号basic表示基本类型. program –> block block –> { decls stmts } dec ...
- linux init.d脚本编写模板
#!/bin/bash ### BEGIN INIT INFO # # Provides: location_server # Required-Start: $local_fs $remote_fs ...
- UML(统一建模语言)
最近看了一个UML图,所以特意来了解一下UML 统一建模语言 锁定 同义词 UML(统一建模语言)一般指统一建模语言 本词条由“科普中国”百科科学词条编写与应用工作项目 审核 . Unified Mo ...
- stripslashes — 反引用一个引用字符串
stripslashes (PHP 4, PHP 5) stripslashes — 反引用一个引用字符串 Report a bug 说明 string stripslashes ( string ...
- Stream探究
http://segmentfault.com/a/1190000003479884 1. 认识Stream Stream的概念最早来源于Unix系统,其可以将一个大型系统拆分成一些小的组件,然后将这 ...
- 《PHP程序设计》读书笔记
好久没有更新过我的博客了,主要前一阵子去了实习,现在实习进入尾声,终于有机会看看书了. 在前一阵子的实习中,用到最多就是PHP的CI框架和Jquery,所以现在再看一本有关PHP的书籍来深刻认识一下P ...
- editplus批量删除重复行(编辑-删除-删除重复行)
editplus快速删除重复数据 多行文本,有些行的文字或数据是重复的,该怎么删除重复部分,只留下不重复的部分?很多人对这个问题感到无比头疼,Editplus同样能快速帮你删除数据. 那么,editp ...
- 搭建DNS服务器
导读 Linux下架设DNS服务器通常是使用Bind程序来实现的.Bind是一款实现DNS服务器的开放源码的软件.DNS即域名系统,主要功能是将人们易于记忆的Domain Name(域名)与不易记忆的 ...
- unity3d 参考坐标系
原地址:http://www.cnblogs.com/88999660/archive/2013/04/01/2993844.html 参考坐标系(Reference Coordinate Syste ...
- 使用 nginx + thin 的配置启动 rails server
http://www.iwangzheng.com 在大师的指导下配置了新的服务器的nginx,通过top命令查看了服务器是8个cpu的,所以起了8个端口,把它们都映射到一个总的端口3600上,需要在 ...