4. API 比较细节 |
信号灯没有在POSIX.4a (pthreads)说明中定义,但它们包含在POSIX.4(实时扩充)说明。这些函数是标准的,而且相似于那些在Solaris线索实现中所提供的。从Solaris到POSIX.4的改变是微小的,仅将sema前缀转换为sem。
#include <synch.h>
int sema_init (sema_t *sp, unsigned int count, int type, void *arg)
int sema_destroy (sema_t *sp)
int sema_wait (sema_t *sp)
int sema_trywait (sema_t *sp)
int sema_post (sema_t *sp)
#include <semaphore.h>
int sem_init (sem_t *sp, int pshared, int value)
int sem_destroy (sem_t *sp)
int sem_wait (sem_t *sp)
int sem_trywait (sem_t *sp)
int sem_post (sem_t *sp)
在Solaris 中
sema_t sp;
int ret;
int count;
count = 4;
ret = sema_init (&sp, count, USYNC_THREAD, 0); /* to be used within this process only */
在POSIX.4 中
sem_t sp;
int ret;
int count = 4;
ret = sem_init (&sp, 0, count); /* to be used within this process only */
在Solaris 中
sema_t sp;
int ret;
int count;
count = 4;
ret = sema_init (&sp, count, USYNC_PROCESS, 0); /* to be used among all the processes */
在POSIX.4 中
pthreads中,如果共享非零,那么所有进程可以使用信号灯。
sem_t sp;
int ret;
int count = 4;
ret = sem_init (&sp, 1, count); /* to be used among all the processes */
在Solaris 中
sema_t sp;
int ret;
ret = sema_destroy (&sp); /*semaphore is destroyed */
在POSIX.4 中
sem_t sp;
int ret;
ret = sem_destroy (&sp); /* semaphore is destroyed */
在Solaris 中
sema_t sp;
int ret;
ret = sema_destroy (&sp); /*wait for semaphore */
在POSIX.4 中
sem_t sp;
int ret;
ret = sem_wait (&sp); /* wait for semaphore */
在Solaris 中
sema_t sp;
int ret;
ret = sema_trywait (&sp); /*try to wait for semaphore */
在POSIX.4 中
sem_t sp;
int ret;
ret = sem_trywait (&sp); /* try to wait for semaphore*/
在Solaris 中
sema_t sp;
int ret;
ret = sema_post (&sp); /*semaphore is posted */
在POSIX.4 中
sem_t sp;
int ret;
ret = sem_post (&sp); /* semaphore is posted */
Copyright: NPACT |