Ex. No.: 7
|
INTER
PROCESS COMMUNICATION USING MESSAGE QUEUE
|
AIM:
To write a C
program to develop Inter Process
Communication application using
Message queue.
PROBLEM DESCRIPTION:
Create a server program to create
a message queue using msgget( ) system call. Create a dynamic structure
containing the string and its type. Create a client program which reads a set
of strings and stores them in the queue using msgsnd( ) system call. The server
program reads the strings from the message queue using msgrcv( ) system call.
It computes the number of vowels in the strings of first client.
SYSTEM
CALLS USED:
Ø msgget(
) –
to initialize
a new message queue
Synopsis
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
int msgget(key_t key,
int msgflg);
Description
The function returns the message queue identifier associated with the value of the key argument. A new message queue is created if key has the value IPC_PRIVATE or key isn’t IPC_PRIVATE, no message queue with the given key key exists, and IPC_CREAT is asserted in msgflg (i.e., msgflg & IPC_CREAT is nonzero).
The presence in msgflg of the fields IPC_CREAT and IPC_EXCL plays the same role, with respect to the existence of the message queue, as the presence of O_CREAT and O_EXCL in the mode argument of the open (2) system call: i.e. the msgget function fails if msgflg asserts both IPC_CREAT and IPC_EXCL and a message queue already exists for key.
Upon creation, the lower 9 bits of the argument msgflg define the access permissions of the message queue. These permission bits have the same format and semantics as the access permissions parameter in open (2) or create (2) system calls. (The execute permissions are not used).
If the message queue already exists the access permissions are verified, and a check is made to see if it is marked for destruction. If successful, the return value will be the message queue identifier (a nonnegative integer), otherwise -1 with errno indicating the error.
Ø
msgsnd(
), msgrcv( ) –message operations
Synopsis
ssize_t msgrcv(int msqid,
struct msgbuf *msgp, size_t msgsz,
long msgtyp, int msgflg);
int msgsnd(int msqid,
struct msgbuf *msgp, size_t msgsz,
int msgflg);
Description
To send or receive a message, the calling process allocates a structure of the following general form:
struct msgbuf {
long mtype; /* message type, must be > 0 */
char mtext[1]; /* message data */
};
The mtext field is an array (or other structure) whose size is specified by msgsz, a non-negative integer value. Messages of zero length (i.e., no mtext field) are permitted. The mtype field must have a strictly positive integer value that can be used by the receiving process for message selection.
The calling process must have write permission to send and read permission to receive a message on the queue.
The msgsnd system call appends a copy of the message pointed to by msgp to the message queue whose identifier is specified by msqid.
If sufficient space is available on the queue, msgsnd succeeds immediately. (The queue capacity is defined by the msg_bytes field in the associated data structure for the message queue. During queue creation this field is initialized to MSGMNB bytes, but this limit can be modified using msgctl.) If insufficient space is available on the queue, then the default behavior of msgsnd is to block until space becomes available. If IPC_NOWAIT is asserted in msgflg then the call instead fails with the error EAGAIN.
Upon successful completion the message queue data structure is updated as follows:
§ msg_lspid is set to the process ID of the calling process.
The argument msgtyp specifies the type of message requested as follows:
§ If msgtyp is 0, then the first message in the queue is read.
§ If msgtyp is greater than 0, then the first message on the queue of type msgtyp is read, unless MSG_EXCEPT was asserted in msgflg, in which case the first message on the queue of type not equal to msgtyp will be read.
§ If msgtyp is less than 0, then the first message on the queue with the lowest type less than or equal to the absolute value of msgtyp will be read.
The msgflg argument asserts none, one or more (or-ing them) of the following flags:
§ IPC_NOWAIT For immediate return if no message of the requested type is on the queue. The system call fails with errno set to ENOMSG.
§ MSG_EXCEPT Used with msgtyp greater than 0 to read the first message on the queue with message type that differs from msgtyp.
§ MSG_NOERROR To truncate the message text if longer than msgsz bytes.
Upon successful completion the message queue data structure is updated as follows:
§ msg_lrpid is set to the process ID of the calling process.
§ msg_qnum is decremented by 1.
§ msg_rtime is set to the current time.
On a failure both functions
return -1 with errno indicating the error.
Ø msgctl(
) – message control operations
Synopsis
int msgctl(int msqid, int cmd,
struct msqid_ds *buf);
Description
This function performs the
control operation specified by cmd on the message queue with identifier msqid. Legal values for cmd are:
- IPC_STAT:
Copy info from the message queue data structure associated with msqid
into the structure pointed to by buf. The caller must have read permission on
the message queue.
- IPC_SET: Write the values of some members of
the msqid_ds structure
pointed to by buf to the message queue data structure, updating
also its msg_ctime member.
The following members of the structure can be updated:
msg_perm.uid
msg_perm.gid
msg_perm.mode /* only lowest 9-bits */
msg_qbytes
- IPC_RMID:
Immediately remove the
message queue and its associated data structure, awakening all
waiting reader and writer
processes (with an error
return and errno set to EIDRM). The calling process must have
appropriate (probably, root) privileges or its effective user-ID must be
either that of the creator or owner of the message queue.
On success, the return value
will be 0, otherwise -1 with errno indicating the error.
ipcs Commands:-
The ipcs command
can be used to obtain the status of all System IPC objects.
ipcs –q : Show only message queues
ipcs –s : Show only semaphores
ipcs –m : Show only shared memory
ipcs –help : Additional arguments
The ipcs command
is a very powerful tool which provides a peek into the kernel's storage
mechanisms for IPC objects.
//SERVER PROGRAM
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
#include <string.h>
struct msgbuf {
int mtype;
char mtext[40];
}*msgp;
int main()
{
char
str[40],rstr[40];
int
msgid,i,l,count,flag=0;
int t;
msgid=msgget(512, 0700 | IPC_CREAT | IPC_EXCL);
printf("\nServer Message ID : %d \n",
msgid);
if(msgid==-1)
{
printf("Message Queue cannot be created\n");
exit(0);
}
msgp=((struct msgbuf*)malloc(sizeof(struct
msgbuf)));
while(1)
{
count=0;
msgrcv(msgid,msgp,sizeof(struct msgbuf),0,0);
l=strlen(msgp->mtext);
t = msgp -> mtype;
strcpy(str,msgp->mtext);
printf("\nProcessing Client1 data\n\n");
if
(strcmp(str,"NULL")==0)
flag++;
else
{
for(i=0;i<l;i++)
if((str[i]=='a')||(str[i]=='e')
||(str[i]=='i')||(str[i]=='o')
||(str[i]=='u'))
count++;
printf("The no. of vowels in the string %s is
%d\n\n",str,count);
} //switch
if (flag==1)
break;
}
msgctl(msgid,IPC_RMID,NULL);
}
[jeyakumar@localhost ~]$ cc ser.c
[jeyakumar@localhost ~]$ ./a.out
Server Message ID : 0
Processing Client1 data
The no. of vowels in the string apple is 2
Processing Client1 data
The no. of vowels in the string orange is 3
Processing Client1 data
[jeyakumar@localhost ~]$
/* CLIENT PROGRAM
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct msgbuf
{
int mtype;
char mtext[40];
}*msgp;
int main()
{
int msgid;
char str[40];
msgid=msgget(512,0700);
printf("\nClient1
msgID : %d \n", msgid);
if(msgid==-1)
{
printf("Message Queue does not exist\n");
exit(0);
}
msgp=((struct
msgbuf *)malloc(sizeof(struct msgbuf)));
printf("\nClient1 Data - To Count Vowels\n\n");
printf("Enter String to end type NULL\n\n");
do
{
scanf("%s",str);
strcpy(msgp->mtext,str);
msgp->mtype=1;
msgsnd(msgid,msgp,sizeof(struct msgbuf),0);
}
while(strcmp(str,"NULL")!=0);
}
"cl1.c" 36L, 757C written
[jeyakumar@localhost ~]$ cc cl1.c
[jeyakumar@localhost ~]$ ./a.out
Client1 msgID : 0
Client1 Data - To Count Vowels
Enter String to end type NULL
apple
orange
NULL
[jeyakumar@localhost ~]$
RESULT:
Thus the C programs were written
to implement the inter Process Communication application using message queue.
The program was executed and its output was verifed successfully.
No comments:
Post a Comment