LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 01-10-2013, 03:24 AM   #1
kdelover
Member
 
Registered: Aug 2009
Posts: 311

Rep: Reputation: 36
C program to kill process


Hi folks,

I am trying to write a C program to kill running processing by their process ids.Although,i have been able to get pids,but i cant seem to kill them.Basically,i programmed in perl and python not in C.So,kindly be gentle


Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <error.h>
#include <signal.h>
#include <unistd.h>
#include <syslog.h>

int main()
{
    FILE *getPIDS;
    char line[130];
    pid_t killpid;
    // setuid to that of root//
    pid_t mypid   = getpid();
    pid_t myppid  = getppid();
    getPIDS = popen("pidof -x yes","r");
    while (fgets(line,sizeof line,getPIDS)) {
            printf("KILL PID: %s",line);
            kill(line,SIGKILL);
    }
}
Output:

Code:
# ./killpids
KILL PID: 11251 11250
 
Old 01-10-2013, 03:32 AM   #2
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 647

Rep: Reputation: 189Reputation: 189
I would suggest you to read first the manual for the kill system call:
Code:
man 2 kill
 
Old 01-10-2013, 03:56 AM   #3
kdelover
Member
 
Registered: Aug 2009
Posts: 311

Original Poster
Rep: Reputation: 36
Code:
int i = atoi(line);
printf("pid: %d",i);
kill(i,SIGKILL);
This works for me.But this will only 1 process and thats the first one.
 
Old 01-10-2013, 04:37 AM   #4
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by kdelover View Post
Code:
int i = atoi(line);
printf("pid: %d",i);
kill(i,SIGKILL);
This works for me.But this will only 1 process and thats the first one.
That is correct - the PID is like an integer (pid_t to be exact). You cannot use a string here.

Using a negative number you can signal a whole process group in one call.

SIGTERM is preferable to SIGKILL to start with.
 
  


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
Alternative to kill system call using c program to test only process. deymrinmoy Linux - Kernel 3 01-16-2013 10:47 PM
Kill a zombie process - which process is the parent that I should kill? Mountain Linux - General 3 12-31-2011 02:44 PM
how to kill or clean a Uninterruptible slept session leader process from process tabl golden_boy615 Linux - General 1 12-14-2010 10:47 AM
bash `kill`: process 'B' silently dies; but process 'A' = `kill` spews back debris! GrapefruiTgirl Programming 9 06-23-2009 09:42 AM
How to KILL a process or program ? il-uvatar Linux - Newbie 25 05-02-2009 07:59 AM

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

All times are GMT -5. The time now is 05:32 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