利用管道进行通信
2014-11-10 18:40
447 查看
管道简介:
管道是单向的、先进先出的、无结构的、固定大小的字节流,它把一个进程的标准输出和另一个进程的标准输入连接在一起。写进程在管道的尾端写入数据,读进程在管道的首端读出数据。数据读出后将从管道中移走,其它读进程都不能再读到这些数据。管道提供了简单的流控制机制。进程试图读空管道时,在有数据写入管道前,进程将一直阻塞。同样,管道已经满时,进程再试图写管道,在其它进程从管道中移走数据之前,写进程将一直阻塞。
关于管道的代码实例
此程序是关于管道的创建和读写和关闭
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int fd[2];
char str[256];
if( pipe(fd) < 0 )
{
perror("pipe");
exit(1);
}
write(fd[1],"create the pipe successfully!\n ",31);
read(fd[0],str,sizeof(str));
printf("%s",str);
printf("pipe file descriptors are %d ,%d\n",fd[0],fd[1]);
close(fd[0]);
close(fd[1]);
return 0;
}
管道是单向的、先进先出的、无结构的、固定大小的字节流,它把一个进程的标准输出和另一个进程的标准输入连接在一起。写进程在管道的尾端写入数据,读进程在管道的首端读出数据。数据读出后将从管道中移走,其它读进程都不能再读到这些数据。管道提供了简单的流控制机制。进程试图读空管道时,在有数据写入管道前,进程将一直阻塞。同样,管道已经满时,进程再试图写管道,在其它进程从管道中移走数据之前,写进程将一直阻塞。
关于管道的代码实例
此程序是关于管道的创建和读写和关闭
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int fd[2];
char str[256];
if( pipe(fd) < 0 )
{
perror("pipe");
exit(1);
}
write(fd[1],"create the pipe successfully!\n ",31);
read(fd[0],str,sizeof(str));
printf("%s",str);
printf("pipe file descriptors are %d ,%d\n",fd[0],fd[1]);
close(fd[0]);
close(fd[1]);
return 0;
}
此程序说明管道内的数据在读出之后就没有了 #include<stdio.h> #include<unistd.h> int main() { int filedes[2]; char buffer[20]; pipe(filedes); if( fork()> 0) { char s[]="hello\n"; write(filedes[1],s,sizeof(s)); } else { read(filedes[0],buffer,80); printf("%s\n",buffer); } read(filedes[0],buffer,80); printf("%s\n",buffer); close(filedes[0]); close(filedes[1]); return 0; }
父子进程之间的通过管道进行通信 #include<unistd.h> #include<stdio.h> #include<fcntl.h> #include<sys/types.h> int main() { int fd[2]; char buf[20]; pid_t pid; int len; if( pipe(fd) < 0 ) { perror("failed to pipe;"); return 1; } if( ( pid = fork() ) < 0 ) { perror("failed to fork;"); return 1; } else if( pid > 0 ) { close(fd[0]); write(fd[1],"hello my son\n",14); return 0; } else { close(fd[1]); read(fd[0],buf,20); printf("%s\n",buf); } return 0; }
兄弟进程之间进行通信 要点:需要在第二次创建一个子进程的时候关闭父进程管道的两端, 而不是第一次,这样做的目的是继承一个存活的管道。 #include<unistd.h> #include<stdio.h> #include<stdlib.h> #include<fcntl.h> #include<sys/types.h> int main( void ) { int fd[2]; char buf[100]; pid_t pid; int len; if( pipe(fd) < 0 ) perror("pipe"); if( ( pid = fork() ) < 0) perror("fork1"); else if( pid == 0 ) { close(fd[0]); write(fd[1],"hello brother!",20); exit(0); } if( ( pid = fork() ) < 0) perror("fork2"); else if( ( pid > 0 ) < 0) { close(fd[0]); close(fd[1]); exit(0); } else { close(fd[1]); read(fd[0],buf,20); printf("%s\n",buf); } return 0; }
相关文章推荐
- Linux初学,利用管道文件进行通信,实现3个人之间的群聊
- 进程间利用管道进行通信:打印斐波那契数列
- 利用管道进行通信
- 进程间通信---在父进程跟子进程之间利用管道进行通信。一个简单的例子
- 线程利用管道字节(字符)流进行通信
- 利用管道进行进程的通信示例
- C++多进程利用管道进行实时通信
- 利用管道进行通信
- 客户端和应用服务器之间利用HttpClient和Servlet交互JSON对象进行通信
- 使用命名管道通过网络在进程之间进行通信
- 转:利用WinInet类进行TCP/IP通信内容
- [Silverlight学习笔记]关于利用WCF RIA Service进行通信并在客户端获取数据
- [ZZ]使用命名管道通过网络在进程之间进行通信
- 利用WM_COPYDATA进行进程间单向通信
- Android 利用httpclient进行网络通信,实现用户登录的方法
- 利用命名管道实现进程之间的通信 .........
- 利用命名管道实现进程之间的通信 .........
- 在libevent中使用线程池(采用管道来进行线程的通信)
- 进程间通信 3 -- 使用管道进行通信
- C#与java利用XML进行通信