Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 14
Hi, I have written one application, where i created one thread that handles one task. I am executing this thread after every 4 mins. and exiting from this thread after ...
  1. #1
    Just Joined!
    Join Date
    Feb 2008
    Posts
    50

    Help: Thread Failed

    Hi,

    I have written one application, where i created one thread that handles one task. I am executing this thread after every 4 mins. and exiting from this thread after use.

    But when i put my application running, after 2 days the thread fails, but application running properly., but the task that i scheduled for this thread getting failed..

    so can any tell me how to recover this failing of thread.

    Please help me on this.

    thanks in advance.

  2. #2
    Just Joined!
    Join Date
    Nov 2008
    Posts
    9
    Hi,

    Thsi is very strange problem and unfortunately I can not help you but plz tell me did you find the solution and if yes plz share with us.

    one possible scenario might be like the thread is doing some improper calculations because of wch it is receiving kill signal.

    plz share.

  3. #3
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    so can any tell me how to recover this failing of thread
    What exactly do you observe? Do you get any error indications from calls of library functions?
    --
    Bill

    Old age and treachery will overcome youth and skill.

  4. #4
    Just Joined!
    Join Date
    Feb 2008
    Posts
    50
    Thanks for replies.

    I putted all my logs in one txt file, where it shows pthread_create returns < 0.

    After that, I have added pthread_join to wait main process for this thread. and pthread_exit((void *)101); at end of thread to know thread correctly exited or not.

    So till from my testing it showing correct pthread_exit from logs (log shows 101, after pthread_join)

    Can I add more in code to find problem if thread fails again.

    Thanks.

  5. #5
    Just Joined!
    Join Date
    Nov 2008
    Posts
    9
    Hi,

    It will be good for analysis if you can add your code and your complete log.

  6. #6
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    I putted all my logs in one txt file, where it shows pthread_create returns < 0.
    That won't be extremely useful.

    With many library calls, a return of -1 means you can examine errno to see what the error is. But that is not the case with POSIX threads.

    With functions in the POSIX threads library, returning 0 means no error, just as before. But if there is an error, the error code is returned; that makes error returns be greater than zero, not less than zero. So examine those greater-than-zero values; they'll have the same meanings as you'd use for the content of errno.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  7. #7
    Just Joined!
    Join Date
    Feb 2008
    Posts
    50
    Yes Now, I have also added logs for return value of pthread_create like EAGAIN, EINVAL, EPERM etc.

    One other questions is, here i am using 5 global structure variables within different threads but i haven't used any locking mechanism. Is it necessary within my code? I am confusing about whether to use locking or not!

  8. #8
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    i am using 5 global structure variables within different threads but i haven't used any locking mechanism. Is it necessary within my code?
    Yes indeed! Processing in one thread may be suspended at any time so that another thread may run. Here's an example.

    Let's say you have a global unsigned long called counter_1. Let's say the current value of that variable is 5.

    Now let's say that there are two threads. I'll call them fred and barney. Both of those threads wish to add 1 to the value of that variable. This is the kind of thing that can happen:
    1. Fred is running. He says this:
      Code:
      counter_1++;
      The machine code picks up the current value, 5, and gets set to store the value 6 into the counter.
    2. But before fred can do that, he gets suspended and barney runs. Barney also says this:
      Code:
      counter_1++;
      The machine code picks up the current value, 5, and stores the value 6 into the counter.
    3. Eventually, fred continues running, and stores the value 6 into the counter, as he had originally planned.

    Not a good situation. The code needs to lock that value before attempting to change it. In fact, it's a good idea to lock the value even if no changes are intended. If it's something as complicated as a struct, one thread could catch the struct in an inconsistent state, while another thread has modified one member of the struct but not yet another member which must be consistent with the first member somehow.

    Hope this helps.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  9. #9
    Just Joined!
    Join Date
    Feb 2008
    Posts
    50
    Thanks a lot Bill, You cleared my confusion.
    Which should I prefer Mutex or Semaphore ? Which is better one in this condition?

  10. #10
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Mutex or Semaphore ?
    If you're using POSIX threads, use a mutex.

    If you're using separate processes with shared memory, use a semaphore.
    --
    Bill

    Old age and treachery will overcome youth and skill.

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
  •  
...