LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Thread Status.. How to know...??? (https://www.linuxquestions.org/questions/programming-9/thread-status-how-to-know-321912/)

rajsun 05-10-2005 05:37 AM

Thread Status.. How to know...???
 
Hi All,
Is it possible to know whether a thread is running or terminated by using the thread id in main function ( Thread id whose address has been passed as argument to the pthread_create function )... ? In C programming language.....

If possible then how plz let me know....
Else how one can know the status of the spawned thread....?

Thanking u all....
With Regard
RajSun

rose_bud4201 05-10-2005 10:17 PM

Yes, it is. The wait() function, or waitpid(), will cause the parent program to wait until the child process terminates (and thus, makes its status known).

It is best used within an if block, so that the waitpid() call is not accidentally called by the child process (which would be messy at worst, and uncompileable at best :))

Code:

#include <sys/types.h>
#include <sys/wait.h>

int main() {
int status;
pid_t id = -1;
pid_t child_id;

id = fork();
if(id < 0) {  //problems with the fork call
  return 1;
}

if(id == 0) {  //this will be executed by the child process
  // do stuff
  return 0;
}

if (id != 0) { //this will be executed by the parent process
  //do stuff
 
  //wait for the child process to exit
  child_id = wait (&status);
  //child_id contains numeric exit status of child process
  //status contains exit status information of child process
}

//only parent running now
exit 0;
}

You can get more dynamic information using semaphores and message passing between the child & parent processes. However, if you're not familiar with that a google search on those topics would probably be more useful than me trying to explain them here (and it's been awhile since I used them, so I couldn't do a very good job anyhow).

G'luck!

rajsun 05-11-2005 04:15 AM

Thanks for replying my question.....

But Sir I m talking about THREADS such as Pthread library supported..... threads.

rose_bud4201 05-11-2005 12:23 PM

Same thing, pretty much. Pthreads are more efficient but they're both ways to make unix fork off child processes, and they use the same message-passing algorithm - use a mutex to lock the memory location you use to store the message you'd like to pass.
i.e.
Parent locks mutex
Parent writes to buffer
Parent unlocks mutex

Child (which has been waiting on the mutex) locks mutex
Child reads from buffer
Child perhaps writes to buffer
Child unlocks mutex

etc etc etc. Take a look around google, there are many good resources for both forked processes and pthreads:
http://www.llnl.gov/computing/tutorials/pthreads/ (The section on Condition Variables is probably the most helpful one here).

I hope that helps a little more, I apologize for the misunderstanding!

rajsun 05-11-2005 11:23 PM

Thanks a lot man. I got a good material.....


All times are GMT -5. The time now is 07:08 PM.