Sponsored Content
Top Forums Programming getting the pid of another C program with unix calls Post 302407545 by gautamdheeraj on Thursday 25th of March 2010 05:15:52 PM
Old 03-25-2010
You can use sysctl() system call to get proc table and search process table. Here is code (taken from killall.c from FreeBSD)

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);
}

 

10 More Discussions You Might Find Interesting

1. IP Networking

Identification of data calls & voice calls

Is there any facility to filter/identify the data calls and voice calls coming throug modem? OR Can we get the data or voice calls information through a script(preferably C Kermit)? (0 Replies)
Discussion started by: pcsaji
0 Replies

2. UNIX for Advanced & Expert Users

UNIX support calls

Does anyone know some support issues on unix? I've worked on unix for years at the lower end and the only support I've done is reset print queues, send jobs to printer, kill phantom processes. I'm looking for a new job and I'm curious as to what other problems occur so I can research them. (1 Reply)
Discussion started by: moodswingz
1 Replies

3. Programming

Tracing Function Calls in a program

Apart from writing debug and statements in constructors is there any way by which we can trace the function call stack at any depth? The issue that we always face is that when program crashes (Web Server running on Linux) we have no idea where it crashes and we have to do the hard way of... (1 Reply)
Discussion started by: uunniixx
1 Replies

4. Programming

Copy a file using UNIX-system calls

#include<unistd.h> #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> #include<stdlib.h> int main( int argc,char *argv ) { char buf; int sourcefile,destfile,n; if(argc!=3) { write(STDOUT_FILENO,"prgm1 <sourcefile> <destination file>\n",50); ... (6 Replies)
Discussion started by: c_d
6 Replies

5. UNIX for Advanced & Expert Users

netstat -p, missing PID/program name

I have a netstat command set up with awk to show which ports my box is listening on. The -p switch shows the PID/program name, too, which ordinarily would be very handy. However, several entries show up as just "-" for the program name which makes it hard to identify what is keeping the port open.... (2 Replies)
Discussion started by: Bilge
2 Replies

6. UNIX for Dummies Questions & Answers

UNIX command to get inode's tid and pid

Hi everyone, I am new here in www.unix.com, i found this site because I am looking for an answer to this problem of mine. I need to know a UNIX command to display an inode's thread id and process id. Hope someone can help me on this. Thanks :D (8 Replies)
Discussion started by: rodkun
8 Replies

7. UNIX for Dummies Questions & Answers

Unix directory system calls question

I'm currently studying for my exam, and is practicing with sample exam questions. However there is a question asking "Name THREE UNIX Directory system calls" and the answer given is "opendir, closedir and readdir", however the next question ask "Why is a write directory system call not included... (1 Reply)
Discussion started by: Izzy123
1 Replies

8. UNIX for Dummies Questions & Answers

System calls in UNIX

Hi i am very new to programming in UNIX and don't understand the difference between a system call and a normal function call. Also can I implement system calls from within a program? If so could someone please give me an example of a system call from within a program. Lastly, when creating a... (1 Reply)
Discussion started by: bjhum33
1 Replies

9. HP-UX

PID and program name from netstat.

Hello All, I am using netstat on HP-UX to retrieve the established network connections on my host. Can anyone please confirm how can I retrieve the PID and program name as well for these connections? These are available from the netstat version on Windows but I don't see PID and program name to... (10 Replies)
Discussion started by: Happy83
10 Replies

10. AIX

How to grep PID and program name from netstat in AIX?

Hi All, I am using netstat on AIX to grep info on all open connections. However, unlike on Linux(Centos), I do not get the PID and program name using netstat on AIX. I need this info to be clubbed along with the information retrieved using netstat version of AIX. Is there a way this can be... (1 Reply)
Discussion started by: Vipin Batra
1 Replies
old_api(3)							     net-snmp								old_api(3)

NAME
old_api - Calls mib module code written in the old style of code. Functions netsnmp_mib_handler * get_old_api_handler (void) returns a old_api handler that should be the final calling handler. int netsnmp_register_old_api (const char *moduleName, struct variable *var, size_t varsize, size_t numvars, oid *mibloc, size_t mibloclen, int priority, int range_subid, oid range_ubound, netsnmp_session *ss, const char *context, int timeout, int flags) Registers an old API set into the mib tree. int netsnmp_register_mib_table_row (const char *moduleName, struct variable *var, size_t varsize, size_t numvars, oid *mibloc, size_t mibloclen, int priority, int var_subid, netsnmp_session *ss, const char *context, int timeout, int flags) registers a row within a mib table int netsnmp_old_api_helper (netsnmp_mib_handler *handler, netsnmp_handler_registration *reginfo, netsnmp_agent_request_info *reqinfo, netsnmp_request_info *requests) implements the old_api handler Detailed Description Calls mib module code written in the old style of code. This is a backwards compatilibity module that allows code written in the old API to be run under the new handler based architecture. Use it by calling netsnmp_register_old_api(). Function Documentation netsnmp_mib_handler* get_old_api_handler (void) returns a old_api handler that should be the final calling handler. Don't use this function. Use the netsnmp_register_old_api() function instead. Definition at line 38 of file old_api.c. int netsnmp_old_api_helper (netsnmp_mib_handler * handler, netsnmp_handler_registration * reginfo, netsnmp_agent_request_info * reqinfo, netsnmp_request_info * requests) implements the old_api handler Definition at line 231 of file old_api.c. int netsnmp_register_mib_table_row (const char * moduleName, struct variable * var, size_t varsize, size_t numvars, oid * mibloc, size_t mibloclen, int priority, int var_subid, netsnmp_session * ss, const char * context, int timeout, int flags) registers a row within a mib table Definition at line 112 of file old_api.c. int netsnmp_register_old_api (const char * moduleName, struct variable * var, size_t varsize, size_t numvars, oid * mibloc, size_t mibloclen, int priority, int range_subid, oid range_ubound, netsnmp_session * ss, const char * context, int timeout, int flags) Registers an old API set into the mib tree. Functionally this mimics the old register_mib_context() function (and in fact the new register_mib_context() function merely calls this new old_api one). netsnmp_handler_registration_free(reginfo); already freed Definition at line 49 of file old_api.c. Author Generated automatically by Doxygen for net-snmp from the source code. Version 5.5 23 Sep 2009 old_api(3)
All times are GMT -4. The time now is 07:19 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy