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

shmtool:»óÈ£ÀÛ¿ëÀÇ °øÀ¯ ¸Þ¸ð¸® Á¶Á¾ÀÚ (An Interactive shared memory manipulator)



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

½Ã½ºÅÛ V IPC °´Ã¼µéÀÇ ¸¶Áö¸· ¿¹Á¦´Â °øÀ¯ ¸Þ¸ð¸® ¼¼±×¸ÕÆ®¸¦ ¸¸µé°í, Àаí, ¾²°í, Áö¿ì±â À§ÇÑ ¸í·É¾î ¶óÀÎ ÅøÀÎ shmtoolÀÌ´Ù. ´Ù½Ã Çѹø, ÀÌÀüÀÇ ¿¹Á¦µéó·³, ¼¼±×¸ÕÆ®´Â ÀÌÀü¿¡ Á¸ÀçÇÏ°í ÀÖÁö ¾Ê¸é, ¾î¶² µ¿ÀÛÀ» ¼öÇàÇÏ´Â µ¿¾È¿¡ ¸¸µé¾î Áø´Ù.


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



¼¼±×¸ÕÆ®¿¡ ¹®ÀÚ¿­ ¾²±â (Writing strings to the segment)
shmtool w "text"


¼¼±×¸ÕÆ®·Î ºÎÅÍ ¹®ÀÚ¿­ Á¶È¸Çϱâ (Retrieving strings from the segment)
shmtool r


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


¼¼±×¸ÕÆ® Áö¿ì±â (Deleting the segment)
shmtool d


¿¹Á¦ (Examples)

shmtool  w   test
shmtool  w   "This is a test"
shmtool  r   
shmtool  d
shmtool  m   660


¼Ò½º (The Source)

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>

#define SEGSIZE 100

main(int argc, char *argv[])
{
        key_t key;
        int   shmid, cntr;
        char  *segptr;

        if(argc == 1)
                usage();

	/* ftok() È£ÃâÀ» °æÀ¯ÇÏ¿© À¯ÀÏÇÑ Å°°ªÀ» ¸¸µç´Ù */
        key = ftok(".", 'S');

	/* °øÀ¯ ¸Þ¸ð¸® ¼¼±×¸ÕÆ®¸¦ ¿¬´Ù - ÇÊ¿äÇÏ¸é ¸¸µç´Ù */
        if((shmid = shmget(key, SEGSIZE, IPC_CREAT|IPC_EXCL|0666)) == -1) 
        {
                printf("Shared memory segment exists - opening as client\n");

                /* Segment probably already exists - try as a client */
                if((shmid = shmget(key, SEGSIZE, 0)) == -1) 
                {
                        perror("shmget");
                        exit(1);
                }
        }
        else
        {
                printf("Creating new shared memory segment\n");
        }

	/* ÇöÀç ÇÁ·Î¼¼½º¿¡ °øÀ¯ ¸Þ¸ð¸® ¼¼±×¸ÕÆ®¸¦ ¿¬°áÇÑ´Ù */
        if((segptr = shmat(shmid, 0, 0)) == -1)
        {
                perror("shmat");
                exit(1);
        }
        
        switch(tolower(argv[1][0]))
        {
                case 'w': writeshm(shmid, segptr, argv[2]);
                          break;
                case 'r': readshm(shmid, segptr);
                          break;
                case 'd': removeshm(shmid);
                          break;
                case 'm': changemode(shmid, argv[2]);
                          break;
                 default: usage();

        }
}

writeshm(int shmid, char *segptr, char *text)
{
        strcpy(segptr, text);
        printf("Done...\n");
}

readshm(int shmid, char *segptr)
{
        printf("segptr: %s\n", segptr);
}

removeshm(int shmid)
{
        shmctl(shmid, IPC_RMID, 0);
        printf("Shared memory segment marked for deletion\n");
}

changemode(int shmid, char *mode) 
{
        struct shmid_ds myshmds;

	/* ³»¹« ÀÚ·á ±¸Á¶Ã¼·ÎºÎÅÍ ÇöÀç °ªÀ» ¾ò´Â´Ù */
        shmctl(shmid, IPC_STAT, &myshmds);

	/* °ú°ÅÀÇ Çã°¡»çÇ×À» Ç¥½ÃÇÑ´Ù */
        printf("Old permissions were: %o\n", myshmds.shm_perm.mode);

	/* ¸ðµå¸¦ ¼öÁ¤ÇÏ°í ÀûÀçÇÑ´Ù */
        sscanf(mode, "%o", &myshmds.shm_perm.mode);

	/* ¸ðµå¸¦ ¾÷µ¥ÀÌÆ®ÇÑ´Ù */
        shmctl(shmid, IPC_SET, &myshmds);

        printf("New permissions are : %o\n", myshmds.shm_perm.mode);
}

usage()
{
        fprintf(stderr, "shmtool - A utility for tinkering with shared memory\n");
        fprintf(stderr, "\nUSAGE:  shmtool (w)rite \n");
        fprintf(stderr, "                (r)ead\n");
        fprintf(stderr, "                (d)elete\n");
        fprintf(stderr, "                (m)ode change \n");
        exit(1);
}


ÀÌÀü:½Ã½ºÅÛ È£Ãâ:shmdt() ´ÙÀ½:7 ¼Ò¸® ÇÁ·Î±×·¡¹Ö (Sound Programming)

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

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