0

I want to kill other application similar to killall command as I know application but can not get PID at run time.

This has to be done in C code. I know kill() but it need PID. Any other way equivalent to killall ?

1

kill(2) is the system call to send a signal to a process there isn't an equivilent to the killall utility.

An easy way to do this in C would be to invoke killall from your C program, using the system(3) library function or possible popen(3).

Alternatively you could read the manual page about the /proc pseudo file system and search for the command names and find the pids yourself.

man 2 kill
man 3 system
man 3 popen
man 5 proc
  • This should be a FAQ answer: 1) read man before asking questions about UNIX commands and 2) there is a syscall for almost every base command. – ajeh Apr 26 '18 at 14:43
0

To answer the question in the title, no there is no C library that does this matching. Not even libprocps does this.

killall is a simple program that:

  • works out what you want to match against
  • effectively does ls on /proc looking for directories with only numbers in their name and matches against files under those directories

Due to it being generic (e.g. it doesn't know what match criteria a user will use beforehand) it has lots of matches. You should know what you want to match already. Your question doesn't really say but it sounds like the name or the command line.

I really caution this entire approach. Processes should be really sure about what other processes they are touching. Name is a terrible match as I can trivially fake that. Also consider you may have two users or two systems using the same name, which process should be killed?

PID files or some other method that records the PID on program commencement is much better because you know exactly which process you are talking about (unless they fork)

As Richard points out above, the killall code is GPL2+ so you can reuse it with the same license. The project moved to gitlab though and is at https://gitlab.com/psmisc/psmisc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.