Jump to content

[POSIX] Kill all processes with name

- - - - -

  • Please log in to reply
2 replies to this topic

#1
jakash3

jakash3

    Newbie

  • Members
  • PipPip
  • 21 posts
Just like the Windows taskkill /f /t /im blah.exe command except this one is for *nix systems and it's a C function. Kills all processes with specified name.

It accomplishes this by reading the status file in each process's directory in /proc/. The status file contains the name of the process prefixed by "Name:".
#include <sys/types.h>
#include <signal.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

bool killp(const char* name) {
    pid_t p;
    size_t i;
    char* s = (char*)malloc(264);
    char buf[128];
    FILE* st;
    DIR* d = opendir("/proc");
    if (d == NULL) { free(s); return false; }
    struct dirent* f;
    while ((f = readdir(d)) != NULL) {
        if (f->d_name[0] == '.') continue;
        for (i = 0; isdigit(f->d_name[i]); i++);
        if (i < strlen(f->d_name)) continue;
        strcpy(s, "/proc/");
        strcat(s, f->d_name);
        strcat(s, "/status");
        st = fopen(s, "r");
        if (st == NULL) { closedir(d); free(s); return false; }
        do {
            if (fgets(buf, 128, st) == NULL) { fclose(st); closedir(d); free(s); return false; }
        } while (strncmp(buf, "Name:", 5));
        fclose(st);
        for (i = 5; isspace(buf[i]); i++);
        *strchr(buf, '\n') = 0;
        if (!strcmp(&(buf[i]), name)) {
            sscanf(&(s[6]), "%d", &p);
            kill(p, SIGKILL);
        }
    }
    closedir(d);
    free(s);
    return true;
}

int main() {
    char s[256];
    fputs("Enter name of process(es) to kill: ", stdout);
    gets(s);
    killp(s);
}

Posted Image
Posted Image

#2
fayyazlodhi

fayyazlodhi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 403 posts
It is certainly good code, specially from the perspective of exploring the proc file system.

However, i was wondering if it does any thing better / different from killall. I see plain kill call inside whose equivalent command line take pid as parameter. But there exists a killall command to


$ killall <name of executable> // if there are more than one such applications running all of them are killed


killall - Wikipedia, the free encyclopedia explains much details but on general Fedora etc Linux it only kills processes by name where as in BSD etc. it is used during restart / shutdown.

Edited by fayyazlodhi, 18 June 2011 - 10:51 AM.
typo


#3
jakash3

jakash3

    Newbie

  • Members
  • PipPip
  • 21 posts

fayyazlodhi said:

It is certainly good code, specially from the perspective of exploring the proc file system.

However, i was wondering if it does any thing better / different from killall. I see plain kill call inside whose equivalent command line take pid as parameter. But there exists a killall command to

$ killall <name of executable> // if there are more than one such applications running all of them are killed
killall - Wikipedia, the free encyclopedia explains much details but on general Fedora etc Linux it only kills processes by name where as in BSD etc. it is used during restart / shutdown.
I just looked at the bsd source code of killall.c and it looks like it uses sysctl() for accessing process and kernel information.

According to wikipedia: "In BSD the system call is implemented directly in the kernel, as described in the sysctl(3) manual page[2]. In Linux, the sysctl is implemented as a wrapper around file system routines that access contents of files in the /proc directory."

So in BSD, the way killall does it should be faster but in Linux it would have almost the same speed. sysctl is not portable anyways and having this function would save a call to setting up the killall process if you used system("killall").
Posted Image
Posted Image




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users