免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1731 | 回复: 3
打印 上一主题 下一主题

[C] 关于system函数 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-12-23 14:53 |只看该作者 |倒序浏览

  1. Figure 8.22. The system function, without signal handling
  2. #include    <sys/wait.h>
  3. #include    <errno.h>
  4. #include    <unistd.h>

  5. int
  6. system(const char *cmdstring)    /* version without signal handling */
  7. {
  8.     pid_t   pid;
  9.     int     status;

  10.     if (cmdstring == NULL)
  11.         return(1);      /* always a command processor with UNIX */

  12.     if ((pid = fork()) < 0) {
  13.         status = -1;    /* probably out of processes */
  14.     } else if (pid == 0) {              /* child */
  15.         execl("/bin/sh", "sh", "-c", cmdstring, (char *)0);
  16.         _exit(127);     /* execl error */
  17.     } else {                             /* parent */
  18.         while (waitpid(pid, &status, 0) < 0) {        //为什么要用循环啊?
  19.             if (errno != EINTR) {
  20.                 status = -1; /* error other than EINTR from waitpid() */
  21.                 break;
  22.             }
  23.         }
  24.     }

  25.     return(status);
  26. }

复制代码

论坛徽章:
0
2 [报告]
发表于 2007-12-23 15:05 |只看该作者
是不是因为execl("/bin/sh", "sh", "-c", cmdstring, (char *)0);
这里面的cmdstring可以是多个命令?

论坛徽章:
0
3 [报告]
发表于 2007-12-23 17:58 |只看该作者
不是一个命令,只有一个,后面的只是参数
这不是会被信号中断吗?
中断了就再等,再等就需要循环

论坛徽章:
0
4 [报告]
发表于 2007-12-23 21:21 |只看该作者
有点明白了,谢谢flw2

[ 本帖最后由 xi2008wang 于 2007-12-23 21:31 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP