June 12th, 2003, 11:04 AM
Converting Bash code to C code
What is the equivalent built in function call in C to kill a process
and to shutdown an ethernet card?
1) killall smbd > /dev/null
2) ifconfig eth0 down > /dev/null
C code ?
June 12th, 2003, 12:20 PM
Of course, the answer is
use one of the three
1) system ("killall smbd > /dev/null")
2) FILE *f;
f = popen ("killall smbd > /dev/null")
3) execlp ("/bin/bash", "bash", "-c", "killall smbd > /dev/null", (char *)0);
The above three call a bash command. I was wondering if there is a C function that kill a process directly.
June 12th, 2003, 12:59 PM
Actually, #2 would be a poor choice since you are redirecting stdout to the null device and will therefore have no need for the pipe.
I find it almost surprising that so many shell commands have a corresponding C function by the same name, including kill:
Since kill really just sends a signal to a process, to signal the process with PID of pid to terminate, you would call:
KILL(2) Linux Programmer's Manual KILL(2)
kill - send signal to a process
int kill(pid_t pid, int sig);
The kill system call can be used to send any signal to any
process group or process.
If pid is positive, then signal sig is sent to pid.
If pid equals 0, then sig is sent to every process in the
process group of the current process.
If pid equals -1, then sig is sent to every process except
for process 1 (init), but see below.
If pid is less than -1, then sig is sent to every process
in the process group -pid.
If sig is 0, then no signal is sent, but error checking is
On success, zero is returned. On error, -1 is returned,
and errno is set appropriately.
EINVAL An invalid signal was specified.
ESRCH The pid or process group does not exist. Note that
an existing process might be a zombie, a process
which already committed termination, but has not
yet been wait()ed for.
EPERM The process does not have permission to send the
signal to any of the receiving processes. For a
process to have permission to send a signal to pro-
cess pid it must either have root privileges, or
the real or effective user ID of the sending pro-
cess must equal the real or saved set-user-ID of
the receiving process. In the case of SIGCONT it
suffices when the sending and receiving processes
belong to the same session.
It is impossible to send a signal to task number one, the
init process, for which it has not installed a signal han-
dler. This is done to assure the system is not brought
POSIX 1003.1-2001 requires that kill(-1,sig) send sig to
all processes that the current process may send signals
to, except possibly for some implementation-defined system
processes. Linux allows a process to signal itself, but
on Linux the call kill(-1,sig) does not signal the current
_exit(2), killpg(2), signal(2), tkill(2), exit(3), signal(7)
And to shut it down hard (ie, "kill 9" it):
(kill -l) will list all the signals and their names
And here's an interesting related Linux-specific function
TKILL(2) Linux Programmer's Manual TKILL(2)
tkill - send a signal to a single process
_syscall2(int, tkill, pid_t, tid, int, sig)
int tkill(pid_t tid, int sig);
The tkill system call is analogous to kill(2), except when
the specified process is part of a thread group (created
by specifying the CLONE_THREAD flag in the call to clone).
Since all the processes in a thread group have the same
PID, they cannot be individually signalled with kill.
With tkill, however, one can address each process by its
June 12th, 2003, 01:21 PM
Kill a process id using C code
will kill a process id.
1) Is there a way to kill a process say smbd directly without having to find out what its process id is ?
2) What C command would I use to find the process id for say smbd without having to use ps -ef | grep smbd.
Another word, I wanted my C program to use as many C function as possible so that it does not have to resort to calling bash command within a C code.