当前位置:网站首页>IO process thread -> communication between processes -> day7
IO process thread -> communication between processes -> day7
2022-08-05 07:03:00 【Whale calls me to take care of the sea QAQ】
目录
练习
1)要求AB进程做通信
1. A进程发送一句话,B进程接收打印
2. 然后B进程发送给A进程一句话,A进程接收打印
3. 重复1,2步骤,直到A进程或者B进程收到quit,退出AB进程;
2)Implemented on the basis of the first question,ABProcesses can send and receive data at any time(附加题)(个人能力有限,Hope some great god can help)
3)捕获2)3)20)号信号
一、 AB通信函数
I use famous communication,Create two well-known channels,A在通道1只能读,通道2只能写,B反过来.
完成A先发送,B打印,然后再写入,B等到A写入,然后打印,再写入,循环往复,直到输出quit,Exit all programs.
1.1 A进程函数
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main(int argc, const char *argv[])
{
umask(0);
if(mkfifo("./mkfifo1",0777) < 0)
{
if(17 != errno)
{
perror("mkfifo");
return -1;
}
}
umask(0);
if(mkfifo("./mkfifo2",0777) < 0)
{
if(17 != errno)
{
perror("mkfifo");
return -1;
}
}
printf("创建成功\n");
int fd_w=open("./mkfifo1",O_WRONLY);
if(fd_w<0)
{
perror("open");
return -1;
}
int fd_r=open("./mkfifo2",O_RDONLY);
if(fd_r<0)
{
perror("open");
return -1;
}
printf("Open the pipe successfully\n");
char str[128]="";
ssize_t res=0;
while(1)
{
printf("A线程写入:\n");
fgets(str,sizeof(str),stdin);
str[strlen(str)-1]='\0';
if(write(fd_w,str,sizeof(str))<0)
{
perror("write");
return -1;
}
if(strcasecmp(str,"quit") == 0)
{
break;
}
printf("线程A写入成功\n");
bzero(str,sizeof(str));
res=read(fd_r,str,sizeof(str));
if(res<0)
{
perror("read");
return -1;
}else if(0 == res)
{
printf("线程B退出\n");
break;
}
if(strcasecmp(str,"quit") == 0)
{
break;
}
printf("线程B:%s\n",str);
}
close(fd_r);
close(fd_w);
printf("程序结束\n");
return 0;
}
1.2 B进程函数
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main(int argc, const char *argv[])
{
umask(0);
if(mkfifo("./mkfifo1",0777) < 0)
{
if(17 != errno)
{
perror("mkfifo");
return -1;
}
}
umask(0);
if(mkfifo("./mkfifo2",0777) < 0)
{
if(17 != errno)
{
perror("mkfifo");
return -1;
}
}
printf("创建成功\n");
int fd_r=open("./mkfifo1",O_RDONLY);
if(fd_r<0)
{
perror("open");
return -1;
}
int fd_w=open("./mkfifo2",O_WRONLY);
if(fd_w<0)
{
perror("open");
return -1;
}
printf("Open the pipe successfully\n");
char str[128]="";
ssize_t res=0;
while(1)
{
bzero(str,sizeof(str));
res=read(fd_r,str,sizeof(str));
if(res<0)
{
perror("read");
return -1;
}else if(0 == res)
{
printf("线程A退出\n");
break;
}
if(strcasecmp(str,"quit") == 0)
{
break;
}
printf("线程A:%s\n",str);
printf("B线程写入:\n");
fgets(str,sizeof(str),stdin);
str[strlen(str)-1]='\0';
if(write(fd_w,str,sizeof(str))<0)
{
perror("write");
return -1;
}
if(strcasecmp(str,"quit") == 0)
{
break;
}
printf("线程B写入成功\n");
}
close(fd_r);
close(fd_w);
printf("程序结束\n");
return 0;
}
1.3 执行结果
三、捕获信号
很简单,Learn about the application of the function and understand what signals are under the ID number,There is also the first capture of the signal from the default function,It is impossible to get the first address of the default function.
3.1 捕获函数
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
typedef void (*sighandler_t)(int);
void han1(int sig)
{
printf("new handler:%d\n",sig);
}
void han3(int sig)
{
printf("new handler:%d\n",sig);
}
void han20(int sig)
{
printf("new handler:%d\n",sig);
}
void handler_1(int sig)
{
printf("默认处理函数:%d\n",sig);
}
int main(int argc, const char *argv[])
{
printf("Default handler function address:%p 捕获2:%p 捕获3:%p 捕获20:%p\n",handler_1,han1,han3,han20);//Default handler function address and new handler function address
/****捕获2******/
sighandler_t s=signal(2,han1);
if(SIG_ERR == s)
{
perror("signal");
return -1;
}
printf("[%d]: %p\n",__LINE__,s);
s=signal(2,handler_1);
if(SIG_ERR == s)
{
perror("signal");
return -1;
}
printf("[%d]: %p\n",__LINE__,s);
/****捕获3******/
s=signal(3,han3);
if(SIG_ERR == s)
{
perror("signal");
return -1;
}
printf("[%d]: %p\n",__LINE__,s);
s=signal(3,handler_1);
if(SIG_ERR == s)
{
perror("signal");
return -1;
}
printf("[%d]: %p\n",__LINE__,s);
/****捕获20******/
s=signal(20,han20);
if(SIG_ERR == s)
{
perror("signal");
return -1;
}
printf("[%d]: %p\n",__LINE__,s);
s=signal(20,handler_1);
if(SIG_ERR == s)
{
perror("signal");
return -1;
}
printf("[%d]: %p\n",__LINE__,s);
while(1)
{
sleep(1);
}
return 0;
}
3.2 执行结果
边栏推荐
- Tips for formatting code indentation
- (4) Rotating object detection data roLabelImg to DOTA format
- 盒子模型中过度约束问题及其解决办法
- DNSlog外带数据注入
- Quick Start to Drools Rule Engine (1)
- In-depth analysis if according to data authority @datascope (annotation + AOP + dynamic sql splicing) [step by step, with analysis process]
- numpy.random usage documentation
- VS Code私有服务器部署(私有化)
- Technical Analysis Patterns (11) How to Trade Head and Shoulders Patterns
- 共享内存+inotify机制实现多进程低延迟数据共享
猜你喜欢
随机推荐
技术分析模式(十一)如何交易头肩形态
技术分析模式(九)三重顶部和底部
17-VMware Horizon 2203 虚拟桌面-Win10 手动桌面池浮动(十七)
MySQL的主从模式搭建
今天虚竹哥又发现了一款好用的国产化API工具
工作3年,回想刚入门和现在的今昔对比,笑谈一下自己的测试生涯
利用将网页项目部署到阿里云上(ngnix)
Email management Filter emails
【8】Docker中部署Redis
如何将.asd恢复为Word文档
Matplotlib plotting notes
The NDK compiler so libraries
Shared memory + inotify mechanism to achieve multi-process low-latency data sharing
After docker is deployed, mysql cannot connect
AH8669-AC380/VAC220V转降5V12V24V500MA内电源芯片IC方案
【2022 DSCTF决赛wp】
lingo入门——河北省第三届研究生建模竞赛B题
更改小程序原生radio的颜色及大小
1、Citrix XenDesktop 2203之AD域系统安装(一)
AI+视频技术助力保障校园安全,校园智能安防平台该如何建设?