一:任务描写叙述

A,B两个进程通过管道通信,像曾经的互相聊天一样,然后A进程每次接收到的数据通过A1进程显示(一个新进程,用于显示A接收到的信息),A和A1间的数据传递採用共享内存,相应的有一个B1进程。用于显示B进程接收到的信息。

针对A,B进程,退出时採用ctrl+c退出,当收到相应信号后。自身进程可以通过信号处理函数进行资源清理,清理后exit退出进程。(A1,B1,手动关闭就可以)。界面图例如以下。

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

二:代码展示

A进程

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/time.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h> int shm_id; struct t
{
char buf[128];
}; struct t *p;
void hanle(int sig)
{
shmdt(p);
shmctl(shm_id,IPC_RMID,NULL);//删除共享内存
printf("delete OK........\n");
exit(0); } int main()
{//A进程 signal(SIGINT,hanle);//捕捉 ctrl+c信号 shm_id=shmget((key_t)1234,4096,0600|IPC_CREAT);
//创建了一个共享内存 // struct t *p;
p=(struct t*)shmat(shm_id,NULL,0);//拿到内存的指针结构体 int f1;
f1=open("1.fifo",O_WRONLY);//对1 打开写端 int f2;
f2=open("2.fifo",O_RDONLY);//对2 打开读端 fd_set readset;
//定义了可读集合
int maxfd;
maxfd=STDIN_FILENO > f2 ? STDIN_FILENO+1:f2+1; struct timeval check;
check.tv_sec=1;//设置查看时间是一秒
char buf[128];
while(1)
{
FD_ZERO(&readset);//初始化
FD_SET(STDIN_FILENO,&readset);//加入监控对象
FD_SET(f2,&readset);
select(maxfd,&readset,NULL,NULL,&check);//返回可读的监控对象 if(FD_ISSET(STDIN_FILENO,&readset))
{//假设监控到了1管道中有标准输入 那么获取到数据 写到管道中
memset(buf,0,sizeof(buf));
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]='\0';
write(f1,buf,sizeof(buf));//写到管道1中 }
if(FD_ISSET(f2,&readset))
{//监控到了管道2中能够读
memset(buf,0,sizeof(buf));
read(f2,buf,sizeof(buf));//从管道2中读到buf了
strcpy(p->buf,buf);//将管道2中读到的buf 弄到共享内存
printf("from 2: %s\n",buf);
}
if(strcmp(buf,"bye")==0)
{
break;
} }
exit(0);
}

B进程

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/shm.h>
#include <sys/sem.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/time.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h> int shm_id;
struct t
{
char buf[128];
};
struct t *p;
void hanle(int sig)
{
shmdt(p);
shmctl(shm_id,IPC_RMID,NULL);//删除共享内存
printf("delete OK........\n");
exit(0); }
int main()
{//B进程 signal(SIGINT,hanle);
shm_id=shmget((key_t)1235,4096,0600|IPC_CREAT); // struct t *p;
p=(struct t*)shmat(shm_id,NULL,0); int f1;
f1=open("1.fifo",O_RDONLY);//对1 打开读端 int f2;
f2=open("2.fifo",O_WRONLY);//对2 打开写端 fd_set readset;
//定义了可读集合
int maxfd;
maxfd=STDIN_FILENO > f1 ? STDIN_FILENO+1:f1+1; struct timeval check;
check.tv_sec=1;//设置查看时间是一秒
char buf[128];
while(1)
{
FD_ZERO(&readset);//初始化
FD_SET(STDIN_FILENO,&readset);//加入监控对象
FD_SET(f1,&readset);
select(maxfd,&readset,NULL,NULL,&check);//返回可读的监控对象 if(FD_ISSET(STDIN_FILENO,&readset))
{//假设监控到了1管道中有标准输入 那么获取到数据 写到管道中
memset(buf,0,sizeof(buf));
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]='\0';
write(f2,buf,sizeof(buf));//写到管道2中
}
if(FD_ISSET(f1,&readset))
{//监控到了管道2中能够读
memset(buf,0,sizeof(buf));
read(f1,buf,sizeof(buf));//从管道1中读
strcpy(p->buf,buf);//将读出的字符 放到共享内存中
printf("from 1: %s\n",buf);
}
if(strcmp(buf,"bye")==0)
{
break;
} }
exit(0);
}

A1进程

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/shm.h>
#include <string.h> int shm_id;
struct t
{
char buf[128];
};
struct t *p;
void hanle(int sig)
{
shmdt(p);
shmctl(shm_id,IPC_RMID,NULL);//删除共享内存
printf("delete OK........\n");
exit(0);
} int main()
{
shm_id=shmget((key_t)1234,4096,0600|IPC_CREAT);
//获得了共享内存
// struct t *p;
p=(struct t*)shmat(shm_id,NULL,0);//这个也拿到了
char s[128]={0};
while(1)
{
if(strcmp(s,p->buf)!=0)
{//字符串不同样 那么就打印出来
strcpy(s,p->buf);
printf("from B :%s\n",p->buf);
}
} exit(0);
}

B1进程

<span style="font-size:14px;">#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/shm.h>
#include <string.h> int shm_id;
struct t
{
char buf[128];
};
struct t *p;
void hanle(int sig)
{
shmdt(p);
shmctl(shm_id,IPC_RMID,NULL);//删除共享内存
printf("delete OK........\n");
exit(0); }
int main()
{
shm_id=shmget((key_t)1235,4096,0600|IPC_CREAT);
//获得了共享内存
// struct t *p;
p=(struct t*)shmat(shm_id,NULL,0);//这个也拿到了
char s[128]={0};
while(1)
{
if(strcmp(s,p->buf)!=0)
{//字符串不同样 那么就打印出来
strcpy(s,p->buf);
printf("from A :%s\n",p->buf);
}
}
exit(0);
}</span><span style="font-size:18px;">
</span>

三:结果显示

撸代码--类QQ聊天实现(基于linux 管道 信号 共享内存)的更多相关文章

  1. Linux环境进程间通信: 共享内存

    Linux环境进程间通信: 共享内存 第一部分 共享内存可以说是最有用的进程间通信方式,也是最快的IPC形式.两个不同进程A.B共享内存的意思是,同一块物理内存被映射到进程A.B各自的进程地址空间.进 ...

  2. Linux系统编程之命名管道与共享内存

    在上一篇博客中,我们已经熟悉并使用了匿名管道,这篇博客我们将讲述进程间通信另外两种常见方式--命名管道与共享内存. 1.命名管道 管道是使用文件的方式,进行进程之间的通信.因此对于管道的操作,实际上还 ...

  3. Linux信号量同步共享内存实验.

    Linux信号量同步共享内存实验. Linux信号量同步共享内存实验. 简述 程序流程 信号量和共享内存的系统函数 信号量系统函数及接口 共享内存系统函数及接口 写程序 读程序 简述 本文主要内容是自 ...

  4. Linux IPC之共享内存C 事例

    Linux IPC之共享内存 标签: linuxrandomnull工作 2011-08-25 11:52 4123人阅读 评论(0) 收藏 举报  分类: Linux(3)  读书札记(3)  版权 ...

  5. Linux进程间通信—使用共享内存

    Linux进程间通信-使用共享内存 转自: https://blog.csdn.net/ljianhui/article/details/10253345 下面将讲解进程间通信的另一种方式,使用共享内 ...

  6. Linux IPC POSIX 共享内存

    模型 #include <unistd.h> //for fstat() #include <sys/types.h> //for fstat() #include <s ...

  7. linux进程间通信之共享内存篇

    本文是对http://www.cnblogs.com/andtt/articles/2136279.html中共享内存(上)的进一步阐释说说明 1 共享内存的实现原理 共享内存是linux进程间通讯的 ...

  8. Linux进程间通信:管道,信号量,消息队列,信号,共享内存,套接字

    Linux下的进程通信手段基本上是从UNIX平台上的进程通信手段继承而来的.而对UNIX发展做出重大贡献的两大主力AT&T的贝尔实验室及BSD(加州大学伯克利分校的伯克利软件发布中心)在进程间 ...

  9. Linux进程间通信(四) - 共享内存

    共享内存的优势 采用共享内存通信的一个显而易见的好处是效率高,因为进程可以直接读写内存,而不需要任何数据的拷贝.对于像管道和消息队列等通信方式,则需要在内核和用户空间进行四次的数据拷贝,而共享内存则只 ...

随机推荐

  1. Excel VBA简单使用——数据缺失处理

    VBA(Visual Basic for Applications)是VB的一种宏语言.用来扩展应用程式的功能.特别是Microsoft Office软件. 转载请注明原文地址:http://blog ...

  2. 对 hiren bootcd 15.2 中的 XP 系统作了汉化, 同时支持中文输入法。提供下载

    对 hiren bootcd 15.2 中的 XP 系统作了汉化, 同时支持中文输入法.提供下载 对该PE 中的 XP 系统作了汉化, 由于一个 中文字库 就要 10M 多:加之原系统过于精简,对中文 ...

  3. thinkphp5 left join

    thinkphp5 left join 一.总结 1.作用:left join就是即使不匹配也返回左表中的数据 2.join使用通式:object join ( mixed join [, mixed ...

  4. 一文了解sun.misc.Unsafe

    Java语言和JVM平台已经度过了20岁的生日.它最初起源于机顶盒.移动设备和Java-Card,同时也应用在了各种服务器系统中,Java已成为物联网(Internet of Things)的通用语言 ...

  5. PatentTips - Interrupt redirection for virtual partitioning

    BACKGROUND The present disclosure relates to the handling of interrupts in a environment that utiliz ...

  6. 【MongoDB】在windows平台下mongodb的分片集群(六)

    在本篇博客中我们主要讨论下博客的管理.因为已经在前面五篇中写了具体的实例,因此这里就不再举例说明. 一.监控 分片集群是整个体系中比較复杂的一块,因此更应该须要监控. 主要命令: serverstat ...

  7. 自定义HTML标签属性

    为HTML元素添加一自定义的属性非常方便,只须将其加到尖括号中即可,与内置属性地位相等. 如我们要为TextBox元素添加属性idvalue: <input type="text&qu ...

  8. .NET中StringBuilder用法实例分析

    string s1 = "33"; string s2 = "44"; string s3 = "55"; //需求是把s1 s2 s3拼接 ...

  9. ORACLE10g R2【RAC+ASM→RAC+ASM】

    ORACLE10g R2[RAC+ASM→RAC+ASM] 本演示案例所用环境:RAC+ASM+OMF   primary standby OS Hostname node1,node2 dgnode ...

  10. Vue 自定义全局消息框组件

    消息弹框组件,默认3秒后自动关闭,可设置info/success/warning/error类型 效果图: 文件目录: Message.vue <template> <transit ...