2006-1-11
时间:2006-01-11 来源:晏东
2006年1月11日星期三 阴
a) 信号量IPC
i. semid_ds结构,用于存储信号量信息
/* One semid data structure for each set of semaphores in the system. */
struct semid_ds {
struct ipc_perm sem_perm; /* permissions .. see ipc.h */
time_t sem_otime; /* last semop time */
time_t sem_ctime; /* last change time */
struct sem *sem_base; /* ptr to first semaphore in array */
struct wait_queue *eventn;
struct wait_queue *eventz;
struct sem_undo *undo; /* undo requests on this array */
ushort sem_nsems; /* no. of semaphores in array */
}
意义:
sem_perm:ipc_perm结构
sem_otime:最后一次semop操作时间
sem_ctime:最后一次改动次数据结构的时间
sem_base:指向数组第一个信号的指针
此结构定义如下:
/* One semaphore structure for each semaphore in the system. */
struct sem {
short sempid; /* pid of last operation */
ushort semval; /* current value */
ushort semncnt; /* num procs awaiting increase in semval */
ushort semzcnt; /* num procs awaiting semval = 0 */
};
sem_pid 最后一个操作的P I D(进程I D)。
sem_semval 信号量的当前值。
sem_semncnt 等待资源的进程数目。
sem_semzcnt 等待资源完全空闲的进程数目。
undo:数组中没有完成的请求个数
sem_nsems:没有完成的请求个数
ii. 系统调用int semget(key_t key, int nsems, int flag);
成功返回IPC标识符,错误-1且生成errno;
nsems:生成新的信号集的个数,有最大个数限制,限制在/sys/sem.h中
flag:和消息队列相同,IPC_CREAT和 IPC_EXCL连用时,只能新建。