linux 线程或进程之间通过管道通信(pipe)
来源:互联网 发布:思科网络技术学院官网 编辑:程序博客网 时间:2024/06/05 09:32
线程间通信:
#include <stdio.h> // printf#include <stdlib.h> // exit#include <unistd.h> // pipe#include <string.h> // strlen#include <pthread.h> // pthread_createusing namespace std;void *func(void * fd){ printf("write fd = %d\n", *(int*)fd); char str[] = "hello everyone!"; write( *(int*)fd, str, strlen(str) );}int main(){ int fd[2]; char readbuf[1024]; if(pipe(fd) < 0) { printf("pipe error!\n"); } // create a new thread pthread_t tid = 0; pthread_create(&tid, NULL, func, &fd[1]); pthread_join(tid, NULL); sleep(3); // read buf from child thread read( fd[0], readbuf, sizeof(readbuf) ); printf("read buf = %s\n", readbuf); return 0;}
// 输出结果
write fd = 4read buf = hello everyone!�
#include <stdio.h> // printf#include <stdlib.h> // exit#include <unistd.h> // pipe#include <string.h> // strlen#include <pthread.h> // pthread_createusing namespace std;int main(){ int fd[2]; int pid = 0; char str[] = "hello"; char readbuf[1024]; if(pipe(fd) < 0) { printf("pipe error!\n"); } if((pid = fork()) < 0) { printf("fork error!\n"); } else if(pid == 0) { printf("child process!\n"); // close read channel close(fd[0]); write(fd[1], str, strlen(str)); } else { printf("father process!\n"); // close write channel close(fd[1]); read(fd[0], readbuf, sizeof(readbuf)); printf("readbuf = %s\n", readbuf); } return 0;}
// 输出结果:
father process!child process!readbuf = hello
- linux 线程或进程之间通过管道通信(pipe)
- linux 进程和线程或线程和线程之间通过管道通信(pipe)
- Linux进程通信-管道(pipe)
- Linux pipe函数 进程之间的管道通信
- Linux--进程间通信(一)-管道(pipe)通信
- 线程之间通过管道通信
- linux管道通信(pipe)
- linux进程间通信之管道(pipe与fifo)
- 深入理解Linux进程间通信(IPC)-- 管道pipe
- 【Linux系统编程】进程间通信--无名管道(pipe)
- Linux进程通信(一)——pipe管道
- Linux进程间通信之管道(pipe)
- Linux环境进程间通信 --- 管道Pipe
- Linux 进程间管道pipe通信
- Linux进程间通信之管道(pipe)
- Linux进程间通信之管道(pipe)
- 进程通信----管道(pipe)
- Linux进程通信IPC--管道Pipe/Named Pipe
- UVa 10305
- GLFW入门学习
- 编程之美--求Fibonacci
- 非常简单的创建多级目录
- 淡墨青春的QQ伤感日志:忘却了幸福,记起了悲伤
- linux 线程或进程之间通过管道通信(pipe)
- linux wget 使用
- 故事再美,还是会有结局:物是人非的空间日志
- hdu - 4321 - Arcane Numbers 2 - 数位统计
- c语言字节对齐
- python深拷贝浅拷贝
- 第十一章 内核数据类型
- 无法掩饰的憔悴,无法抗拒的痛紧紧相随
- 在winform程序中启动wpf窗体