Results 1 to 10 of 12
Thread: mutex never fails
|
Enjoy an ad free experience by logging in. Not a member yet? Register.
|
|
-
07-30-2012 #1
- Join Date
- Dec 2011
- Posts
- 23
mutex never fails
I have tried in many ways to get the
printf("lock failed in 1\n"); or
printf("lock failed in 2\n");
Plese help me in achieving that.
Code:#include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <unistd.h> pthread_mutex_t mutex= PTHREAD_MUTEX_INITIALIZER; void *fun1(void* ); void *fun2(void* ); int arg = 20; int main( void ) { pthread_t tid1,tid2; pthread_attr_t attr; pthread_attr_init(&attr); printf(" arg = %d\n", arg); pthread_create(&tid1, NULL, fun1,(void*)1); pthread_join(tid1,NULL); printf(" 1arg = %d\n", arg); pthread_create(&tid2, NULL, fun2,(void*)2); pthread_join(tid2,NULL); printf(" 2arg = %d\n", arg); printf("end\n"); pthread_exit(NULL); return 0; } void * fun1( void *p) { int i; printf(" thrd %ld schded \n",(long) p); if (-1 != pthread_mutex_trylock(&mutex)) { printf("locked in 1\n"); if (arg == 20) arg = 10; sleep(15); pthread_mutex_unlock(&mutex); } else { printf("lock failed in 1\n"); } pthread_exit(NULL); } void* fun2( void *p) { int i; printf(" thrd %ld schded \n",(long) p); if (-1 != pthread_mutex_trylock(&mutex)) { printf("locked in 2\n"); if (arg == 10) arg = 30; pthread_mutex_unlock(&mutex); } else { printf("lock failed in 2\n"); } pthread_exit(NULL); }
-
08-01-2012 #2
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
- Posts
- 14,038
You aren't testing the lock correctly. Here is some information from the man page for the pthread_mutex_lock() functions:
Code:RETURN VALUE If successful, the pthread_mutex_lock() and pthread_mutex_unlock() functions shall return zero; otherwise, an error number shall be returned to indicate the error. The pthread_mutex_trylock() function shall return zero if a lock on the mutex object referenced by mutex is acquired. Otherwise, an error number is returned to indicate the error.
Code:void * fun1( void *p) { int i; printf(" thrd %ld schded \n",(long) p); if (0 == pthread_mutex_trylock(&mutex)) { printf("locked in 1\n"); if (arg == 20) arg = 10; sleep(15); pthread_mutex_unlock(&mutex); } else { printf("lock failed in 1\n"); } pthread_exit(NULL); }
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
-
08-01-2012 #3
- Join Date
- Dec 2011
- Posts
- 23
I tried by changing as you suggested , but the result is same.
arg = 20
thrd 1 schded
locked in 1
1arg = 10
thrd 2 schded
locked in 2
2arg = 30
end
i think it is some thing to do with the scheduling.
i dont know what kind of scheduling is used in my system
once i printed the scheduling policy it gave me "OTHER".
so i guess the system is executing one thread after the other.
can somebody point out where exactly the real problem is?
Thanks in advance.
-
08-01-2012 #4
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
- Posts
- 14,038
The wrong logic in the fun1() and fun2() code notwithstanding, your problem is likely a scheduling one as you surmise. Try a short sleep before you try to take the lock in fun2() (shorter than the 15 second sleep in fun1()) so you know that fun1() has established and is holding the lock.
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
-
08-01-2012 #5
- Join Date
- Dec 2011
- Posts
- 23
Code:#include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <unistd.h> pthread_mutex_t mutex= PTHREAD_MUTEX_INITIALIZER; void *fun1(void* ); void *fun2(void* ); int arg = 20; int main( void ) { pthread_t tid1,tid2; pthread_attr_t attr; pthread_attr_init(&attr); printf(" arg = %d\n", arg); pthread_create(&tid1, NULL, fun1,(void*)1); pthread_join(tid1,NULL); printf(" 1arg = %d\n", arg); pthread_create(&tid2, NULL, fun2,(void*)2); pthread_join(tid2,NULL); printf(" 2arg = %d\n", arg); printf("end\n"); pthread_exit(NULL); return 0; } void * fun1( void *p) { int i; printf(" thrd %ld schded \n",(long) p); if (0 == pthread_mutex_trylock(&mutex)) { printf("locked in 1\n"); if (arg == 20) arg = 10; sleep(15); pthread_mutex_unlock(&mutex); } else { printf("lock failed in 1\n"); } pthread_exit(NULL); } void* fun2( void *p) { int i; printf(" thrd %ld schded \n",(long) p); sleep(2); if (0 == pthread_mutex_trylock(&mutex)) { printf("locked in 2\n"); if (arg == 10) arg = 30; pthread_mutex_unlock(&mutex); } else { printf("lock failed in 2\n"); } pthread_exit(NULL); }
dont get whats wrong exaclty..
can i change the scheduling policy, will it effect the system in any other wayy.
-
08-02-2012 #6
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
- Posts
- 14,038
Well, it seems to be working as though pthread_mutex_lock() was called instead of trylock() - at least that's how it behaves on my RHEL 6 system. It may be that in our systems, the trylock() version is not yet implemented. There is no notice of that in the man pages, so I will have to do some more study.
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
-
08-02-2012 #7
- Join Date
- Dec 2011
- Posts
- 23
But we are not getting any undefined symbol errors here.
how to check a particular system call is implemented in the system or not?Last edited by oxpac; 08-02-2012 at 05:04 AM. Reason: gramatical error
-
08-02-2012 #8
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
- Posts
- 14,038
I haven't looked at the source yet since it was late last night when I made my previous reply (and I am engaged this evening as well), but I don't think it's a matter of implementation as much a matter of how you are setting up your environment and using the function. It may be that on the 2.6.24 kernel, it is not using the sys_msgctl() function, or are setting it up differently than you are. A full listing of your kernel module source code, and how your user-side functions are accessing it, would be helpful in my analysis.
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
-
08-03-2012 #9
- Join Date
- Dec 2011
- Posts
- 23
-
08-03-2012 #10