当前位置:网站首页>Homework 8.4 Interprocess Communication Pipes and Signals
Homework 8.4 Interprocess Communication Pipes and Signals
2022-08-05 04:20:00 【Unknown college student M】
题目一:要求ABprocess to communicate
1.AThe process sends a sentence,BThe process receives the print
2.然后B进程发送给AProcess in a word,AThe process receives the print
3.重复1,2步骤,直到A进程或者B进程收到quit,退出AB进程;
实现代码
进程A
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
int main(int argc, const char *argv[])
{
umask(0);
//创建有名管道1
if(mkfifo( "./myfifo1",0777) < 0)
{
//printf( "errno = %d\n",errno);
if(errno != 17)
{
//如果errno==17Indicates that the pipeline file already exists,is a legitimate error not handled
perror( "mkfifo");
return -1;
}
}
printf( "mkfifo1 success\n" );
//创建有名管道2
if(mkfifo( "./myfifo2",0777) < 0)
{
//printf( "errno = %d\n",errno);
if(errno != 17)
{
//如果errno==17Indicates that the pipeline file already exists,is a legitimate error not handled
perror( "mkfifo");
return -1;
}
}
printf( "mkfifo2 success\n" );
//Open the named pipe file for write only1
int fd1 = open( "./myfifo1",O_WRONLY);
if(fd1 < 0)
{
perror( "open" );
return -1;
}
//Open the named pipe file as read-only2
int fd2 = open( "./myfifo2",O_RDONLY);
if(fd2 < 0)
{
perror( "open" );
return -1;
}
printf( "open readonly success\n");
char buf1[128]="";
char buf2[128]="";
ssize_t res=0;
while(1)
{
bzero(buf1,sizeof(buf1));
printf("请输入>>>");
fgets(buf1,sizeof(buf1),stdin);
buf1[strlen(buf1)-1]=0;
if(write(fd1,buf1,sizeof(buf1))<0)
{
perror("write");
return -1;
}
if(strcasecmp(buf1, "quit") == 0)
break;
printf("write success\n");
bzero(buf2,sizeof(buf2));
res=read(fd2,buf2,sizeof(buf2));
if(strcasecmp(buf2, "quit") == 0)
break;
if(res<0)
{
perror("read");
return -1;
}
else if(0==res)
{
printf("The other process exits\n");
break;
}
printf("read success:%s\n",buf2);
}
//关闭文件描述符
close(fd1);
close(fd2);
return 0;
}
进程B
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
int main(int argc, const char *argv[])
{
umask(0);
//创建有名管道1
if(mkfifo( "./myfifo1",0777) < 0)
{
//printf( "errno = %d\n",errno);
if(errno != 17)
{
//如果errno==17Indicates that the pipeline file already exists,is a legitimate error not handled
perror( "mkfifo");
return -1;
}
}
printf( "mkfifo1 success\n" );
//创建有名管道2
if(mkfifo( "./myfifo2",0777) < 0)
{
//printf( "errno = %d\n",errno);
if(errno != 17)
{
//如果errno==17Indicates that the pipeline file already exists,is a legitimate error not handled
perror( "mkfifo");
return -1;
}
}
printf( "mkfifo2 success\n" );
//Open the named pipe file as read-only1
int fd1 = open( "./myfifo1",O_RDONLY);
if(fd1 < 0)
{
perror( "open" );
return -1;
}
//Open the named pipe file for write only2
int fd2 = open( "./myfifo2",O_WRONLY);
if(fd2 < 0)
{
perror( "open" );
return -1;
}
printf( "open readonly success\n");
char buf1[128]="";
char buf2[128]="";
ssize_t res=0;
while(1)
{
bzero(buf1,sizeof(buf1));
res=read(fd1,buf1,sizeof(buf1));
if(strcasecmp(buf1, "quit") == 0)
break;
if(res<0)
{
perror("read");
return -1;
}
else if(0==res)
{
printf("The other process exits\n");
break;
}
printf("read success:%s\n",buf1);
bzero(buf2,sizeof(buf2));
printf("请输入>>>");
fgets(buf2,sizeof(buf2),stdin);
buf2[strlen(buf2)-1]=0;
if(write(fd2,buf2,sizeof(buf2))<0)
{
perror("write");
return -1;
}
if(strcasecmp(buf2, "quit") == 0)
break;
printf("write success\n");
}
//关闭文件描述符
close(fd1);
close(fd2);
return 0;
}
运行结果
题目二:捕获2)3)20)号信号
实现代码
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
typedef void (*sighandler_t)(int);
//New handler function
void handler_1(int sig)
{
printf("this is handler_1 \n");
printf("成功捕获%d号信号\n", sig);
}
void handler_2(int sig)
{
printf("this is handler_2\n");
printf("成功捕获%d号信号\n", sig);
}
void handler_3(int sig)
{
printf("this is handler_3 \n");
printf("成功捕获%d号信号\n", sig);
}
int main(int argc, const char *argv[])
{
//捕获2号信号SIGINT
sighandler_t s = signal(2, handler_1);
if(SIG_ERR == s)
{
perror("signal");
return -1;
}
//捕获3号信号SIGINT
s = signal(3, handler_2);
if(SIG_ERR == s)
{
perror("signal");
return -1;
}
//捕获20号信号SIGINT
s = signal(20, handler_3);
if(SIG_ERR == s)
{
perror("signal");
return -1;
}
while(1)
{
printf("this is main\n");
sleep(1);
}
return 0;
}
运行结果
边栏推荐
- mutillidae下载及安装
- 数字孪生技术在电力系统中的应用现状
- UE4 更改组件变量 (以修改第一人称角色模板的最大行走速度和跳跃高度为例)
- Qixi Festival code confession
- UE4 opens doors with overlapping events
- Machine Learning Overview
- The production method of the powered small sailboat is simple, the production method of the electric small sailboat
- 大学物理---质点运动学
- 【背包九讲——01背包问题】
- bytebuffer 使用demo
猜你喜欢
In the WebView page of the UI automation test App, the processing method when the search bar has no search button
UE4 第一人称角色模板 添加蹲伏功能
虚证、实证如何鉴别?
iMedicalLIS监听程序(2)
UE4 通过与其它Actor互动开门
特征预处理
4T硬盘剩余很多提示“No space left on device“磁盘空间不足
How to solve the three major problems of bank data collection, data supplementary recording and index management?
JeeSite New Report
upload上传图片到腾讯云,如何上传图片
随机推荐
The production method of the powered small sailboat is simple, the production method of the electric small sailboat
pyqt5 + socket 实现客户端A经socket服务器中转后主动向客户端B发送文件
DEJA_VU3D - Cesium功能集 之 059-腾讯地图纠偏
Day019 方法重写与相关类的介绍
浅析主流跨端技术方案
国学*周易*梅花易数 代码实现效果展示 - 梅花心易
【测量学】速成汇总——摘录高数帮
App快速开发建设心得:小程序+自定义插件的重要性
UI自动化测试 App的WebView页面中,当搜索栏无搜索按钮时处理方法
MySql的索引学习和使用;(本人觉得足够详细)
The first performance test practice, there are "100 million" a little nervous
[BSidesCF 2019] Kookie
数组常用方法总结
如何解决复杂的分销分账问题?
Redis1: Introduction to Redis, basic features of Redis, relational database, non-relational database, database development stage
数字孪生技术在电力系统中的应用现状
UE4 通过重叠事件开启门
The log causes these pits in the thread block, you have to guard against
What is ASEMI photovoltaic diode, the role of photovoltaic diode
token, jwt, oauth2, session parsing