Forking and executing process
来源:互联网 发布:蚁群算法公式解析 编辑:程序博客网 时间:2024/05/20 04:11
Summary
This passage tells you how to fork a child process and execute another command in child process on Linux.
Though we are using C library on Linux to do such stuff, however I would like to use C++ to handle outputs.
Libraries
unistd.h
iostream
(For C++)stdio.h
(For C)
Code
#include<iostream>#include<unistd.h>using std::cout;using std::endl;int main(int argc, char* argv[]){ pid_t fpid, sp; int retValue; if((fpid = fork()) < 0){ cout<<"Error"<<endl; }else if(fpid == 0){ cout<<"Child process PID: "<<getpid()<<endl; if(argc > 1){ execl(argv[1], NULL); } }else{ cout<<"Main process:"<<endl; } cout<<"Before wait"<<endl; sp = wait(&retValue); cout<<WEXITSTATUS(retValue)<<endl; cout<<sp<<endl; cout<<"After wait"<<endl; return 0;}
Let’s say we have a program that prints Test
and return 7 at the end of execution (return 7;
in main
).
Explanations
if((fpid = fork()) < 0)
:fork()
returns -1 when it cannot start a process.else if(fpid == 0)
:fork()
returns 0 when it’s in child process.else{cout<<"Main process:"<<endl;}
:fork()
returns PID when it’s main process.cout<<"Son process PID: "<<getpid()<<endl;
: Prints child process PID.sp = wait(&retValue);
:wait
returns waited PID while&retValue
passed pointer toretValue
towait()
, andwait()
will put process ended information intorefValue
.cout<<WEXITSTATUS(retValue)<<endl;
:WEXITSTATUS(retValue)
gets return value inmain()
of the process.
Results
Run the code with YOUR_PROGRAM_NAME ANOTHER_PROGRAM
while YOUR_PROGRAM_NAME is the compilation of codes above and ANOTHER_PROGRAM is another program that you want to execute in our program.
This code will print
Main process: # cout<<"Main process:"<<endl;Before wait # cout<<"Before wait"<<endl;Son process PID: 16608 # cout<<"Son process PID: "<<getpid()<<endl;Test # Output of another program7 # Return value of another in main16608 # Child process PIDAfter wait # cout<<"After wait"<<endl;
on Terminal.
FYI, Phases after # are comments.
阅读全文
0 0
- Forking and executing process
- Dynamic forking (Process hollowing)
- Forking a Daemon Process on Unix
- Bottomley在Linux大会的发言:Android,forking,and control
- zombie process and orphan process
- zombie process and orphan process
- Forking工作流
- Forking工作流
- operation is executing and cannot be enqueued
- 提示 Could not start process "**.sh" When executing step 'Custom Process Step'
- Symbian Thread and Process
- ASM and Process 杂项
- Symbian Thread and Process
- sys port and process
- user port and process
- Process and Thread
- Fork and exit process
- Android------Process and Threads
- 【案例分享】项目施工进度报告
- 安卓FragmentTabHost+Fragment+ViewPager+TabLayout仿今日头条
- 数据库系统概率学习攻略(第8组)
- CSS把边框 border变成内边框,就是不改变宽度高度
- 高通lcd调试总结
- Forking and executing process
- 1954【线性规划与网络流24题 19】负载平衡问题
- poj2676Sudoku
- 51nod 1163 最高的奖励 (贪心+优先队列/并查集)&& 1191 消灭兔子(贪心+优先队列维护) && 1475 建设国家 (思维枚举+优先队列)
- StateList资源
- caffe过程中的错误(二)
- 2017年7月 bug总结
- Apache ab性能测试
- ArrayList在多线程调用Add()添加元素时的下标越界问题(java.lang.ArrayIndexOutOfBoundsException)