08.04有名管道

阅读: 评论:0

08.04有名管道

08.04有名管道

1)要求AB进程做通信

  1. A进程发送一句话,B进程接收打印

  2. 然后B进程发送给A进程一句话,A进程接收打印

  3. 重复1,2步骤,直到A进程或者B进程收到quit,退出AB进程;

A进程:

#include<stdio.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<unistd.h>
#include<fcntl.h>
#include<errno.h>
#include<string.h>int main(int argc, const char *argv[])
{//管道1if(mkfifo("./myfifo",0664) < 0){if(errno != 17){perror("mkfifo");return -1;}}//管道2if(mkfifo("./fifo",0664) < 0){if(errno != 17){perror("mkfifo");return -1;}}printf("mkfifo successn");int fd = open("./myfifo",O_WRONLY);    //读写if(fd < 0){perror("open");return -1;}int fd1 = open("./fifo",O_RDONLY);  //读写if(fd < 0){perror("open");return -1;}ssize_t res = 0;char buf[128];while(1){//输入管道1printf("A-->B:");fgets(buf,sizeof(buf),stdin);buf[strlen(buf)-1] = 0;if(write(fd,buf,sizeof(buf)) < 0){perror("write");return -1;}if(strcasecmp(buf,"quit") == 0)break;//接收管道2bzero(buf,sizeof(buf));	res = read(fd1,buf,sizeof(buf));if(res < 0){perror("read");return -1;}else if(0 == res){printf("接收结束n");break;}printf("A_get:%ld %sn",res,buf);if(strcasecmp(buf,"quit") == 0)break;}close(fd);close(fd1);return 0;
}

B进程:

#include<stdio.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<unistd.h>
#include<fcntl.h>
#include<errno.h>
#include<string.h>int main(int argc, const char *argv[])
{//管道1if(mkfifo("./myfifo",0664) < 0){if(errno != 17){perror("mkfifo");return -1;}}//管道2if(mkfifo("./fifo",0664) < 0){if(errno != 17){perror("mkfifo");return -1;}}printf("mkfifo successn");int fd = open("./myfifo",O_RDONLY);  //读写if(fd < 0){perror("open");return -1;}int fd1 = open("./fifo",O_WRONLY);  //读写if(fd < 0){perror("open");return -1;}ssize_t res = 0;char buf[128];while(1){//接收管道1bzero(buf,sizeof(buf));	res = read(fd,buf,sizeof(buf));if(res < 0){perror("read");return -1;}else if(0 == res){printf("接收结束n");break;}printf("B get:%ld %sn",res,buf);if(strcasecmp(buf,"quit") == 0)break;//写入管道2printf("B-->A:");fgets(buf,sizeof(buf),stdin);buf[strlen(buf)-1] = 0;if(write(fd1,buf,sizeof(buf)) < 0){perror("write");return -1;}if(strcasecmp(buf,"quit") == 0)break;}close(fd);close(fd1);return 0;
}

 图中1代表B进程,2代表A进程

2)在第一题的基础上实现,AB进程能够随时收发数据(附加题)

A进程:

#include<stdio.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<unistd.h>
#include<fcntl.h>
#include<errno.h>
#include<string.h>
#include<pthread.h>int fd,fd1;
ssize_t res = 0;
char buf[128];void* putB(void* arg)
{//写入管道2while(1){fgets(buf,sizeof(buf),stdin);buf[strlen(buf)-1] = 0;if(write(fd1,buf,sizeof(buf)) < 0){perror("write");return NULL;}}pthread_exit(NULL);
}void* getA(void* arg)
{//接收管道1while(1){bzero(buf,sizeof(buf));	res = read(fd,buf,sizeof(buf));if(res < 0){perror("read");return NULL;}else if(0 == res){printf("接收结束n");break;}printf("B get:%ld %sn",res,buf);}pthread_exit(NULL);}int main(int argc, const char *argv[])
{//管道1if(mkfifo("./myfifo",0664) < 0){if(errno != 17){perror("mkfifo");return -1;}}//管道2if(mkfifo("./fifo",0664) < 0){if(errno != 17){perror("mkfifo");return -1;}}printf("mkfifo successn");fd = open("./myfifo",O_RDONLY);  //读写if(fd < 0){perror("open");return -1;}fd1 = open("./fifo",O_WRONLY);  //读写if(fd < 0){perror("open");return -1;}//创建线程pthread_t tid1,tid2;if(pthread_create(&tid2,NULL,putB,NULL) != 0){perror("pthread_create");return -1;}if(pthread_create(&tid1,NULL,getA,NULL) != 0){perror("pthread_create");return -1;}pthread_join(tid1,NULL);pthread_join(tid2,NULL);close(fd);close(fd1);return 0;
}

B进程:

#include<stdio.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<unistd.h>
#include<fcntl.h>
#include<errno.h>
#include<string.h>
#include<pthread.h>int fd,fd1;
ssize_t res = 0;
char buf[128];void* getB(void* arg)
{//接收管道2while(1){bzero(buf,sizeof(buf));	res = read(fd1,buf,sizeof(buf));if(res < 0){perror("read");return NULL;}else if(0 == res){printf("接收结束n");break;}printf("A_get:%ld %sn",res,buf);}pthread_exit(NULL);
}void* putA(void* arg)
{//输入管道1while(1){fgets(buf,sizeof(buf),stdin);buf[strlen(buf)-1] = 0;if(write(fd,buf,sizeof(buf)) < 0){perror("write");return NULL;}}pthread_exit(NULL);
}int main(int argc, const char *argv[])
{//管道1if(mkfifo("./myfifo",0664) < 0){if(errno != 17){perror("mkfifo");return -1;}}//管道2if(mkfifo("./fifo",0664) < 0){if(errno != 17){perror("mkfifo");return -1;}}printf("mkfifo successn");fd = open("./myfifo",O_WRONLY);    //读写if(fd < 0){perror("open");return -1;}fd1 = open("./fifo",O_RDONLY);  //读写if(fd1 < 0){perror("open");return -1;}//创建线程pthread_t tid1,tid2;if(pthread_create(&tid1,NULL,putA,NULL) != 0){perror("pthread_create");return -1;}if(pthread_create(&tid2,NULL,getB,NULL) != 0){perror("pthread_create");return -1;}pthread_join(tid1,NULL);pthread_join(tid2,NULL);close(fd);close(fd1);return 0;
}

捕获2)3)20)号信号

#include<stdio.h>	
#include<signal.h>
#include<unistd.h>typedef void (*sighandler_t)(int);void handler1(int sig)
{printf("%d %dn",sig,__LINE__);
}void handler2(int sig)
{printf("%d %dn",sig,__LINE__);
}int main(int argc, const char *argv[])
{//捕获20号信号SIGINTsighandler_t s = signal(20,handler1);if(SIG_ERR == s){perror("signal");return -1;}printf("%p %dn",s,__LINE__);s = signal(20,handler2);if(SIG_ERR == s){perror("signal");return -1;}printf("%p %dn",s,__LINE__);while(1){printf("main");sleep(1);}return 0;
}

本文发布于:2024-01-31 09:18:43,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170666392227473.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:管道   有名
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23