操作系统中execl()函数问题

一叶孤星坠空城 2014-04-03 03:20:24






execl("/bin/ls","-a",NULL);
printf("You should never see this because the child is already gone.\n");

请解释下语句
为什么后面那个语句没有输出呢?是切换到其它进程了吗?切换了然后呢?


bin/ls 是列出bin这个目录的列表吗?-a是什么意思?
怎样才能输出后面那句,不能删除execl
...全文
205 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
拜谢!!!!
__cc__ 2014-04-03
  • 打赏
  • 举报
回复
引用 2 楼 u012104329 的回复:
你这里不是还有个fork嘛, fork后就分成两个进程了, 子进程调用了execl,最后被ls命令替换掉 父进程执行了 printf("Process %d | I forked one child process--%d.\n",getpid(),pid); printf("Process %d | I am waiting for child to exit.\n",getpid()); waitpid(pid,NULL,0);//在这里会等待ls执行完成 printf("Process %d | I exited.\n",getpid()); 所以父进程最后退出。
  • 打赏
  • 举报
回复
引用 3 楼 u012104329 的回复:
进程链是 19644-3684-18556
  • 打赏
  • 举报
回复


运行截图
  • 打赏
  • 举报
回复
引用 1 楼 neustar1 的回复:
[quote=引用 楼主 u012104329 的回复:] execl创建的进程会把原来的进程的指令给替换掉,换句话,execl执行后,执行语句里面没有了后面那个printf,接下来执行的是指定的那个进程的指令。 bin/ls是指bin目录下的ls程序,-a传入的一个参数。 你要想不删除execl执行后面那条语句,不可能!!!
int main(void)
{
  int i=0,j=0;
  pid_t pid;
  printf("Hello from the main Process,PID is %d.\n",getpid());
  
  pid=fork();
  printf("Process %d | My Parent PID is %d.\n",getpid(),getppid());
  sleep(1);
  
  if(pid==0)       // child process
  {
    sleep(1);
	printf("Process %d | I am calling exec.\n",getpid());
	execl("/bin/ls","-a",NULL);
	printf("You should never see this because the child is already gone.\n");
  }
  
  else if(pid!=-1) // parent process
  {
    printf("Process %d | I forked one child process--%d.\n",getpid(),pid);
	printf("Process %d | I am waiting for child to exit.\n",getpid());
	waitpid(pid,NULL,0);
	printf("Process %d | I exited.\n",getpid());
  }
  
  else 
    printf("Everything was done.\n");
	
  return 0;
}
那子进程切换成另一个进程了,然后怎么运行? 父进程printf("Process %d | I exited.\n",getpid());为什么还能输出?
__cc__ 2014-04-03
  • 打赏
  • 举报
回复
引用 楼主 u012104329 的回复:
execl("/bin/ls","-a",NULL); printf("You should never see this because the child is already gone.\n"); 请解释下语句 为什么后面那个语句没有输出呢?是切换到其它进程了吗?切换了然后呢? bin/ls 是列出bin这个目录的列表吗?-a是什么意思? 怎样才能输出后面那句,不能删除execl
execl创建的进程会把原来的进程的指令给替换掉,换句话,execl执行后,执行语句里面没有了后面那个printf,接下来执行的是指定的那个进程的指令。 bin/ls是指bin目录下的ls程序,-a传入的一个参数。 你要想不删除execl执行后面那条语句,不可能!!!

64,439

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧