LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-10-2005, 05:37 AM   #1
rajsun
Member
 
Registered: Mar 2005
Posts: 61

Rep: Reputation: 15
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
 
Old 05-10-2005, 10:17 PM   #2
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 927

Rep: Reputation: 30
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!

Last edited by rose_bud4201; 05-10-2005 at 10:20 PM.
 
Old 05-11-2005, 04:15 AM   #3
rajsun
Member
 
Registered: Mar 2005
Posts: 61

Original Poster
Rep: Reputation: 15
Thanks for replying my question.....

But Sir I m talking about THREADS such as Pthread library supported..... threads.
 
Old 05-11-2005, 12:23 PM   #4
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 927

Rep: Reputation: 30
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!
 
Old 05-11-2005, 11:23 PM   #5
rajsun
Member
 
Registered: Mar 2005
Posts: 61

Original Poster
Rep: Reputation: 15
Thanks a lot man. I got a good material.....
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Main thread sending notification to child thread rajesh_b Programming 1 09-22-2004 09:15 AM
Status of thread iftiuk Programming 1 07-29-2004 05:50 PM
configure qt thread issue (just compiled qt w/ -thread option) cleff Linux - Software 8 05-07-2004 11:11 PM
Thread status? LinuxLala LQ Suggestions & Feedback 11 01-19-2004 12:10 AM
New thread status suggestion ugge LQ Suggestions & Feedback 1 09-16-2001 10:04 AM


All times are GMT -5. The time now is 06:14 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration