Opa, tenho uma dúvida quanto ao Message Queue, como faço para me comunicar com os processos filhos em um mesmo código? Tenho que abrir as MQ denovo a cada processo? Segue meu email, em ingles, já que perguntei em mais de um forum. Obrigadão!!! --- Hey all, I'm having some troubles using Message Queue, could you please help me? I have to send a Message from the root process to it's children after inserting a char (if char == number, send to child 1, if char == letter, send to child 2). I'm not getting the buffer back in the children process, what am i doing wrong? #include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <mqueue.h>
int main() {
int pid1;
int pid2;
mqd_t mq;
struct mq_attr attr;
char buffer;
attr.mq_flags = 0;
attr.mq_maxmsg = 1;
attr.mq_msgsize = 1024;
attr.mq_curmsgs = 0;
pid1 = fork();
if (pid1 == 0) {
mq = mq_open("/test_queue", O_CREAT | O_RDONLY, 0644, &attr);
while(mq_receive(mq, &buffer, 1024, NULL) != 1024);
if ((char)*buffer >= 'A' && (char)*buffer <= 'z') {
printf("1 - %c\n", toupper((char)*buffer));
}
}
else if (pid1 > 0) {
pid2 = fork();
if (pid2 == 0 && pid1 > 0) {
mq = mq_open("/test_queue", O_CREAT | O_RDONLY, 0644, &attr);
while(mq_receive(mq, &buffer, 1024, NULL) != 1024);
if(isdigit((char)*buffer)) {
printf("2 - %c\n", (char)*buffer);
}
}
else if (pid2 > 0) {
mq = mq_open("/test_queue", O_WRONLY);
printf("Inform a char:\n");
scanf("%c", &buffer);
if (isdigit((char)*buffer)) {
//SEND TO CHILD 1
mq_send(mq, &buffer, 1024, 0);
}
else if ((char)*buffer >= 'A' && (char)*buffer <= 'z') {
//SEND TO CHILD 2
mq_send(mq, &buffer, 1024, 0);
}
else {
printf("Ignored.\n");
exit(0);
}
}
else {
printf("Err.\n");
}
}
else {
printf("Err.\n");
}
wait();
mq_close(mq);
mq_unlink("/test_queue");
return 0;
} Thanks very much, Tiago