LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 03-11-2009, 05:07 PM   #1
the_satsuma_man
LQ Newbie
 
Registered: Feb 2009
Posts: 8

Rep: Reputation: 0
Killing a Specific Process


How can I kill a specific process in C on linux. I have the PID of the process and want to kill it.

Thanks, Sunil
 
Old 03-11-2009, 05:25 PM   #2
frieza
Senior Member
 
Registered: Feb 2002
Location: harvard, il
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 3,233

Rep: Reputation: 406Reputation: 406Reputation: 406Reputation: 406Reputation: 406
if you want c code to do it
probably
system('kill -9 PID');
 
Old 03-11-2009, 05:39 PM   #3
the_satsuma_man
LQ Newbie
 
Registered: Feb 2009
Posts: 8

Original Poster
Rep: Reputation: 0
If I have stored the PID as a variable called 'id' can I get use the variable instead of PID.
eg system('kill -9 id');

or will id need to be in quotes as well, eg ('kill -9 'id'');
 
Old 03-11-2009, 05:41 PM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
I'd rather write:

Code:
#include <sys/types.h>
#include <signal.h>
...
kill(id, SIGTERM);
 
Old 03-11-2009, 09:42 PM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,642
Blog Entries: 4

Rep: Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933
Be sure to review the documentation in man kill so that you send the appropriate signal (whatever it is...) to the process in question.

Signal #9 (SIGKILL) is an unconditional kill-signal that a process cannot block. It dies, right then and there. But maybe a more "graceful" signal like SIGTERM might be more appropriate, giving the process a reasonable chance to clean-up its own affairs.
 
  


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
Killing a specific instance of a program created by a script derriva Programming 2 12-27-2006 04:42 AM
something is killing my daemon process KM3 Linux - General 1 10-23-2006 10:43 AM
Killing process by username mijohnst Linux - Software 4 05-30-2005 08:50 PM
Killing a process by its name rabeea Linux - Networking 3 03-17-2005 05:30 AM
Really Killing a Process! lazlow69 Linux - Newbie 14 05-10-2003 10:31 PM

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

All times are GMT -5. The time now is 06:43 AM.

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