序:引子

对话功能实际上就是利用管道见得通信。最原始的是一方发另一方收,不能进项交互,发送方的代码如下:

 /*============================================
> 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

recev_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
 以上表格表示的具体含义我在这里举例说明一下。eg:对于A(聊天猪头)而言共有四根管道与其相连,两根用于读,另外两根用于写,1-2管道代表A、B之间的管道A需要进行写操作,而B进行读操作。 
  • 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。命令行参数分别为:(其实和表格里分析的一样)

            1. ./A.out   12.fifo  13.fifo  21.fifo   31.fifo
            2. ./B.out   12.fifo  21.fifo  23.fifo   32.fifo
            3. ./C.out   13.fifo  31.fifo  23.fifo   32.fifo

三人以上的用fork就没必要了,需要另寻方,一般采用服务器的处理方式。

linux下实现两人、三人无序对话功能的更多相关文章

  1. Linux 下启动两个tomcat

    Linux 下启动两个tomcat 闲来无事学习nginx,想要配置个load balance.可是先决条件是:得有两个web容器.两个电脑是不用想了.只能想办法在一个机器上启动两个tomcat.原以 ...

  2. Linux下配置两个或多个Tomcat启动

    Linux下配置两个或多个Tomcat启动 (2012-08-14 11:59:31) 转载▼ 标签: 杂谈 分类: linux_tomcat 步骤如下: (1)修改/etc/profile文件.添加 ...

  3. rlwrap: command not found和解决linux下sqlplus 提供浏览历史命令行的功能

    rlwrap工具可以解决linux下sqlplus 提供浏览历史命令行的功能,和删除先前输入错误的字母等问题 1.安装 需要readline包 这个安装光盘就有 [root@asm RedHat]# ...

  4. Linux下的两种磁盘分区工具的使用

    如何使用fdisk和parted分区工具来进行硬盘分区,下面我来说一下在Linux系统中这两种硬盘分区工具的使用方法:     ----------fdisk分区工具----------       ...

  5. Linux下使用两个线程协作完成一个任务的简易实现

    刚解决了之前的那个Linux下Pthread库的问题,这次就来使用两个线程来协作,共同完成一个求和的任务. 打一下基础吧 本次需要使用到的知识点有: lpthread,编译的时候用到,相当于一个声明的 ...

  6. Linux 下的两种分层存储方案

    背景介绍 随着固态存储技术 (SSD),SAS 技术的不断进步和普及,存储介质的种类更加多样,采用不同存储介质和接口的存储设备的性能出现了很大差异.SSD 相较于传统的机械硬盘,由于没有磁盘的机械转动 ...

  7. Linux下的两个经典宏定义 转

    http://www.linuxidc.com/Linux/2016-08/134481.htm http://www.linuxidc.com/Linux/2013-01/78003.htm htt ...

  8. Linux下的两个经典宏定义【转】

    转自:http://www.linuxidc.com/Linux/2015-07/120014.htm 本文首先介绍Linux下的经典宏定义,感受极客的智慧,然后根据该经典定义为下篇文章作铺垫. of ...

  9. Linux下性能监控的三把军刀

    Linux主机怎么管,十八般兵器件件都可以算得上是瑞士军刀,称手的兵器一两件即可,最常用的,莫过于stat家族三兄弟吧. 计算机主要资源是什么?CPU.内存和磁盘?尽管现在云计算技术有多普及,查看一个 ...

随机推荐

  1. F - The Circumference of the Circle

    Description To calculate the circumference of a circle seems to be an easy task - provided you know ...

  2. 浅谈AngularJS启动引导过程

    我们都知道AngularJS默认会执行app.js来启动整个angular项目,但你知道angular具体执行过程吗? 一.自动引导启动框架 例如我们有如下代码,我们想要完成一个指令功能: <h ...

  3. Centos6.5下安装php

    安装php: yum -y install php 重启httpd服务激活php: /etc/init.d/httpd restart 测试php是否安装完成 vim /var/www/html/in ...

  4. 连接Xively云

    1.注册Xively开发账号 在https://xively.com注册一个开发账户,收到激活邮件后激活,注意,如果登陆后无法进入develop页面,请退出重新登陆. 2.添加设备 3.获取FeedI ...

  5. SQL Server 日志文件增长原因定位

    方法 1.sys.databases; -------------------------------------------------------------------------------- ...

  6. 构建混合云:配置Azure site to site VPN连接(3)

    9. 那么我们来创建网关,创建网关的时候需要注意,看看你的设备是否支持动态网关,在本示例中的Cisco ASA 5550不支持动态网关,所以我们只能创建静态网关: 该创建会花费一定的时间,稍等即可. ...

  7. jquery跨域访问解决方案(转)

    客户端“跨域访问”一直是一个头疼的问题,好在有jQuery帮忙,从jQuery-1.2以后跨域问题便迎刃而解.由于自己在项目中遇到跨域问题,借此机会对跨域问题来刨根问底,查阅了相关资料和自己的实践,算 ...

  8. Oracle EBS-SQL (OM-3):销售连接停靠站时冲减库存出错处理.sql

    DELETE FROM INV.MTL_RESERVATIONS MRWHERE EXISTS (SELECT 1 FROM WSH.WSH_DELIVERY_ASSIGNMENTS WDA      ...

  9. C#多显示器转换的两种方法——SetWindowPos,Screen

    原文 http://blog.csdn.net/hejialin666/article/details/6057551 实现多屏显示目的:一般情况下是一个电脑显示屏,外接一个电视显示屏.在电脑上显示的 ...

  10. JVM虚拟机选项:Xms Xmx PermSize MaxPermSize区别(转)

    java虽然是自动回收内存,但是应用程序,尤其服务器程序最好根据业务情况指明内存分配限制.否则可能导致应用程序宕掉. 举例说明含义:-Xms128m表示JVM Heap(堆内存)最小尺寸128MB,初 ...