ucos 建立信号量OSSemCreate什么时候计数初值用1,什么时候用0?

ucos 建立信号量OSSemCreate什么时候计数初值用1,什么时候用0?,第1张

我拷贝了之前回答的答案:

OSSemPost 和OSSemPend是成对出现的,在程序OSSemPost 尚未运行到的时候,在等待Sem的

OSSemPend是会把当前的任务挂起,直到另外一个任务的OSSemPost 运行完毕都得到Sem。但是可以通过改变OSSemCreate(x)里面的值x改变这种局面,当x不为0时,OSSemPend会马上得到Sem继续运行当前任务至结束,并将x的数值减一,直到为0。为0后,只有等其他任务的OSSemPost了。

semget() 可以使用系统调用semget()创建一个新的信号量集,或者存取一个已经存在的信号量集:

系统调用:semget()

原型:intsemget(key_t key,int nsems,int semflg)

返回值:如果成功,则返回信号量集的IPC标识符。如果失败,则返回-1:errno=EACCESS(没有权限)

EEXIST(信号量集已经存在,无法创建)

EIDRM(信号量集已经删除)

ENOENT(信号量集不存在,同时没有使用IPC_CREAT)

ENOMEM(没有足够的内存创建新的信号量集)

ENOSPC(超出限制)

系统调用semget()的第一个参数是关键字值(一般是由系统调用ftok()返回的)。系统内核将此值和系统中存在的其他的信号量集的关键字值进行比 较。打开和存取操作与参数semflg中的内容相关。IPC_CREAT如果信号量集在系统内核中不存在,则创建信号量集。IPC_EXCL当和 IPC_CREAT一同使用时,如果信号量集已经存在,则调用失败。如果单独使用IPC_CREAT,则semget()要么返回新创建的信号量集的标识 符,要么返回系统中已经存在的同样的关键字值的信号量的标识符。如果IPC_EXCL和IPC_CREAT一同使用,则要么返回新创建的信号量集的标识 符,要么返回-1。IPC_EXCL单独使用没有意义。参数nsems指出了一个新的信号量集中应该创建的信号量的个数。信号量集中最多的信号量的个数是 在linux/sem.h中定义的:

这是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/119442.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存