August 1st, 2003, 06:47 PM
C code for killall function
What is the C code equivalent for this bash code below
In C code, I can only find this
kill (pid, SIGTERM)
I know how to search for a process name and find its pid to shut down a process, but is there a killall function in C code as in
killall (pid, SIGTERM) ?
August 2nd, 2003, 06:44 AM
There is not such a function. There are a couple of ways to approach this problem.
The first method is hairy and as non-portable as it gets. You'll need to make a call to whatever function gets a process listing on your operating system. Look into the ps source code to see what that is. Then filter the list to choose only the desired target and get the process id. With process id in hand, kill it with the desired signal. Every operating system has a slightly different call to get the process list, and returns a different structure. To make matters even more fun, the structure changes with each version of the operating system, and many of the components are not documented.
The second is more portable, but you might as well use the shell code. Open a pipe to ps, again filtering for the matching processed. Get the process ids and kill with the desired signal.
Articles and commentary on web development
August 2nd, 2003, 12:21 PM
Instead of looking at the source for ps, why not look at the source for killall, wouldn't that basically provide you with what you want?