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

作业社区

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

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; } 【图片】

得分 100
学习任务

Felixxx 的学生作业:

一、阻塞 I/O(Blocking I/O) 工作原理: 当应用程序发起一个 I/O 请求(如读取文件、接收网络数据)时,如果数据尚未就绪(例如缓冲区中没有数据),该调用会一直等待,直到数据准备好并完成拷贝到用户空间,才返回控制权给程序。在此期间,线程/进程处于阻塞状态,不能执行其他任务。 优点: 编程模型简单:代码逻辑清晰,易于理解和实现。 资源开销小:不需要频繁轮询或回调机制,系统开销较低。 适合低并发场景:在请求量不大、I/O 延迟可接受的情况下效率高。 缺点: 并发能力差:一个线程只能处理一个 I/O 请求,若需处理多个连接,必须创建多个线程/进程,导致资源消耗大。 响应延迟高:在高延迟 I/O(如网络请求)中,线程长时间阻塞,浪费 CPU 资源。 扩展性受限:面对成千上万并发连接时,线程数量激增,系统难以承受。 二、非阻塞 I/O(Non-blocking I/O) 工作原理: 当应用程序发起 I/O 请求时,如果数据未就绪,系统立即返回一个错误(如 EAGAIN 或 EWOULDBLOCK),而不是等待。程序可以继续执行其他任务,并在稍后再次尝试 I/O 操作(通常通过轮询或事件驱动机制判断数据是否就绪)。 优点: 高并发支持:单个线程可轮询或监听多个 I/O 操作,适合高并发场景(如 Web 服务器)。 资源利用率高:避免线程长时间阻塞,CPU 可用于处理其他任务。 响应更快:不会因某个 I/O 阻塞整个程序流程。 缺点: 编程复杂度高:需手动管理 I/O 状态、重试逻辑,通常需配合 select/poll/epoll 或异步框架。 轮询开销:若采用忙等待(busy-waiting)方式检查 I/O 状态,会浪费 CPU 资源。 调试困难:状态管理和错误处理更复杂,容易引入 bug。

微信客服

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

帮助反馈 APP下载

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

公众号

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