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 15th, 2003, 02: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
C function to find a process ID and kill it

1) What is the C function to obtain a process id of say smbd and nmbd ?

2) What is the C function to stop a process ID?

I do not want to use system("killall nmbd") since this will execute a bash command from within a C program. I wanted to use all C function.

Reply With Quote
  #2  
Old June 16th, 2003, 02:53 AM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,587 Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 21 h 22 m 44 sec
Reputation Power: 997
>> 1) What is the C function to obtain a process id of say smbd and nmbd ?
man 3 sysctl
In particular, look in the man page of the above function for the sub-sections under CTL_KERN and KERN_PROC_ALL

>> 2) What is the C function to stop a process ID?
man 2 kill

I suggest that u look at the source code for killall, and pull out the relevant sections of code out, for your own app.

Reply With Quote
  #3  
Old June 16th, 2003, 12:08 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
obtaining a process id by using a C function.

sysctl is a system call that gives you the ability to fine tune kernel parameters.

I have not figured out how to use the sysctl C function to obtain a process id of say nmbd, smbd or any process by name.

Reply With Quote
  #4  
Old June 16th, 2003, 03:38 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,587 Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 21 h 22 m 44 sec
Reputation Power: 997
No, but you can use sysctl to get a list of all the current running processes and their pids. Then, all you have to do is run through the list and compare the names, till you find the process you want. That's how killall does it.

Reply With Quote
  #5  
Old June 19th, 2003, 11:11 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
use sysctl to get a list of all the current running processes and their pids

I got stuck.

Please show me the code example of how to use sysctl to get a list of all the current running processes and their pids

Reply With Quote
  #6  
Old June 20th, 2003, 11:11 AM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,587 Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 21 h 22 m 44 sec
Reputation Power: 997
Borrowing heavily from the FreeBSD killall source, I came up with this:
Code:
#include <sys/param.h>
#include <sys/user.h>
#include <sys/sysctl.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
  struct kinfo_proc *procs = NULL, *newprocs;
  char          thiscmd[MAXCOMLEN + 1];
  pid_t           thispid;
  int           mib[4];
  size_t                miblen;
  int           i, st, nprocs;
  size_t                size;
  
  
  size = 0;
  mib[0] = CTL_KERN;
  mib[1] = KERN_PROC;
  mib[2] = KERN_PROC_ALL;
  mib[3] = 0;
  miblen = 3;
  
  st = sysctl(mib, miblen, NULL, &size, NULL, 0);
  do {
    size += size / 10;
    newprocs = realloc(procs, size);
    if (newprocs == 0) {
      if (procs)
        free(procs);
      errx(1, "could not reallocate memory");
    }
    procs = newprocs;
    st = sysctl(mib, miblen, procs, &size, NULL, 0);
  } while (st == -1 && errno == ENOMEM);
  
  nprocs = size / sizeof(struct kinfo_proc);

  /* Now print out the data */
  for (i = 0; i < nprocs; i++) {
    thispid = procs[i].kp_proc.p_pid;
    strncpy(thiscmd, procs[i].kp_proc.p_comm, MAXCOMLEN);
    thiscmd[MAXCOMLEN] = '\0';
    printf("%d\t%s\n", thispid, thiscmd);
  }
  
  /* Clean up */
  free(procs);
  return(0);
}

Hope this helps

Last edited by Scorpions4ever : June 20th, 2003 at 11:15 AM.

Reply With Quote
  #7  
Old June 20th, 2003, 11:41 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
Look for a process running

The program that Scorpionforever gave did not ran, but gave me
a compiled error.

1) What is the number range that MAXCOMLEN must have?
2) What value do other variable such as KERN_PROC, KERN_PROC_ALL, CTL_KERN must be set to?
3) When I run this program do I have to include external parameter like ./look_for_process_running2 nmbd.
I asked because it have the argc and argv in main.
int main(int argc, char **argv)


=======================================
root@galaxian:~# gcc -o look_for_process_running2 look_for_process_running2.c
look_for_process_running2.c: In function `main':
look_for_process_running2.c:10: `MAXCOMLEN' undeclared (first use in this function)
look_for_process_running2.c:10: (Each undeclared identifier is reported only once
look_for_process_running2.c:10: for each function it appears in.)
look_for_process_running2.c:10: size of array `thiscmd' has non-integer type
look_for_process_running2.c:20: `KERN_PROC' undeclared (first use in this function)
look_for_process_running2.c:21: `KERN_PROC_ALL' undeclared (first use in this function)
look_for_process_running2.c:39: `errno' undeclared (first use in this function)
look_for_process_running2.c:39: `ENOMEM' undeclared (first use in this function)
look_for_process_running2.c:41: sizeof applied to an incomplete type
look_for_process_running2.c:46: arithmetic on pointer to an incomplete type
look_for_process_running2.c:46: dereferencing pointer to incomplete type
look_for_process_running2.c:47: arithmetic on pointer to an incomplete type
look_for_process_running2.c:47: dereferencing pointer to incomplete type
======================================

1 #include <sys/param.h>
2 #include <sys/user.h>
3 #include <sys/sysctl.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6
7 int main(int argc, char **argv)
8{
9 struct kinfo_proc *procs = NULL, *newprocs;
10 char thiscmd[MAXCOMLEN + 1];
11 pid_t thispid;
12 int mib[4];
13 size_t miblen;
14 int i, st, nprocs;
15 size_t size;
16
17
18 size = 0;
19 mib[0] = CTL_KERN;
20 mib[1] = KERN_PROC;
21 mib[2] = KERN_PROC_ALL;
22 mib[3] = 0;
23 miblen = 3;
24
25 st = sysctl(mib, miblen, NULL, &size, NULL, 0);
26 do
27 {
28 size += size / 10;
29 newprocs = realloc(procs, size);
30 if (newprocs == 0)
31 {
32 if (procs)
33 free(procs);
34 errx(1, "could not reallocate memory");
35 }
36 procs = newprocs;
37 st = sysctl(mib, miblen, procs, &size, NULL, 0);
38 }
39 while (st == -1 && errno == ENOMEM);
40
41 nprocs = size / sizeof(struct kinfo_proc);
42
43 /* Now print out the data */
44 for (i = 0; i < nprocs; i++)
45 {
46 thispid = procs[i].kp_proc.p_pid;
47 strncpy(thiscmd, procs[i].kp_proc.p_comm, MAXCOMLEN);
48 thiscmd[MAXCOMLEN] = '\0';
49 printf("%d\t%s\n", thispid, thiscmd);
50 }
51
52 /* Clean up */
53 free(procs);
54 return(0);
55}

==================================

Reply With Quote
  #8  
Old June 21st, 2003, 10:42 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,587 Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 21 h 22 m 44 sec
Reputation Power: 997
Oops, I gave you code that works on *BSD (I tested on Free and Open BSD). For Linux, you can try using opendir() and readdir() to read the contents of the /proc directory. Just look for the directories that have all digits and then look in those directories for the exe link. Hope this helps

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > C function to find a process ID and kill it


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 2 hosted by Hostway
Stay green...Green IT