Don't forget to use fourth argument if O_CREAT is set in argument oflag.
The fourth specifies initial value for the semaphore.
This commit is contained in:
parent
e33b1a7e54
commit
323d80a0a7
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=201715
@ -143,6 +143,7 @@ _sem_open(const char *name, int flags, ...)
|
||||
struct sem_nameinfo *ni = NULL;
|
||||
sem_t *sem = NULL;
|
||||
int fd = -1, mode, len;
|
||||
int value = 0;
|
||||
|
||||
if (name[0] != '/') {
|
||||
errno = EINVAL;
|
||||
@ -170,6 +171,7 @@ _sem_open(const char *name, int flags, ...)
|
||||
if (flags & O_CREAT) {
|
||||
va_start(ap, flags);
|
||||
mode = va_arg(ap, int);
|
||||
value = va_arg(ap, int);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
@ -203,7 +205,7 @@ _sem_open(const char *name, int flags, ...)
|
||||
|
||||
tmp._magic = SEM_MAGIC;
|
||||
tmp._kern._has_waiters = 0;
|
||||
tmp._kern._count = 0;
|
||||
tmp._kern._count = value;
|
||||
tmp._kern._flags = USYNC_PROCESS_SHARED | SEM_NAMED;
|
||||
if (_write(fd, &tmp, sizeof(tmp)) != sizeof(tmp)) {
|
||||
flock(fd, LOCK_UN);
|
||||
|
@ -58,10 +58,10 @@ test_named(void)
|
||||
|
||||
printf("testing named process-shared semaphore\n");
|
||||
sem_unlink(SEM_NAME);
|
||||
s = sem_open(SEM_NAME, O_CREAT, 0777);
|
||||
s = sem_open(SEM_NAME, O_CREAT, 0777, 0);
|
||||
if (s == SEM_FAILED)
|
||||
err(1, "sem_open failed");
|
||||
s2 = sem_open(SEM_NAME, O_CREAT, 0777);
|
||||
s2 = sem_open(SEM_NAME, O_CREAT, 0777, 0);
|
||||
if (s2 == SEM_FAILED)
|
||||
err(2, "second sem_open call failed");
|
||||
if (s != s2)
|
||||
|
Loading…
Reference in New Issue
Block a user