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

作业社区

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

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

沫颖 的学生作业:

代码 #include #include #include #include #include #include #include #include #include int main(void) { // 进程id pid_t cpid; // 创建主进程、第一个子进程 cpid = fork(); // fork失败 if (cpid < 0) { perror("[ERROR] fork() 1:"); exit(EXIT_FAILURE); } else if (cpid == 0) { // 子进程A printf("Child Process A running,pid: %d\n", getpid()); } else if (cpid > 0) { // 让子进程A先运行 sleep(2); // 主进程 printf("Main Process running,pid: %d\n", getpid()); // 给子进程A发送SIGKILL信号 int retA; retA = kill(cpid, SIGKILL); // 成功发送 if (retA == 0) { printf("Main Process %d Killed Child Process A %d\n", getpid(), cpid); } // 等待子进程A退出 waitpid(cpid, NULL, 0); // 创建子进程B pid_t ccpid; ccpid = fork(); // fork失败 if (ccpid < 0) { perror("[ERROR] fork() 2:"); exit(EXIT_FAILURE); } else if (ccpid == 0) { // 子进程B printf("Child Process B running,pid: %d\n", getpid()); } else if (ccpid > 0) { // 先让子进程B运行 sleep(2); // 给子进程B发送SIGKILL信号 int retB; retB = kill(ccpid, SIGKILL); // 成功发送 if (retB == 0) { printf("Main Process %d Killed Child Process B %d\n", getpid(), ccpid); } // 等待子进程B退出 waitpid(ccpid, NULL, 0); } wait(NULL); // 退出 exit(EXIT_SUCCESS); } return 0; } 运行结果 linux@linux:~/Desktop/Study/CProjects/imooc-embedded/Stage05/Week12/Class04$ gcc demo_4_8.c linux@linux:~/Desktop/Study/CProjects/imooc-embedded/Stage05/Week12/Class04$ ./a.out Child Process A running,pid: 6531 Main Process running,pid: 6530 Main Process 6530 Killed Child Process A 6531 Child Process B running,pid: 6543 Main Process 6530 Killed Child Process B 6543 linux@linux:~/Desktop/Study/CProjects/imooc-embedded/Stage05/Week12/Class04$

得分 100
学习任务

沫颖 的学生作业:

代码 fifo_read.c #include #include #include #include #include #include #include // 定义有名管道文件 路径 #define PATHNAME "./fifo_test" int main(void) { // IO操作符 int fd; // 存放时间字符串 char rbuffer[20]; // 打开文件 fd = open(PATHNAME, O_RDONLY); if (fd == -1) { perror("[ERROR] open() :"); close(fd); exit(EXIT_FAILURE); } int rbytes; rbytes = read(fd, rbuffer, sizeof(rbuffer)); if (rbytes > 0) { printf("rbuffer:%s\n", rbuffer); } close(fd); return 0; } fifo_write.c #include #include #include #include #include #include #include #include // 定义有名管道文件 路径 #define PATHNAME "./fifo_test" int main(void) { // IO操作符 int fd; // 存放时间字符串 char wbuffer[20]; // 获取当前系统时间 time_t now = time(NULL); struct tm *tm_info = localtime(&now); strftime(wbuffer, sizeof(wbuffer), "%Y-%m-%d %H:%M:%S", tm_info); // 创建有名管道文件 int ret; ret = access(PATHNAME, F_OK); if (ret == -1) { mkfifo(PATHNAME, 0644); } // 打开文件 fd = open(PATHNAME, O_WRONLY); if (fd == -1) { perror("[ERROR] open() :"); close(fd); exit(EXIT_FAILURE); } int wbytes; wbytes = write(fd, wbuffer, strlen(wbuffer) + 1); if (wbytes < 0) { perror("[ERROR] write:"); } close(fd); return 0; } 执行效果 linux@linux:~/Desktop/Study/CProjects/imooc-embedded/Stage05/Week12/Class04/demo_4_5$ ./fifo_read rbuffer:2024-12-11 22:12:50

微信客服

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

帮助反馈 APP下载

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

公众号

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