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

作业社区

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

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

慕先生4541263 的学生作业:

#include #include #include #include #include #include #include #include #include #include #define PATHNAME "." #define PROID 10 union semun { int val; }; void sem_p(int semid, int sem_num) { struct sembuf sops = {sem_num, -1, 0}; if (semop(semid, &sops, 1) == -1) { perror("[ERROR] semop(): "); exit(EXIT_FAILURE); } } void sem_v(int semid, int sem_num) { struct sembuf sops = {sem_num, 1, 0}; if (semop(semid, &sops, 1) == -1) { perror("[ERROR] semop(): "); exit(EXIT_FAILURE); } } int main() { key_t key; int semid; union semun arg; pid_t pid; FILE *fp; int i; key = ftok(PATHNAME, PROID); semid = semget(key, 2, IPC_CREAT | 0666); if (semid == -1) { perror("[ERROR] semget(): "); exit(EXIT_FAILURE); } arg.val = 1; if (semctl(semid, 0, SETVAL, arg) == -1) { perror("[ERROR] semctl(): "); semctl(semid, 0, IPC_RMID); exit(EXIT_FAILURE); } arg.val = 0; if (semctl(semid, 1, SETVAL, arg) == -1) { perror("[ERROR] semctl(): "); semctl(semid, 0, IPC_RMID); exit(EXIT_FAILURE); } fp = fopen("output.txt", "w"); if (fp == NULL) { perror("[ERROR] fopen(): "); semctl(semid, 0, IPC_RMID); exit(EXIT_FAILURE); } pid = fork(); if (pid < 0) { perror("[ERROR] fork(): "); fclose(fp); semctl(semid, 0, IPC_RMID); exit(EXIT_FAILURE); } else if (pid == 0) { for (i = 0; i < 10; i++) { sem_p(semid, 1); time_t current_time = time(NULL); struct tm *local_time = localtime(&current_time); char time_str[26]; strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S", local_time); fputs(time_str, fp); fflush(fp); sem_v(semid, 0); } } else { for (i = 1; i

微信客服

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

帮助反馈 APP下载

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

公众号

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