popen pclose测试
来源:互联网 发布:淘宝手机搜索排名规则 编辑:程序博客网 时间:2024/05/16 05:13
#include <stdio.h>#include <unistd.h>#include <pthread.h>#include <iostream>using namespace std;void* thread(void* arg){ printf("thread created.\n"); int ch; ch = getc((FILE*)arg); while (EOF != ch) { printf("%c", ch); ch = getc((FILE*)arg); } printf("thread exist.\n"); return (void*)NULL;}int main(){ FILE *pFile = popen("./test", "r"); if (pFile != NULL) { pthread_t pid; pthread_create(&pid, NULL, thread, (void*)pFile); pclose(pFile); cout << "pclose returned" << endl; } else { cout << "popen failed. " << endl; } while(1) { sleep(100); } return 0;}
test每隔一秒输出一个字符
经测试没有一个字符输出。
#include <stdio.h>#include <unistd.h>#include <pthread.h>#include <iostream>using namespace std;void* thread(void* arg){ printf("thread created.\n"); int ch; ch = getc((FILE*)arg); while (EOF != ch) { printf("%c", ch); ch = getc((FILE*)arg); } printf("thread exist.\n"); return (void*)NULL;}int main(){ FILE *pFile = popen("./test", "r"); if (pFile != NULL) { pthread_t pid; //pthread_create(&pid, NULL, thread, (void*)pFile); int ch; ch = getc(pFile); while (EOF != ch) { printf("%c", ch); ch = getc(pFile); } pclose(pFile); cout << "pclose returned" << endl; } else { cout << "popen failed. " << endl; } while(1) { sleep(100); } return 0;}
此处正确输出了。
结论:pclose在发现管道里有内容时就会立马返回并关闭管道。
因此要想使popen执行完命令,只有在pclose前检查管道文件是否到达EOF。
- popen pclose测试
- apue--popen and pclose
- popen 和 pclose 函数
- popen和pclose函数
- Linux中的popen-pclose
- popen和pclose函数
- popen与pclose
- popen和pclose函数
- linux popen和pclose
- popen和pclose
- popen()和pclose()
- popen和pclose
- popen+pclose 执行 shell 命令
- Linux popen和pclose函数
- 基于管道的popen和pclose函数
- 基于管道的popen和pclose函数
- 基于管道的popen和pclose函数
- 基于管道的popen和pclose函数
- 终于揭开了makefile之神秘面纱。。。
- 邮件和邮件服务器
- LinkList的新建,插入,删除等的实现
- linux rpm 合集
- ubuntu下设置开机启动 sysv-rc-conf
- popen pclose测试
- Android HAL实例解析
- 药家鑫案真相!做父母的必看
- 2011阿里巴巴程序设计公开赛 / 解题报告 8.18
- hdu 3001 Travelling//状态压缩DP
- Java调用WASMQ
- 将Xml字符串转换成(DataTable || DataSet || XML)对象
- 关于static
- 堆和栈的区别(转过无数次的文章)