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

作业社区

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

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

cjozGV 的学生作业:

#include #include #include #include #include #include #define FIFO_PATH "mypipe" #define MAXEVENTS 10 int main(void) { int epfd, fd, ret; struct epoll_event ev; struct epoll_event ret_ev[MAXEVENTS]; char buffer[64]; // 如果管道不存在,创建有名管道 if (access(FIFO_PATH, F_OK) == -1) { if (mkfifo(FIFO_PATH, 0666) == -1) { perror("mkfifo"); exit(EXIT_FAILURE); } } // 打开管道读端(非阻塞) fd = open(FIFO_PATH, O_RDONLY | O_NONBLOCK); if (fd == -1) { perror("open"); exit(EXIT_FAILURE); } // 创建 epoll 实例 epfd = epoll_create1(0); if (epfd == -1) { perror("epoll_create1"); exit(EXIT_FAILURE); } printf("epfd = %d\n", epfd); // 注册管道 fd 到 epoll,监听可读事件 ev.data.fd = fd; ev.events = EPOLLIN; ret = epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev); if (ret == -1) { perror("epoll_ctl"); exit(EXIT_FAILURE); } // 主循环:等待事件发生 for (;;) { ret = epoll_wait(epfd, ret_ev, MAXEVENTS, 5000); // 最多等待 5 秒 if (ret == -1) { perror("epoll_wait"); exit(EXIT_FAILURE); } else if (ret == 0) { printf("Timeout.\n"); } else { for (int i = 0; i < ret; i++) { if (ret_ev[i].events & EPOLLIN) { ssize_t len = read(ret_ev[i].data.fd, buffer, sizeof(buffer) - 1); if (len > 0) { buffer[len] = '\0'; printf("收到数据:%s\n", buffer); } else if (len == 0) { // 写端关闭,重新打开管道 printf("写端关闭,重新打开管道...\n"); close(fd); fd = open(FIFO_PATH, O_RDONLY | O_NONBLOCK); ev.data.fd = fd; epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev); } } } } } close(epfd); close(fd); return 0; }

得分 100
讨论题

学渣小白 的学生作业:

#include // 求出下列数据的答案。(上一个表达式x,y的值会影响下面x和y的值) int main() { int x = 10 ,y =20; printf(“int x = 10 ,y =20;” “\n”); int z = 0; printf(“int z = 0;” “\n”); printf("\n*************delimiter**************\n"); z = (++x) + (y++); printf("z = (++x) + (y++);" "\n"); //X is first increased by 1, then assigned to 11, and y is first assigned to 20 before adding 1, so z=11+20=31; x=11; y=21; printf("//X is first increased by 1, then assigned to 11, and y is first assigned to 20 before adding 1, so z=11+20=31;x=11;y=21;" "\n"); printf("z = %d\n",z); printf("x=%d\n",x); printf("y=%d\n",y); printf("\n*************delimiter**************\n"); z = (--x) + (y++); printf("z = (--x) + (y++);" "\n"); // x先减1,再赋值为10,y先赋值为21,再减1,所以z=10+21=31;x=10;y=22; printf("//X is first subtracted by 1 and then assigned a value of 10, y is first assigned a value of 21 and then subtracted by 1, so z=10+21=31; x=10; y=22;" "\n"); printf("z = %d\n",z); printf("x=%d\n",x); printf("y=%d\n",y); printf("\n*************delimiter**************\n"); z= (++x) + (--y); printf("z= (++x) + (--y);" "\n"); //x先增加1,再赋值为11,y先减1,再赋值为21,所以z=11+21=32;x=11;y=21; printf("//X is first increased by 1 and then assigned a value of 11, y is first decreased by 1 and then assigned a value of 21, so z=11+21=32; x=11; y=21;" "\n"); printf("z = %d\n",z); printf("x=%d\n",x); printf("y=%d\n",y); printf("\n*************delimiter**************\n"); printf("x = %d y = %d\n",x,y); return 0; } linux@linux:~/test01$ gcc 1i4practicer.c linux@linux:~/test01$ ./a.out int x = 10 ,y =20; int z = 0; delimiter* z = (++x) + (y++); //X is first increased by 1, then assigned to 11, and y is first assigned to 20 before adding 1, so z=11+20=31;x=11;y=21; z = 31 x=11 y=21 delimiter* z = (–x) + (y++); //X is first subtracted by 1 and then assigned a value of 10, y is first assigned a value of 21 and then subtracted by 1, so z=10+21=31; x=10; y=22; z = 31 x=10 y=22 delimiter* z= (++x) + (–y); //X is first increased by 1 and then assigned a value of 11, y is first decreased by 1 and then assigned a value of 21, so z=11+21=32; x=11; y=21; z = 32 x=11 y=21 delimiter* x = 11 y = 21 linux@linux:~/test01$

得分 100
学习任务

cjozGV 的学生作业:

#include #include #include #include #include #include #define FIFO_PATH "mypipe" #define MAX_EVENTS 10 int main() { int fd, epfd; struct epoll_event ev, events[MAX_EVENTS]; char buffer[128]; // 创建有名管道 if (access(FIFO_PATH, F_OK) == -1) { mkfifo(FIFO_PATH, 0666); } // 打开管道读端(非阻塞) fd = open(FIFO_PATH, O_RDONLY | O_NONBLOCK); if (fd == -1) { perror("open"); exit(EXIT_FAILURE); } // 创建 epoll 实例 epfd = epoll_create1(0); if (epfd == -1) { perror("epoll_create1"); exit(EXIT_FAILURE); } // 设置监听事件(可读) ev.events = EPOLLIN; ev.data.fd = fd; epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev); printf("开始使用 epoll 监听管道 %s ...\n", FIFO_PATH); while (1) { int nfds = epoll_wait(epfd, events, MAX_EVENTS, 5000); // 最多等待 5 秒 if (nfds == -1) { perror("epoll_wait"); break; } else if (nfds == 0) { printf("等待超时,没有数据。\n"); } else { for (int i = 0; i < nfds; i++) { if (events[i].events & EPOLLIN) { ssize_t len = read(events[i].data.fd, buffer, sizeof(buffer) - 1); if (len > 0) { buffer[len] = '\0'; printf("收到数据:%s\n", buffer); } else if (len == 0) { printf("写端关闭,重新打开管道...\n"); close(fd); fd = open(FIFO_PATH, O_RDONLY | O_NONBLOCK); ev.data.fd = fd; epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev); } } } } } close(epfd); close(fd); return 0; } 【图片】

微信客服

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

帮助反馈 APP下载

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

公众号

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