Pergunta

The broadcasting program is this.I have used 3 different keys and yet only the first listener gets the message.the other's get a message sending error.The keys should identify different user shouldn't it??

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <pthread.h>

struct msgbuf
{
long mtype;
char mtext[140];
}send_buf,recv_buf;

int send_msg_id,send_msg_id1,send_msg_id2;
key_t send_key,send_key1,send_key2;

int main()
{
send_key=ftok("broadcast.c",'A');
send_key1=ftok("broadcast.c",'B');
send_key2=ftok("broadcast.c",'C');
char save[140];
if (send_key==-1)
{
    perror("\nCaller send key error(ftok)");
    exit(1);
}
send_msg_id=msgget(send_key,0666 | IPC_CREAT);
send_msg_id1=msgget(send_key,0555 | IPC_CREAT);
send_msg_id2=msgget(send_key,0777 | IPC_CREAT);
if (send_msg_id==-1)
{   
    printf("\nCaller send msgget error");
    exit(1);
}
printf("\nCALLER:");
while (fgets(send_buf.mtext,sizeof(send_buf.mtext),stdin)!=NULL)
{
    send_buf.mtype=1;
    int len=strlen(send_buf.mtext);
    strcpy(save,send_buf.mtext);
    if (send_buf.mtext[len-1]=='\n');
        send_buf.mtext[len-1]='\0';
    if (msgsnd(send_msg_id,&send_buf,len+1,0)==-1)
        printf("\nMsg sending error\n");
    strcpy(send_buf.mtext,save);
    if (msgsnd(send_msg_id1,&send_buf,len+1,0)==-1)
        perror("\nMsg sending error (send_msg_id1)");
    strcpy(send_buf.mtext,save);
    if (msgsnd(send_msg_id2,&send_buf,len+1,0)==-1)
        perror("\nMsg sending error (send_msg_id2)");
}
int i=0;
while(i<9999)
    i++;
msgctl(send_msg_id,IPC_RMID,NULL);
return 0;
}
Foi útil?

Solução

I noticed a few possible issues while reviewing your code.

First, you call msgget() three times. Each time you use the same key stored in the variable send_key. I believe this code should be:

send_msg_id= msgget(send_key,  0666 | IPC_CREAT);
send_msg_id1=msgget(send_key1, 0555 | IPC_CREAT);
send_msg_id2=msgget(send_key2, 0777 | IPC_CREAT);

Note the changing of send_key to send_key1 in the second line and send_key to send_key3 in the third line.

Second, the values 0666, 0555, and 0777 represent the permissions for the message queue. As described by the msgget() manual page:

Upon creation, the least significant bits of the argument msgflg define the permissions of the message queue. These permission bits have the same format and semantics as the permissions specified for the mode argument of open(2). (The execute permissions are not used.)

Thus, 0666 gives read/write permissions to all, 0555 gives read permissions to all, and 0777 gives read/write permission to all (the execute bit is ignored as noted in the manual page). This probably wasn't your intent. The simplest change would be to make these all 0666 for read/write (or 0600 if you want to restrict access to only your user). 0555 will be a problem as it is read-only. So, the above is further changed to:

send_msg_id= msgget(send_key,  0666 | IPC_CREAT);
send_msg_id1=msgget(send_key1, 0666 | IPC_CREAT);
send_msg_id2=msgget(send_key2, 0666 | IPC_CREAT);

Third, you only check the return codes for the first ftok and msgget calls. You should probably check the return code from all three to ensure there isn't a problem there.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top