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

作业社区

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

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

qq_康斯坦丁_0 的学生作业:

#include #include #include #include #include #include #include #include #define FIFO_PATH “/tmp/time_fifo” #define BUFFER_SIZE 128 // 时间发送进程 void time_sender() { int fd; char buffer[BUFFER_SIZE]; time_t current_time; struct tm *time_info; // 创建FIFO mkfifo(FIFO_PATH, 0666); printf("[发送进程] 等待接收进程连接...\n"); fd = open(FIFO_PATH, O_WRONLY); while (1) { // 获取当前时间 time(&current_time); time_info = localtime(&current_time); strftime(buffer, BUFFER_SIZE, "%Y-%m-%d %H:%M:%S", time_info); // 写入FIFO write(fd, buffer, strlen(buffer) + 1); printf("[发送进程] 发送时间: %s\n", buffer); sleep(1); // 每秒发送一次 } close(fd); unlink(FIFO_PATH); } // 时间接收进程 void time_receiver() { int fd; char buffer[BUFFER_SIZE]; ssize_t bytes_read; printf("[接收进程] 正在连接到FIFO...\n"); fd = open(FIFO_PATH, O_RDONLY); while (1) { // 从FIFO读取时间 bytes_read = read(fd, buffer, BUFFER_SIZE); if (bytes_read > 0) { printf("[接收进程] 接收到时间: %s\n", buffer); } } close(fd); } int main(int argc, char *argv[]) { if (argc != 2) { printf(“使用方法: %s [sender|receiver]\n”, argv[0]); return 1; } if (strcmp(argv[1], "sender") == 0) { time_sender(); } else if (strcmp(argv[1], "receiver") == 0) { time_receiver(); } else { printf("无效的参数。请使用 'sender' 或 'receiver'\n"); return 1; } return 0; }

微信客服

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

帮助反馈 APP下载

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

公众号

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