#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <unistd.h> #include <sys/wait.h> #include <string.h> #include <error.h> char command[256]; void main() { int rtn; /*子进程的返回数值*/ while(1) { printf( ">" ); //从终端读取要执行的命令 fgets(command, 256, stdin); //将命令数据存放到command中 command[strlen(command)-1] = 0; if (fork() == 0 ) //在子进程中执行这个命令 { execlp(command,command,NULL); //如果exec函数返回,表明没有正常执行命令,打印错误信息 perror(command); exit(1); } else //在父进程中等待字进程结束,并且打印子进程的返回值 { wait( &rtn ); printf("子进程返回%d\n",rtn); exit(0); } } exit(0); }
当前文章:[Linux进程]一个完整的进程操作实例
文章地址:https://www.cdcxhl.com/article16/gsphdg.html
成都网站建设公司_创新互联,为您提供全网营销推广、网页设计公司、服务器托管、云服务器、网站维护、网站设计公司
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联