免费注册 查看新帖 |

Chinaunix

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

[函数] 请教如何判断system函数执行完成 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-11-22 10:12 |只看该作者 |倒序浏览
以前用system函数没有想太多,都是直接用一下.现在感觉用这个函数是一定要判断它是否以正常执行完.
例:

  1. ...
  2. system("rm 123.txt");
  3. i++;
  4. ...
复制代码

如果是这样的话,有可能123文件还没有删除,i已经加1了,我要的是确保123删除后,i再加1
谢谢

论坛徽章:
0
2 [报告]
发表于 2005-11-22 10:14 |只看该作者
很多辅助手段啊

比如access一下看看文件是不是存在。。。

论坛徽章:
0
3 [报告]
发表于 2005-11-22 10:15 |只看该作者
system("rm 123.txt");
sleep(9);
i++;

这样试试呢?

论坛徽章:
0
4 [报告]
发表于 2005-11-22 10:17 |只看该作者
system() executes a command specified in command by calling /bin/sh -c command, and returns after the command has been completed.

论坛徽章:
0
5 [报告]
发表于 2005-11-22 10:18 |只看该作者
原帖由 松赞,干不? 于 2005-11-22 10:14 发表
很多辅助手段啊

比如access一下看看文件是不是存在。。。

对,可以利用stat函数判断

论坛徽章:
0
6 [报告]
发表于 2005-11-22 10:20 |只看该作者
原帖由 assiss 于 2005-11-22 10:17 发表
system() executes a command specified in command by calling /bin/sh -c command, and returns after the command has been completed.


我觉得还是加判断好一些,执行完成并不代表成功

论坛徽章:
0
7 [报告]
发表于 2005-11-22 10:24 |只看该作者
RETURN VALUE
The value returned is -1 on error (e.g. fork failed), and the return status of the command otherwise. This latter return status is in the format specified in wait(2). Thus, the exit code of the command will be WEXITSTATUS(status). In case /bin/sh could not be executed, the exit status will be that of a command that does exit(127).
If the value of command is NULL, system() returns nonzero if the shell is available, and zero if not.

论坛徽章:
0
8 [报告]
发表于 2005-11-22 10:27 |只看该作者
如果实在需要确认,不如用POPEN或者EXEC家族函数。

论坛徽章:
0
9 [报告]
发表于 2005-11-22 10:30 |只看该作者
正解!
其实就相当于fork+exec+wait(阻塞)
所以下一句一定是在system运行完以后才运行的。而且可以根据返回值是否为0来判断运行结果。
原帖由 assiss 于 2005-11-22 10:17 发表
system() executes a command specified in command by calling /bin/sh -c command, and returns after the command has been completed.

论坛徽章:
0
10 [报告]
发表于 2005-11-22 10:32 |只看该作者
原帖由 assiss 于 2005-11-22 10:24 发表
RETURN VALUE
The value returned is -1 on error (e.g. fork failed), and the return status of the command otherwise. This latter return status is in the format specified in wait(2). Thus, the exit c ...




这个是关键!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP