PHP: 세마포어(semaphore)와 공유 메모리(shared memory) 함수 - Manual
PHP  
downloads | documentation | faq | getting help | mailing lists | | php.net sites | links | my php.net 
search for in the  
<sql_regcaseftok>
view the version of this page
Last updated: Wed, 29 Jan 2003

XCI. 세마포어(semaphore)와 공유 메모리(shared memory) 함수

이 모듈은 System V 세마포어가 사용하는 세마포어 함수를 포함한다. 세마포어는 현재 머신에 자원을 한정적으로 접근하는데 사용되거나 프로세스가 일제히 자원을 사용하는 개수를 제한하는데 사용된다.

이 모듈은 또한 System V의 공유 메모리를 사용하기 위한 공유 메모리 함수도 포함한다. 공유 메모리는 전역 변수에 접근하는데 사용된다. 각기 다른 httpd-daemon과 다른 프로그램(Perl, C와 같은) 조차도 전역 데이터 교환을 지원하여 이 데이터를 접근할 수 있다. 주의할 점은, 공유 메모리는 동시 접근에는 안전하지 않다는 것이다. 동기화를 위해서는 세마포어를 사용하면 된다.

표 1. Unix OS에 의한 공유 메모리의 제한

SHMMAX공유 메모리의 최대값, 보통 131072 바이트임.
SHMMIN공유 메모리의 최소값, 보통 1바이트임.
SHMMNI 시스템 상의 공유 메모리 세그먼트의 최대 합계, 보통 100임.
SHMSEG 프로세서당 공유 메모리 세그먼트의 최대 합계, 보통 6임.

참고: 이 함수는 윈도우즈 시스템에서 동작하지 않는다.

차례
ftok --  Convert a pathname and a project identifier to a System V IPC key
msg_get_queue --  Create or attach to a message queue
msg_receive --  Receive a message from a message queue
msg_remove_queue --  Destroy a message queue
msg_send --  Send a message to a message queue
msg_set_queue --  Set information in the message queue data structure
msg_stat_queue --  Returns information from the message queue data structure
sem_acquire -- 세마포어를 획득
sem_get -- 세마포어 id를 얻음
sem_release -- 세마포어 릴리즈
sem_remove -- Remove a semaphore
shm_attach -- 공유 메모리 세그먼트를 열거나 생성
shm_detach -- 공유 메모리 세그먼트 접속을 종료
shm_get_var -- 공유 메모리로부터 변수를 반환
shm_put_var -- 공유 메모리 변수를 삽입하거나 경신
shm_remove_var -- 공유 메모리로부터 변수를 제거
shm_remove -- Unix 시스템에서 공유 메모리를 제거


User Contributed Notes
세마포어(semaphore)와 공유 메모리(shared memory) 함수
add a note add a note
php at stolt dot de
21-Sep-2000 06:58

The integer keys for sem_get() and shm_attach() have to be systemwide unique. There is no method to ensure that no other process on the system will use your specific key (security! and possible malfunction). Also shared memory is very seldom used there are possibilities for conflicts! To see the used id's you can use the program 'ipcs' (at least under SuseLinux;) ). Thanks Christian C.
mikew at php dot net
01-Jun-2001 04:46

As for security,   please look at the perm argument to shm_get.   Shared Memory blocks has the same permission semantics as unix user/group/other file permissions.   As long as your webserver is running as a user that no other users can script to..  and as long as the permissions are set to 600,  you should be fine and have no security concerns.
joe at floodwerks dot com
02-May-2002 01:30

If only Sys V message queues were supported, I would be happy.
elran70 at hotmail dot com
22-Jun-2002 05:54

Samlpe code for using most of the functions here:

   $MEMSIZE    = 512;//  size of shared memory to allocate
   $SEMKEY     =   1;  // Semaphore key
   $SHMKEY     =   2;  //  Shared memory key

  echo "Start.\n";
   // Get semaphore
   $sem_id = sem_get($SEMKEY, 1);
   if ($sem_id === false)
   {
      echo "Fail to get semaphore";
       exit;
   }
  else
       echo "Got semaphore $sem_id.\n";

   // Accuire semaphore
   if (! sem_acquire($sem_id))
   {
      echo "Fail to aquire semaphore $sem_id.\n";
      sem_remove($sem_id);
       exit;
   }
   else
       echo "Success aquire semaphore $sem_id.\n";

   $shm_id =   shm_attach($SHMKEY, $MEMSIZE);
   if ($shm_id === false)
   {
     echo "Fail to attach shared memory.\n";
      sem_remove($sem_id);
       exit;
   }
   else
       echo "Success to attach shared memory : $shm_id.\n";

   // Write variable 1
   if (!shm_put_var($shm_id, 1, "Variable 1"))
   {
       echo "Fail to put var 1 on shared memory $shm_id.\n";
       sem_remove($sem_id);
      shm_remove ($shm_id);
       exit;
   }
   else
      echo "Write var1 to shared memory.\n";

   // Write variable 2
   if (!shm_put_var($shm_id, 2, "Variable 2"))
   {
       echo "Fail to put var 2 on shared memory $shm_id.\n";
       sem_remove($sem_id);
      shm_remove ($shm_id);
       exit;
   }
   else
      echo "Write var2 to shared memory.\n";

   // Read variable 1
   $var1   =   shm_get_var ($shm_id, 1);
   if ($var1 === false)
   {
       echo "Fail to retrive Var 1 from Shared memory $shm_id, return value=$var1.\n";
   }
  else
       echo "Read var1=$var1.\n";

   // Read variable 1
   $var2   =   shm_get_var ($shm_id, 2);
   if ($var1 === false)
   {
       echo "Fail to retrive Var 2 from Shared memory $shm_id, return value=$var2.\n";
   }
  else
       echo "Read var2=$var2.\n";

   // Release semaphore
   if (!sem_release($sem_id))
       echo "Fail to release $sem_id semaphore.\n";
   else
       echo "Semaphore $sem_id released.\n";

   // remove shared memory segmant from SysV
   if (shm_remove ($shm_id))
       echo "Shared memory successfully removed from SysV.\n";
  else
       echo "Fail to remove $shm_id shared memory from SysV.\n";

   // Remove semaphore
   if (sem_remove($sem_id))
       echo "semaphore removed successfully from SysV.\n";
   else
       echo "Fail to remove $sem_id semaphore from SysV.\n";
   echo "End.\n";

Roman Laptev <tmp at laptev dot org>
02-Apr-2003 02:50

If you going to work with semaphore, which was created by some external program, you can try the following code for this program (C example):

#define SVSEM_MODE (SEM_R | SEM_A | SEM_R>>3 | SEM_R>>6) /* 0644 */
#define PHP_SEM_NEED_NUMBER 3

/*.......*/
int semid, semflag = SVSEM_MODE | IPC_CREAT | IPC_EXCL;
struct sembuf semptr;
union semun semopts;
/*.......*/
if( (semid = semget(sempath, PHP_SEM_NEED_NUMBER, semflag)) >= 0 ) {
 semopts.val = 1; /* initial value for sem */
 if( semctl( semid, 0, SETVAL, semopts) < 0 ) {/*error*/}
 if( semctl( semid, 1, SETVAL, semopts) < 0 ) {/*error*/}
 /* PHP wanna zero for its own semget at third sem.
  * look at ./PHP_SOURCE_PATH/ext/sysvsem/sysvsem.c
  */
semopts.val = 0;
 if( semctl( semid, 2, SETVAL, semopts) < 0 ) {/*error*/}
}
else if(errno == EEXIST) { /* connect only  */
if( (semid = semget(sempath, PHP_SEM_NEED_NUMBER, SVSEM_MODE | IPC_CREAT)) < 0 ) {/*error*/}
}
else {/*error*/}

/*.......*/
/* If you want acquire the sem */
 semptr.sem_num = 0;
 semptr.sem_op = -1; /* lock it */
 semptr.sem_flg = SEM_UNDO;
 while( semop(semid, &semptr, 1) < 0 ) {/*error*/}
/*.......*/

Thanks,
Roma

add a note add a note

<sql_regcaseftok>
 Last updated: Wed, 29 Jan 2003
show source | credits | mirror sites 
Copyright © 2001-2003 The PHP Group
All rights reserved.
This mirror generously provided by: /
Last updated: Fri May 23 21:10:19 2003 CEST