SEM_INIT(3) Linux Programmer's Manual SEM_INIT(3)
NAME topsem_init - initialize an unnamed semaphore SYNOPSIS top#include <semaphore.h> int sem_init(sem_t *sem, int pshared, unsigned int value); Link with -pthread. DESCRIPTION topsem_init() initializes the unnamed semaphore at the address pointed to by sem. The value argument specifies the initial value for the semaphore. The pshared argument indicates whether this semaphore is to be shared between the threads of a process, or between processes. If pshared has the value 0, then the semaphore is shared between the threads of a process, and should be located at some address that is visible to all threads (e.g., a global variable, or a variable allocated dynamically on the heap). If pshared is nonzero, then the semaphore is shared between processes, and should be located in a region of shared memory (see shm_open(3), mmap(2), and shmget(2)). (Since a child created by fork(2) inherits its parent's memory mappings, it can also access the semaphore.) Any process that can access the shared memory region can operate on the semaphore using sem_post(3), sem_wait(3), and so on. Initializing a semaphore that has already been initialized results in undefined behavior. RETURN VALUE topsem_init() returns 0 on success; on error, -1 is returned, and errno is set to indicate the error. ERRORS topEINVAL value exceeds SEM_VALUE_MAX. ENOSYS pshared is nonzero, but the system does not support process-shared semaphores (see sem_overview(7)). ATTRIBUTES topFor an explanation of the terms used in this section, see attributes(7). ┌──────────────────────────────────────┬───────────────┬─────────┐ │Interface │ Attribute │ Value │ ├──────────────────────────────────────┼───────────────┼─────────┤ │sem_init() │ Thread safety │ MT-Safe │ └──────────────────────────────────────┴───────────────┴─────────┘ CONFORMING TO topPOSIX.1-2001. NOTES topBizarrely, POSIX.1-2001 does not specify the value that should be returned by a successful call to sem_init(). POSIX.1-2008 rectifies this, specifying the zero return on success. EXAMPLES topSee shm_open(3) and sem_wait(3). SEE ALSO topsem_destroy(3), sem_post(3), sem_wait(3), sem_overview(7) COLOPHON top This page is part of release 5.13 of the Linux man-pages project.
A description of the project, information about reporting bugs,
and the latest version of this page, can be found at
https://www.kernel.org/doc/man-pages/.
Linux 2021-03-22 SEM_INIT(3)
Pages that refer to this page: sem_destroy(3), sem_overview(7) Show Copyright and license for this manual page ------------------------------ 실전 예제~ --------------------------------- pthread 가 들어있는 파일 컴파일시! gcc -lpthread 파일명.c 하면 딘다. 직접 구현해보고자 해서 이것저것 검색하다가 쉬운 예제가 나왔다. 이 에제를 따라하면서 이해하면 위에서 공부한 이론이 한방에 이해갈 것이다. 퍼온 소스에 주석만 좀 자세히 달아보았다. = 초기화 함수 int sem_init(sem_t *sem, int pshared, unsigned int value); = 세마포어 값 감소 함수 int sem_wait(sem_t *sem); = 세마포어 값 감소 함수인데 넌 블로킹이라서 0에서 대기하지 않고 바로 EAGAIN(error의 일종) 를 리턴 int sem_trywait(sem_t *sem); = 세마포어 값 증가 함수 int sem_post(sem_t *sem); = sval 이 가리키는 위치에 sem 세마포어의 현재값을 저장 int sem_getvalue(sem_t *sem, int *sval); = 세마포어 객체 삭제(할당된 자원 해제) int sem_destroy(sem_t *sem); ----------------- 반환값 ----------------------- sem_wait와 sem_getvalue 는 항상 0을 반환 다른 모든 세마포어 함수들은 성공시 0, 실패시 -1 반환 실패 반환시 errno변수에 해당 에러코드를 기록! ----------------- 에러 ------------------------ sem_init 함수 에러시 errno 변수에 설정된다. EINVAL : value는 카운터의 최대값인 SEM_VALUE_MAX 값보다 크다 ENOSYS : pshared 값이 0이 아니다. sem_trywait 함수 에러시 errno 변수에 설정된다. EAGAIN : 현재 세마포어의 카운터 값이 0이다. sem_post 함수 에러시 errno 변수에 설정된다. ERANGE : 증가 후 세마포어의 값이 SEM_VALUE_MAX 값보다 커진다. sem_destroy 함수 에러시 errno 변수에 설정된다. 다른 쓰레드가현재 해당 세마포어를 기다리는 중이다. ----------------- SOURCE ----------------------- |