0% found this document useful (0 votes)
14 views32 pages

Lect 06

Uploaded by

venkatagopinath4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views32 pages

Lect 06

Uploaded by

venkatagopinath4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 32

CSCI7645 - Systems Programming

Lecture 6: Message Queues


Instructor:Yongming Tang, Ph. D.
School of Computer Sciences and Engineering
Fairleigh Dickinson University
Message Queues
• Introduction
– Message queues are a tool for sending data
blocks(messages) between processes.
– These messages are placed in a predefined message
structure.
– The process generating the message specifies its type
and places the message in a system-maintained
message queue.
– Processes accessing the message queue can use the
message type to selectively read messages of specific
types in a FIFO manner.
– Message queues provide the user with a means of
multiplexing data from multiple producers.
11/08/24 Dr. Tang, FDU 2
Message Queues
• IPC Facilities
– Message queue is one of the three IPC facilities.
– The other two are semaphore and shared memory.
– Each IPC facility has a creator, owner and access
permissions established when the IPC is created.
– At a system level, information on IPCs can be
obtained with the ipcs command.
– IPCs exist and maintain their contents even after
the process that created them has terminated.
– An IPC facility can be removed by its owner.
11/08/24 Dr. Tang, FDU 3
Message Queues
• Some ipcs outputs
alpha.fdu.edu> ipcs -b
Message Queues:
T ID KEY MODE OWNER GROUP QBYTES
q 0 0x41d00759 --rw------- root system 16384
Shared Memory:
T ID KEY MODE OWNER GROUP SEGSZ
m 640 0xc716b8c --rw-r----- oracle8 dba8 8388608
Semaphores:
T ID KEY MODE OWNER GROUP NSEMS
s 0 0x41d00759 --ra------- root system 1
s 1 0x61d00f1d --ra-ra-ra- levine faculty 5
11/08/24 Dr. Tang, FDU 4
Message Queues

• Mode Indicator for an IPC

R Waiting on an msgrcv
S Waiting on an msgsnd
D Shared memory segment has been removed.
Will disappear when last process detaches it.
C Shared memory segment will be cleared when
first attached.
- Flag not set.

11/08/24 Dr. Tang, FDU 5


Message Queues
• IPC System Calls for Message Queues
– These are used to create an MQ facility and
manipulate MQ information.

msgget Allocate an MQ, gain access to an MQ.


msgctl Control an MQ, obtain/modify status
information, remove an MQ.
msgsnd MQ operations: send/receive message.
msgrcv

11/08/24 Dr. Tang, FDU 6


Message Queues
• System Call ftok()
//ftok(“/tmp”, 5), ftok(“/tmp”, ‘b’)
Include Files <sys/types.h> <sys/ipc.h>
Summary key_t ftok(const char *path, int id);
Return Success Failure Sets errno
key value for an -1
IPC get system call

11/08/24 Dr. Tang, FDU 7


Message Queues
• System Call ftok()
– The ftok() function requires two arguments. The
first, path, is a reference to an existing
accessible file.
– The second argument for ftok(), id, is an integer
value most commonly represented by a single
character.
– The value returned by a successful call to ftok()
is of defined type key_t. The key value is
usually for the get system calls.

11/08/24 Dr. Tang, FDU 8


Message Queues
• System Call msgget()

Include Files <sys/types.h> <sys/ipc.h> <sys/msg.h>


Summary int msgget(key_t key, int msgflg);
Return Success Failure Sets errno
Non-negetive -1 Yes
message queue id
associated with key

11/08/24 Dr. Tang, FDU 9


Message Queues
• System Call msgget()
– The 1st argument, known as the key value, is
used by the msgget() to generate the IPC
identifier.
– The 2nd argument common to all of the IPC get
system call is the message flag. The message
flag, an integer value, is used to set the access
permissions when IPC facility is created.
– Permissions are only write(alter) and read (see
Table 6.4). No execute bit is relevant for IPC
facilities.

11/08/24 Dr. Tang, FDU 10


Message Queues
• System Call msgget()
– A new message queue is created if the defined
constant IPC_PRIVATE is used as the key
argument, or if the IPC_CREAT flag is ored with
the access permissions and no previously existing
message queue is associated with the key value.
– If IPC_CREAT is specified and the message
queue already exists, msgget will not fail but will
return the message queue id associated with the
key value.

11/08/24 Dr. Tang, FDU 11


Message Queues
• System Call msgget()
– IPC_PRIVATE: Instruct the get system call to
create an IPC facility with a unique identifier.
An IPC facility created with IPC_PRIVATE is
normally shared between related processes.
– IPC_CREAT: Direct the get system call to
create an IPC facility if one does not presently
exist. When specifying IPC_CREAT, if the
facility is already present, and it was not
created using IPC_PRIVATE, its IPC id is
returned.
11/08/24 Dr. Tang, FDU 12
Message Queues

• Generate a Message Queue


key = ftok(“.”, u_char);
if ((mid[I] = msgget(key, IPC_CREAT | 0660)) == -1) {
perror(“Queue creation failed”);
exit(1);
}

11/08/24 Dr. Tang, FDU 13


Message Queues
• Program Example 1
#include <stdio.h>
#include <unistd.h> int
#include <string.h> main() {
#include <stdlib.h> key_t key;
#include <sys/types.h> long i=0;
#include <sys/ipc.h> int intValue, mid;
#include <sys/msg.h> char intStr[3];
struct msgbuf { struct msgbuf value;
long mtype; key = ftok("/", 'q');
char mtext[1];
};
11/08/24 Dr. Tang, FDU 14
if ((mid = msgget(key, IPC_CREAT | 0666)) == -1) {
perror("MS Queue Creation");
exit(1);
}
for(i=0; i<100; i++) {
srand(getpid()+i);
intValue =(random()*99)/RAND_MAX + 1;
sprintf(intStr, "%d", intValue);
if ((intValue%2)==0)
value.mtype = 2;
else
value.mtype = 1;
strcpy(value.mtext, intStr);
if (msgsnd(mid, &value, sizeof(value), 0) == -1) {
perror("Message send");
exit(2);
}
}
}
11/08/24 Dr. Tang, FDU 15
Message Queues
• Program Example 2
#include <stdio.h>
#include <unistd.h> int
#include <string.h> main() {
#include <stdlib.h> key_t key;
#include <sys/types.h> long i=0;
#include <sys/ipc.h> int mid, n;
#include <sys/msg.h> struct msgbuf value;
struct msgbuf { key = ftok("/", 'q');
long mtype;
char mtext[1];
};
11/08/24 Dr. Tang, FDU 16
if ((mid = msgget(key, IPC_CREAT | 0666)) == -1) {
perror("MS Queue Creation");
exit(1);
}

for(i=1; i<=10; i++) {


if ((n=msgrcv(mid, &value, sizeof(value), 1, 0)) == -1) {
perror("MS Queue Creation");
exit(2);
}
printf("%s\t", value.mtext);
}
//msgctl(mid, IPC_RMID, (struct msqid_ds *) 0);
}

11/08/24 Dr. Tang, FDU 17


Message Queues

• System Call msgsnd()


Include Files <sys/types.h> <sys/ipc.h> <sys/msg.h>
Summary int msgsnd(int msqid,
const void *msgp,
size_t msgsz,
int msgflg);
Return Success Failure Sets errno
0 -1 Yes

11/08/24 Dr. Tang, FDU 18


Message Queues
• System Call msgsnd()
– The 1st argument is a valid message queue id
returned from a prior msgget system call.
– The 2nd argument is a pointer to the message
to be sent.
– The 3rd argument is the size of the message to
be sent.
– The 4th argument is used to indicate what
action should be taken if system limits for the
message queue have been reached.

11/08/24 Dr. Tang, FDU 19


Message Queues
• System Call msgsnd() – value for the flag
– The values for the flag could be IPC_NOWAIT
or 0.
– If set to IPC_NOWAIT and a system limit has
been reached, msgsnd will not send the
message and will return to the calling process
immediately with errno set to EAGAIN.
– If set to 0, msgsnd will block until the limit is
no longer at system maximum(at that time the
message is sent), the message queue is
removed, or the calling process catches a
signal.
11/08/24 Dr. Tang, FDU 20
Message Queues

• System Call msgrcv()


Include Files <sys/types.h> <sys/ipc.h> <sys/msg.h>
Summary int msgrcv(int msqid,
void *msgp,
size_t msgsz,
long msgtyp,
int msgflg);
Return Success Failure Sets errno
No. of bytes received -1 Yes

11/08/24 Dr. Tang, FDU 21


Message Queues
• System Call msgrcv() - arguments
– The 1st argument is the message queue id.
– The 2nd one is a pointer to the location where
the received message will be placed. The
receiving location should have as its first field a
long integer to accommodate the message type
information.
– The 3rd one is the maximum size of the message
in bytes. This value should be equal to the
longest message to be received.
– The 4th one is the type of the message to be
received.
11/08/24 Dr. Tang, FDU 22
Message Queues
• System Call msgrcv() – arguments
– The last argument is used to indicate what
actions should be taken if a given message type
is not in the message queue, or if the message
to be retrieved is larger in size than the number
of bytes indicated by msgsz.
– One action is IPC_NOWAIT indicating that it
should not block if the requested message type
is not in the message queue.
– The other is MSG_NOERROR. It allows
msgrcv to silently truncate messages to msgsz
bytes if they are found to be too long.
11/08/24 Dr. Tang, FDU 23
Message Queues

• System Call msgrcv() – message types


– When msgtyp is 0, msgrcv retrieves the first
message of any msgtyp.
– When msgtyp is >0, msgrcv retrieves the first
message equal to msgtyp.
– When msgtyp is <0, msgrcv retrieves the first
message of the lowest type <= to absolute value of
msgtyp. 2

11/08/24 Dr. Tang, FDU 24


Message Queues
• Generate a Message Queue
– When a message queue is created, a system
message queue data structure called msqid_ds is
generated. //struct msqid_ds AnotherMsq;
– This structure, maintained by the system, is
defined in the header file<sys/msg.h>, see details
on page 152.
– An individual message in the queue is a structure
of type msg which is defined in the header file
<sys/msg.h>. These individual messages are
placed in a linked list form by the system.

11/08/24 Dr. Tang, FDU 25


Message Queues
• Message Structure
struct msg {
struct msg *msg_next; /* ptr to next message on q*/
long msg_type; /* message type */
ushort msg_ts; /* message text size */
short msg_spot; /* message text map address*/ };
struct msqid_ds {
struct ipc_perm msg_perm; /*operation permission struct*/
struct msg *msg_first; /*ptr to 1st message on q*/
struct msg *msg_last; /*ptr to last message on q*/
……;
};
See Fig. 6.4 on Page 153
11/08/24 Dr. Tang, FDU 26
Message Queues

• System Call msgctl()


Include Files <sys/types.h> <sys/ipc.h> <sys/msg.h>
Summary int msgctl(int msqid, int cmd,
/*struct msqid_ds *buf*/);
Return Success Failure Sets errno
0 -1 Yes

11/08/24 Dr. Tang, FDU 27


Message Queues
• System Call msgctl()
– This system call is used to examine and modify
the ownership and access permissions.
– This system call will reference the message
queue indicated by the msqid argument.
– The value of the cmd argument is used to
indicate the action that msgctl should take.
– Argument three provides the address of the
storage location of a message queue. The
operations will be applied on it.
11/08/24 Dr. Tang, FDU 28
Message Queues
• System Call msgctl()
– There are 3 values the cmd may take.
– IPC_STAT: Return the current values for each
member of msqid_ds data structure(including the
permission structure).
– IPC_SET: Used for modifying a limited number
of msqid_ds member values. These members are
msg_perm.uid, msg_perm.gid, msg_perm.mode,
and msg_qbytes.
– IPC_RMID: Destroy the contents of the IPC and
remove it from the system.

11/08/24 Dr. Tang, FDU 29


Message Queues
• System Call msgctl()
int mid;
key_t key;
struct msqid_ds buf;
key = ftok(“.”, ‘z’);
if ((mid=msgget(key, IPC_CREAT | 0660)) == -1) {
perror(“Queue create failure”);
exit(1);
}
msgctl(mid, IPC_STAT, &buf);
……; buf. msg_qbytes=2048;
msgctl(mid, IPC_SET, &buf);
……
msgctl(mid, IPC_RMID, (struct msqid_ds *) 0);
11/08/24 Dr. Tang, FDU 30
Message Queues
• Applications – A Client/Server Example
– The client process will obtain input from the
keyboard and send it, via a message queue, to
the server. Then it will read the processed
message by server from the queue and display
the message to standard output.
– The server will read the message from the
queue, process the message by converting all
alphabetic text in the message to uppercase and
place the message back in the queue for the
client to read.
11/08/24 Dr. Tang, FDU 31
Message Queues
• Program Sample(6.4 & 6.5)
if (msgsnd(mid, &msg, sizeof(msg), 0) == -1) {
perror(“Client: msgsend failed”);
exit(5);
}
if ((n=msgrcv(mid, &msg, sizeof(msg), cli_pid, 0)) != -1)
write(fileno(stdout), msg.buffer, strlen(msg.buffer));
……;
msgsnd(mid, &msg, 0,0);

n=msgrcv(mid, &msg, sizeof(msg), cli_pid, 0))


……;
msgsnd(mid, &msg, sizeof(msg), 0)
11/08/24 Dr. Tang, FDU 32

You might also like