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

Linux 信号量概述(翻译 man 7 sem_overview)

2014年10月13日 ⁄ 综合 ⁄ 共 4837字 ⁄ 字号 评论关闭
准备做几个man 7 部分综述之类的内容,主要是termios(串口),sem(信号量),threads(线程).
 
SEM_OVERVIEW(7)               Linux Programmer's Manual               SEM_OVERVIEW(7)

NAME

       sem_overview - Overview of POSIX semaphores

DESCRIPTION

       POSIX semaphores allow processes and threads to synchronize their actions.
//POSIX 信号量允许进程和线程同步他们的动作
       A semaphore is an integer whose value is never allowed to fall below zero.
       Two operations can be performed on semaphores: increment the semaphore value
       by one (sem_post(3)); and decrement the semaphore value by one (sem_wait(3)).
       If the value of a semaphore is currently zero, then a sem_wait(3) operation
       will block until the value becomes greater than zero.
//信号量是一个不会小于0的整数值.在信号量上允许2种操作:对信号量加(使用sem_post(3));和对信号量减(使用sem_wait(3)).
//如果信号量当前的值为0,那么sem_wait(3)操作将会阻塞知道信号量的值大于0.
       POSIX semaphores come in two forms: named semaphores and unnamed semaphores.
//POSIX 信号量有2种形式:命名信号量和未命名信号量
       Named semaphores
              A named semaphore is identified by a name of the form /somename; that
              is, a null-terminated string of up to NAME_MAX-4 (i.e., 251) characters
              consisting of an initial slash, followed by one or more characters,
              none of which are slashes.  Two processes can operate on the same named
              semaphore by passing the same name to sem_open(3).
//命名信号量使用一个形如 /名称 的形式来标识;也就是说一个以null结尾的字符串长度最大到 NAME_MAX-4并包含一个斜杠,
//斜杠后面跟着若干不包含斜杠的字符.2个进程可以通过向sem_open(3)传递name来在相同的命名信号量上进行操作.
              The sem_open(3) function creates a new named semaphore or opens an
              existing named semaphore.  After the semaphore has been opened, it can
              be operated on using sem_post(3) and sem_wait(3).  When a process has
              finished using the semaphore, it can use sem_close(3) to close the
              semaphore.  When all processes have finished using the semaphore, it
              can be removed from the system using sem_unlink(3).
//sem_open(3)函数创建一个新的命名信号量或者打开一个存在的命名信号量.信号量打开之后可以被sem_post(3)和sem_wait(3)来操作.
//当进程操作完毕信号量的时候,可以使用sem_close(3)来关闭信号量.当所有的进程都操作完毕信号量的时候,可以使用sem_unlink(3)在系统中移除信号量.
       Unnamed semaphores (memory-based semaphores)(基于内存的信号量)
              An unnamed semaphore does not have a name.  Instead the semaphore is
              placed in a region of memory that is shared between multiple threads (a
              thread-shared semaphore) or processes (a process-shared semaphore).  A
              thread-shared semaphore is placed in an area of memory shared between
              by the threads of a process, for example, a global variable.  A
              process-shared semaphore must be placed in a shared memory region
              (e.g., a System V shared memory segment created using shmget(2), or a
              POSIX shared memory object built created using shm_open(3)).
//未命名的信号量没有名字.取而代之的是信号量放置在被多个线程(线程共享信号量)或者是进程(进程共享信号量)中的一个共享内存区域中,
//线程共享信号量放置在进程中线程共享的内存区域中,例如,一个全局变量.进程共享信号量必须放置在共享内存区域中(例如,
//System V 使用shmget(2)创建的共享内存段,或者使用shm_open(3)创建的 POSIX共享内存对象).
              Before being used, an unnamed semaphore must be initialized using
              sem_init(3).  It can then be operated on using sem_post(3) and
              sem_wait(3).  When the semaphore is no longer required, and before the
              memory in which it is located is deallocated, the semaphore should be
              destroyed using sem_destroy(3).
//在使用前,未命名的信号量必须使用sem_init(3)进行初始化,之后可以使用sem_post(3)和sem_wait(3)来操作.
//当信号量不再被需要的时候,在占用的内存归还之前,信号量必须使用sem_destory(3)来进行销毁.
       The remainder of this section describes some specific details of the Linux
       implementation of POSIX semaphores.
//剩余的区域描述这POSIX信号量在Linux中的一些特殊细节.
Versions(版本)
       Prior to kernel 2.6, Linux only supported unnamed, thread-shared semaphores.
       On a system with Linux 2.6 and a glibc that provides the NPTL threading
       implementation, a complete implementation of POSIX semaphores is provided.
//在2.6内核中,Linux只支持未命名的,线程共享的信号量.在一个Linux内核为2.6的系统上,并且使用glibc,就能够提供NPTL的线程实现,
//即提供一个完整的POSIX信号量实现.
Persistence(持续性)
       POSIX named semaphores have kernel persistence: if not removed by
       sem_unlink(3), a semaphore will exist until the system is shut down.
//POSIX命名信号量有内核的持续性:如果不使用sem_unlink(3)移除,那么直到系统关闭前信号量都会存在.
Linking
       Programs using the POSIX semaphores API must be compiled with cc -lrt to link
       against the real-time library, librt.
//系统使用POSIX信号量的API必须在编译的时候使用 cc –lrt 来链接实时库(librt).
Accessing named semaphores via the file system(通过文件系统访问命名的信号量)
       On Linux, named semaphores are created in a virtual file system, normally
       mounted under /dev/shm, with names of the form sem.somename.  (This is the
       reason that semaphore names are limited to NAME_MAX-4 rather than NAME_MAX
       characters.)
//在Linux上,命名的信号量通过虚拟的文件系统来创建,一般在 /dev/shm 下通过sem的somename来挂载(这就是信号量名字的长度
//限制在NAME_MAX-4而不是NAME_MAX个字符的原因).
       Since Linux 2.6.19, ACLs can be placed on files under this directory, to
       control object permissions on a per-user and per-group basis.
//在Linux 2.6.19之前,ACL(存取控制表)可以放置在这个目录的文件中,通过每个用户和每个组为基础来控制目标的权限.

CONFORMING TO(兼容性)

       POSIX.1-2001.

NOTES

       System V semaphores (semget(2), semop(2), etc.) are an older semaphore API.
       POSIX semaphores provide a simpler, and better designed interface than System
       V semaphores; on the other hand POSIX semaphores are less widely available
       (especially on older systems) than System V semaphores.
//System V 信号量(semget(2)和semop(2)等)是旧的信号量API.POSIX信号量比System V提供一个更轻量,更好的接口设计;
//另一方面,POSIX信号量比System V在更小的范围内有效(尤其在老的系统上).

EXAMPLE

       An example of the use of various POSIX semaphore functions is shown in
       sem_wait(3).

SEE ALSO

       sem_close(3), sem_destroy(3), sem_getvalue(3), sem_init(3), sem_open(3),
       sem_post(3), sem_unlink(3), sem_wait(3), pthreads(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                                 2010-05-22                      SEM_OVERVIEW(7)

抱歉!评论已关闭.