Witam chcę napisać prosty program w którym w procesie np potomnym wysyłm wiadomości na kolejkę komunikatów, a w procesie macierzystym je z tamtąd odbieram.
Napisałem coś takiego:
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
struct msgbuf{
long type;
char text[1024];
} my_msg;
int main(){
int mid = mid=msgget(0x1235, 0600 | IPC_CREAT);
if(fork==0) {
while(1){
printf("wypelniam dane\n");
strcpy(my_msg.text, "Text");
my_msg.type=5;
printf("wysylam\n");
msgsnd(mid, &my_msg, strlen(my_msg.text)+1,0);
printf("wyslalem");
}
} else {
while(1) {
printf("odbieram\n");
msgrcv(mid,&my_msg, 1024, 5, 0);
printf("odebralem\n");
printf("%s \n", my_msg.text);
}
}
return 0;
}
Czy mógłby mnie ktoś poprawić, wytłumaczyć dlaczego program nie działa tak jakbym tego oczekiwał?