为了账号安全,请及时绑定邮箱和手机立即绑定

作业社区

探索学习新天地,共享知识资源!

0 提交作业
0 布置作业
0 满分作业
得分 100
学习任务

Felixxx 的学生作业:

read: #include #include #include #include #include #include #include #define FIFOPATH "./fifo_test_3" #define MAXEVENTS 10 int main() { int epfd,ret,fd,rbytes; char rbuffer[64] = {0}; struct epoll_event ev;//输入事件 struct epoll_event res_ev[MAXEVENTS];//接收就绪事件(输出) ret = mkfifo(FIFOPATH,0644); if(ret == -1) { perror("[ERROR] mkfifo():"); exit(EXIT_FAILURE); } fd = open(FIFOPATH,O_RDONLY | O_NONBLOCK); if(fd == -1) { perror("[ERROR] open():"); exit(EXIT_FAILURE); } epfd = epoll_create(1); ev.data.fd = fd; ev.events = EPOLLIN; ret = epoll_ctl(epfd,EPOLL_CTL_ADD,fd,&ev); if(ret == -1) { perror("[ERROR] epoll_ctl():"); exit(EXIT_FAILURE); close(fd); } while(1) { ret = epoll_wait(epfd,res_ev,MAXEVENTS,5000); if(ret == -1) { perror("[ERROR] epoll_wait():"); exit(EXIT_FAILURE); close(fd); } else if(ret == 0) printf("Time Out.\n"); else if(ret > 0) { for(int i = 0 ; i < ret ; i ++) { if(res_ev[i].data.fd == fd) { rbytes = read(fd,rbuffer,sizeof(rbuffer)); if(rbytes == -1) { perror("[ERROR] read():"); exit(EXIT_FAILURE); close(fd); } else if(rbytes == 0) { close(fd); unlink(FIFOPATH); } else if(rbytes > 0) printf("buffer is %s \n",rbuffer); } } } } return 0; } write: #include #include #include #include #include #include #include #define FIFOPATH "./fifo_test_3" int main() { int fd,ret; char wbuffer[64] = "fifo pipe."; ret = access(FIFOPATH,F_OK); if(ret == -1) mkfifo(FIFOPATH,0644); fd = open(FIFOPATH,O_WRONLY); if(fd == -1) { perror("[ERROR] open():"); exit(EXIT_FAILURE); } int wbytes; wbytes = write(fd,wbuffer,strlen(wbuffer)+1); if(wbytes < 0) perror("[ERROR] write():"); else printf("success!buffer:%s\n",wbuffer); close(fd); return 0; } 【图片】

得分 100
学习任务

Felixxx 的学生作业:

read: #include #include #include #include #include #include #include #define FIFOPATH "./fifo_test_2" int main() { int ret, fd; struct pollfd pfds; char buffer[64] = {0}; // 关键:用 O_RDONLY pfds.fd = open(FIFOPATH, O_RDONLY); if (pfds.fd == -1) { perror("[ERROR] open():"); exit(EXIT_FAILURE); } pfds.events = POLLIN; printf("Waiting for data...\n"); while (1) { ret = poll(&pfds, 1, 5000); // 5秒超时,方便测试 if (ret == -1) { perror("[ERROR] poll():"); break; } else if (ret == 0) { printf("Timeout, still waiting...\n"); } else if (ret > 0) { if (pfds.revents & POLLIN) { int bytes = read(pfds.fd, buffer, sizeof(buffer) - 1); if (bytes > 0) { buffer[bytes] = '\0'; // 确保字符串结尾 printf("Received: %s\n", buffer); break; // 收到就退出,或继续循环 } else if (bytes == 0) { printf("Writer closed.\n"); break; } } } } close(pfds.fd); return 0; } write: #include #include #include #include #include #include #include #define FIFOPATH "./fifo_test_2" int main() { int ret, fd; char wbuffer[64] = "fifo pipe."; ret = access(FIFOPATH, F_OK); if (ret == -1) { mkfifo(FIFOPATH, 0644); } // 关键:用 O_WRONLY fd = open(FIFOPATH, O_WRONLY); if (fd == -1) { perror("[ERROR] open():"); exit(EXIT_FAILURE); } int wbytes = write(fd, wbuffer, strlen(wbuffer) + 1); if (wbytes < 0) { perror("[ERROR] write():"); } else { printf("Wrote %d bytes\n", wbytes); } close(fd); return 0; } 【图片】

微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号