目录
1)要求AB进程做通信
1. A进程发送一句话,B进程接收打印
2. 然后B进程发送给A进程一句话,A进程接收打印
3. 重复1,2步骤,直到A进程或者B进程收到quit,退出AB进程;
rdonly
#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);
//创建有名管道
if (mkfifo("./myfifo1",0777)<0)
{
if (errno!=17)
{
perror("mkfifo");
return -1;
}
}
printf("mkfifo1 success\n");
//只读打开
int fd=open("./myfifo1",O_RDONLY);
if (fd<0)
{
perror("open");
return -1;
}
printf("open success\n");
//只写打开
int fd2=open("./myfifo2",O_WRONLY);
if (fd2<0)
{
perror("open");
return -1;
}
printf("open success\n");
char buf[128]="";
ssize_t res=0;
while(1)
{
while (1)
{
bzero(buf,sizeof(buf));
res=read(fd,buf,sizeof(buf));
if (res<0)
{
perror("read");
return -1;
}else if (0==res)
{
printf("对方退出\n");
return -1;
}
printf("2.1输出: %s\n",buf);break;
}
char str[128]="";
while (1)
{
printf("2.1请输入>>>");
fgets(str,sizeof(str),stdin);
str[strlen(str)-1]=0;
if (write(fd2,str,sizeof(str))<0)
{
perror("write");
return -1;
}
printf("write success\n");break;
}
}
//关闭
close(fd);
close(fd2);
return 0;
}
wronly
#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)
{
if (errno!=17)
{
perror("mkfifo1");
return -1;
}
}
printf("mkfifo1 success\n");
//创建有名管道2
if (mkfifo("./myfifo2",0777)<0)
{
if (errno!=17)
{
perror("mkfifo2");
return -1;
}
}
printf("mkfifo2 success\n");
//只写打开
int fd=open("./myfifo1",O_WRONLY);
if (fd<0)
{
perror("open");
return -1;
}
printf("open success\n");
//只读打开
int fd2=open("./myfifo2",O_RDONLY);
if (fd2<0)
{
perror("open");
return -1;
}
printf("open success\n");
char buf[128]="";
while(1)
{
while (1)
{
printf("1.1请输入>>>");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]=0;
if (write(fd,buf,sizeof(buf))<0)
{
perror("write");
return -1;
}
printf("write success\n");break;
}
char str[128]="";
ssize_t res=0;
while (1)
{
bzero(str,sizeof(str));
res=read(fd2,str,sizeof(str));
if (res<0)
{
perror("read");
return -1;
}else if (0==res)
{
printf("对方退出\n");
return -1;
}
printf("1.2输出: %s\n",buf);break;
}
}
//关闭
close(fd);
close(fd2);
return 0;
}
运行结果
3)捕获2)3)20)号信号
代码
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
typedef void (*sighandler_t)(int);
//新的处理函数
void handler(int sig)
{
printf("this is handler %d\n", sig);
}
void handler_1(int sig)
{
printf("this is handler_1 %d\n", sig);
}
void handler_3(int sig)
{
printf("this is handler_3 %d\n", sig);
}
void handler_20(int sig)
{
printf("this is handler_20 %d\n", sig);
}
int main(int argc, const char *argv[])
{
printf("h:%p h_1:%p\n", handler, handler_1); //h:0x55c187a7e76a h_1:0x55c187a7e78e
//捕获2号信号SIGINT
sighandler_t s = signal(2, handler);
if(SIG_ERR == s)
{
perror("signal");
return -1;
}
printf("%p %d\n", s, __LINE__); //默认处理函数的首地址获取不到,所以打印NULL;
s = signal(2, handler_1);
if(SIG_ERR == s)
{
perror("signal");
return -1;
}
printf("%p %d\n", s, __LINE__); //0x55c187a7e76a 38
s = signal(3, handler_3);
if(SIG_ERR == s)
{
perror("signal");
return -1;
}
s = signal(20, handler_20);
if(SIG_ERR == s)
{
perror("signal");
return -1;
}
while(1)
{
printf("this is main\n");
sleep(1);
}
return 0;
}
运行效果