Hi all.
I want compile my program that uses semaphore for processes with the posix standard...but if I compile my program with:
I don't have the behavior of the posix semaphore.Code:gcc -o myprogram myprogram.c
Do you know any option for gcc?
Hi all.
I want compile my program that uses semaphore for processes with the posix standard...but if I compile my program with:
I don't have the behavior of the posix semaphore.Code:gcc -o myprogram myprogram.c
Do you know any option for gcc?
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
if i write:
i have this man:Code:man semop
where there is a specification of semop different from the specification of posix.Code:SEMOP(2) Linux Programmer’s Manual SEMOP(2) NAME semop, semtimedop - semaphore operations SYNOPSIS #include <sys/types.h> #include <sys/ipc.h> #include <sys/sem.h> int semop(int semid, struct sembuf *sops, unsigned nsops); int semtimedop(int semid, struct sembuf *sops, unsigned nsops, struct timespec *timeout); Feature Test Macro Requirements for glibc (see feature_test_macros(7)): semtimedop(): _GNU_SOURCE DESCRIPTION Each semaphore in a semaphore set has the following associated values: unsigned short semval; /* semaphore value */ unsigned short semzcnt; /* # waiting for zero */ unsigned short semncnt; /* # waiting for increase */ pid_t sempid; /* process that did last op */ ...
in fact if i write
i have:Code:man 3 semop
and I'm interested in this!Code:SEMOP(P) POSIX Programmer’s Manual SEMOP(P) NAME semop - XSI semaphore operations SYNOPSIS #include <sys/sem.h> int semop(int semid, struct sembuf *sops, size_t nsops); DESCRIPTION The semop() function operates on XSI semaphores (see the Base Definitions volume of IEEE Std 1003.1-2001, Section 4.15, Semaphore). It is unspecified whether this function interoperates with the realtime interprocess communication facilities defined in Realtime . The semop() function shall perform atomically a user-defined array of semaphore operations on the set of semaphores associated with the semaphore identifier specified by the argument semid. The argument sops is a pointer to a user-defined array of semaphore operation structures. The implementation shall not modify elements of this array unless the application uses implementation-defined extensions. ....
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
no...i'm not in luck...
when i program the software with semaphore i don't have the behavior described in the second man pages...
the behavior of the semaphore. more precisely, offunction meets the specifications of the first man page!!Code:semop
You understand?
Hmm. The man page I found most useful when doing semaphore programming was this one:
semop
Which looks to me to be identical to the man 3 page (so I can attest to the fact that the linux implementation does work this way). The Open Group are POSIX, that site is the POSIX standard.
I did not closely compare them, but these (man 2 and man 3) look more or less identical to me, the only thing that jumped out at me was that struct sembuf sem_id is specifically unsigned in the linux page. I do not think this matters.
Also, semval (for example) is not explicitly defined in the POSIX page -- it just says "see <sys/sem.h>", whereas sem.h is quoted specifically in the linux page. However, if you look at the online version of man 3 via that link you'll notice it's "see <sys/sem.h>" wherein semval is defined exactly as it is in the linux page.
Anyway, that's the limit of my experience. If you know more than me and there is some specific incompatibility between the POSIX standard and the linux implementation, please let me know!
If not, I think they are two different descriptions of the same thing. Obviously, the linux one is more specific because it is a real implementation and not a standard governing the parameters of such. I found the POSIX page more helpful, tho, and you can use just that. If you haven't done any semaphore programming, pay particular attention to this:
I just wanted to emphasize that the absolute value (a positive integer) of sem_op is used there, this threw me for a loop initially.The variable sem_op specifies one of three semaphore operations:
1. If sem_op is a negative integer and the calling process has alter permission, one of the following shall occur:
* If semval(see <sys/sem.h>) is greater than or equal to the absolute value of sem_op, the absolute value of sem_op is subtracted from semval. Also, if (sem_flg &SEM_UNDO) is non-zero, the absolute value of sem_op shall be added to the semadj value of the calling process for the specified semaphore.
* If semval is less than the absolute value of sem_op and (sem_flg &IPC_NOWAIT) is non-zero, semop() shall return immediately.
* If semval is less than the absolute value of sem_op and (sem_flg &IPC_NOWAIT) is 0, semop() shall increment the semncnt associated with the specified semaphore and suspend execution of the calling thread until one of the following conditions occurs:
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
MK27...you were in right...
it's my fault. the problem is another!!!!
the answer is: why the program prints "after"? why semop doesn't block the process?Code:#include <stdio.h> #include <sys/stat.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/sem.h> #include <stdlib.h> //A = 1,2,1 ---- B = 3,0,5 union semun { int val; struct semid_ds* buf; unsigned short int* array; struct seminfo* __buf; }; int main() { int sem_id; union semun argument; unsigned short values[3]; sem_id = semget(IPC_PRIVATE, 3, IPC_CREAT |IPC_EXCL |S_IRUSR | S_IWUSR); values[0] = 1; values[1] = 1; values[2] = 0; argument.array = values; semctl(sem_id,0,SETALL,argument); semctl(sem_id,1,SETALL,argument); semctl(sem_id,2,SETALL,argument); struct sembuf operations[3]; operations[0].sem_num = 0; operations[0].sem_op = -1; operations[0].sem_flg = 0; operations[1].sem_num = 1; operations[1].sem_op = -2; operations[1].sem_flg = 0; operations[2].sem_num = 2; operations[2].sem_op = -1; operations[2].sem_flg = 0; semop(sem_id,operations,1); printf("after...\n") union semun ignored_argument; semctl (sem_id, 1, IPC_RMID, ignored_argument); return 0; }
the values of semaphore is:
1 1 0
but the operations is, respectly
-1 -2 -1
so: 1-1 = 0 OK, but 1-2 <0 KO => blocked!
but i have the "after" string....
This: Semaphores in Linux - O'Reilly Media might help.
i've found the problem...
the right line of semop functions is:
because the last argument nsops is the number of sembuf structures in the array!Code:semop(sem_id,operations,3)
i.e.
thank you jeffcobbnsops:
Determines how many sembuf you are passing. The nsops argument is provided in case
the operation needs to be performed on bunch of semaphores at one time.