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 ...
随机推荐
- js中网页图片自动更换的效果
<script> var arr=new Array(); arr[]="url(images/city.jpg)"; arr[]="url(images/d ...
- Protocol Buffers(Protobuf)开发者指南---概览
Protocol Buffers(Protobuf)开发者指南---概览 欢迎来到protocol buffers的开发者指南文档,protocol buffers是一个与编程语言无关‘.系统平台无关 ...
- MSF溢出实战教程
1. 进入终端,开启MSF相关服务 2. 连接数据库 3. 主机扫描 发现如果有MS08_067漏洞,就可以继续渗透 4. 开始溢出 溢出成功的话 sessions -l 查看 ...
- tuning-primer.sh mysql 报表
[root@server-mysql mytop-1.6]# wget http://www.day32.com/MySQL/tuning-primer.sh --2016-07-03 19:42:3 ...
- Struts2拦截器的应用
拦截器类 public class AdminInterceptor extends AbstractInterceptor { private static final long serialVer ...
- linux在安装jdk时报错
用脚本执行jdk时下面的错误 can not initialize UI, Running in headless mode, No X11 DISPLAY variable was set, but ...
- 转 XenServer、XenCenter安装测试
本文转自:http://blog.sina.com.cn/s/blog_5611597901014ze4.html 系统环境:win7 64bit vmware-8.0.1 镜像文件:XenServ ...
- javascript和HTML5上传图片之前实现预览效果
一:FileList对象与file对象 FileList对象表示用户选择的文件列表,在HTML4中,file控件内只允许放置一个文件,但是到了HTML5中,通过添加multiple属性,file控件内 ...
- jQuery类库的设计
目前为止,jquery是js社区中最活跃.用户最多的前端类库,具有链式操作.兼容性.基于数组的操作.强大的插件机制等特点,也是很多前端入门同学最早接触到的库.但是内部如何实现的,一直吸引着我.因此最近 ...
- DOM: 如何获取元素下的第一个子元素
Element.firstChild ?,是的,这是第一种方法,当然,通常来说支持 W3C 规范的浏览器,如 Firefox 等取到的应该是 TEXT_NODE.很早之前,或者说现在最流行的方法可能是 ...