/* @(#) sem.h 1.3 1/27/86 17:47:09 */
/*ident "@(#)cfront:incl/sys/sem.h 1.3"*/
/*
** IPC Semaphore Facility.
*/
/*
** Implementation Constants.
*/
#define PSEMN (PZERO + 3) /* sleep priority waiting for greater value */
#define PSEMZ (PZERO + 2) /* sleep priority waiting for zero */
/*
** Permission Definitions.
*/
#define SEM_A 0200 /* alter permission */
#define SEM_R 0400 /* read permission */
/*
** Semaphore Operation Flags.
*/
#define SEM_UNDO 010000 /* set up adjust on exit entry */
/*
** Semctl Command Definitions.
*/
#define GETNCNT 3 /* get semncnt */
#define GETPID 4 /* get sempid */
#define GETVAL 5 /* get semval */
#define GETALL 6 /* get all semval's */
#define GETZCNT 7 /* get semzcnt */
#define SETVAL 8 /* set semval */
#define SETALL 9 /* set all semval's */
/*
** Structure Definitions.
*/
/*
** There is one semaphore id data structure for each set of semaphores
** in the system.
*/
struct semid_ds {
struct ipc_perm sem_perm/* operation permission struct */
struct sem *sem_base/* ptr to first semaphore in set */
ushort sem_nsems/* # of semaphores in set */
time_t sem_otime/* last semop time */
time_t sem_ctime/* last change time */
}
/*
** There is one semaphore structure for each semaphore in the system.
*/
struct sem {
ushort semval /* semaphore text map address */
short sempid /* pid of last operation */
ushort semncnt/* # awaiting semval >cval */
ushort semzcnt/* # awaiting semval = 0 */
}
/*
** There is one undo structure per process in the system.
*/
struct sem_undo {
struct sem_undo *un_np/* ptr to next active undo structure */
short un_cnt/* # of active entries */
struct undo {
short un_aoe/* adjust on exit values */
short un_num/* semaphore # */
int un_id/* semid */
} un_ent[1]/* undo entries (one minimum) */
}
/*
** semaphore information structure
*/
struct seminfo {
int semmap, /* # of entries in semaphore map */
semmni, /* # of semaphore identifiers */
semmns, /* # of semaphores in system */
semmnu, /* # of undo structures in system */
semmsl, /* max # of semaphores per id */
semopm, /* max # of operations per semop call */
semume, /* max # of undo entries per process */
semusz, /* size in bytes of undo structure */
semvmx, /* semaphore maximum value */
semaem /* adjust on exit max value */
}
/*
** User semaphore template for semop system calls.
*/
struct sembuf {
ushort sem_num/* semaphore # */
short sem_op /* semaphore operation */
short sem_flg/* operation flags */
}
//
union semum {
int val
struct semid_ds *bf
ushort *array
} arg
extern int semctl (int, int, int, semum),
semget (key_t, int, int),
semop (int, sembuf**, int)
信号量在进程是以有名信号量进行通信的,在线程是以无名信号进行通信的,因为线程linux还没有实现进程间的通信,所以在sem_init的第二个参数要为0,而且在多线程间的同步是可以通过有名信号量也可通过无名信号,但是一般情况线程的同步是无名信号量,无名信号量使用简单,而且sem_t存储在进程空间中,有名信号量必须LINUX内核管理,由内核结构struct ipc_ids 存储,是随内核持续的,系统关闭,信号量则删除,当然也可以显示删除,通过系统调用删除,消息队列,信号量,内存共享,这几个都是一样的原理。,只不过信号量分为有名与无名
无名使用 <semaphore.h>,
有名信号量<sys/sem.h>
无名信号量不能用进程间通信,
//无名与有名的区别,有名需要KEY值与IPC标识
所以sem_init的第二个参数必须为0,,,,
你看的是内核编程或驱动,用的是内核的头文件,在内核目录源码的include里:<asm/semaphore.h>
void down(struct semaphore *sem)
int down_interruptible(struct semaphore *sem)
int down_trylock(struct semaphore *sem)
。。。。。。。
而应用编程对应的在/usr/include
一个是semaphore set 在 "sys/sem.h" 里
一个是semaphore 在semaphore里
至于这连个的区别和标准参见unix system program 一书
欢迎分享,转载请注明来源:夏雨云
评论列表(0条)