LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 07-07-2007, 06:43 AM   #1
piyush.kansal
LQ Newbie
 
Registered: May 2007
Location: Pune, India
Distribution: Fedora Core
Posts: 29

Rep: Reputation: 15
Lightbulb Does killing the thread kill its parent process?


Hi,

If i want to kill the thread of a parent process, but its killing the whole parent process. This seems to be obvious thing as threads does not have different PIDs but before proceeding further, i would like you to read my first thread http://www.linuxquestions.org/questi...d.php?t=567347

So, as per this thread, i can see that doing ps -efm shows me that a different PID has been allocated to the thread. So, taking that PID i tried to kill it, but it was killing the parent process?

Can anyone tell me where i am doing wrong and then how can i achieve my objective?
 
Old 07-07-2007, 07:37 AM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
pthreads programming requires a certain amount of cooperation between threads. There's cleanup code and other stuff going on under the hood that would get messed up if you were able simply to kill a thread.

The only way to kill a thread is, within your program, calling pthread_cancel(). Even then it's complex and often dangerous.

The best way in the long run is to write the code so that it cleanly does a pthread_exit() if it senses that you want it to stop.

Hope this helps.
 
Old 07-07-2007, 11:06 AM   #3
piyush.kansal
LQ Newbie
 
Registered: May 2007
Location: Pune, India
Distribution: Fedora Core
Posts: 29

Original Poster
Rep: Reputation: 15
Ok...
So what you mean is that its a construct imposed by the kernel that we cant kill the pthreads just like that.
 
Old 07-07-2007, 10:22 PM   #4
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
Each individual thread in a program using pthreads is part of the same process (you can not signal individual threads). You have to code your program to do any thread management you want done.
 
Old 07-08-2007, 07:53 AM   #5
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
Quoth piyush.kansal:

Quote:
So what you mean is that its a construct imposed by the kernel that we cant kill the pthreads just like that.
No, what I mean is that it's a detail of pthreads itself, for your own protection. If you were able to kill a thread, then the coordination between threads would be broken, and your program would misbehave.
 
Old 07-08-2007, 08:02 AM   #6
piyush.kansal
LQ Newbie
 
Registered: May 2007
Location: Pune, India
Distribution: Fedora Core
Posts: 29

Original Poster
Rep: Reputation: 15
Quote:
No, what I mean is that it's a detail of pthreads itself, for your own protection. If you were able to kill a thread, then the coordination between threads would be broken, and your program would misbehave.
Ok, thanks a lot.
 
Old 07-08-2007, 08:09 AM   #7
piyush.kansal
LQ Newbie
 
Registered: May 2007
Location: Pune, India
Distribution: Fedora Core
Posts: 29

Original Poster
Rep: Reputation: 15
Smile

Quote:
Each individual thread in a program using pthreads is part of the same process (you can not signal individual threads). You have to code your program to do any thread management you want done.
I agree with you. In fact, I was told the same thing by other members also. But, the problem is that since we can't catch SIGKILL so even if I want to implement my own thread killing mechanism, I am helpless

So, I just thought of consulting you guys. Anyways thanks a lot
 
Old 07-09-2007, 08:11 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Actually, you can use another SIG eg SIGUSR1 to mean kill a thr when you catch it.
 
  


Reply



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
child process usses same amount of ram as parent process socialjazz Programming 7 10-19-2006 05:48 PM
getting parent process ID in sh pradeepp Programming 2 09-22-2005 06:13 AM
how to make ppid of a process as init without killing its parent shantha Programming 10 12-17-2004 07:36 AM
When I kill parent process, child isn't killed BengeBoy Linux - Software 0 08-19-2004 04:38 PM
Bash Scripting - child process affecting parent process mthaddon Linux - General 1 05-02-2004 01:19 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:15 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration