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

msgtool:»óÈ£ÀÛ¿ë ¸Þ¼¼Áö Å¥ Á¶Á¾ÀÚ (An interactive message queue manipulator)

½±°Ô ÀÌ¿ëÇÒ ¼ö ÀÖ´Â Á¤È®ÇÑ ±â¼úÀûÀÎ Á¤º¸¸¦ °®´Â Á÷Á¢ÀûÀÎ ÀÌÀÍÀ» ¼Ò¼öÀÇ »ç¶÷¸¸ÀÌ °¡Áø´Ù´Â °ÍÀº ºÎÀÎÇÒ ¼ö ¾ø´Ù. ¾î¶² ¿ä¼ÒµéÀº »õ·Î¿î ¿µ¿ªÀÇ ¹è¿ò°ú Ž±¸¸¦ À§ÇÑ ¸ÚÁø ±â±³(mechanism)¸¦ Á¦°øÇÑ´Ù. °°Àº ¸Æ¶ô¿¡¼­, ¾î¶² ±â¼úÀûÀÎ Á¤º¸¸¦ µû¸£´Â ½Ç¼¼°èÀÇ ¿¹Á¦¸¦ °¡Áö´Â °ÍÀº ÇÁ·Î¼¼½º¸¦ ¹è¿ì´Â °ÍÀ» »¡¸®ÇÏ°í °­È­½Ãų °ÍÀÌ´Ù.

Áö±Ý±îÁö, ¾ð±ÞµÈ À¯¿ëÇÑ ¿¹Á¦µéÀº ¸Þ¼¼Áö Å¥¸¦ Á¶Á¾ÇÏ´Â wrapper ÇÔ¼öµéÀÌ¿´´Ù. ±×°ÍµéÀº ¸Å¿ì À¯¿ëÇÏÁö¸¸, ¾î¶² Àǹ̿¡¼­´Â º¸´Ù ´õ ±íÀº °øºÎ¿Í ½ÇÇèÀ» º¸ÀåÇÏÁö´Â ¸øÇÏ°í ÀÖ´Ù. ÀÌ·± Á¡À» °³¼±Çϱâ À§ÇØ, IPC ¸Þ¼¼Áö Å¥¸¦ Á¶Á¾Çϱâ À§ÇÑ msgtool, »óÈ£ÀÛ¿ë ¸í·É¾î ¶óÀÎ À¯Æ¿¸®Æ¼(interative command line utility)¸¦ »ç¿ëÇÒ ¼ö ÀÖ´Ù. ÀÌ°ÍÀº È®½ÇÈ÷ ±³À° °­È­¸¦ À§ÇÑ Àû´çÇÑ µµ±¸·Î¼­ÀÇ ±â´ÉÀ» ÇÏÁö¸¸, Ç¥ÁØ ½© ½ºÅ©¸³Æ®¸¦ °æÀ¯ÇÏ¿© ¸Þ¼¼Áö Å¥ ±â´ÉÀ» Á¦°øÇÏ´Â °Í¿¡ ÀÇÇØ ½Ç ¼¼°èÀÇ °úÁ¦¿¡ Á÷Á¢ Àû¿ëÇÒ ¼ö´Â ¾ø´Ù.



¹é±×¶ó¿îµå (Background)

msgtool ÇÁ·Î±×·¥Àº ¸í·É¾î ¶óÀÎÀÇ ¾Æ±Ô¸ÕÆ®¿¡ ÀÇÇØ µ¿ÀÛÀÌ °áÁ¤µÈ´Ù. ÀÌ°ÍÀº ½© ½ºÅ©¸³Æ®·Î ºÎÅÍ È£ÃâµÇ¾îÁú ¶§, ¸Å¿ì À¯¿ëÇÏ°Ô »ç¿ëµÉ °ÍÀÌ´Ù. Å¥¸¦ ¸¸µé°í ¸Þ¼¼Áö¸¦ º¸³»°í Á¶È¸ÇÏ°í Å¥ÀÇ Çã°¡»çÇ×À» ¹Ù²Ù°í ³¡À¸·Î Á¦°ÅÇÏ´Â ¸ðµç ´É·ÂÀÌ Á¦°øµÈ´Ù. ÇöÀç, º»¹® »óÀÇ ¸Þ¼¼Áö¸¦ º¸³»´Âµ¥ Çã¿ëµÇ´Â µ¥ÀÌŸ·Î´Â ¹®ÀÚ ¹è¿­À» »ç¿ëÇÑ´Ù. ºÎ°¡ÀûÀÎ ÀÚ·á ŸÀÔÀ» ¿ëÀÌÇÏ°Ô ¹Ù²Ù´Â °ÍÀº µ¶ÀÚµéÀÇ °úÁ¦·Î ³²°ÜÁ® ÀÖ´Ù.


¸í·É¾î ¶óÀÎ ¹®¹ý (Command Line Syntax)


¸Þ¼¼Áö º¸³»±â (Sending Messages)

msgtool s (type) "text"

¸Þ¼¼Áö Á¶È¸Çϱâ (Retrieving Messages)

msgtool r (type)

Çã°¡»çÇ× ¹Ù²Ù±â (Changing the Permissions(mode))

msgtool m (mode)

Å¥ Áö¿ì±â (Deleting a Queue)

msgtool d


¿¹Á¦ (Examples)

msgtool  s   1 test
msgtool  s   5 test
msgtool  s   1 "This is a test"
msgtool  r   1
msgtool  d
msgtool  m   660


¼Ò½º (The Source)

´ÙÀ½Àº msgtool »ç¿ëÀ» À§ÇÑ ¼Ò½º ÄÚµåÀÌ´Ù. ½Ã½ºÅÛ V IPC¸¦ Áö¿øÇÏ´Â ÃÖ½ÅÀÇ Ä¿³Î °³Á¤ÆÇ¿¡¼­ ±ú²ýÀÌ ÄÄÆÄÀϵǾî¾ß ÇÑ´Ù. ÀçÀÛ¼º(rebuild)½Ã Ä¿³Î¿¡¼­ ½Ã½ºÅÛ V IPC¸¦ »ç¿ëÇÒ ¼ö ÀÖ´ÂÁö È®ÀÎÇ϶ó.

ÇÑÆíÀ¸·Î, ÀÌ À¯Æ¿¸®Æ¼´Â ¾î¶² µ¿ÀÛÀÌ ¿äûµÇµçÁö¿¡ »ó°ü¾øÀÌ ¸Þ¼¼Áö Å¥°¡ Á¸ÀçÇÏÁö ¾ÊÀ¸¸é ¸Þ¼¼Áö Å¥¸¦ ¸¸µé °ÍÀÌ´Ù.

NOTE:ÀÌ ÅøÀº IPC Å° °ªÀ» ¹ß»ý½ÃÅ°±âÀ§ÇØ ftok()ÇÔ¼ö¸¦ »ç¿ëÇÔÀ¸·Î, µð·ºÅ丮 Ãæµ¹À» ¸¸³¯Áö ¸ð¸¥´Ù. ½ºÅ©¸³Æ® ³»ÀÇ ¾î´À ÇÑÁöÁ¡¿¡¼­ µð·ºÅ丮¸¦ ¹Ù²Û´Ù¸é, ¾Æ¸¶µµ µ¿ÀÛÇÏÁö ¾ÊÀ» °ÍÀÌ´Ù. ¶Ç ´Ù¸¥ ÇØ°áÃ¥Àº msgtool¾È¿¡ "/tmp/msgtool/"°ú °°ÀÌ º¸´Ù Àý´ëÀûÀÎ °æ·Î(path)¸¦ ÇϵåÄÚµùÇϰųª Á¶ÀÛ»ó(operational)ÀÇ ¾Æ±Ô¸ÕÆ®¿¡ µû¶ó, ¸í·É¾î ¶óÀλ󿡼­ ³Ñ°ÜÁø °æ·Î(path)¸¦ »ç¿ëÅä·Ï Çã¶ôÇÏ´Â °ÍÀÌ´Ù.

/*****************************************************************************
 ¸®´ª½º ÇÁ·Î±×·¡¸Ó¸¦ À§ÇÑ °¡À̵å - 6Àå ¿¡¼­ ¹ßÃé
 (C)opyright 1994-1995, Scott Burkett
 ***************************************************************************** 
 MODULE: msgtool.c
 *****************************************************************************
 ½Ã½ºÅÛ V ½ºÅ¸ÀÏÀÇ ¸Þ¼¼Áö Å¥¸¦ »ç¿ëÇϱâ À§ÇÑ ¸í·É¾î ¶óÀÎ Åø
 *****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

#define MAX_SEND_SIZE 80

struct mymsgbuf {
        long mtype;
        char mtext[MAX_SEND_SIZE];
};

void send_message(int qid, struct mymsgbuf *qbuf, long type, char *text);
void read_message(int qid, struct mymsgbuf *qbuf, long type);
void remove_queue(int qid);
void change_queue_mode(int qid, char *mode);
void usage(void);


int main(int argc, char *argv[])
{
        key_t key;
        int   msgqueue_id;
        struct mymsgbuf qbuf;

        if(argc == 1)
                usage();

	/* ftok() È£ÃâÀ» ÅëÇØ À¯ÀÏÇÑ Å°¸¦ ¸¸µç´Ù */
        key = ftok(".", 'm');

	/* ÇÊ¿äÇÏ´Ù¸é Å¥¸¦ ¸¸µé°í ¿¬´Ù */
        if((msgqueue_id = msgget(key, IPC_CREAT|0660)) == -1) {
                perror("msgget");
                exit(1);
        }
        
        switch(tolower(argv[1][0]))
        {
                case 's': send_message(msgqueue_id, (struct mymsgbuf *)&qbuf,
                                       atol(argv[2]), argv[3]); 
                          break;
                case 'r': read_message(msgqueue_id, &qbuf, atol(argv[2])); 
                          break;
                case 'd': remove_queue(msgqueue_id); 
                          break;        
                case 'm': change_queue_mode(msgqueue_id, argv[2]); 
                          break;

                 default: usage();

        }
        
        return(0);
}

void send_message(int qid, struct mymsgbuf *qbuf, long type, char *text)
{
	/* Å¥¿¡ ¸Þ¼¼Áö¸¦ º¸³½´Ù */
        printf("Sending a message ...\n");
        qbuf->mtype = type;
        strcpy(qbuf->mtext, text);

        if((msgsnd(qid, (struct msgbuf *)qbuf,
                strlen(qbuf->mtext)+1, 0)) ==-1)
        {
                perror("msgsnd");
                exit(1);
        }
}

void read_message(int qid, struct mymsgbuf *qbuf, long type)
{
	/* Å¥·Î ºÎÅÍ ¸Þ¼¼Áö¸¦ Àд´Ù. */
        printf("Reading a message ...\n");
        qbuf->mtype = type;
        msgrcv(qid, (struct msgbuf *)qbuf, MAX_SEND_SIZE, type, 0);
        
        printf("Type: %ld Text: %s\n", qbuf->mtype, qbuf->mtext);
}

void remove_queue(int qid)
{
	/* Å¥¸¦ Áö¿î´Ù */
        msgctl(qid, IPC_RMID, 0);
}

void change_queue_mode(int qid, char *mode)
{
        struct msqid_ds myqueue_ds;

	/* ÇöÀç Á¤º¸¸¦ Àд´٠*/
        msgctl(qid, IPC_STAT, &myqueue_ds);

	/* ¸ðµå¸¦ Àоî¿Í ¹Ù²Û´Ù */
        sscanf(mode, "%ho", &myqueue_ds.msg_perm.mode);

	/* ¸ðµå¸¦ ¼öÁ¤ÇÑ´Ù */
        msgctl(qid, IPC_SET, &myqueue_ds);
}

void usage(void)
{
        fprintf(stderr, "msgtool - A utility for tinkering with msg queues\n");
        fprintf(stderr, "\nUSAGE: msgtool (s)end  \n");
        fprintf(stderr, "               (r)ecv \n");
        fprintf(stderr, "               (d)elete\n");
        fprintf(stderr, "               (m)ode \n");
        exit(1);
}


ÀÌÀü:½Ã½ºÅÛ È£Ãâ:msgctl() ´ÙÀ½:6.4.3 ¼¼¸¶ÆÛ (Semaphores)

Copyright (c) 1996,1997 by Euibeom.Hwang & SangEun.Oh All Rights Reserved

Email To:Webmaster , Another address
LAST UPDATE Nov 21,1997
Created Nov 21,1997