Monday, July 11, 2011

Message text Transfor using IPC Message Q's

/* msg text trans using message Qs (IPC )<F3><F2

/* msg_tra_q.c compiled by RVS RNEC */

    # include <stdio.h>
    # include <string.h>
    # include <sys/stat.h>
    # include <sys/msg.h>
    # include <sys/ipc.h>
    # include <sys/types.h>
    # include <fcntl.h>
   
    struct message
    {
    long msg_type;
    char msg_data[1024];
    };
    struct message msg_obj1={100,"I Love Unix"};
    struct message msg_obj2={100,"M"};

    int main(int argc,char*argv[])
    {
    int msg_id,x,y;
    //creating msg q id
    if((msg_id=msgget(IPC_PRIVATE,IPC_CREAT/0644))==-1)
    perror("\nUnable to get message id...");
    else
    printf("\nmsgid=%d",msg_id);

   

    /*writing message to the q */
    if((x=msgsnd(msg_id,&msg_obj1,strlen(msg_obj1.msg_data),IPC_NOWAIT))==-1)
    perror("\nUnable to send message...");
    else
    printf("\nSend Msg Success : return 0");

   
    /* rec message from q*/
    if((y=msgrcv(msg_id,&msg_obj2,12,100,MSG_NOERROR))==-1)
    perror(":- msgrcv error...");
    else
    printf("\nRec Bytes : %d",y);

   
    printf("\nThe Message :-\n%s",msg_obj2.msg_data);
    return 0;   
       
    }


       
       

No comments:

Post a Comment