linux编程时的信号量问题。 我以前用过的信号量头文件是<semaphore.h>,而现在又发现还有个<syssem.h>

linux编程时的信号量问题。 我以前用过的信号量头文件是<semaphore.h>,而现在又发现还有个<syssem.h>,第1张

信号量在进程是以有名信号量进行通信的,在线程是以无名信号进行通信的,因为线程linux还没有实现进程间的通信,所以在sem_init的第二个参数要为0,而且在多线程间的同步是可以通过有名信号量也可通过无名信号,但是一般情况线程的同步是无名信号量,无名信号量使用简单,而且sem_t存储在进程空间中,有名信号量必须LINUX内核管理,由内核结构struct ipc_ids 存储,是随内核持续的,系统关闭,信号量则删除,当然也可以显示删除,通过系统调用删除,

消息队列,信号量,内存共享,这几个都是一样的原理。,只不过信号量分为有名与无名

无名使用 <semaphore.h>,

有名信号量<sys/sem.h>

无名信号量不能用进程间通信,

//无名与有名的区别,有名需要KEY值与IPC标识

所以sem_init的第二个参数必须为0,,,,

这是sys/sem.h文件的内容

/* @(#) 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)


欢迎分享,转载请注明来源:夏雨云

原文地址:https://www.xiayuyun.com/zonghe/314258.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-29
下一篇2023-04-29

发表评论

登录后才能评论

评论列表(0条)

    保存