Linux 进程间通讯之创建无名管道和读写无名管道
2012-12-21 21:15
471 查看
Linux进程间通讯的方式:
1. 管道(pipe)和有名管道(FIFO).
2. 信号(signal)
3. 消息队列
4. 共享内存
5. 信号量
6. 套接字(socket)
管道通讯:
无名管道:由pipe()函数创建,int
pipe(int filedis[2]), 当管道建立时有两个文件描述符,filedis[0]用于读管道,filedis[1]用于写管道。关闭管道,仅需将两个文件描述符关闭即可。
创建无名管道pipe():
#include <unistd.h>
#include <stdio.h>
int main(){
intpipe_filed[2];
if(pipe(pipe_filed)<0){
printf("pipecreate failed.\n");
return-1;
}
else{
printf("pipecreate successfully.\n");
close(pipe_filed[0]); //关闭相应读管道(必须要)
close(pipe_filed[1]); //关闭相应写管道(必须要)
}
}
注:fork()必须在pipe()之前来创建子进程,否则子进程无法进程相应的文件描述符,如果在fork之后,那么fork会在子/父进程各执行一次,属不同的管道。
读写无名管道示例:
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
int main(){
charcache[100];
intfiledis[2];
if(pipe(filedis)< 0){
printf("Createpipe failed.\n");
return-1;
}
memset(cache,0, sizeof(cache)); //在一段内存块中填充某个特定的值,此为,对cache数组以0进行填充
intpid;
pid= fork(); // 创建子进程
if(pid== 0){ // 进入子进程
printf("Inchild process.\n");
close(filedis[1]); // 关闭写管道
sleep(3); // 等待,为了等父进程写入东西
read(filedis[0],cache, 100); // 读管道,并将内容写入cache之中
printf("%s.\n",cache); // 打印cache内容
close(filedis[0]); // 关闭读管道
}
elseif(pid > 0){ // 进入父进程
printf("Inparent process.\n");
close(filedis[0]); // 关闭读管道
write(filedis[1],"Hello ",6); // 第一次写入
write(filedis[1],"linuxdba", 8); // 第二次写入
close(filedis[1]); // 关闭写管道
wait(NULL); // 等待子进程退出再往下执行
}
else{
printf("Createchild process failed.\n");
return-1;
}
return0;
}
1. 管道(pipe)和有名管道(FIFO).
2. 信号(signal)
3. 消息队列
4. 共享内存
5. 信号量
6. 套接字(socket)
管道通讯:
无名管道:由pipe()函数创建,int
pipe(int filedis[2]), 当管道建立时有两个文件描述符,filedis[0]用于读管道,filedis[1]用于写管道。关闭管道,仅需将两个文件描述符关闭即可。
创建无名管道pipe():
#include <unistd.h>
#include <stdio.h>
int main(){
intpipe_filed[2];
if(pipe(pipe_filed)<0){
printf("pipecreate failed.\n");
return-1;
}
else{
printf("pipecreate successfully.\n");
close(pipe_filed[0]); //关闭相应读管道(必须要)
close(pipe_filed[1]); //关闭相应写管道(必须要)
}
}
注:fork()必须在pipe()之前来创建子进程,否则子进程无法进程相应的文件描述符,如果在fork之后,那么fork会在子/父进程各执行一次,属不同的管道。
读写无名管道示例:
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
int main(){
charcache[100];
intfiledis[2];
if(pipe(filedis)< 0){
printf("Createpipe failed.\n");
return-1;
}
memset(cache,0, sizeof(cache)); //在一段内存块中填充某个特定的值,此为,对cache数组以0进行填充
intpid;
pid= fork(); // 创建子进程
if(pid== 0){ // 进入子进程
printf("Inchild process.\n");
close(filedis[1]); // 关闭写管道
sleep(3); // 等待,为了等父进程写入东西
read(filedis[0],cache, 100); // 读管道,并将内容写入cache之中
printf("%s.\n",cache); // 打印cache内容
close(filedis[0]); // 关闭读管道
}
elseif(pid > 0){ // 进入父进程
printf("Inparent process.\n");
close(filedis[0]); // 关闭读管道
write(filedis[1],"Hello ",6); // 第一次写入
write(filedis[1],"linuxdba", 8); // 第二次写入
close(filedis[1]); // 关闭写管道
wait(NULL); // 等待子进程退出再往下执行
}
else{
printf("Createchild process failed.\n");
return-1;
}
return0;
}
相关文章推荐
- Linux 进程间通讯之创建无名管道和读写无名管道
- Linux下进程的创建及其使用管道进行进程之间的通讯[00原创]
- Linux进程间通讯一--无名管道PIPE
- Linux中进程通讯--无名管道
- linux进程通信机制之无名管道&有名管道
- linux c之管道的介绍、创建关闭和简单读写(父进程向子进程写入数据)
- linux进程通信--无名管道
- 进程通讯之无名管道
- 【linux】驱动程序创建和读写进程文件
- 无名管道创建和读写
- linux 进程编程:无名管道pipe
- Linux笔记_进程通讯——管道
- Linux c==进程通信--无名管道(2)
- 进程间通讯摘录(1)-----无名管道
- linux 管道创建进程
- 进程间通讯-无名管道
- 进程返回linux系统编程之管道(二):管道读写规则和Pipe Capacity、PIPE_BUF
- 进程间通讯——无名管道
- linux_c开发(5-2)进程间通讯_管道通讯
- linux 进程间管道通讯 popen以及pclose