Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 12
Hi, I have tried in many ways to get the printf("lock failed in 1\n"); or printf("lock failed in 2\n"); atleast once, but i never could do it. Plese help me ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Dec 2011
    Posts
    21

    mutex never fails


    Hi,

    I have tried in many ways to get the
    printf("lock failed in 1\n"); or
    printf("lock failed in 2\n");
    atleast once, but i never could do it.

    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);
    }

  2. #2
    Linux Guru Rubberman's Avatar
    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
    11,655
    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.
    IE, you need to try this instead:
    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!

  3. #3
    Just Joined!
    Join Date
    Dec 2011
    Posts
    21
    Quote Originally Posted by Rubberman View Post
    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.
    IE, you need to try this instead:
    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);
    }


    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.

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Guru Rubberman's Avatar
    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
    11,655
    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!

  6. #5
    Just Joined!
    Join Date
    Dec 2011
    Posts
    21
    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);
    }
    i modified , but the result is same...

    dont get whats wrong exaclty..

    can i change the scheduling policy, will it effect the system in any other wayy.

  7. #6
    Linux Guru Rubberman's Avatar
    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
    11,655
    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!

  8. #7
    Just Joined!
    Join Date
    Dec 2011
    Posts
    21
    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

  9. #8
    Linux Guru Rubberman's Avatar
    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
    11,655
    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!

  10. #9
    Just Joined!
    Join Date
    Dec 2011
    Posts
    21
    Quote Originally Posted by Rubberman View Post
    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.

    this is my system info.

    skylab@skylab:~$ uname -r
    3.0.0-22-generic-pae
    skylab@skylab:~$ uname -v
    #36-Ubuntu SMP Tue Jun 12 17:31:49 UTC 2012

  11. #10
    Linux Guru Rubberman's Avatar
    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
    11,655
    Quote Originally Posted by Rubberman View Post
    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.
    Oops. I meant to post this reply on another thread... Doh! Sorry...
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •