现在的位置: 首页 > 综合 > 正文

Linux 信号量 sem_init(3) (翻译 man 3)

2014年09月05日 ⁄ 综合 ⁄ 共 2286字 ⁄ 字号 评论关闭
鉴于之前man 7 sem_overview 中提到的Linux 2.6只支持未命名的信号量,那么就只看这部分的接口,从创建开始看起
SEM_INIT(3)                   Linux Programmer's Manual                   SEM_INIT(3)

NAME

       sem_init - initialize an unnamed semaphore

SYNOPSIS

       #include 

       int sem_init(sem_t *sem, int pshared, unsigned int value);

       Link with -lrt or -pthread.

DESCRIPTION

       sem_init() initializes the unnamed semaphore at the address pointed to by sem.
       The value argument specifies the initial value for the semaphore.
//sem_init() 在有sem指向的地址初始化未命名的信号量,value参数指定了信号量的初始值.
       

The pshared argument indicates whether this semaphore is to be shared between
       the threads of a process, or between processes.
//pshared参数标明信号量是否在进程的线程间或者是进程间被共享.
       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).

//如果pshared的值为0,那么信号量就是在一个进程的线程间进行共享,而且要被定义在所有线程都有效的地址(如全局变量,或者是在栈中分配的动态地址).
       

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), etc.

//如果pshared的值不为0,那么信号量就是在进程间进行共享,应该定义在共享内存区域内(参见shm_open(3),mmap(3),shmget(3)).
//(由fork(2)创建的子进程继承父进程的内存映射,那么子进程也可以访问这个信号量).任何能够访问共享内存区域的进程
//都能够通过使用sem_post(3)和sem_wait(3)来操作信号量.
       
Initializing a semaphore that has already been initialized results in undefined behavior.
//初始化一个已经初始化好的信号量将会导致未知的行为.

RETURN VALUE

       sem_init() returns 0 on success; on error, -1 is returned, and errno is set to
       indicate the error.
//成功返回0,错误返回-1,errno自动设定

ERRORS

       EINVAL value exceeds SEM_VALUE_MAX.

//value的值超过了SEM_VALUE_MAX
       ENOSYS pshared is nonzero, but the system does not support process-shared
              semaphores (see sem_overview(7)).

//pshared 非0,但是系统不支持进程间共享信号量(参见 sem_overview(7))(我已经翻译完毕)

CONFORMING TO

       POSIX.1-2001.

NOTES

       Bizarrely, 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.

//奇怪的是,POSIX.1-2001 并没有指定sem_init(3)成功时候的返回值,POSIX.1-2008 规定了成功的时候返回0.

SEE ALSO

       sem_destroy(3), sem_post(3), sem_wait(3), sem_overview(7)

COLOPHON

       This page is part of release 3.31 of the Linux man-pages project.  A
       description of the project, and information about reporting bugs, can be found
       at http://www.kernel.org/doc/man-pages/.

Linux                                 2008-07-27                          SEM_INIT(3)

抱歉!评论已关闭.