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

semtool:»óÈ£ÀÛ¿ë ¼¼¸¶ÆÛ Á¶Á¾ÀÚ(An Interative Semaphore Manipulator)



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

semtool ÇÁ·Î±×·¥Àº ¸í·É¾î ¶óÀÎ ¾Æ±Ô¸ÕÆ®¿¡ µû¶ó ÇൿÀÌ °áÁ¤µÈ´Ù. ÀÌ°ÍÀº ½© ½ºÅ©¸³Æ®·Î ºÎÅÍ È£ÃâµÇ¾îÁú ¶§ ƯÈ÷ À¯¿ëÇÏ°Ô »ç¿ëµÈ´Ù. ¼¼¸¶ÆÛ ÁýÇÕÀ» ¸¸µé°í Á¶Á¾ÇÏ°í Çã°¡»çÇ×À» ¹Ù²Ù°í Á¦°ÅÇÏ´Â ¸ðµç ´É·ÂÀÌ Á¦°øµÈ´Ù. Ç¥ÁØ ½© ½ºÅ©¸³Æ®¸¦ °æÀ¯ÇÏ¿© °øÀ¯µÈ ÀÚ¿øÀ» ÅëÁ¦Çϴµ¥ »ç¿ëµÈ´Ù.


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


¼¼¸¶ÆÛ ÁýÇÕ ¸¸µé±â (Creating a Semaphore Set)
semtool c (number of semaphores in set)


¼¼¸¶ÆÛ Àá±×±â (Locking a Semaphore)
semtool l (semaphore number to lock)


¼¼¸¶ÆÛ Àá±Ý Ç®±â (Unlocking a Semaphore)
semtool u (semaphore number to unlock)


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


¼¼¸¶ÆÛ ÁýÇÕ Áö¿ì±â (Deleting a Semaphore Set)
semtool d


¿¹Á¦ (Examples)

semtool  c 5
semtool  l
semtool  u
semtool  m 660
semtool  d


¼Ò½º (The Source)

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

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

#define SEM_RESOURCE_MAX        1       /* Initial value of all semaphores */

void opensem(int *sid, key_t key);
void createsem(int *sid, key_t key, int members);
void locksem(int sid, int member);
void unlocksem(int sid, int member);
void removesem(int sid);
unsigned short get_member_count(int sid);
int getval(int sid, int member);
void dispval(int sid, int member);
void changemode(int sid, char *mode);
void usage(void);

int main(int argc, char *argv[])
{
        key_t key;
        int   semset_id;

        if(argc == 1)
                usage();

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

        switch(tolower(argv[1][0]))
        {
                case 'c': if(argc != 3)
                                usage();
                          createsem(&semset_id, key,  atoi(argv[2]));
                          break;
                case 'l': if(argc != 3)
                                usage();
                          opensem(&semset_id, key);
                          locksem(semset_id, atoi(argv[2]));
                          break;
                case 'u': if(argc != 3)
                                usage();
                          opensem(&semset_id, key);
                          unlocksem(semset_id, atoi(argv[2]));
                          break;
                case 'd': opensem(&semset_id, key);
                          removesem(semset_id);
                          break;        
                case 'm': opensem(&semset_id, key);
                          changemode(semset_id, argv[2]);
                          break;        
                 default: usage();

        }
        
        return(0);
}

void opensem(int *sid, key_t key)
{
        /* ¼¼¸¶ÆÛ ÁýÇÕÀ» ¿¬´Ù - ¸¸µå´Â °ÍÀÌ ¾Æ´Ï´Ù! */

        if((*sid = semget(key, 0, 0666)) == -1) 
        {
                printf("Semaphore set does not exist!\n");
                exit(1);
        }

}

void createsem(int *sid, key_t key, int members)
{
        int cntr;
        union semun semopts;

        if(members > SEMMSL) {
                printf("Sorry, max number of semaphores in a set is %d\n",
                        SEMMSL);
                exit(1);
        }

        printf("Attempting to create new semaphore set with %d members\n",
                                members);

        if((*sid = semget(key, members, IPC_CREAT|IPC_EXCL|0666))
                        == -1) 
        {
                fprintf(stderr, "Semaphore set already exists!\n");
                exit(1);
        }

        semopts.val = SEM_RESOURCE_MAX;
        
        /* ¸ðµç ¸â¹ö¸¦ ÃʱâÈ­ÇÑ´Ù (SETALLÀ» °¡Áö°í ¼öÇàµÉ ¼ö ÀÖÀ½) */        
        for(cntr=0; cntr(get_member_count(sid)-1))
        {
                fprintf(stderr, "semaphore member %d out of range\n", member);
                return;
        }

        /* ¼¼¸¶ÆÄ ÁýÇÕÀÇ Àá±Ý(lock)À» ½ÃµµÇÑ´Ù */
        if(!getval(sid, member))
        {
                fprintf(stderr, "Semaphore resources exhausted (no lock)!\n");
                exit(1);
        }
        
        sem_lock.sem_num = member;
        
        if((semop(sid, &sem_lock, 1)) == -1)
        {
                fprintf(stderr, "Lock failed\n");
                exit(1);
        }
        else
                printf("Semaphore resources decremented by one (locked)\n");

        dispval(sid, member);
}

void unlocksem(int sid, int member)
{
        struct sembuf sem_unlock={ member, 1, IPC_NOWAIT};
        int semval;

        if( member<0 || member>(get_member_count(sid)-1))
        {
                fprintf(stderr, "semaphore member %d out of range\n", member);
                return;
        }

        /* ¼¼¸¶ÆÛ ÁýÇÕÀÌ Àá°ÜÀִ°¡? */
        semval = getval(sid, member);
        if(semval == SEM_RESOURCE_MAX) {
                fprintf(stderr, "Semaphore not locked!\n");
                exit(1);
        }

        sem_unlock.sem_num = member;

        /* ¼¼¸¶ÆÛ ÁýÇÕÀÇ Àá±ÝÇØÁ¦(unlock)¸¦ ½ÃµµÇÑ´Ù */
        if((semop(sid, &sem_unlock, 1)) == -1)
        {
                fprintf(stderr, "Unlock failed\n");
                exit(1);
        }
        else
                printf("Semaphore resources incremented by one (unlocked)\n");

        dispval(sid, member);
}

void removesem(int sid)
{
        semctl(sid, 0, IPC_RMID, 0);
        printf("Semaphore removed\n");
}

unsigned short get_member_count(int sid)
{
        union semun semopts;
        struct semid_ds mysemds;

        semopts.buf = &mysemds;

        /* ¼¼¸¶ÆÛ ÁýÇÕ¾ÈÀÇ ¸â¹öÀÇ ¼ö¸¦ ¹ÝȯÇÑ´Ù */
        return(semopts.buf->sem_nsems);
}

int getval(int sid, int member)
{
        int semval;

        semval = semctl(sid, member, GETVAL, 0);
        return(semval);
}

void changemode(int sid, char *mode)
{
        int rc;
        union semun semopts;
        struct semid_ds mysemds;

        /* ³»ºÎ ÀÚ·á ±¸Á¶ÀÇ ÇöÀç °ªÀ» ±¸ÇÑ´Ù */
        semopts.buf = &mysemds;

        rc = semctl(sid, 0, IPC_STAT, semopts);

        if (rc == -1) {
                perror("semctl");
                exit(1);
        }
                
        printf("Old permissions were %o\n", semopts.buf->sem_perm.mode);
                
        /* ¼¼¸¶ÆÛÀÇ Çã°¡»çÇ×À» ¼öÁ¤ÇÑ´Ù */
        sscanf(mode, "%ho", &semopts.buf->sem_perm.mode);

        /* ³»ºÎ ÀÚ·á ±¸Á¶¸¦ ¾÷µ¥ÀÌÆ®ÇÑ´Ù */
        semctl(sid, 0, IPC_SET, semopts);

        printf("Updated...\n");

}

void dispval(int sid, int member)
{
        int semval;

        semval = semctl(sid, member, GETVAL, 0);
        printf("semval for member %d is %d\n", member, semval);
}

void usage(void)
{
        fprintf(stderr, "semtool - A utility for tinkering with semaphores\n");
        fprintf(stderr, "\nUSAGE:  semtool4 (c)reate \n");
        fprintf(stderr, "                 (l)ock \n");
        fprintf(stderr, "                 (u)nlock \n");
        fprintf(stderr, "                 (d)elete\n");
        fprintf(stderr, "                 (m)ode \n");
        exit(1);
}


ÀÌÀü:½Ã½ºÅÛ È£Ãâ:semctl() ´ÙÀ½:semstat:semtool µ¿·á ÇÁ·Î±×·¥

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

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