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

作业社区

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

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

慕九州9493288 的学生作业:

一、代码 #include #include #include #include #include #include #include /** * 创建两个子进程 A 与 B,给子进程 A 发送 SIGUSR1 信号, 子进程 B 发送 SIGUSR2 * 信号,子进程A的处理方式设置为默认,子进程B的处理方式为使用自定义处理函数,并打印接收的信号的字符串信息(使用strsignal() 函数) */ void do_sig_usr(int signo) { printf("receive signal: %s\n", strsignal(signo)); } int main(void) { __sighandler_t ret; pid_t cpid_a, cpid_b; ret = signal(SIGUSR2, do_sig_usr); if (ret == SIG_ERR) { perror("[ERROR] signal():"); exit(EXIT_FAILURE); } cpid_a = fork(); if (cpid_a == -1) { printf("[ERROR] fork() a:"); exit(EXIT_FAILURE); } else if (cpid_a == 0) { printf("[INFO] child process A: %d start.\n", getpid()); pause(); //等待信号处理,默认处理就会退出,不执行后面代码 printf("[INFO] child process A: %d end.\n", getpid()); exit(EXIT_SUCCESS); } cpid_b = fork(); if (cpid_b == -1) { kill(cpid_a, SIGKILL); wait(NULL); printf("[ERROR] fork() b:"); exit(EXIT_FAILURE); } else if (cpid_b == 0) { printf("[INFO] child process B: %d start.\n", getpid()); pause(); printf("[INFO] child process B: %d end.\n", getpid()); exit(EXIT_SUCCESS); } if (cpid_a > 0 && cpid_b > 0) { sleep(1); kill(cpid_a, SIGUSR1); kill(cpid_b, SIGUSR2); waitpid(cpid_a, NULL, 0); waitpid(cpid_b, NULL, 0); } return EXIT_SUCCESS; } 二、结果 linux@DESKTOP-4M60T5C:~/homework/2026/0318$ gcc hw_signal.c linux@DESKTOP-4M60T5C:~/homework/2026/0318$ ./a.out [INFO] child process A: 1953 start. [INFO] child process B: 1954 start. receive signal: User defined signal 2 [INFO] child process B: 1954 end. linux@DESKTOP-4M60T5C:~/homework/2026/0318$

得分 100
学习任务

慕九州9493288 的学生作业:

一、代码 #include #include #include #include #include #include #include /** * 创建两个子进程,由父进程分别给两个子进程发送 SIGKILL 信号 */ int main(void) { pid_t cpid_1, cpid_2; int ret_1, ret_2; // 创建第一个子进程 cpid_1 = fork(); if (cpid_1 == -1) { perror("[Error] fork():"); return EXIT_FAILURE; } if (cpid_1 == 0) { fprintf(stdout, "Child 1 (pid: %d) is running...\n", getpid()); while (1) { // 保持子进程存活 sleep(1); } exit(EXIT_SUCCESS); // 理论上不会执行到 } // 创建第二个子进程 cpid_2 = fork(); if (cpid_2 == -1) { perror("[Error] fork():"); // 如果第二次fork失败,需要终止第一个子进程并回收其资源 kill(cpid_1, SIGKILL); waitpid(cpid_1, NULL, 0); return EXIT_FAILURE; } if (cpid_2 == 0) { fprintf(stdout, "Child 2 (pid: %d) is running...\n", getpid()); while (1) { // 保持子进程存活 sleep(1); } exit(EXIT_SUCCESS); // 理论上不会执行到 } // 父进程:等待一段时间后,向两个子进程发送SIGKILL信号 if (cpid_1 > 0 && cpid_2 > 0) { sleep(2); // 向两个子进程发送SIGKILL信号 ret_1 = kill(cpid_1, SIGKILL); ret_2 = kill(cpid_2, SIGKILL); // 检查向子进程1发送信号的结果 if (ret_1 == -1) { perror("[Error] kill() child 1:"); return EXIT_FAILURE; } else if (ret_1 == 0) { fprintf(stdout, "Father (pid: %d) killed child 1 (pid: %d)\n", getpid(), cpid_1); } // 检查向子进程2发送信号的结果 if (ret_2 == -1) { perror("[Error] kill() child 2:"); return EXIT_FAILURE; } else if (ret_2 == 0) { fprintf(stdout, "Father (pid: %d) killed child 2 (pid: %d)\n", getpid(), cpid_2); } // 等待两个子进程结束并回收其资源 waitpid(cpid_1, NULL, 0); waitpid(cpid_2, NULL, 0); fprintf(stdout, "Father (pid: %d) has waited for all children exit\n", getpid()); exit(EXIT_SUCCESS); } return 0; } 二、结果 linux@DESKTOP-4M60T5C:~/homework/2026/0318$ gcc hw_sigkill.c linux@DESKTOP-4M60T5C:~/homework/2026/0318$ ./a.out Child 1 (pid: 18064) is running... Child 2 (pid: 18065) is running... Father (pid: 18063) killed child 1 (pid: 18064) Father (pid: 18063) killed child 2 (pid: 18065) Father (pid: 18063) has waited for all children exit linux@DESKTOP-4M60T5C:~/homework/2026/0318$

得分 100
学习任务

城仔 的学生作业:

#include #include #include #include #include #include //记录超时次数,子进程ID int timeout_count = 0; pid_t timer_pid = -1; //信号处理函数,收到子进程超时信号时触发 void timeout_handler(int sig){ //收到子进程超时信号 if(sig == SIGALRM){ timeout_count++; printf("\t 当前超时次数: %d \n",timeout_count); //如果超时3次,退出程序 if(timeout_count == 3){ printf("超时三次,退出程序! \n"); exit(EXIT_FAILURE); } //未达到3次,重新创建子进程进行计时 timer_pid = fork(); if(timer_pid == 0){ sleep(3); kill(getppid(),SIGALRM); //向父进程发送超时信号 exit(EXIT_SUCCESS); }else if(timer_pid == -1){ perror("ERROR fork() "); exit(EXIT_FAILURE); } } } int main(int argc, const char *argv[]) { char intput_buf[1024]; int status; //注册信号处理函数,处理子进程的超时信号 signal(SIGALRM,timeout_handler); printf("\t 请输入内容,3秒内无输入视为超时,超时3次退出程序\n"); //创建第一个子进程 timer_pid = fork(); if(timer_pid == 0){ sleep(3); kill(getppid(),SIGALRM); exit(EXIT_SUCCESS); }else if(timer_pid == -1 ){ perror("创建子进程失败"); exit(EXIT_FAILURE); } //父进程循环获取输入 while(1){ //读取用户输入 if(fgets(intput_buf,sizeof(intput_buf),stdin) != NULL){ //读取输入,收到换行符时推出程序 intput_buf[strcspn(intput_buf,"\n")] = '\0'; printf("收到输入: %s\n",intput_buf); //推出程序 if(timer_pid > 0){ kill(timer_pid,SIGKILL); waitpid(timer_pid,&status,0); } //重置超时次数,重新创建子进程 timeout_count = 0; timer_pid = fork(); if(timer_pid == 0){ sleep(3); kill(getppid(),SIGALRM); exit(EXIT_SUCCESS); }else if(timer_pid == -1){ perror("ERROR fork()"); exit(EXIT_FAILURE); } } } return 0; } 【图片】

得分 100
学习任务

慕九州9493288 的学生作业:

一、代码 hw_fifo_read.c #include #include #include #include #include #include #include #define BUF_SIZE 1024 #define FIFO_NAME "./fifo_homework" int main(void) { int ret, fd; char buf[BUF_SIZE] = {0}; ssize_t rbype; if (access(FIFO_NAME, F_OK) == -1) { ret = mkfifo(FIFO_NAME, 0644); if (ret == -1) { perror("[ERROR] mkfifo(): "); exit(EXIT_FAILURE); } } fd = open(FIFO_NAME, O_RDONLY); if (fd == -1) { perror("[ERROR] open(): "); exit(EXIT_FAILURE); } rbype = read(fd, buf, sizeof(buf)); if (rbype == -1) { perror("[ERROR] read(): "); close(fd); exit(EXIT_FAILURE); } printf("当前时间:%s\n", buf); close(fd); return EXIT_SUCCESS; } hw_fifo_write.c #include #include #include #include #include #include #include #include #define BUF_SIZE 100 #define FIFO_NAME "./fifo_homework" void get_current_time(char *time_buf) { time_t t; struct tm *tmp; time(&t); tmp = localtime(&t); strftime(time_buf, BUF_SIZE, "%Y-%m-%d %H:%M:%S", tmp); } int main(void) { int ret, fd; char time_buf[BUF_SIZE]; ssize_t wbype; fd = open(FIFO_NAME, O_WRONLY); if (fd == -1) { perror("[ERROR] open(): "); exit(EXIT_FAILURE); } get_current_time(time_buf); wbype = write(fd, time_buf, strlen(time_buf)); if (wbype == -1) { perror("[ERROR] write(): "); close(fd); exit(EXIT_FAILURE); } close(fd); return 0; } 二、结果 read端命令窗口 linux@DESKTOP-4M60T5C:~/homework/2026/0209$ gcc hw_fifo_read.c -o hw_fi_read linux@DESKTOP-4M60T5C:~/homework/2026/0209$ gcc hw_fifo_write.c -o hw_fi_write linux@DESKTOP-4M60T5C:~/homework/2026/0209$ ./hw_fi_read 当前时间:2026-03-17 18:24:41 linux@DESKTOP-4M60T5C:~/homework/2026/0209$ write端命令窗口 linux@DESKTOP-4M60T5C:~/homework/2026/0209$ ls execl.c fi_write fifo_read.c fifo_write.c hw_fi_read hw_fifo_read.c hw_fork.c hw_wait.c minishell.c wait.c fi_read fifo_homework fifo_test fork.c hw_fi_write hw_fifo_write.c hw_pipe.c main.c pipe.c linux@DESKTOP-4M60T5C:~/homework/2026/0209$ ./hw_fi_write linux@DESKTOP-4M60T5C:~/homework/2026/0209$

首页上一页1234567下一页尾页
微信客服

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

帮助反馈 APP下载

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

公众号

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