/* * O processo pai envia uma mensagem e o processo filho a recebe. */ #include #include #include #include #include #include #define MY_MSGTYPE 1 typedef struct msgbuf { long mtype; /* type of message */ char mtext[1]; /* message text .. why is this not a ptr? */ } Msgbuf; #define MSG_SIZE sizeof(Msgbuf) Msgbuf inbuf, outbuf; int main() { int queue_id; if ((queue_id = msgget(IPC_PRIVATE, IPC_CREAT | 0700)) == -1) perror("Erro em msgget"); if (fork()) { /* Processo pai */ outbuf.mtype = MY_MSGTYPE; outbuf.mtext[0] = '#'; if (msgsnd(queue_id, &outbuf, MSG_SIZE, 0) == -1) perror("Erro em msgsnd"); printf("Mensagem enviada = %c\n", outbuf.mtext[0]); } else { /* Processo filho */ if (msgrcv(queue_id, &inbuf, MSG_SIZE, MY_MSGTYPE, MSG_NOERROR) == -1) perror("Erro em msgrcv"); printf("Mensagem recebida = %c\n", inbuf.mtext[0]); } return 0; }