当前位置:网站首页>作业8.4 进程间的通信 管道与信号
作业8.4 进程间的通信 管道与信号
2022-08-05 04:15:00 【不知名大学生M】
题目一:要求AB进程做通信
1.A进程发送一句话,B进程接收打印
2.然后B进程发送给A进程一句话,A进程接收打印
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==17代表管道文件已经存在,是合法错误不处理
perror( "mkfifo");
return -1;
}
}
printf( "mkfifo1 success\n" );
//创建有名管道2
if(mkfifo( "./myfifo2",0777) < 0)
{
//printf( "errno = %d\n",errno);
if(errno != 17)
{
//如果errno==17代表管道文件已经存在,是合法错误不处理
perror( "mkfifo");
return -1;
}
}
printf( "mkfifo2 success\n" );
//以只写的方式打开有名管道文件1
int fd1 = open( "./myfifo1",O_WRONLY);
if(fd1 < 0)
{
perror( "open" );
return -1;
}
//以只读的方式打开有名管道文件2
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("对方进程退出\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==17代表管道文件已经存在,是合法错误不处理
perror( "mkfifo");
return -1;
}
}
printf( "mkfifo1 success\n" );
//创建有名管道2
if(mkfifo( "./myfifo2",0777) < 0)
{
//printf( "errno = %d\n",errno);
if(errno != 17)
{
//如果errno==17代表管道文件已经存在,是合法错误不处理
perror( "mkfifo");
return -1;
}
}
printf( "mkfifo2 success\n" );
//以只读的方式打开有名管道文件1
int fd1 = open( "./myfifo1",O_RDONLY);
if(fd1 < 0)
{
perror( "open" );
return -1;
}
//以只写的方式打开有名管道文件2
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("对方进程退出\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);
//新的处理函数
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;
}
运行结果

边栏推荐
- Mysql的redo log详解
- 【背包九讲——01背包问题】
- Redis1:Redis介绍、Redis基本特性、关系型数据库、非关系型数据库、数据库发展阶段
- 七夕节赚徽章拉
- Industry Status?Why do Internet companies prefer to spend 20k to recruit people rather than raise their salary to retain old employees~
- What is the function of industrial-grade remote wireless transmission device?
- UE4 第一人称角色模板 添加冲刺(加速)功能
- In the WebView page of the UI automation test App, the processing method when the search bar has no search button
- Mysql's redo log detailed explanation
- 日志导致线程Block的这些坑,你不得不防
猜你喜欢

bytebuffer internal structure

How to wrap markdown - md file

flink reads mongodb data source
![[MRCTF2020]PYWebsite](/img/d4/57e8e5ee45b742894679f3f5671516.png)
[MRCTF2020]PYWebsite

UE4 opens doors with overlapping events

JeeSite New Report

日志导致线程Block的这些坑,你不得不防
![【 8.4 】 source code - [math] [calendar] [delete library 】 【 is not a simple sequence (Bonus) 】](/img/b5/716627b370e489ccf320a86540f7ba.png)
【 8.4 】 source code - [math] [calendar] [delete library 】 【 is not a simple sequence (Bonus) 】

The most effective seven performance testing techniques of software testing techniques

Visibility of multi-column attribute column elements: display, visibility, opacity, vertical alignment: vertical-align, z-index The larger it is, the more it will be displayed on the upper layer
随机推荐
【8.2】代码源 - 【货币系统】【硬币】【新年的问题(数据加强版)】【三段式】
How to solve complex distribution and ledger problems?
The log causes these pits in the thread block, you have to guard against
Bosses, I noticed that a mysql CDC connector parameters scan. The incremental. Sna
UE4 opens door via interaction (keyboard key)
Machine Learning Overview
SkiaSharp 之 WPF 自绘 粒子花园(案例版)
bytebuffer internal structure
overloaded operator
What is the difference between SAP ERP and ORACLE ERP?
JeeSite New Report
Redis1:Redis介绍、Redis基本特性、关系型数据库、非关系型数据库、数据库发展阶段
机器学习概述
【8.4】代码源 - 【数学】【历法】【删库】【不朴素的数列(Bonus)】
UE4 后期处理体积 (角色受到伤害场景颜色变淡案例)
Ali's local life's single-quarter revenue is 10.6 billion, Da Wenyu's revenue is 7.2 billion, and Cainiao's revenue is 12.1 billion
【测量学】速成汇总——摘录高数帮
程序开发的一些常规套路(一)
Some conventional routines of program development (1)
DEJA_VU3D - Cesium功能集 之 058-高德地图纠偏