getting the pid of another C program with unix calls


 
Thread Tools Search this Thread
Top Forums Programming getting the pid of another C program with unix calls
# 1  
Old 03-25-2010
getting the pid of another C program with unix calls

I have a C program called server.c which is supposed to get the pid of another program, client.c, and send a signal to it, but I'm not sure how to do it. Server.c is first run in the background then client is run in the foreground.

I tried
Code:
pid_t pid;
pid = system("pidof -s client.c");
kill(pid, SIGINT);

doesn't seem to be returning the right one or anything at all. Is there another way I can do it with either a C function or unix one?
# 2  
Old 03-25-2010
system() does not work that way. it doesn't return text, it returns success or failure.

Since you're running a shell anyway by using system(), why not:

Code:
system("kill -INT `pidof -s client.c`");

Really though, this isn't a very good design. You won't be able to run more than one client on the system since the server will never be able to tell between them by name alone; and why does the server need to send them SIGINT anyway?
# 3  
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);
}

# 4  
Old 03-25-2010
Also, if you're running on Solaris there's the "utssys()" system call that's the basis for the "fuser" utility. The Solaris implementation of "fuser" is here:

Cross Reference: /onnv/onnv-gate/usr/src/cmd/fuser/fuser.c

On Linux, you can look at how the "lsof" utility works, although unless the implementation has changed since I last looked at it quite a while ago, the "lsof" implementation can be daunting, to say the least.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

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

8. 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

9. 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

10. 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
Login or Register to Ask a Question