linux下使用管道pipe和select创建阻塞
2017-01-20 10:10
1206 查看
linux下使用管道pipe和select创建阻塞的模型,大家参考一下
#include <unistd.h> #include <stdio.h> #include <sys/types.h> #include <sys/wait.h> #include <pthread.h> pthread_t tid; int pip_ios_fd[2] ; struct test { int* pip_opt; } t1; void* CallBack(void* arg) { printf("------enter callBack\n"); sleep(3); printf("------ unlock\n"); int wret = write(t1.pip_opt[1], "x", 1); //当往管道写入时 SELECT 解除阻塞 sleep(3); printf("------ check is over\n"); // 不会执行这一步的 return NULL; } bool select_test() { int pip_ret = pipe(pip_ios_fd); int ret_ = pthread_create(&tid, NULL, CallBack, NULL); pthread_detach(tid); /* 等待线程也初始化完成 */ fd_set fdsr; FD_ZERO(&fdsr); FD_SET(pip_ios_fd[0], &fdsr); struct timeval tv; tv.tv_sec = 10; tv.tv_usec = 0; t1.pip_opt = pip_ios_fd; //复制管道 int ret = select(pip_ios_fd[0] + 1, &fdsr, NULL, NULL, &tv); //阻塞10s printf("ret=%d.\n", ret); if (ret == -1) { //ret=-1 的时候说明轮巡 出错 printf("error!!!\n"); } else if (ret == 0) { //ret=0 的时候说明轮巡 超时 printf("No data within 10 seconds.\n\n"); } else { //符合条件的时候 printf("------ ok\n"); } printf("------go to read\n"); char c[2]; read(t1.pip_opt[0], c, 2); printf("c-------------------------->==%s\n", c); // pthread_kill(tid, SIGQUIT); // sleep(10); return true; } int main(void) { select_test(); return 0; }
相关文章推荐
- [Linux管道和IPC]使用pipe函数创建管道
- Linux管道pipe使用实例
- linux网络编程_1 管道的创建和使用
- Linux环境编程--如何用C语言创建多进程运行以及简单的pipe管道使用
- 用管道(pipe)使消息队列通知(mq_notify)可以在Select和poll中使用
- 关于Linux_shell中的管道命令pipe “|”的简单学习和使用
- Linux系统编程:pipe匿名管道的使用,实现linux命令下管道命令
- [Linux管道和IPC]管道的高级应用--使用popen函数创建管道
- Linux 管道(pipe)原理及使用
- linux c之通过popen和pclose函数创建管道执行shell 运行命令使用总结
- Python中使用PIPE操作Linux管道
- Linux操作系统中的管道(pipe)使用方法
- 用管道(pipe)使消息队列通知(mq_notify)可以在Select和poll中使用
- Linux 管道(pipe)原理及使用
- [Linux管道和IPC]命名管道操作--使用mkfifo函数创建命名管道
- [Linux管道和IPC]使用msgget创建消息队列
- Python中使用PIPE操作Linux管道
- [linux C]使用select进行非阻塞socket通信
- 2013年12.2本周计划(内存映射,管道的创建,strcpy的使用,linux中库的封装,EXCEL数据的拟合)
- Linux下的有名管道(03)---使用函数创建和删除有名管道