linux下实现两人、三人无序对话功能
序:引子
对话功能实际上就是利用管道见得通信。最原始的是一方发另一方收,不能进项交互,发送方的代码如下:
/*============================================
> Copyright (C) 2014 All rights reserved.
> FileName:send.c
> author:donald
> details:
==============================================*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define N 1024
int main(int argc, const char *argv[])
{ if(- == mkfifo(argv[],)){//creat 2
perror("failed");
exit(-);
} printf("mkfifo over!\n");
int fd_send,fd_recev;
printf("open fifo...\n"); fd_send = open(argv[],O_WRONLY); if(fd_send == -){//必须另一方打开
perror("open failed");
unlink(argv[]);
exit(-);
}
printf("open success!\n"); char send_buf[N];
while(memset(send_buf,,N),fgets(send_buf,N,stdin) != NULL){
write(fd_send,send_buf,strlen(send_buf));
} printf("send over\n");
unlink(argv[]);
return ;
}
send.c
接收方的代码:
/*============================================
> Copyright (C) 2014 All rights reserved.
> FileName:recv.c
> author:donald
> details:
==============================================*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define N 1024
int main(int argc, const char *argv[])
{
int fd_send,fd_recev;
printf("open fifo...\n");
fd_recev = open(argv[],O_RDONLY); if(fd_send == -){
perror("open failed");
unlink(argv[]);
exit(-);
}
printf("open success!\n"); char recev_buf[];
while(memset(recev_buf,,N),read(fd_recev,recev_buf,N) > ){
printf("%s",recev_buf);
} printf("recev over\n");
close(fd_recev);
unlink(argv[]);
return ;
}
recev.c
两人回话
对于两个程序之间的进行交互式对话有两种方式:
A.一问一答式,这是最简单的方式,但也是最不人性化的方法,运行后你就会产生一种回到上个世纪的错觉
B.没有固定的顺序,和QQ一样,想说就说,不用等到另一方说完一句才能说。
A方法比较简单,直接上代码:
启动方:
/*============================================
> Copyright (C) 2014 All rights reserved.
> FileName:send.c
> author:donald
> date:2014/08/22/ 14:45:45
> details:
==============================================*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define N 1024
int main(int argc, const char *argv[])
{ if(- == mkfifo(argv[],)){//creat 1
perror("failed");
exit(-);
} printf("mkfifo over!\n");
int fd_send,fd_recev;
printf("open fifo...\n"); fd_send = open(argv[],O_WRONLY); if(fd_send == -){//必须另一方打开
perror("open failed");
unlink(argv[]);
// exit(-1);
}
printf("open success!\n"); char send_buf[N];
while(memset(send_buf,,N),fgets(send_buf,N,stdin) != NULL){
write(fd_send,send_buf,strlen(send_buf));
} printf("send over\n");
unlink(argv[]);
return ;
}
send.c
B方法利用创建一个进程(fork)来实现交互,其代码为:
/*===========================================
> Copyright (C)2014All rights reserved
> File Name: fork_re_se.c
> Author: Donald
=============================================*/ #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define N 1024
int main(int argc, const char *argv[])
{
#if 1
if(- == mkfifo(argv[],)){
perror("failed");
exit(-);
}
printf("mkfifo over!\n");
#endif
int fd_send,fd_recev;
printf("open fifo...\n");
fd_recev = open(argv[],O_RDONLY);
fd_send = open(argv[],O_WRONLY); if(fd_send == - || fd_recev == -){
perror("open failed");
unlink(argv[]);
exit(-);
}
printf("open success!\n"); char recev_buf[];
char send_buf[N];
if(fork() == ){//child recev
close(fd_send);
//while(memset(recev_buf,0,N),read(fd_recev,recev_buf,strlen(recev_buf)) != 0){这里不能用strlen(recev_buf在while中进行了初始化,所以长度为0)
while(memset(recev_buf,,N),read(fd_recev,recev_buf,N) != ){
//write(1,recev_buf,N);
printf("%s",recev_buf);
}
exit();
}else{//parent send
close(fd_recev);
while(memset(send_buf,,N),fgets(send_buf,N,stdin) != NULL){
write(fd_send,send_buf,strlen(send_buf));
}
close(fd_send);
wait(NULL);
unlink(argv[]);
}
printf("recev over\n");
return ;
}
fork_re_se.c
/*===========================================
> Copyright (C)2014All rights reserved
> File Name: fork_se_re.c
> Author: Donald
=============================================*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define N 1024
int main(int argc, const char *argv[])
{
#if 1
if(- == mkfifo(argv[],)){
perror("failed");
exit(-);
}
printf("mkfifo over!\n");
#endif
int fd_send,fd_recev;
printf("open fifo...\n");
fd_send = open(argv[],O_WRONLY);
fd_recev = open(argv[],O_RDONLY); if(fd_send == - || fd_recev == -){
perror("open failed");
unlink(argv[]);
exit(-);
}
printf("open success!\n"); char recev_buf[];
char send_buf[N];
if(fork() == ){//child recev
close(fd_send);
while(memset(recev_buf,,N),read(fd_recev,recev_buf,N) != ){
//write(1,recev_buf,N);
printf("%s",recev_buf);
}
exit();
}else{//parent send
close(fd_recev);
while(memset(send_buf,,N),fgets(send_buf,N,stdin) != NULL){
write(fd_send,send_buf,strlen(send_buf));
}
close(fd_send);
wait(NULL);
unlink(argv[]);
} printf("send over\n");
return ;
}
fork_se_re.c
三人无序对话
本程序主要通过父进程创建两个子进程,通过管道来实现,和两人无序对话的功能一样。只要逻辑清晰,并不难。共需要pipe(有名管道)六根,功能为用于读、写,为了使逻辑清晰,方便讨论,以下1、2、3分别代表程序1、2、3之间的管道,分别对程序之间的管道进项讨论分析:
| A | B | C | |||
| 1-2 | write | 1-2 | read | 1-3 | read |
| 1-3 | write | 2-1 | write | 3-1 | write |
| 2-1 | read | 2-3 | write | 2-3 | read |
| 3-1 | read | 3-2 | read | 3-2 | write |
- A:第一人
/*============================================
> Copyright (C) 2014 All rights reserved.
> FileName:1.c
> author:donald
> date:2014/08/22/ 20:28:53
> details:
==============================================*/ #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define N 1024
int main(int argc, const char *argv[])//12,13,21,31
{
if(mkfifo(argv[],) == - || mkfifo(argv[],) == -){
perror("mkfifo");
exit();
} int fd_12,fd_13,fd_21,fd_31;
char buf[N];
fd_12 = open(argv[],O_WRONLY);
fd_13 = open(argv[],O_WRONLY);
fd_21 = open(argv[],O_RDONLY);
fd_31 = open(argv[],O_RDONLY); printf("open sucess\n"); if(fork() == ){//21 r
close(fd_13);
close(fd_12);
close(fd_31);
while(memset(buf,,N),read(fd_21,buf,N) != ){
printf("from 2:");
write(,buf,strlen(buf));
}
close(fd_21);
exit();
}
if(fork() == ){//31 r
close(fd_13);
close(fd_12);
close(fd_21);
while(memset(buf,,N),read(fd_31,buf,N) != ){
printf("from 3:");
write(,buf,strlen(buf));
}
close(fd_31);
exit();
} //12 13 w
close(fd_21);
close(fd_31);
while(memset(buf,,N),fgets(buf,N,stdin) != NULL){
write(fd_13,buf,strlen(buf));
write(fd_12,buf,strlen(buf));
}
close(fd_13);
close(fd_12);
wait(NULL);
wait(NULL); unlink(argv[]);
unlink(argv[]);
printf("program 1 over\n");
return ;
}
1.c
- B:第二人
/*============================================
> Copyright (C) 2014 All rights reserved.
> FileName:2.c
> author:donald
> date:2014/08/22/ 20:29:02
> details:
==============================================*/ #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define N 1024
int main(int argc, const char *argv[])
{ if(mkfifo(argv[],) == - || mkfifo(argv[],) == -){
perror("mkfifo");
exit();
} int fd_12,fd_21,fd_23,fd_32;
char buf[N];
fd_12 = open(argv[],O_RDONLY);
fd_21 = open(argv[],O_WRONLY);
fd_23 = open(argv[],O_WRONLY);
fd_32 = open(argv[],O_RDONLY); printf("open success\n"); if(fork() == ){//12 r
close(fd_32);
close(fd_21);
close(fd_23);
while(memset(buf,,N),read(fd_12,buf,N) != ){
printf("from 1:");
write(,buf,strlen(buf));
}
close(fd_12);
exit();
} if(fork() == ){//32 r
close(fd_12);
close(fd_21);
close(fd_23);
while(memset(buf,,N),read(fd_32,buf,N) != ){
printf("from 3:");
write(,buf,strlen(buf));
}
close(fd_32);
exit();
} //21 23 w
close(fd_12);
close(fd_32);
while(memset(buf,,N),fgets(buf,N,stdin) != NULL){
write(fd_21,buf,strlen(buf));
write(fd_23,buf,strlen(buf));
}
close(fd_21);
close(fd_23);
wait(NULL);
wait(NULL); unlink(argv[]);
unlink(argv[]);
printf("program 2 over\n");
return ;
}
2.c
- C:第三人
/*============================================
> Copyright (C) 2014 All rights reserved.
> FileName:3.c
> author:donald
> date:2014/08/22/ 20:29:13
> details:
==============================================*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define N 1024
int main(int argc, const char *argv[])
{ if(mkfifo(argv[],) == - || mkfifo(argv[],) == -){
perror("mkfifo");
exit();
} int fd_13,fd_31,fd_23,fd_32;
char buf[N];
fd_13 = open(argv[],O_RDONLY);
fd_31 = open(argv[],O_WRONLY);
fd_23 = open(argv[],O_RDONLY);
fd_32 = open(argv[],O_WRONLY); printf("open success\n"); if(fork() == ){//13 r
close(fd_31);
close(fd_23);
close(fd_32);
while(memset(buf,,N),read(fd_13,buf,N) != ){
printf("from 1:");
write(,buf,strlen(buf));
}
close(fd_13);
exit();
}
if(fork() == ){//23 r
close(fd_13);
close(fd_31);
close(fd_32);
while(memset(buf,,N),read(fd_23,buf,N) != ){
printf("from 2:");
write(,buf,strlen(buf));
}
close(fd_23);
exit();
} //31 32 w
close(fd_13);
close(fd_23);
while(memset(buf,,N),fgets(buf,N,stdin) != NULL){
write(fd_31,buf,strlen(buf));
write(fd_32,buf,strlen(buf));
}
close(fd_31);
close(fd_32);
wait(NULL);
wait(NULL); unlink(argv[]);
unlink(argv[]);
printf("program 3 over\n");
return ;
}
3.c
PS:在命令行参数中还需注意的,假设1.c、2.c、3.c的可执行文件为A.out、B.out、C.out。命令行参数分别为:(其实和表格里分析的一样)
- ./A.out 12.fifo 13.fifo 21.fifo 31.fifo
- ./B.out 12.fifo 21.fifo 23.fifo 32.fifo
- ./C.out 13.fifo 31.fifo 23.fifo 32.fifo
三人以上的用fork就没必要了,需要另寻方,一般采用服务器的处理方式。
linux下实现两人、三人无序对话功能的更多相关文章
- Linux 下启动两个tomcat
Linux 下启动两个tomcat 闲来无事学习nginx,想要配置个load balance.可是先决条件是:得有两个web容器.两个电脑是不用想了.只能想办法在一个机器上启动两个tomcat.原以 ...
- Linux下配置两个或多个Tomcat启动
Linux下配置两个或多个Tomcat启动 (2012-08-14 11:59:31) 转载▼ 标签: 杂谈 分类: linux_tomcat 步骤如下: (1)修改/etc/profile文件.添加 ...
- rlwrap: command not found和解决linux下sqlplus 提供浏览历史命令行的功能
rlwrap工具可以解决linux下sqlplus 提供浏览历史命令行的功能,和删除先前输入错误的字母等问题 1.安装 需要readline包 这个安装光盘就有 [root@asm RedHat]# ...
- Linux下的两种磁盘分区工具的使用
如何使用fdisk和parted分区工具来进行硬盘分区,下面我来说一下在Linux系统中这两种硬盘分区工具的使用方法: ----------fdisk分区工具---------- ...
- Linux下使用两个线程协作完成一个任务的简易实现
刚解决了之前的那个Linux下Pthread库的问题,这次就来使用两个线程来协作,共同完成一个求和的任务. 打一下基础吧 本次需要使用到的知识点有: lpthread,编译的时候用到,相当于一个声明的 ...
- Linux 下的两种分层存储方案
背景介绍 随着固态存储技术 (SSD),SAS 技术的不断进步和普及,存储介质的种类更加多样,采用不同存储介质和接口的存储设备的性能出现了很大差异.SSD 相较于传统的机械硬盘,由于没有磁盘的机械转动 ...
- Linux下的两个经典宏定义 转
http://www.linuxidc.com/Linux/2016-08/134481.htm http://www.linuxidc.com/Linux/2013-01/78003.htm htt ...
- Linux下的两个经典宏定义【转】
转自:http://www.linuxidc.com/Linux/2015-07/120014.htm 本文首先介绍Linux下的经典宏定义,感受极客的智慧,然后根据该经典定义为下篇文章作铺垫. of ...
- Linux下性能监控的三把军刀
Linux主机怎么管,十八般兵器件件都可以算得上是瑞士军刀,称手的兵器一两件即可,最常用的,莫过于stat家族三兄弟吧. 计算机主要资源是什么?CPU.内存和磁盘?尽管现在云计算技术有多普及,查看一个 ...
随机推荐
- (转)三角函数计算,Cordic 算法入门
由于最近要使用atan2函数,但是时间上消耗比较多,因而网上搜了一下简化的算法. 原帖地址:http://blog.csdn.net/liyuanbhu/article/details/8458769 ...
- Web服务器和动态语言如何交互--CGI&FastCGI&FPM浅谈
一个用户的Request是如何经过Web服务器(Apache,Nginx,IIS,Light)与后端的动态语言(如PHP等)进行交互并将结果返回给用户的呢? 本文浅谈个人观点,可能有误,欢迎拍砖,共同 ...
- 检测delphi的程序的内存泄漏
在主窗体的FormCreate 加入ReportMemoryLeaksOnShutdown := True;
- racle undo 解析
racle undo 解析 声明一下:关于oracle的文章基于boobooke小布老师视频,在我学习的过程中,每有体会拿来分享,虽然从理解到整理分享很耗时,但我想这样的学习是扎实的. Undo是干嘛 ...
- grok 正则解析日志例子<1>
<pre name="code" class="html">下面是日志的样子 55.3.244.1 GET /index.html 15824 0. ...
- linux之SQL语句简明教程---IN
在 SQL 中,在两个情况下会用到 IN 这个指令:这一页将介绍其中之一 -- 与 WHERE 有关的那一个情况.在这个用法下,我们事先已知道至少一个我们需要的值,而我们将这些知道的值都放入IN 这个 ...
- HDU1879 继续畅通工程 (并查集)
继续畅通工程 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- 国内常用ntp服务器ip地址
ntp.sjtu.edu.cn 202.120.2.101 (上海交通大学网络中心NTP服务器地址)s1a.time.edu.cn 北京邮电大学s1b.time.edu.cn 清华大学s1c.time ...
- 从ora10g 刷数据到 8I基本操作步骤
从ora10g 刷数据到 8I基本操作步骤 master :oracle 10g snapshot site: oralce 8i 在oracle 8i 中物化视图称为快照,oracle 8i建快照的 ...
- hdu 5500 Reorder the Books(规律)
题意: 有一个1→n的排列形成的数列,我们要用最少的操作次数把这个数列从小到大排序,每次操作都是把一个数放到整个数列的最前面. 思路: 首先最大的数n是不用操作的(其他数操作好了,n ...