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

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


  SYSTEM CALL: semop();                                                          
  PROTOTYPE: int semop ( int semid, struct sembuf *sops, unsigned nsops);
    RETURNS: 0 on success (all operations performed)
             -1 on error: errno = E2BIG (nsops greater than max number of ops allowed atomically)
                                  EACCESS (permission denied)
                                  EAGAIN (IPC_NOWAIT asserted, operation could not go through)
                                  EFAULT (invalid address pointed to by sops argument)
                                  EIDRM (semaphore set was removed)
                                  EINTR (Signal received while sleeping)
                                  EINVAL (set doesn't exist, or semid is invalid)
                                  ENOMEM (SEM_UNDO asserted, not enough memory to create the
                                          undo structure necessary)
                                  ERANGE (semaphore value out of range)
  NOTES:

semop()ÀÇ Ã¹¹ø° ¾Æ±¸¸ÕÆ®´Â semget() È£Ãâ¿¡ ÀÇÇØ ¹ÝȯµÈ Å°°ªÀÌ´Ù. µÎ¹ø° ¾Æ±Ô¸ÕÆ®(sops)´Â ¼¼¸¶ÆÛ ÁýÇÕ¿¡¼­ ¼öÇàµÉ µ¿ÀÛ ¹è¿­À» °¡¸®Å°´Â Æ÷ÀÎÅÍÀÌ°í ¼¼¹ø° ¾Æ±Ô¸ÕÆ®(nsops)´Â ¹è¿­¾È¿¡ ÀÖ´Â µ¿ÀÛÀÇ °¹¼öÀÌ´Ù.

sops ¾Æ±Ô¸ÕÆ®´Â sembuf ŸÀÔÀÇ ¹è¿­À» °¡¸®Å²´Ù. ÀÌ ±¸Á¶´Â ´ÙÀ½°ú °°ÀÌ linux/sem.h¿¡ ¼±¾ðµÇ¾î ÀÖ´Ù.:


	/* semop ½Ã½ºÅÛ È£ÃâÀº ÀÌ·¯ÇÑ ¹è¿­À» °®´Â´Ù */
        struct sembuf {
                ushort  sem_num;        /* ¹è¿­¾È¿¡¼­ÀÇ ¼¼¸¶ÆÛ À妽º */
                short   sem_op;         /* ¼¼¸¶ÆÛ µ¿ÀÛ */
                short   sem_flg;        /* µ¿ÀÛ Ç÷¡±× */
        };

sem_num
´Ù·ç°íÀÚ ÇÏ´Â ¼¼¸¶ÆÛÀÇ ¹øÈ£
sem_op
¼öÇàÇÒ µ¿ÀÛ (¾ç¼ö,À½¼ö, ¶Ç´Â 0)
sem_flg
µ¿ÀÛ Ç÷¡±×
sem_op°¡ À½¼öÀ̸é, ¼¼¸¶ÆÛ·Î ºÎÅÍ ±×°ªÀ» »«´Ù. ÀÌ°ÍÀº ¼¼¸¶ÆÛ°¡ Á¢±ÙÀ» °¨½ÃÇÏ°í ÅëÁ¦ÇÏ´Â ÀÚ¿øµéÀ» »ç¿ëÇÔÀ» ÀǹÌÇÑ´Ù. IPC_NOWAIT°¡ ÁöÁ¤µÇÁö ¾Ê¾ÒÀ¸¸é, È£ÃâÇÑ ÇÁ·Î¼¼½º´Â ÀÚ¿øÀÇ ¿äûÇÑ ¾ç¸¸Å­ ¼¼¸¶ÆÛ¿¡¼­ »ç¿ë°¡´ÉÇÒ ¶§±îÁö ÀáÀ» ÀÜ´Ù. (¶Ç ´Ù¸¥ ÇÁ·Î¼¼½º°¡ ±×°ÍÀ» ³õÀ» ¶§±îÁö)

sem_op°¡ ¾ç¼öÀ̸é, ±×°ªÀº ¼¼¸¶ÆÛ¿¡ ´õÇØÁø´Ù. ÀÌ°ÍÀº ÀÀ¿ëÇÁ·Î±×·¥ÀÇ ¼¼¸¶ÆÛ ÁýÇÕ¿¡°Ô ÀÚ¿øÀ» µ¹·ÁÁشٴ °ÍÀ» ÀǹÌÇÑ´Ù. ÀÚ¿øµéÀº ´õ ÀÌ»ó ÇÊ¿äÇÏÁö ¾ÊÀ» ¶§ ¼¼¸¶ÆÛ ÁýÇÕ¿¡°Ô µ¹·ÁÁÖ¾î¾ß ÇÑ´Ù.

³¡À¸·Î, sem_op°¡ 0(zero)À̸é, È£ÃâÇÑ ÇÁ·Î¼¼½º´Â ¼¼¸¶ÆÛÀÇ °ªÀÌ 0ÀÌ µÉ¶§±îÁö ÀáÀ» Àß °ÍÀÌ´Ù. ÀÌ°ÍÀº ¼¼¸¶ÆÛ°¡ 100% È°¿ëµÉ ¶§±îÁö ±â´Ù¸°´Ù´Â °ÍÀ» ÀǹÌÇÑ´Ù. ÀÌ·¯ÇÑ ÁÁÀº ¿¹´Â 100% È°¿ë¿¡ µµ´ÞÇϵµ·Ï ¼¼¸¶ÆÛÀÇ Å©±â¸¦ µ¿ÀûÀ¸·Î Á¶Á¤ÇÏ´Â, ¼öÆÛÀ¯Àú(superuser)ÀÇ ±ÇÇÑÀ» °¡Áö°í ¼öÇàµÇ´Â ´ë¸ó(daemon)ÀÏ °ÍÀÌ´Ù.

sem_op È£ÃâÀ» ¼³¸íÇϱâ À§ÇÏ¿©, ¿ì¸®ÀÇ Àμâ¹æ ½Ã³ª¸®¿À¸¦ ´Ù½Ã Çѹø »ý°¢Çغ¸ÀÚ. Çѹø¿¡ ÇÑ°¡Áö Àϸ¸À» ¼öÇàÇÒ ¼ö ÀÖ´Â ¿ÀÁ÷ ÇÑ´ëÀÇ ÇÁ¸°Å͸¸ ÀÖ´Ù°í °¡Á¤ÇÏÀÚ. ¿ì¸®´Â ÇÑ°³ÀÇ ¼¼¸¶ÆÛ(ÇÑ°³ÀÇ ÇÁ¸°ÅÍ)¸¸À» °¡Áø ¼¼¸¶ÆÛ ÁýÇÕÀ» ¸¸µé°í ¼¼¸¶ÆÛÀÇ ÃʱⰪÀ» 1·Î ÃʱâÈ­ÇÑ´Ù.(Çѹø¿¡ ÇÑ°¡Áö ÀÏ)

¿ì¸®°¡ ÀÌ ÇÁ¸°ÅÍ¿¡ ÀÛ¾÷À» º¸³»°íÀÚ ÇÒ ¶§¸¶´Ù, ÀÚ¿øÀÌ ÀÌ¿ë°¡´ÉÇÑÁö¸¦ È®ÀÎÇؾßÇÒ ÇÊ¿ä°¡ ÀÖ´Ù. ¿ì¸®´Â ¼¼¸¶ÆÛ·Î ºÎÅÍ ÇÑ°³ÀÇ ´ÜÀ§(unit)¸¦ ¾ò´Â °ÍÀ¸·Î ÀÌ·¯ÇÑ ÀÛ¾÷À» ¼öÇàÇÑ´Ù. µ¿ÀÛÀ» ¼öÇàÇϱâ À§ÇØ sembuf ¹è¿­À» ÀûÀç(load)ÇÏÀÚ.:


       struct sembuf sem_lock = { 0, -1, IPC_NOWAIT };

À§¿Í°°ÀÌ -1°ªÀ¸·Î ÃʱâÈ­µÈ ±¸Á¶ÀÇ º¯È¯Àº ¼¼¸¶ÆÛ ÁýÇվȿ¡¼­ ¼¼¸¶ÆÛ ¹øÈ£ 0¿¡ ÷°¡µÈ´Ù. ¹Ù²Ù¾î ¸»Çϸé, ÀÚ¿øµéÀÇ ÇÑ ´ÜÀ§(one unit)´Â ¿ÀÁ÷ ÁýÇÕ¾ÈÀÇ ¼¼¸¶ÆÛ(0th member)·Î ºÎÅÍ ¾ò¾îÁø´Ù. IPC_NOWAIT°¡ ÁöÁ¤µÇ¸é È£ÃâÀº Áï½Ã Åë°úÇϰųª ´Ù¸¥ ÀμâÀÛ¾÷ÀÌ ¼öÇà ÁßÀÌ¸é ½ÇÆÐÇÑ´Ù. ¿©±â semop ½Ã½ºÅÛ È£ÃâÀ» °¡Áö°í ÃʱâÈ­µÈ sembuf ±¸Á¶¸¦ »ç¿ëÇÏ´Â ¿¹°¡ ÀÖ´Ù.:


        if((semop(sid, &sem_lock, 1) == -1)
                perror("semop");

¼¼¹ø° ¾Æ±Ô¸ÕÆ®(nsops)´Â ¿ì¸®°¡ ¿ÀÁ÷ ÇÑ°³ÀÇ µ¿ÀÛ¸¸À» ¼öÇàÇÏ°í ÀÖÀ½À» ¸»ÇØÁÖ°í ÀÖ´Ù. (µ¿ÀÛ ¹è¿­¾È¿¡ ¿ÀÁ÷ ÇÑ°³ÀÇ sembuf ±¸Á¶¸¸ÀÌ ÀÖ´Ù.) sid ¾Æ±Ô¸ÕÆ®´Â ¼¼¸¶ÆÛ ÁýÇÕ¿¡ ´ëÇÑ IPC È®ÀÎÀÚ(identifier)ÀÌ´Ù.

ÀμâÀÛ¾÷ÀÌ ¿Ï·áµÇ¾úÀ» ¶§, ¼¼¸¶ÆÛ ÁýÇÕÀ¸·Î ÀÚ¿øÀ» µ¹·ÁÁÖ¾î¾ß ÇÑ´Ù. ±×·¡¾ß ´Ù¸¥ »ç¶÷ÀÌ ÇÁ¸°Å͸¦ »ç¿ëÇÒ ¼ö ÀÖ´Ù.


       struct sembuf sem_unlock = { 0, 1, IPC_NOWAIT };

À§¿Í °°ÀÌ '1'°ªÀÌ ÁöÁ¤µÈ ÃʱâÈ­µÈ ±¸Á¶ÀÇ º¯È¯Àº ¼¼¸¶ÆÛ ÁýÇÕ³»ÀÇ 0¹ø ¼¼¸¶ÆÛ¿¡ Àû¿ëµÈ´Ù. ¹Ù²Ù¾î ¸»Çϸé, ÀÚ¿øµéÀÇ ÇÑ ´ÜÀ§(one unit)°¡ ÁýÇÕÀ¸·Î ¹ÝȯµÈ´Ù.


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

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

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