 
 
 
 ¸®´ª½º ÇÁ·Î±×·¡¸Ó¸¦ À§ÇÑ °¡À̵å
¸®´ª½º ÇÁ·Î±×·¡¸Ó¸¦ À§ÇÑ °¡À̵å
SYSTEM CALL: msgsnd();                                                          
  PROTOTYPE: int msgsnd ( int msqid, struct msgbuf *msgp, int msgsz, int msgflg );
    RETURNS: 0 on success
             -1 on error: errno = EAGAIN (queue is full, and IPC_NOWAIT was asserted)
                                  EACCES (permission denied, no write permission)
                                  EFAULT (msgp address isn't accessable - invalid)
                                  EIDRM  (The message queue has been removed)
                                  EINTR  (Received a signal while waiting to write)
                                  EINVAL (Invalid message queue identifier, nonpositive
                                          message type, or invalid message size) 
                                  ENOMEM (Not enough memory to copy message buffer)
  NOTES:
msgflg ¾Æ±Ô¸ÕÆ®´Â 0À¸·Î ÁöÁ¤µÉ ¼ö ÀÖ´Ù.(ignored), or:
int send_message( int qid, struct mymsgbuf *qbuf )
{
	int	result, length;
	/* ±æÀÌ´Â ±âº»ÀûÀ¸·Î ±¸Á¶Ã¼ÀÇ Å©±â - mtypeÀÇ Å©±â ÀÌ´Ù. */
	length = sizeof(struct mymsgbuf) - sizeof(long);
	if((result = msgsnd( qid, qbuf, length, 0)) == -1)
	{
		return(-1);
	}
	return(result);
}
#include <stdio.h>
#include <stdlib.h>
#include <linux/ipc.h>
#include <linux/msg.h>
main()
{
	int	qid;
	key_t	msgkey;
	struct	mymsgbuf {
		long	mtype;		/* ¸Þ¼¼Áö ŸÀÔ */
		int	request;	/* ÀÛ¾÷ ¿äû ¹øÈ£ */
		double	salary;		/* Á÷¿øÀÇ ±Þ¿© */
	} msg;
	/* IPC Ű °ªÀ» ¹ß»ý½ÃŲ´Ù */
	if(( qid = open_queue( mdgkey)) == -1) {
		perror("open_queue");
		exit(1);
	}
	/* ÀÓÀÇÀÇ Å×½ºÆ® ÀڷḦ ¸Þ¼¼Áö¿¡ ÀûÀçÇÑ´Ù */
	msg.mtype   = 1;	/* ¸Þ¼¼Áö ŸÀÔÀº ¾ç¼ö¿©¾ß ÇÑ´Ù */
	msg.request = 1;	/* ÀÚ·á ¿ä¼Ò #1 */
	msg.salary  = 1000.00;	/* ÀÚ·á ¿ä¼Ò #2 (³ªÀÇ ¿¬°£ ±Þ¿©) */
	/* »½ ³¯·Áº¸³½´Ù */
	if((send_message( qid, &msg )) == -1) {
		perror("send_message");
		exit(1);
	}
}
ÇöÀç ¿ì¸®´Â Å¥¿¡ ¸Þ¼¼Áö¸¦ °¡Áö°í ÀÖÀ¸¸ç, Å¥ÀÇ »óŸ¦ º¸±âÀ§ÇØ ipcs ¸í·É¾î¸¦ »ç¿ëÇØ¶ó. Å¥·Î ºÎÅÍ ½ÇÁ¦·Î ¸Þ¼¼Áö¸¦ Á¶È¸Çϱâ À§ÇÑ Åä·Ð¿¡ µé¾î°¡ º¸ÀÚ. ±×·± ÀÏÀ» ÇϱâÀ§ÇØ msgrcv() ½Ã½ºÅÛ È£ÃâÀ» »ç¿ëÇÑ´Ù.:
  SYSTEM CALL: msgrcv();                                                          
  PROTOTYPE: int msgrcv ( int msqid, struct msgbuf *msgp, int msgsz, long mtype, int msgflg );
    RETURNS: Number of bytes copied into message buffer
             -1 on error: errno = E2BIG  (Message length is greater than msgsz, no MSG_NOERROR)
                                  EACCES (No read permission)
                                  EFAULT (Address pointed to by msgp is invalid)
                                  EIDRM  (Queue was removed during retrieval)
                                  EINTR  (Interrupted by arriving signal)
                                  EINVAL (msgqid invalid, or msgsz less than 0)
                                  ENOMSG (IPC_NOWAIT asserted, and no message exists
                                          in the queue to satisfy the request) 
  NOTES:
msgsz = sizeof(struct mymsgbif) - sizeof(long);
IPC_NOWAIT°¡ flagó·³ ³Ñ°ÜÁö°í À̿밡´ÉÇÑ ¸Þ¼¼Áö°¡ ¾øÀ¸¸é, È£ÃâÇÑ ÇÁ·Î¼¼½º¿¡°Ô ENOMSG¸¦ ¹ÝȯÇÑ´Ù. ±×·¸Áö ¾ÊÀ¸¸é, È£ÃâÇÑ ÇÁ·Î¼¼½º´Â msgrcv() ÆÄ¸®¹ÌÅ͸¦ ¸¸Á·½ÃŰ´Â ¸Þ¼¼Áö°¡ Å¥¿¡ µµÂøÇÒ ¶§±îÁö ±â´Ù¸°´Ù. Ŭ¶óÀÌ¾ðÆ®°¡ ¸Þ¼¼Áö¸¦ ±â´Ù¸®°í ÀÖ´Â µ¿¾È Å¥°¡ Áö¿öÁö¸é, EIDRMÀÌ ¹ÝȯµÈ´Ù. ÇÁ·Î¼¼½º°¡ Â÷´Ü »óÅ¿¡ Àְųª ¸Þ¼¼Áö°¡ µµÂøÇϱ⸦ ±â´Ù¸®´Â µ¿¾È ½ÅÈ£°¡ ÀâÈ÷¸é, EINTR°¡ ¹ÝȯµÈ´Ù.
Å¥·Î ºÎÅÍ ¸Þ¼¼Áö¸¦ Á¶È¸ÇÏ´Â ºü¸¥ wrapper ÇÔ¼ö¸¦ Á¡°ËÇØ º¸ÀÚ.:
int read_message( int qid, long type, struct mymsgbuf *qbuf )
{
        int     result, length;
	/* Å©±â´Â ±âº»ÀûÀ¸·Î ±¸Á¶Ã¼ÀÇ Å©±â - mtypeÀÇ Å©±â ÀÌ´Ù. */
        length = sizeof(struct mymsgbuf) - sizeof(long);        
        if((result = msgrcv( qid, qbuf, length, type,  0)) == -1)
        {
                return(-1);
        }
        
        return(result);
}
Å¥·Î ºÎÅÍ ¸Þ¼¼ÁöÀÇ Á¶È¸°¡ ¼º°øÀûÀ¸·Î ÀÌ·ç¾îÁö¸é, Å¥ ¾È¿¡ ÀÖ´Â ¸Þ¼¼Áö´Â ÆÄ±«µÈ´Ù.
msgflg ¾Æ±Ô¸ÕÆ® ¾ÈÀÇ MSG_NOERROR ºñÆ®´Â ¸î¸îÀÇ ºÎ°¡ÀûÀÎ ´É·ÂÀ» Á¦°øÇÑ´Ù. ¹°¸®ÀûÀÎ ¸Þ¼¼Áö ÀÚ·áÀÇ Å©±â°¡ msgszº¸´Ù Å©°í MSG_NOERROR°¡ »ç¿ëµÇ¸é, ¸Þ¼¼Áö´Â Àß·ÁÁ® ¿ÀÁ÷ msgsz ¹ÙÀÌÆ®¸¸Å¸¸ ¹ÝȯµÈ´Ù. ÀϹÝÀûÀ¸·Î msgrcv() ½Ã½ºÅÛ È£ÃâÀº -1(E2BIG)À» ¹ÝȯÇÏ°í ´ÙÀ½ÀÇ Á¶È¸¸¦ À§ÇØ Å¥ »ó¿¡ ¸Þ¼¼Áö¸¦ ³²°Ü ³õ´Â´Ù. ÀÌ·± µ¿ÀÛÀº ¿ì¸®ÀÇ ¿äû¿¡ ¸¸Á·ÇÏ´Â ¸Þ¼¼Áö°¡ µµÂøÇß´ÂÁö¸¦ ¾Ë¾Æº¸±â À§ÇØ ¿ì¸®°¡ Å¥ ¾ÈÀ» ¿³º¼(peek) ¼ö ÀÖ´Â ¶Ç ´Ù¸¥ wrapper ÇÔ¼ö¸¦ ¸¸µé¾î »ç¿ëÇÒ ¼ö ÀÖ´Ù.
int peek_message( int qid, long type )
{
        int     result, length;
        if((result = msgrcv( qid, NULL, 0, type,  IPC_NOWAIT)) == -1)
        {
                if(errno == E2BIG)
                        return(TRUE);
        }
        
        return(FALSE);
}
Copyright (c) 1996,1997 by Euibeom.Hwang & SangEun.Oh All Rights Reserved 
Email To:Webmaster ,
Another address 
LAST UPDATE Nov 18,1997
Created Nov 17,1997