0

I'm doing some coding on message queues.
What is the use of ftoK()?
What is creation of keys?
What is the use of key?
In my code i use this as key "(key_t)1234", the code is running fine. What the meaning of this key "(key_t)1234"? How can i create my own key?

SENDER:

struct mesg_q
{
    char msg_txt[100];
    long msg_typ;
};

int main()
{
    int msgid;
    //key_t msg_key;
    char buffer[100]; 

    struct mesg_q msgq;
    msgid=msgget((key_t)1234, 0666 | IPC_CREAT);

    if(msgid== -1)
    {    
        printf("msgget failed\n");
        return -1;
    }

    while(1)
    { 
        printf("Text Message\n");
        fgets(msgq.msg_txt,100,stdin);

        if(msgsnd(msgid,&msgq,100,0)==-1)
        {
            printf("Send failed\n");
            return -1;
        }  
        else
        {
            printf("Message send\n");
        }    
    }   
}    

RECEIVER:

struct mesg_q
{
    char msg_txt[100];
    long msg_typ;
};

int main()
{
    int msgid;
    char buffer[100];
    long int rec_buff=0;
    key_t key;
    struct mesg_q msgq;
    msgid=msgget((key_t)1234, 0666 | IPC_CREAT);

    if(msgid == -1)
    {
        printf("Msgget failed\n");
        return -1;
    }

    while(1)
    {
        if(msgrcv(msgid,&msgq,100,rec_buff,0)==-1)
        {
            printf("Mesg recv failed\n");
            return -1;
        }
        else
        {
            printf("Mesg Recvd\n");
        }
        printf("Recvd mesg=%s\n",msgq.msg_txt);
    }     
}
Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • 1
    I did quite some formatting for you, before I noticed that you are not exactly a newbie.... Please learn how to do that for yourself at the earliest possible convenient time. – Yunnosch Apr 30 '18 at 08:22
  • Please refer to the spec of ftok() (presumably you refer to e.g. http://man7.org/linux/man-pages/man3/ftok.3.html) to explain more clearly what detail of what is described there you are asking about. At first glance, that spec seems to explain your questions. So you need to be more specific about what is unclear to you. In case you are asking for a tutorial on basic use of this and the situtations in which you need it, please keep in mind that requests for tutorials are off-topic on StackOverflow. – Yunnosch Apr 30 '18 at 08:33

1 Answers1

1

Firstly, I'm doing some coding on message queues. what is the use of ftoK() and creation of keys? what is the use of key? Read the manual page ftok it says

key_t ftok(const char *pathname, int proj_id);

The ftok() function uses the identity of the file named by the given pathname (which must refer to an existing, accessible file) and Today proj_id is an int, but still only 8 bits are used. Typical usage has an ASCII character proj_id, that is why the behavior is said to be undefined when proj_id is zero.

ftok requires that the file to be exists, as it uses the inode information of that file to create the key.

Secondly, what the meaning of this key "(key_t)1234"? check the msgget() first arguement, it's of key_t type and 1234 is not of key_t type, its an integer, so you are typecasting it to key_t type.

In your code you didn't create the key using ftok(), you can create it like.

key_t key;
key = ftok("file.c", 'b'));/*instead of taking random 1234, you are generating key from file based on proj_id */
msgid=msgget(key_t, 0666 | IPC_CREAT);

What is the use of key? key is one of the identifier to identifying the message queue in the kernel apart from message queue id. Run ipcs -q on command line & check.

See this Whats is purpose ftok in Message queues

Achal
  • 11,821
  • 2
  • 15
  • 37
  • what the key will do? is it for performing any operation? –  Apr 30 '18 at 10:50
  • Every message queue associated with it's `key` and `id`. It's for the kernel reference to identify the message queue. – Achal Apr 30 '18 at 10:54