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

作业社区

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

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

jelasin 的学生作业:

#ifndef __USE_GNU #define __USE_GNU #endif #ifndef _GNU_SOURCE #define _GNU_SOURCE #endif #include #include #include #include #include #include #include #include #include #include #include #include pid_t r_wait(int *stat_loc) { int retval; while(((retval = wait(stat_loc)) == -1 && (errno == EINTR))); return retval; } void sigaction_a(int sig, siginfo_t *si, void *uc) { fprintf(stderr, "Child process 1 received signal %s\n", strsignal(sig)); } int main() { pid_t child_pid[2]; cpu_set_t cpuset; CPU_ZERO(&cpuset); CPU_SET(0, &cpuset); if (sched_setaffinity(0, sizeof(cpu_set_t), &cpuset) == -1) { perror("sched_setaffinity"); exit(EXIT_FAILURE); } child_pid[0] = fork(); if (child_pid[0] 0) { sched_yield(); // let child process 1 run child_pid[1] = fork(); if (child_pid[1] 0) { sched_yield(); // let child process 2 run fprintf(stderr, "Parent process sending SIGUSR1 to child 2\n"); if (sigqueue(child_pid[0], SIGUSR1, (union sigval){0}) == -1) { perror("sigqueue"); exit(EXIT_FAILURE); } fprintf(stderr, "Parent process sending SIGUSR2 to child 1\n"); if (sigqueue(child_pid[1], SIGUSR2, (union sigval){0}) == -1) { perror("sigqueue"); exit(EXIT_FAILURE); } int status; int cpid; while((cpid = r_wait(&status)) != -1) { continue; } if (errno != ECHILD) { perror("wait"); exit(EXIT_FAILURE); } } } return 0; } ➜ 5 ./main Child process 1 paused Child process 2 paused Parent process sending SIGUSR1 to child 2 Parent process sending SIGUSR2 to child 1 Child process 1 received signal User defined signal 1

得分 100
学习任务

jelasin 的学生作业:

#ifndef __USE_GNU #define __USE_GNU #endif // CPU亲和相关要定义这个 #ifndef _GNU_SOURCE #define _GNU_SOURCE #endif #include #include #include #include #include #include #include #include #include #include #include #include pid_t r_wait(int *stat_loc) { int retval; while(((retval = wait(stat_loc)) == -1 && (errno == EINTR))); return retval; } int main() { pid_t child_pid[2]; cpu_set_t cpuset; CPU_ZERO(&cpuset); CPU_SET(0, &cpuset); if (sched_setaffinity(0, sizeof(cpu_set_t), &cpuset) == -1) { perror("sched_setaffinity"); exit(EXIT_FAILURE); } child_pid[0] = fork(); if (child_pid[0] < 0) { perror("fork #1"); exit(EXIT_FAILURE); } else if (child_pid[0] == 0) { fprintf(stderr, "Child process 1 paused\n"); pause(); } else if (child_pid[0] > 0) { sched_yield(); // let child process 1 run child_pid[1] = fork(); if (child_pid[1] < 0) { perror("fork #2"); exit(EXIT_FAILURE); } else if (child_pid[1] == 0) { fprintf(stderr, "Child process 2 raised SIGSTOP\n"); raise(SIGSTOP); } else if (child_pid[1] > 0) { sched_yield(); // let child process 2 run fprintf(stderr, "Parent process sending SIGCONT to child 2\n"); if (kill(child_pid[1], SIGCONT) == -1) { perror("kill"); exit(EXIT_FAILURE); } fprintf(stderr, "Parent process sending SIGTERM to child 1\n"); if (kill(child_pid[0], SIGTERM) == -1) { perror("kill"); exit(EXIT_FAILURE); } int status; int cpid; while((cpid = r_wait(&status)) != -1) { continue; } if (errno != ECHILD) { perror("wait"); exit(EXIT_FAILURE); } } } return 0; } ➜ 5 ./main Child process 1 paused Child process 2 raised SIGSTOP Parent process sending SIGCONT to child 2 Parent process sending SIGTERM to child 1 因为父进程缓存多,现代Linux默认父进程先执行,为了让父进程让出cpu资源可以选择让父进程sleep进入就绪态。 更好的方案是使用sched_yield(),对于SMP来说,需要进行绑核,以免父进程让出的CPU不是子进程要抢占的CPU。

微信客服

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

帮助反馈 APP下载

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

公众号

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