About the failure of semaphore processing in Linux-Linux general technology-Linux programming and kernel information, the following is a detailed description. OS: SUSE Linux unzip ise9.0
Problem description: The created semaphore. An error is returned after sem_wait is called. The error code 22 (EINVAL) indicates that it is invalid with the sem address space.
Analysis: the establishment and use of semaphores are related to the first semaphores address and cannot be on odd addresses. The comparison structure in the test is as follows,
The definition of the data structure adopts byte alignment (only in this way will there be odd addresses)
Int main (void ){
Int iii;
Pthread_t t1, t2, t3, t4;
Ss = (SSSS *) malloc (sizeof (SSSS ));
Ss-> iii = 0xFF;
Sem_init (& ss-> sem, 0, 1 );
Sem_getvalue (& ss-> sem, & val );
Printf ("val1 = % d \ n", val );
Sleep (2 );
Sem_getvalue (& ss-> sem, & val );
Printf ("val2 = % d \ n", val );
Iii = sem_wait (& ss-> sem );
Printf ("main & ss-> sem = % d OK \ n", & ss-> sem );
Sem_getvalue (& ss-> sem, & val );
Printf ("val3 = % d \ n", val );
Iii = sem_wait (& ss-> sem );
Printf ("main & ss-> sem = % d OK \ n", & ss-> sem );
Sem_post (& ss-> sem );
Getchar ();
}
Because semaphores involve Linux core code, the restrictions may be imposed to improve access efficiency. However, no official explanation has been found for such problems, prepare to take a look at the implementation source code, you should be able to know the exact cause of the problem, first record it, for XDJM to solve the problem first.
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