1)要求AB进程做通信
1. A进程发送一句话,B进程接收打印
2. 然后B进程发送给A进程一句话,A进程接收打印
3. 重复1,2步骤,直到A进程或者B进程收到quit,退出AB进程
A线程
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
int main(int argc, const char *argv[])
{
printf("A进程\n");
if(mkfifo("./myfifo_1",0777) < 0)
{
if(errno != 17)
{
perror("mkfifo");
return -1;
}
}
if(mkfifo("./myfifo_2",0777) < 0)
{
if(errno != 17)
{
perror("mkfifo");
return -1;
}
}
int fd_1 = open("./myfifo_1",O_RDONLY);
if(fd_1 < 0)
{
perror("open");
return -1;
}
int fd_2 = open("./myfifo_2",O_WRONLY);
if(fd_2 < 0)
{
perror("open");
return -1;
}
char buf[128] = "";
ssize_t res = 0;
while(1)
{
printf("A发送>>>");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = '\0';
if(strcmp(buf,"quit") == 0)
{
break;
}
if(write(fd_2,buf,sizeof(buf)) < 0)
{
perror("open");
return -1;
}
bzero(buf,sizeof(buf));
printf("等待B进程的发送\n");
res = read(fd_1,buf,sizeof(buf));
if(res < 0)
{
perror("read");
return -1;
}
else if(0 == res)
{
break;
}
printf("接受到B的信息:%s\n",buf);
}
close(fd_1);
close(fd_2);
printf("A进程退出\n");
return 0;
}
B线程
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
int main(int argc, const char *argv[])
{
printf("B进程\n");
//创建二个有名管道
if(mkfifo("./myfifo_1",0777) < 0)
{
if(errno != 17)
{
perror("mkfifo");
return -1;
}
}
if(mkfifo("./myfifo_2",0777) < 0)
{
if(errno != 17)
{
perror("mkfifo");
return -1;
}
}
//打开有名管道
int fd_1 = open("./myfifo_1",O_WRONLY);
if(fd_1 < 0)
{
perror("open");
return -1;
}
int fd_2 = open("./myfifo_2",O_RDONLY);
if(fd_2 < 0)
{
perror("open");
return -1;
}
char buf[128] = "";
ssize_t res = 0;
while(1)
{
bzero(buf,sizeof(buf));
printf("等待A进程的发送\n");
res = read(fd_2,buf,sizeof(buf));//读取A线程的数据
if(res < 0)
{
perror("read");
return -1;
}
else if(0 == res)
{
break;
}
printf("接受到A的信息:%s\n",buf);
printf("B发送>>>");
fgets(buf,sizeof(buf),stdin);//发送给A线程
buf[strlen(buf)-1] = '\0';
if(strcmp(buf,"quit") == 0)
{
break;//输入quit退出
}
if(write(fd_1,buf,sizeof(buf)) < 0)
{
perror("open");
return -1;
}
}
close(fd_1);//关闭文件
close(fd_2);
printf("B进程退出\n");
return 0;
}
2)在第一题的基础上实现,AB进程能够随时收发数据(附加题)
A进程
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <stdlib.h>
void* callBack_w(void* arg)
{
int fd_1 = *(int*)arg;
char buf[128] = "";
ssize_t res = 0;
while(1)
{
printf("A发送>>>");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = '\0';
if(write(fd_1,buf,sizeof(buf)) < 0)
{
perror("open");
return NULL;
}
if(strcmp(buf,"quit") == 0)
{
printf("退出程序\n");
exit(0);
}
}
close(fd_1);
}
void* callBack_r(void* arg)
{
int fd_2 = *(int*)arg;
char buf[128] = "";
ssize_t res = 0;
while(1)
{
bzero(buf,sizeof(buf));
res = read(fd_2,buf,sizeof(buf));
if(res < 0)
{
perror("read");
return NULL;
}
else if(0 == res)
{
printf("退出程序\n");
exit(0);
}
printf("接受到B的信息:%s\n",buf);
}
close(fd_2);
}
int main(int argc, const char *argv[])
{
printf("A进程\n");
//创建二个有名管道
if(mkfifo("./myfifo_1",0777) < 0)
{
if(errno != 17)
{
perror("mkfifo");
return -1;
}
}
if(mkfifo("./myfifo_2",0777) < 0)
{
if(errno != 17)
{
perror("mkfifo");
return -1;
}
}
//打开有名管道
int fd_1 = open("./myfifo_1",O_WRONLY);
if(fd_1 < 0)
{
perror("open");
return -1;
}
int fd_2 = open("./myfifo_2",O_RDONLY);
if(fd_2 < 0)
{
perror("open");
return -1;
}
//创建线程
pthread_t tid_1,tid_2;
if(pthread_create(&tid_1,NULL,callBack_w,(void*)&fd_1) != 0)
{
perror("pthread_create");
return -1;
}
if(pthread_create(&tid_1,NULL,callBack_r,(void*)&fd_2) != 0)
{
perror("pthread_create");
return -1;
}
pthread_join(tid_1,NULL);
pthread_join(tid_2,NULL);
return 0;
}
B进程
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <stdlib.h>
void* callBack_w(void* arg)
{
int fd_2 = *(int*)arg;
char buf[128] = "";
ssize_t res = 0;
while(1)
{
printf("B发送>>>");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = '\0';
if(write(fd_2,buf,sizeof(buf)) < 0)
{
perror("open");
return NULL;
}
if(strcmp(buf,"quit") == 0)
{
printf("退出程序\n");
exit(0);
}
}
close(fd_2);
}
void* callBack_r(void* arg)
{
int fd_1 = *(int*)arg;
char buf[128] = "";
ssize_t res = 0;
while(1)
{
bzero(buf,sizeof(buf));
res = read(fd_1,buf,sizeof(buf));
if(res < 0)
{
perror("read");
return NULL;
}
else if(0 == res)
{
printf("退出程序\n");
exit(0);
}
printf("接受到A的信息:%s\n",buf);
}
close(fd_1);
}
int main(int argc, const char *argv[])
{
printf("B进程\n");
//创建二个有名管道
if(mkfifo("./myfifo_1",0777) < 0)
{
if(errno != 17)
{
perror("mkfifo");
return -1;
}
}
if(mkfifo("./myfifo_2",0777) < 0)
{
if(errno != 17)
{
perror("mkfifo");
return -1;
}
}
//打开有名管道
int fd_1 = open("./myfifo_1",O_RDONLY);
if(fd_1 < 0)
{
perror("open");
return -1;
}
int fd_2 = open("./myfifo_2",O_WRONLY);
if(fd_2 < 0)
{
perror("open");
return -1;
}
//创建线程
pthread_t tid_1,tid_2;
if(pthread_create(&tid_1,NULL,callBack_r,(void*)&fd_1) != 0)
{
perror("pthread_create");
return -1;
}
if(pthread_create(&tid_1,NULL,callBack_w,(void*)&fd_2) != 0)
{
perror("pthread_create");
return -1;
}
pthread_join(tid_1,NULL);
pthread_join(tid_2,NULL);
return 0;
}
3)捕获2)3)20)号信号
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
typedef void(*sighandler_t)(int);
void handler_2(int sig)
{
printf("输入了ctrl + c\n");
}
void handler_3(int sig)
{
printf("输入了ctrl + \\ \n");
}
void handler_20(int sig)
{
printf("输入了ctrl + z\n");
}
int main(int argc, const char *argv[])
{
//捕获2号信号SIGINT
sighandler_t s = signal(2,handler_2);
if(SIG_ERR == s)
{
perror("signal");
return -1;
}
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)
sleep(1);
return 0;
}