#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    245
    Rep Power
    12

    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?

    Bash code
    1) killall smbd > /dev/null
    2) ifconfig eth0 down > /dev/null
    =========================

    C code ?

    1)
    2)
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    245
    Rep Power
    12
    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.
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,253
    Rep Power
    2222
    Originally posted by linh
    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);
    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.

    Originally posted by linh
    The above three call a bash command. I was wondering if there is a C function that kill a process directly.
    I find it almost surprising that so many shell commands have a corresponding C function by the same name, including kill:
    Code:
    KILL(2)             Linux Programmer's Manual             KILL(2)
    
    
    NAME
           kill - send signal to a process
    
    SYNOPSIS
           #include <sys/types.h>
           #include <signal.h>
    
           int kill(pid_t pid, int sig);
    
    DESCRIPTION
           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
           still performed.
    
    RETURN VALUE
           On success, zero is returned.  On error, -1  is  returned,
           and errno is set appropriately.
    
    ERRORS
           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.
    
    NOTES
           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
           down accidentally.
    
           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
           process.
    
    ...
    
    SEE ALSO
           _exit(2), killpg(2), signal(2),  tkill(2),  exit(3),  signal(7)
    Since kill really just sends a signal to a process, to signal the process with PID of pid to terminate, you would call:
    kill(pid,SIGTERM);
    And to shut it down hard (ie, "kill 9" it):
    kill(pid,SIGKILL);

    (kill -l) will list all the signals and their names

    And here's an interesting related Linux-specific function
    Code:
    TKILL(2)            Linux Programmer's Manual            TKILL(2)
    
    NAME
           tkill - send a signal to a single process
    
    SYNOPSIS
           #include <sys/types.h>
           #include <linux/unistd.h>
    
           _syscall2(int, tkill, pid_t, tid, int, sig)
    
           int tkill(pid_t tid, int sig);
    
    DESCRIPTION
           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
           unique TID.
    ...
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    245
    Rep Power
    12

    Kill a process id using C code


    The command
    kill(pid,SIGTERM);
    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.

IMN logo majestic logo threadwatch logo seochat tools logo