perror("fork"); exit(EXIT_FAILURE); } else if (pid == 0) { // 子进程 struct msgbuf msg; msg.mtype = 1; strcpy(msg.mtext, "Hello from child"); if (msgsnd(msgid, &msg, MSG_SIZE, 0) == -1) { perror("msgsnd"); exit(EXIT_FAILURE); } exit(EXIT_SUCCESS); } else { // 父进程 struct msgbuf msg; int len; if (msgrcv(msgid, &msg, MSG_SIZE, 1, 0) == -1) { perror("msgrcv"); exit(EXIT_FAILURE); } printf("Received message from child: %s\n", msg.mtext); exit(EXIT_SUCCESS); } }