´ÙÀ½ À§·Î ÀÌÀü ¸ñÂ÷ ¸®´ª½º ÇÁ·Î±×·¡¸Ó¸¦ À§ÇÑ °¡À̵å

½Ã½ºÅÛ È£Ãâ:msgsnd() (SYSTEM CALL:msgsnd())

Å¥ÀÇ È®ÀÎÀÚ¸¦ °¡Áö°í ÀÖÀ¸¸é, Å¥ »ó¿¡¼­ ¿©·¯°¡Áö µ¿ÀÛÀ» ¼öÇàÇÒ ¼ö ÀÖ´Ù. Å¥¿¡ ¸Þ¼¼Áö¸¦ Àü´ÞÇϱâ À§ÇØ msgsnd ½Ã½ºÅÛ È£ÃâÀ» »ç¿ëÇÑ´Ù.:


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:

msgsndÀÇ Ã¹¹ø° ¾Æ±Ô¸ÕÆ®´Â ÀÌÀüÀÇ msgget È£Ãâ¿¡ ÀÇÇØ ¹ÝȯµÈ Å¥ È®ÀÎÀÚÀÌ´Ù. µÎ¹ø° ¾Æ±Ô¸ÕÆ®ÀÎ msgp´Â Àç¼±¾ðµÇ°í ÀûÀçµÈ ¸Þ¼¼Áö ¹öÆÛÀÇ Æ÷ÀÎÅÍÀÌ´Ù. msgsz ¾Æ±Ô¸ÕÆ®´Â ¸Þ¼¼Áö ŸÀÔÀÇ ±æÀÌ(4¹ÙÀÌÆ®)¸¦ Á¦¿ÜÇÑ ¸Þ¼¼ÁöÀÇ ¹ÙÀÌÆ® Å©±â¸¦ Æ÷ÇÔÇÑ´Ù.

msgflg ¾Æ±Ô¸ÕÆ®´Â 0À¸·Î ÁöÁ¤µÉ ¼ö ÀÖ´Ù.(ignored), or:

IPC_NOWAIT

¸Þ¼¼Áö Å¥°¡ °¡µæ áÀ¸¸é, ¸Þ¼¼Áö´Â Å¥¿¡ ¾º¿©Áú ¼ö ¾ø°í, ÅëÁ¦±Ç(control)Àº È£ÃâÇÑ ÇÁ·Î¼¼½º¿¡°Ô µ¹¾Æ°£´Ù. ÁöÁ¤µÇÁö ¾Ê¾ÒÀ¸¸é, È£ÃâÇÑ ÇÁ·Î¼¼½º´Â ¸Þ¼¼Áö°¡ ¾º¿©Áú ¼ö ÀÖÀ» ¶§±îÁö ±â´Ù¸± °ÍÀÌ´Ù.
¸Þ¼¼Áö¸¦ º¸³»´Â ¶Ç ´Ù¸¥ wrapper ÇÔ¼ö¸¦ ¸¸µé¾îº¸ÀÚ:


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);
}

ÀÌ ÀÛÀº ÇÔ¼ö´Â ³Ñ°ÜÁø Å¥ È®ÀÎÀÚ(qid)¿¡ ÀÇÇØ °í¾ÈµÈ ¸Þ¼¼Áö Å¥¿¡ ³Ñ°ÜÁø ÁÖ¼Ò(qbuf)¿¡ ÀÖ´Â ¸Þ¼¼Áö¸¦ º¸³»·Á°í ÇÑ´Ù. ¿©±â ¿ì¸®°¡ Áö±Ý±îÁö °³¹ßÇÑ µÎ°³ÀÇ wrapper ÇÔ¼ö¸¦ È°¿ëÇÑ °£´ÜÇÑ Äڵ尡 ÀÖ´Ù.:


#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);
	}
}

¸Þ¼¼Áö Å¥¸¦ ¸¸µé°í ¿¬ ÈÄ¿¡, ¸Þ¼¼Áö ¹öÆÛ¿¡ Å×½ºÆ®¿ë ÀڷḦ ÀûÀçÇϴµ¥ ±îÁö Çß´Ù. (ÀÌÁø Á¤º¸¸¦ º¸³»´Â °Í¿¡ ´ëÇÑ ¿ì¸®ÀÇ °üÁ¡À» ¼³¸íÇϱ⿡´Â ¹®ÀÚ ÀÚ·á°¡ ºÎÁ·ÇßÀ½À» ÁÖ¸ñÇ϶ó) ºü¸¥ send_messageÀÇ È£ÃâÀº ´Ü¼øÈ÷ ¸Þ¼¼Áö¸¦ ¸Þ¼¼Áö Å¥·Î ºÐ¹èÇÑ´Ù.

ÇöÀç ¿ì¸®´Â Å¥¿¡ ¸Þ¼¼Áö¸¦ °¡Áö°í ÀÖÀ¸¸ç, Å¥ÀÇ »óŸ¦ º¸±âÀ§ÇØ 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:

¸í¹éÇÏ°Ô, ù¹ø° ¾Æ±Ô¸ÕÆ®´Â ¸Þ¼¼Áö Á¶È¸ 󸮸¦ ÇÏ´Â µ¿¾È »ç¿ëµÉ Å¥¸¦ ÁöÁ¤Çϴµ¥ »ç¿ëµÈ´Ù. (ÀÏÂïÀÌ msgget¸¦ È£ÃâÇÏ¿© ¹ÝȯµÈ °ÍÀÌ¿©¾ß ÇÔ) µÎ¹ø° ¾Æ±Ô¸ÕÆ®(msgp)´Â Á¶È¸µÈ ¸Þ¼¼Áö¸¦ ÀúÀåÇÒ ¸Þ¼¼Áö ¹öÆÛ º¯¼öÀÇ ÁÖ¼Ò¸¦ ³ªÅ¸³½´Ù. ¼¼¹ø° ¾Æ±Ô¸ÕÆ®(msgsz)´Â mtype ¸â¹öÀÇ ±æÀ̸¦ Á¦¿ÜÇÑ ¸Þ¼¼Áö ¹öÆÛ ±¸Á¶Ã¼ÀÇ Å©±â¸¦ ³ªÅ¸³½´Ù. ´Ù½Ã Çѹø ´ÙÀ½Ã³·³ ½±°Ô °è»êµÉ ¼ö ÀÖ´Ù.:


msgsz = sizeof(struct mymsgbif) - sizeof(long);


³×¹ø° ¾Æ±Ô¸ÕÆ®(mtype)´Â Å¥·Î ºÎÅÍ Á¶È¸µÉ ¸Þ¼¼ÁöÀÇ Å¸ÀÔÀ» ÁöÁ¤ÇÑ´Ù. Ä¿³ÎÀº ŸÀÔ°ú ¸ÅĪµÇ´Â °¡Àå ¿À·¡µÈ ¸Þ¼¼Áö¸¦ Å¥¿¡¼­ ãÀ» °ÍÀÌ°í msgp ¾Æ±Ô¸ÕÆ®¿¡ ÀÇÇØ ÁöÀûµÈ ÁÖ¼Ò¿¡ ±×°ÍÀ» º¹»çÇÏ¿© ¹ÝȯÇÒ °ÍÀÌ´Ù. ÇÑ°¡Áö Ưº°ÇÑ °æ¿ì°¡ Á¸ÀçÇÑ´Ù. mtype ¾Æ±Ô¸ÕÆ®¿¡ 0 °ªÀÌ ³Ñ¾î¿À¸é, ŸÀÔÀ» ¹«½ÃÇÏ°í Å¥¿¡¼­ °¡Àå ¿À·¡µÈ ¸Þ¼¼Áö¸¦ ¹ÝȯÇÑ´Ù.

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);
}

À§¿¡¼­ ´ç½ÅÀº ¹öÆÛÀÇ ÁÖ¼Ò¿Í ±æÀÌ°¡ ºÎÁ·ÇÔÀ» ¾Ë °ÍÀÌ´Ù. ÀÌ·± Ưº°ÇÑ °æ¿ì¿¡ À־, ¿ì¸®´Â È£ÃâÀÌ ½ÇÆÐÇϱ⸦ ¿øÇÑ´Ù. ±×·¸Áö¸¸ ¿ì¸®´Â ¿ì¸®°¡ ¿äûÇÑ Å¸ÀÔ°ú ÀÏÄ¡µÇ´Â ¸Þ¼¼Áö°¡ Á¸ÀçÇÏ´ÂÁö¸¦ ³ªÅ¸³»´Â E2BIGÀÇ ¹ÝȯÀ» È®ÀÎÇÑ´Ù. wrapper ÇÔ¼ö´Â ¼º°øÇϸé TRUE¸¦, ±×·¸Áö¾ÊÀ¸¸é FALSE¸¦ ¹ÝȯÇÑ´Ù. ¶ÇÇÑ ÀÌÀü¿¡ ¾ð±ÞÇß´ø Â÷´Ü µ¿ÀÛ(Blocking behavior)À» ¸·±âÀ§ÇÑ IPC_NOWAIT flagÀÇ »ç¿ë¿¡ ÁÖ¸ñÇ϶ó.


ÀÌÀü:½Ã½ºÅÛ È£Ãâ:msgget() (SYSTEM CALL:msgget()) ´ÙÀ½:½Ã½ºÅÛ È£Ãâ:msgctl() (SYSTEM CALL:msgctl())

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