题目一、要求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 <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
int main(int argc, const char *argv[])
{
umask(0);
//创建有名管道
if(mkfifo("./myfifo",0777) < 0)
{
if(errno != 17) //如果errno==17代表管道文件已经存在,是合法错误不处理
{
perror("mkfifo");
return -1;
}
}
//printf("mkfifo success\n");
if(mkfifo("./myfifo2",0777) < 0)
{
if(errno != 17)
{
perror("mkfifo");
return -1;
}
}
int fd = open("./myfifo",O_RDONLY);
if(fd < 0)
{
perror("open");
return -1;
}
//printf("open readonly success\n");
int fd2 = open("./myfifo2",O_WRONLY);
if(fd < 0)
{
perror("open");
return -1;
}
char buf[128]="";
ssize_t res;
while(1)
{
//接收B进程的消息
bzero(buf,sizeof(buf));
res = read(fd,buf,sizeof(buf));
if(res == 0)
{
printf("对方进程已退出\n");
break;
}else if(res < 0)
{
perror("read");
return -1;
}
printf("已接收>>>%s\n",buf);
if(strcmp(buf,"quit") == 0)
break;
//向B进程发送信息
bzero(buf,sizeof(buf));
printf("请输入>>>");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = '\0';
if(write(fd2,buf,sizeof(buf)) < 0)
{
perror("write");
return -1;
}
if(strcmp(buf,"quit") == 0)
break;
//printf("write success\n");
}
close(fd);
close(fd2);
return 0;
}
B进程
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
int main(int argc, const char *argv[])
{
umask(0);
//创建有名管道
if(mkfifo("./myfifo",0777) < 0)
{
if(errno != 17) //如果errno==17代表管道文件已经存在,是合法错误不处理
{
perror("mkfifo");
return -1;
}
}
//printf("mkfifo success\n");
if(mkfifo("./myfifo2",0777) < 0)
{
if(errno != 17)
{
perror("mkfifo");
return -1;
}
}
int fd = open("./myfifo",O_WRONLY);
if(fd < 0)
{
perror("open");
return -1;
}
//printf("open writeonly success\n");
int fd2 = open("./myfifo2",O_RDONLY);
if(fd < 0)
{
perror("open");
return -1;
}
char buf[128]="";
ssize_t res;
while(1)
{
//向A进程发送信息
bzero(buf,sizeof(buf));
printf("请输入>>>");
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");
if(strcmp(buf,"quit") == 0)
break;
//接收A进程的信息
bzero(buf,sizeof(buf));
res = read(fd2,buf,sizeof(buf));
if(res == 0)
{
printf("对方进程已退出\n");
break;
}else if(res < 0)
{
perror("read");
return -1;
}
printf("已接收>>>%s\n",buf);
if(strcmp(buf,"quit") == 0)
break;
}
close(fd);
close(fd2);
return 0;
}
测试结果
题目二、捕获2)3)20)号信号
代码示例
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
typedef void (*sighandler_t)(int);
void my_signal_2(int sig)
{
printf("this is 2)signal %d\n",sig);
}
void my_signal_3(int sig)
{
printf("this is 3)signal %d\n",sig);
}
void my_signal_20(int sig)
{
printf("this is 20)signal %d\n",sig);
}
int main(int argc, const char *argv[])
{
sighandler_t s = signal(2,my_signal_2);
if(SIG_ERR == s)
{
perror("signal");
return -1;
}
s = signal(3,my_signal_3);
if(SIG_ERR == s)
{
perror("signal");
return -1;
}
s = signal(20,my_signal_20);
if(SIG_ERR == s)
{
perror("signal");
return -1;
}
//printf("%p %d \n",s,__LINE__);
while(1)
{
printf("this is main\n");
sleep(1);
}
return 0;
}