C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 12th, 2003, 11:04 AM
linh linh is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 245 linh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 m 27 sec
Reputation Power: 6
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)

Reply With Quote
  #2  
Old June 12th, 2003, 12:20 PM
linh linh is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 245 linh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 m 27 sec
Reputation Power: 6
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.

Reply With Quote
  #3  
Old June 12th, 2003, 12:59 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,864 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 23 h 39 m 49 sec
Reputation Power: 462
Quote:
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.

Quote:
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.
...

Reply With Quote
  #4  
Old June 12th, 2003, 01:21 PM
linh linh is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 245 linh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 m 27 sec
Reputation Power: 6
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Converting Bash code to C code


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT