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

作业社区

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

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

慕先生4541263 的学生作业:

#include #include #include #include #include #include #include #include #include #include #include #define PATHNAME "." #define PROID 10 #define SHM_SIZE 1024 int main(int argc, int argv[]) { if (argc != 2) { exit(EXIT_FAILURE); } char *filename = argv[1]; key_t key; int shmid, fd; void *addr; ssize_t rbytes; key = ftok(PATHNAME, PROID); if (key == -1) { perror("[ERROR] ftok(): "); exit(EXIT_FAILURE); } shmid = shmget(key, SHM_SIZE, IPC_CREAT | 0666); if (shmid == -1) { perror("[ERROR] shmget(): "); exit(EXIT_FAILURE); } addr = shmat(shmid, NULL, 0); if (addr == (void *)-1) { perror("[ERROR] shmat(): "); exit(EXIT_FAILURE); } fd = open(filename, "O_RDNOLY"); if (fd == -1) { perror("[ERROR] open(): "); exit(EXIT_FAILURE); } rbytes = read(fd, addr, SHM_SIZE); if (rbytes == -1) { perror("[ERROR] read(): "); close(fd); shmdt(addr); shmctl(shmid, IPC_RMID, NULL); exit(EXIT_FAILURE); } sleep(5); close(fd); shmdt(addr); shmctl(shmid, IPC_RMID, NULL); printf("file transfer completed\n"); return 0; } receiver.c #include #include #include #include #include #include #include #include #include #include #include #define PATHNAME "." #define PROID 10 #define SHM_SIZE 1024 int main(int argc, int argv[]) { if (argc != 2) { exit(EXIT_FAILURE); } char *filename = argv[1]; key_t key; int shmid, fd; void *addr; ssize_t wbytes; key = ftok(PATHNAME, PROID); if (key == -1) { perror("[ERROR] ftok(): "); exit(EXIT_FAILURE); } shmid = shmget(); if (shmid == -1) { perror("[ERROR] shmget(): "); exit(EXIT_FAILURE); } addr = shmat(shmid, NULL, 0); if (addr == (void *)-1) { perror("[ERROR] shmat(): "); exit(EXIT_FAILURE); } fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666); if (fd == -1) { perror("[ERROR] open(): "); shmdt(addr); exit(EXIT_FAILURE); } wbytes = write(fd, addr, SHM_SIZE); if (wbytes == -1) { perror("[ERROR] write(): "); close(fd); shmdt(addr); exit(EXIT_FAILURE); } close(fd); shmdt(addr); printf("file receive completed\n"); return 0; }

得分 100
学习任务

慕先生4541263 的学生作业:

#include #include #include #include #include #include #include #include #include #define PATHNAME "." #define PROID 10 #define MAX_MSG_SIZE 100 #define MTYPE_A 100 #define MTYPE_B 200 typedef struct msgbuf { long mtype; char mtext[MAX_MSG_SIZE]; } Message; void process_a(int msgid) { Message message; while (1) { if (msgrcv(msgid, &message, MAX_MSG_SIZE, MTYPE_A, 0) == -1) { perror("[ERROR] msgrcv():"); exit(EXIT_FAILURE); } if (strcmp(message.mtext, "quit") == 0) { break; } printf("pid(%d) receive the message: %s\n", getpid(), message.mtext); } printf("pid(%d) exit\n", getpid()); } void process_b(int msgid) { Message message; while (1) { if (msgrcv(msgid, &message, MAX_MSG_SIZE, MTYPE_B, 0) == -1) { perror("[ERROR] msgrcv():"); exit(EXIT_FAILURE); } if (strcmp(message.mtext, "quit") == 0) { break; } printf("pid(%d) receive the message: %s\n", getpid(), message.mtext); } printf("pid(%d) exit\n", getpid()); } int main() { key_t key; int msgid; pid_t pid_a, pid_b; Message message; key = ftok(PATHNAME, PROID); if (key == -1) { perror("[ERROR] ftok():"); exit(EXIT_FAILURE); } msgid = msgget(key, IPC_CREAT | 0666); if (msgid == -1) { perror("[ERROR] msgget():"); exit(EXIT_FAILURE); } pid_a = fork(); if (pid_a < 0) { perror("[ERROR] fork():"); msgctl(msgid, IPC_RMID, NULL); exit(EXIT_FAILURE); } else if (pid_a == 0) { process_a(msgid); exit(EXIT_SUCCESS); } pid_b = fork(); if (pid_b < 0) { perror("[ERROR] fork():"); kill(pid_a, SIGKILL); msgctl(msgid, IPC_RMID, NULL); exit(EXIT_SUCCESS); } else if (pid_b == 0) { process_b(msgid); exit(EXIT_SUCCESS); } char buffer[MAX_MSG_SIZE]; while (1) { printf("please input messages: "); fgets(buffer, sizeof(buffer), stdin); buffer[strcspn(buffer, "\n")] = 0; message.mtype = MTYPE_A; strcpy(message.mtext, input); if (msgsnd(msgid, &message, strlen(message.mtext) + 1, 0) == -1) { perror("[ERROR] msgsnd():"); break; } message.mtype = MTYPE_B; strcpy(message.mtext, input); if (msgsnd(msgid, &message, strlen(message.mtext) + 1, 0) == -1) { perror("[ERROR] msgsnd():"); break; } if (strcmp(buffer, "quit") == 0) { break; } } waitpid(pid_a, NULL, 0); waitpid(pid_b, NULL, 0); msgctl(msgid, IPC_RMID, NULL); return 0; }

微信客服

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

帮助反馈 APP下载

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

公众号

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