1.要求AB进程做通信
A进程发送一句话,B进程接收打印
然后B进程发送给A进程一句话,A进程接收打印
重复1,2步骤,直到A进程或者B进程收到quit,退出AB进程;
A进程代码
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
umask(0);//文件掩码
//创建用于发送信息的管道文件
if(mkfifo("./myfifo1", 0777) < 0)
{
if(errno != 17)
{
perror("mkfifo");
return -1;
}
}
//创建用于接收信息的管道文件
if(mkfifo("./myfifo2", 0777) < 0)
{
if(errno != 17)
{
perror("mkfifo");
return -1;
}
}
//只写打开管道文件1
int op1 = open("./myfifo1", O_WRONLY);
if(op1 < 0)
{
perror("open");
return -1;
}
//只读打开管道文件2
int op2 = open("./myfifo2", O_RDONLY);
if(op2 < 0)
{
perror("open");
return -1;
}
printf("准备就绪\n");
char send[128] = "";
char accept[128] = "";
int res = 0;
while(1)
{
//发送信息
bzero(send, 128);
printf("A要发送的信息--->");
fgets(send, 128, stdin);
send[strlen(send)-1] = '\0';
write(op1, send, 128);
//接收信息
bzero(accept, 128);
res = read(op2, accept, 128);
send[strlen(send)-1];
if(strcmp(accept, "quit")==0)
{
write(op1, "quit", 5);
break;
}
printf("A接收到的信息--->%s\n",accept);
}
close(op1);
close(op2);
return 0;
}
B进程代码
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
int main(int argc, const char *argv[])
{
umask(0);//文件掩码
//创建用于发送信息的管道文件
if(mkfifo("./myfifo1", 0777) < 0)
{
if(errno != 17)
{
perror("mkfifo");
return -1;
}
}
//创建用于接收信息的管道文件
if(mkfifo("./myfifo2", 0777) < 0)
{
if(errno != 17)
{
perror("mkfifo");
return -1;
}
}
//只读打开管道文件1
int op1 = open("./myfifo1", O_RDONLY);
if(op1 < 0)
{
perror("open");
return -1;
}
//只写打开管道文件2
int op2 = open("./myfifo2", O_WRONLY);
if(op2 < 0)
{
perror("open");
return -1;
}
printf("准备就绪\n");
char send[128] = "";
char accept[128] = "";
int res = 0;
while(1)
{
//接收信息
bzero(accept, 128);
res = read(op1, accept, 128);
if(strcmp(accept, "quit")==0)
{
write(op2, "quit", 5);
break;
}
printf("B接收到的信息--->%s\n",accept);
//发送信息
bzero(send, 128);
printf("B要发送的信息--->");
fgets(send, 128, stdin);
send[strlen(send)-1] = '\0';
write(op2, send, 128);
}
close(op1);
close(op2);
return 0;
}
测试结果
2.捕获2、3、20号信号
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
typedef void (*sighandler_t)(int);
void A(int sig) //2号
{
printf(".........%d.........\n",sig);
}
void B(int sig)//3号
{
printf(".........%d.........\n",sig);
}
void C(int sig)//20号
{
printf(".........%d.........\n",sig);
exit(1);//退出
}
int main(int argc, const char *argv[])
{
//捕获2号信号
sighandler_t s = signal(2, A);
if(s ==SIG_ERR)
{
perror("signal");
return -1;
}
//捕获3号信号
sighandler_t s2 = signal(3, B);
if(s ==SIG_ERR)
{
perror("signal");
return -1;
}
//捕获20号信号
sighandler_t s3 = signal(20, C);
if(s ==SIG_ERR)
{
perror("signal");
return -1;
}
while(1)
{
printf(">>>>main<<<<\n");
sleep(1);
}
return 0;
}
结果:
ubuntu@ubuntu:day7$ gcc hw2.c
ubuntu@ubuntu:day7$ ./a.out
>>>>main<<<<
>>>>main<<<<
>>>>main<<<<
>>>>main<<<<
>>>>main<<<<
^C.........2.........
>>>>main<<<<
^C.........2.........
>>>>main<<<<
^C.........2.........
>>>>main<<<<
>>>>main<<<<
^\.........3.........
>>>>main<<<<
^\.........3.........
>>>>main<<<<
>>>>main<<<<
>>>>main<<<<
>>>>main<<<<
^Z.........20.........
ubuntu@ubuntu:day7$