撸代码--类QQ聊天实现(基于linux 管道 信号 共享内存)
一:任务描写叙述
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 管道 信号 共享内存)的更多相关文章
- Linux环境进程间通信: 共享内存
Linux环境进程间通信: 共享内存 第一部分 共享内存可以说是最有用的进程间通信方式,也是最快的IPC形式.两个不同进程A.B共享内存的意思是,同一块物理内存被映射到进程A.B各自的进程地址空间.进 ...
- Linux系统编程之命名管道与共享内存
在上一篇博客中,我们已经熟悉并使用了匿名管道,这篇博客我们将讲述进程间通信另外两种常见方式--命名管道与共享内存. 1.命名管道 管道是使用文件的方式,进行进程之间的通信.因此对于管道的操作,实际上还 ...
- Linux信号量同步共享内存实验.
Linux信号量同步共享内存实验. Linux信号量同步共享内存实验. 简述 程序流程 信号量和共享内存的系统函数 信号量系统函数及接口 共享内存系统函数及接口 写程序 读程序 简述 本文主要内容是自 ...
- Linux IPC之共享内存C 事例
Linux IPC之共享内存 标签: linuxrandomnull工作 2011-08-25 11:52 4123人阅读 评论(0) 收藏 举报 分类: Linux(3) 读书札记(3) 版权 ...
- Linux进程间通信—使用共享内存
Linux进程间通信-使用共享内存 转自: https://blog.csdn.net/ljianhui/article/details/10253345 下面将讲解进程间通信的另一种方式,使用共享内 ...
- Linux IPC POSIX 共享内存
模型 #include <unistd.h> //for fstat() #include <sys/types.h> //for fstat() #include <s ...
- linux进程间通信之共享内存篇
本文是对http://www.cnblogs.com/andtt/articles/2136279.html中共享内存(上)的进一步阐释说说明 1 共享内存的实现原理 共享内存是linux进程间通讯的 ...
- Linux进程间通信:管道,信号量,消息队列,信号,共享内存,套接字
Linux下的进程通信手段基本上是从UNIX平台上的进程通信手段继承而来的.而对UNIX发展做出重大贡献的两大主力AT&T的贝尔实验室及BSD(加州大学伯克利分校的伯克利软件发布中心)在进程间 ...
- Linux进程间通信(四) - 共享内存
共享内存的优势 采用共享内存通信的一个显而易见的好处是效率高,因为进程可以直接读写内存,而不需要任何数据的拷贝.对于像管道和消息队列等通信方式,则需要在内核和用户空间进行四次的数据拷贝,而共享内存则只 ...
随机推荐
- 【直接拿来用のandroid公共代码模块解析与分享】の Notification和NotificationManager
本文源代码托管在https://github.com/ASCE1885/asce-common,欢迎fork Android项目做得多了.会发现原来非常多基础的东西都是能够复用,这个系列介绍一些自己项 ...
- 终结者:借助pinyin4j相关jar包提取汉字的首字母
import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.pinyin4j.format.HanyuPinyinCase ...
- HDU 4607 Park Visit HDU暑期多校1
10W个点的一棵树,边权为1 求访问K个点要走过的最小路程 BFS求出一条最长路以后,我们可以YY出其他的边都要重复走两次 树上的最长路可以从任意一点开始BFS求出这点的最大距离,再把终点设置为起点再 ...
- CF-558E (线段树/分块)
解题思路: 很显然突破口就是字符集比较小,分块和线段树都能A 话说线段树时间是分块5倍啊 代码(线段树): #include<cstdio> #include<cstring> ...
- 趣闻|Python之禅(The Zen of Python)
在Python解释器中输入“import this”会发生什么?如果你不知道这个彩蛋,推荐继续阅读这篇文章. 2001年秋,Foretec(一家会议组织公司)正在准备召开第十届Internationa ...
- git还原本地提交的某个历史记录
转载地址:http://jingyan.baidu.com/article/e4511cf33479812b855eaf67.html 1.以还原index2.html文件为例,打开index2.ht ...
- ArcGIS 点要素新增点
IFeatureLayer layer = FrmMain.m_mapControl.get_Layer(0) as IFeatureLayer; IFeatureClass featureClass ...
- R语言-有负下标里才干有零
1.仅仅有负下标里才干有零 先看一个样例 >a<-c(1,2,3,4) >a[-1:1] > a[-1:1] Error in a[-1:1] : 仅仅有负下标里才干有零 (1 ...
- 字串乱序 PHP&JS
<?php /** * 字串乱序 PHP&JS * * php 中把字串乱序后输出给客户机的 JAVASCRIPT , JAVASCRIPT 中恢复 * 在指定长度提取一个字符,并把这一 ...
- 40.【IntelliJ IDEA】使用idea解决新建jsp文件而找不到jsp文件模版的新建选项
转自:https://www.cnblogs.com/sxdcgaq8080/p/7676294.html 使用idea解决新建jsp文件而找不到jsp文件模版的新建选项,这样每次创建一个新的jsp文 ...