SETVAL semaphores? -- Linux general technology-Linux programming and kernel information. The following is a detailed description. Main ()
{
Int flag1, flag2, key, I, init_ OK, tmperrno;
Struct semid_ds sem_info;
Struct seminfo sem_info2;
Union semun arg; // union semun: See appendix 2.
Struct sembuf askfor_res, free_res;
Flag1 = IPC_CREAT | IPC_EXCL | 00666;
Flag2 = IPC_CREAT | 00666;
Key = ftok (SEM_PATH, 'A ');
// Error handling for ftok here;
Init_ OK = 0;
Semid = semget (key, 1, flag1); // create a semaphore set that only includes des one semphore.
If (semid <0)
{
Tmperrno = errno;
Perror ("semget ");
If (tmperrno = EEXIST)
// Errno is undefined after a successful
// Library call (including perror call)
// So it is saved
// In tmperrno.
{
Semid = semget (key, 1, flag2 );
// Flag2 only contains the IPC_CREAT flag. The nsems parameter (
// Here is 1) it must be consistent with the number of original signal lights
Arg. buf = & sem_info;
For (I = 0; I {
If (semctl (semid, 0, IPC_STAT, arg) =-1)
{
Perror ("semctl error ");
I = max_tries;
}
Else
{// Sem_otime is not 0, indicating Initialization is complete
If (arg. buf-> sem_otime! = 0)
{
I = max_tries;
Init_ OK = 1;
}
Else
Sleep (1 );
}
}
If (! Init_ OK)
// Do some initializing, here we assume that the first process that creates the sem will
// Finish initialize the sem and run semop in max_tries * 1 seconds. else it will not run
// Semop any more.
{
Arg. val = 1;
If (semctl (semid, 0, SETVAL, arg) =-1) perror ("semctl setval error ");}
}
The red part of the Code sets the semaphores created by other processes as mutex.
Can this be done? If the process that creates this semaphore sets it as a Count semaphore, What effect will this code produce?
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service