40 lines · c
1// RUN: %clang -O1 %s -o %t && %run %t2// UNSUPPORTED: android3#include <assert.h>4#include <errno.h>5#include <stdio.h>6#include <string.h>7#include <sys/msg.h>8 9#define CHECK_STRING "hello, world!"10#define MSG_BUFLEN 0x10011 12int main() {13 int msgq = msgget(IPC_PRIVATE, 0666);14 if (msgq == -1) perror("msgget:");15 assert(msgq != -1);16 17 struct msg_s {18 long mtype;19 char string[MSG_BUFLEN];20 };21 22 struct msg_s msg = {23 .mtype = 1};24 strcpy(msg.string, CHECK_STRING);25 int res = msgsnd(msgq, &msg, MSG_BUFLEN, IPC_NOWAIT);26 if (res) {27 fprintf(stderr, "Error sending message! %s\n", strerror(errno));28 msgctl(msgq, IPC_RMID, NULL);29 return -1;30 }31 32 struct msg_s rcv_msg;33 ssize_t len = msgrcv(msgq, &rcv_msg, MSG_BUFLEN, msg.mtype, IPC_NOWAIT);34 assert(len == MSG_BUFLEN);35 assert(msg.mtype == rcv_msg.mtype);36 assert(!memcmp(msg.string, rcv_msg.string, MSG_BUFLEN));37 msgctl(msgq, IPC_RMID, NULL);38 return 0;39}40