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 ...
随机推荐
- appium按钮定位,去掉弹出框
#coding=utf-8 这个一定要加上,不然脚本中注释中都不能有中文 ''' Created on 2015年7月2日 @author: liujuan ''' import sys reload ...
- CentOS 6.4 32位系统 LAMP(Apache+MySQL+PHP)安装步骤
先来解释一下,什么是 LAMP.正如标题所言,LAMP 实际上就是 Linux.Apache.MySQL.PHP 四个名称的缩写,当然最后一个 “P” 还有其他说法是 Perl 或者 Python.不 ...
- Practical Machine Learning For The Uninitiated
Practical Machine Learning For The Uninitiated Last fall when I took on ShippingEasy's machine learn ...
- iOS @try @catch简单应用举例
- Visual Studio Online Integrations-Collaboration
原文:http://www.v ...
- ubuntu快速清理磁盘垃圾
.快速清理磁盘垃圾 磁盘空间又不够用了?尝试在终端窗口中输入sudo apt-get autoremove然后输入sudo apt-get clean,前一个命令会卸载系统中所有未被使用的依赖关系,后 ...
- 15个超实用的php正则表达式
在这篇文章里,我已经编写了15个超有用的正则表达式,WEB开发人员都应该将它收藏到自己的工具包. 验证域名 检验一个字符串是否是个有效域名. $url = "http://komunitas ...
- kindle paperwhite折腾记
在亚马逊官网上买了一个kindle paperwhite 一代(849元) , 打算再买个皮套, 淘宝店 http://detail.tmall.com/item.htm?spm=a230r.1.1 ...
- Ubuntu下编译SuiteSparse-4.4.1和METIS-4.0.3
网上关于编译的介绍非常多,其实ubuntu系统自带编译好的SuiteSparse,不想折腾的话,用新立得很容易就搞定 准备工作: 下载并编译OpenBLAS(会连带Lapack也下载和编译),图省事请 ...
- Spring3中的mvc:interceptors标签配置拦截器
mvc:interceptors 这个标签用于注册一个自定义拦截器或者是WebRequestInterceptors. 可以通过定义URL来进行路径请求拦截,可以做到较为细粒度的拦截控制. 例如在配置 ...