1、要求A、B进程做通信:
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 <unistd.h>
#include <errno.h>
#include <string.h>
#define ERR_MSG(msg) do{\
printf("%s %d\n",__func__,__LINE__);\
perror(msg);\
return -1;\
}while(0)
int main(int argc, const char *argv[])
{
if(mkfifo("./fifo1",0664)!=0){
if(errno!=17)
ERR_MSG("mkfifo");
}
if(mkfifo("./fifo2",0664)!=0){
if(errno!=17)
ERR_MSG("mkfifo");
}
int fd1=open("./fifo1",O_WRONLY);
if(fd1<0)
ERR_MSG("open");
int fd2=open("./fifo2",O_RDONLY);
if(fd2<0)
ERR_MSG("open");
char buf[128];
int res=0;
while(1){
bzero(buf,sizeof(buf));
printf("请输入>>>");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]=0;
res=write(fd1,buf,sizeof(buf));
if(res<0)
ERR_MSG("write");
if(strcasecmp(buf,"quit")==0)
break;
bzero(buf,sizeof(buf));
res=read(fd2,buf,sizeof(buf));
if(0==res){
printf("对方进程退出\n");
break;
}else if(res<0){
ERR_MSG("read");
}else{
if(strcasecmp(buf,"quit")==0)
break;
printf("read success:%s\n",buf);
}
}
close(fd1);
close(fd2);
return 0;
}
B进程:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#define ERR_MSG(msg) do{\
printf("%s %d\n",__func__,__LINE__);\
perror(msg);\
return -1;\
}while(0)
int main(int argc, const char *argv[])
{
if(mkfifo("./fifo1",0664)!=0){
if(errno!=17)
ERR_MSG("mkfifo");
}
if(mkfifo("./fifo2",0664)!=0){
if(errno!=17)
ERR_MSG("mkfifo");
}
int fd1=open("./fifo1",O_RDONLY);
if(fd1<0)
ERR_MSG("open");
int fd2=open("./fifo2",O_WRONLY);
if(fd2<0)
ERR_MSG("open");
char buf[128];
int res=0;
while(1){
bzero(buf,sizeof(buf));
res=read(fd1,buf,sizeof(buf));
if(0==res){
printf("对方进程退出\n");
break;
}else if(res<0){
ERR_MSG("read");
}else{
if(strcasecmp(buf,"quit")==0)
break;
printf("read success:%s\n",buf);
}
bzero(buf,sizeof(buf));
printf("请输入>>>");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]=0;
res=write(fd2,buf,sizeof(buf));
if(res<0)
ERR_MSG("write");
if(strcasecmp(buf,"quit")==0)
break;
}
close(fd1);
close(fd2);
return 0;
}
测试:
2、在上一题的基础上实现A、B进程可以随时收发数据
思路:在A、B进程中各再开一个线程,分别实现收数据与发数据功能
A进程:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>
#include <stdlib.h>
#define ERR_MSG(msg) do{\
printf("%s %d\n",__func__,__LINE__);\
perror(msg);\
}while(0)
void *receive(void *arg)
{
int fd1=*(int *)arg;
int fd2=*((int *)arg+1);
int res;
char buf[128];
while(1){
bzero(buf,sizeof(buf));
res=read(fd2,buf,sizeof(buf));
if(0==res){
printf("\n对方进程退出\n");
close(fd1);
close(fd2);
exit(0);
}else if(res<0){
ERR_MSG("read");
return NULL;
}else{
if(strcasecmp(buf,"quit")==0){
putchar(10);
close(fd1);
close(fd2);
exit(0);
}
printf("\nB:%s",buf);
fflush(stdout);
printf("\n请输入>>>");
fflush(stdout);
}
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
if(mkfifo("./fifo1",0664)!=0){
if(errno!=17){
ERR_MSG("mkfifo");
return -1;
}
}
if(mkfifo("./fifo2",0664)!=0){
if(errno!=17){
ERR_MSG("mkfifo");
return -1;
}
}
int fd1=open("./fifo1",O_WRONLY);
if(fd1<0){
ERR_MSG("open");
return -1;
}
int fd2=open("./fifo2",O_RDONLY);
if(fd2<0){
ERR_MSG("open");
return -1;
}
int fd[2];
fd[0]=fd1;
fd[1]=fd2;
pthread_t tid;
if(pthread_create(&tid,NULL,receive,(void *)fd)!=0){
ERR_MSG("pthread_create");
return -1;
}
char buf[128];
int res=0;
while(1){
bzero(buf,sizeof(buf));
printf("请输入>>>");
fflush(stdout);
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]=0;
res=write(fd1,buf,sizeof(buf));
if(res<0){
ERR_MSG("write");
return -1;
}
if(strcasecmp(buf,"quit")==0){
close(fd1);
close(fd2);
exit(0);
}
}
return 0;
}
B进程:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>
#include <stdlib.h>
#define ERR_MSG(msg) do{\
printf("%s %d\n",__func__,__LINE__);\
perror(msg);\
}while(0)
void *send(void *arg)
{
int fd1=*(int *)arg;
int fd2=*((int *)arg+1);
int res;
char buf[128];
while(1){
bzero(buf,sizeof(buf));
printf("请输入>>>");
fflush(stdout);
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1]=0;
res=write(fd2,buf,sizeof(buf));
if(res<0){
ERR_MSG("write");
return NULL;
}
if(strcasecmp(buf,"quit")==0){
close(fd1);
close(fd2);
exit(0);
}
}
pthread_exit(NULL);
}
int main(int argc, const char *argv[])
{
if(mkfifo("./fifo1",0664)!=0){
if(errno!=17){
ERR_MSG("mkfifo");
return -1;
}
}
if(mkfifo("./fifo2",0664)!=0){
if(errno!=17){
ERR_MSG("mkfifo");
return -1;
}
}
int fd1=open("./fifo1",O_RDONLY);
if(fd1<0){
ERR_MSG("open");
return -1;
}
int fd2=open("./fifo2",O_WRONLY);
if(fd2<0){
ERR_MSG("open");
return -1;
}
int fd[2];
fd[0]=fd1;
fd[1]=fd2;
pthread_t tid;
if(pthread_create(&tid,NULL,send,(void *)fd)!=0){
ERR_MSG("pthread_create");
return -1;
}
char buf[128];
int res=0;
while(1){
bzero(buf,sizeof(buf));
res=read(fd1,buf,sizeof(buf));
if(0==res){
printf("\n对方进程退出\n");
close(fd1);
close(fd2);
exit(0);
}else if(res<0){
ERR_MSG("read");
return -1;
}else{
if(strcasecmp(buf,"quit")==0){
putchar(10);
close(fd1);
close(fd2);
exit(0);
}
printf("\nA:%s",buf);
fflush(stdout);
printf("\n请输入>>>");
fflush(stdout);
}
}
return 0;
}
测试:
3、捕获 2)、3)、20)号信号
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
typedef void (*sighandler_t)(int);
void handler1(int sig)
{
printf("2信号->SIGINT被捕获\n");
}
void handler2(int sig)
{
printf("3信号->SIGQUIT被捕获\n");
}
void handler3(int sig)
{
printf("20信号->SIGTSTP被捕获\n");
}
int main(int argc, const char *argv[])
{
if(signal(2,handler1)==SIG_ERR){
perror("signal");
printf("%s %d\n",__func__,__LINE__);
}
if(signal(3,handler2)==SIG_ERR){
perror("signal");
printf("%s %d\n",__func__,__LINE__);
}
if(signal(20,handler3)==SIG_ERR){
perror("signal");
printf("%s %d\n",__func__,__LINE__);
}
while(1){
sleep(1);
}
return 0;
}
测试: