Forking and executing process
2017-07-12 10:43
399 查看
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
Testand 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);:
waitreturns waited PID while
&retValuepassed pointer to
retValueto
wait(), and
wait()will put process ended information into
refValue.
cout<<WEXITSTATUS(retValue)<<endl;:
WEXITSTATUS(retValue)gets return value in
main()of the process.
Results
Run the code withYOUR_PROGRAM_NAME ANOTHER_PROGRAMwhile 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 program 7 # Return value of another in main 16608 # Child process PID After wait # cout<<"After wait"<<endl;
on Terminal.
FYI, Phases after # are comments.
相关文章推荐
- Shell script for logging cpu and memory usage of a Linux process
- psutil - A cross-platform process and system utilities module for Python - Google Project Hosting
- OSWorkflow: A guide for Java developers and architects to integrating open-source Business Process M
- Business Process Implementation for IT Professionals and Managers
- Jenkins 安装启动提示“iJob for jenkins.service failed because the control process exited with error code. See "systemctl status jenkins.service" and "journalctl -xe" for details.”
- Unity 2017 - error: Failed executing external process for 'Clustering' job.
- Unity Failed executing external process for 'Bake Runtime' job
- 动态二进制代码翻译[1][VM Versatile platforms for systems and process]
- 动态二进制代码翻译[2][VM Versatile platforms for systems and process]
- 对数据集“dsArea”执行查询失败。 (rsErrorExecutingCommand),Query execution failed for dataset 'dsArea'. (rsErrorExecutingCommand),Manually process the TFS data warehouse and analysis services cube
- CMMI : Guidelines for Process Integration and Product Improvement
- Unable to query for default vendor from RPM: Error while executing process. Cannot run program "rpm"
- OSWorkflow: A guide for Java developers and architects to integrating, open-source Business Process Management (Paperback)
- What Is - Process flow for submitted applications and releases
- Unity2017 - error: Failed executing external process for 'Bake Runtime' job.
- Software Reuse : Architecture,Process and Organization for Business Success
- MS SQL错误:SQL Server failed with error code 0xc0000000 to spawn a thread to process a new login or connection. Check the SQL Server error log and the Windows event logs for information about possible related problems
- Job for httpd.service failed because the control process exited with error code. See "systemctl status httpd.service" and "journalctl -xe" for details
- crontab “tput: No value for $TERM and no -T specified ” error logged by CRON process
- 转-9.13. Forking a Daemon Process on Unix