shmctl() 함수는 공유 메모리에 대한 정보를 구하거나 변경 또는 제거합니다. 공유 메모리에 대한 자세한 내용은 shmget() 함수의 내용을 참고하여 주십시오.
헤더 |
#include <sys/ipc.h> | |||||||
형태 | int shmctl(int shmid, int cmd, struct shmid_ds *buf); | |||||||
인수 |
| |||||||
반환 |
|
예제
예제를 위해 두 개의 프로세스를 만들겠습니다. counter.c 는 공유 메모리에 1초 마다 0부터 계속 증가하는 카운터 문자열을 공유 메모리에 넣으면 show_counter.c에서 공유 메모리를 화면에 출력하도록 하겠습니다.
#include <stdio.h> // printf() #include <unistd.h> // sleep() #include <sys/ipc.h> #include <sys/shm.h> #include <sys/types.h> #define KEY_NUM 9527 #define MEM_SIZE 1024 int main( void) { int shm_id; void *shm_addr; struct shmid_ds shm_info; if ( -1 == ( shm_id = shmget( (key_t)KEY_NUM, MEM_SIZE, IPC_CREAT¦0666))) { printf( "공유 메모리 생성 실패n"); return -1; } else { printf( "공유 메모리 생성 성공n"); } if ( ( void *)-1 == ( shm_addr = shmat( shm_id, ( void *)0, 0))) { printf( "공유 메모리 첨부 실패n"); return -1; } else { printf( "공유 메모리 첨부 성공n"); } if ( -1 == shmctl( shm_id, IPC_STAT, &shm_info)) { printf( "공유 메모리 정보 구하기에 실패했습니다.n"); return -1; } printf( "공유 메모리를 사용하는 프로세스의 개수 : %dn", shm_info.shm_nattch); if ( -1 == shmdt( shm_addr)) { printf( "공유 메모리 분리 실패n"); return -1; } else { printf( "공유 메모리 분리 성공n"); } if ( -1 == shmctl( shm_id, IPC_RMID, 0)) { printf( "공유 메모리 제거 실패n"); return -1; } else { printf( "공유 메모리 제거 성공n"); } return 0; }
]$ gcc main.c
]$ ./a.out
공유 메모리 생성 성공 공유 메모리 첨부 성공 공유 메모리를 사용하는 프로세스의 개수 : 1 공유 메모리 분리 성공 공유 메모리 제거 성공 ]$
'Development > Linux' 카테고리의 다른 글
You do not appear to be using the NVIDIA X driver. Please edit your X configuration file (just run `nvidia-xconfig` as root), and restart the X server. (0) | 2009.03.24 |
---|---|
There was an error starting the GNOME Settings Daemon (0) | 2009.03.24 |
shmat (0) | 2009.02.20 |
shared memory (0) | 2009.02.20 |
proxy server 설치하기 (0) | 2009.02.17 |