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

作业社区

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

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

jelasin 的学生作业:

//time_server.c #include #include #include #include #include #include #include #include #include #include #define FIFO_NAME "/tmp/time_fifo" void cleanup() { unlink(FIFO_NAME); } void signal_handler(int signum) { exit(EXIT_SUCCESS); } int main() { int fd; char buffer[128]; time_t currentTime; if (access(FIFO_NAME, F_OK) == -1) { if (mkfifo(FIFO_NAME, 0666) != 0) { perror("mkfifo"); exit(EXIT_FAILURE); } } atexit(cleanup); if (signal(SIGINT, signal_handler) == SIG_ERR) { perror("signal"); exit(EXIT_FAILURE); } if (signal(SIGTERM, signal_handler) == SIG_ERR) { perror("signal"); exit(EXIT_FAILURE); } if (signal(SIGPIPE, signal_handler) == SIG_ERR) { perror("signal"); exit(EXIT_FAILURE); } fd = open(FIFO_NAME, O_WRONLY); if (fd == -1) { perror("open"); exit(EXIT_FAILURE); } while (true) { currentTime = time(NULL); if (currentTime == ((time_t)-1)) { perror("time"); exit(EXIT_FAILURE); } snprintf(buffer, sizeof(buffer), "%s", ctime(&currentTime)); if (write(fd, buffer, strlen(buffer) + 1) == -1) { perror("write"); exit(EXIT_FAILURE); } sleep(1); } close(fd); return 0; } #include #include #include #include #include #define FIFO_NAME "/tmp/time_fifo" int main() { int fd; char buffer[128]; fd = open(FIFO_NAME, O_RDONLY); if (fd == -1) { perror("open"); exit(EXIT_FAILURE); } while (true) { if (read(fd, buffer, sizeof(buffer)) > 0) { printf("Current time: %s", buffer); } else { perror("read"); exit(EXIT_FAILURE); } } close(fd); return 0; } 【图片】

微信客服

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

帮助反馈 APP下载

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

公众号

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