Thread: process id

  1. #1
    Registered User
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    45

    process id

    Hello everyone,
    I am stuck with fork(), pid, popen(). I am trying to execute a command, capture it's pid so that when I choose to stop the command I can use kill(). What I have works as far as executing the command and spitting out a pid, but it is the wrong pid. I have tried two versions one with execl and the other with popen and both hail the same results. I am just not understanding fork.
    Thanks in advance. Here is the execl example:
    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/dir.h>
    #include <signal.h>
    
    void print_cwd();
    void process();
    int main (void)
    {
     	int c;
    	FILE *IPFile;	 
    	FILE *OPFile;
    	FILE *IPFILE2;
    	FILE *OPFILE2;
    
    	IPFile = fopen("Sprint_Evdo","r");
    	OPFile = fopen("/etc/ppp/peers/Sprint_Evdo","w");
    	IPFILE2 = fopen("sprint-evdo","r");
    	OPFILE2 = fopen("/etc/ppp/sprint-evdo","w");
    		
        while ((c = fgetc(IPFile)) != EOF)
       {
           fputc(c, OPFile);		
       }
    
        fclose(IPFile);		
        fclose(OPFile);		
    
        while ((c = fgetc(IPFILE2)) != EOF)
        {
           fputc(c, OPFILE2);		
        }
    
        fclose(IPFILE2);		
        fclose(OPFILE2);		
        process();
        //printf("%d\n",pid);
    	print_cwd();
    	return(0);
    
    }
    void print_cwd()
    {
    char *cwd = getcwd(NULL, 0);
    printf("%s\n", cwd);
    }
    void process()
    {
    	int pid;
    	pid = fork();
        if ( pid == 0 ) {
           execl("/usr/sbin/pppd", "pppd", "call", "Sprint_Evdo", (char *)NULL );
           
        }
        printf("%d\n",pid);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    But pppd, being a daemon, will internally call fork() to detach itself from the controlling terminal (by committing parenticide). So the pid() you have is no longer the pid of the effective pppd process.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    45
    ok, do you know how I can get the pid when I execute pppd daemon from within the code?
    Thanks for the response.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    http://www.linuxmanpages.com/man8/pppd.8.php
    Scroll down to the "Files" section at the end.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    45
    so basically I can read the contents of /var/run/pidn.pid and that will give me the pid of the process I just started. I have tried other things like gedit in place of pppd call and it gives the correct pid. Is this because pppd calls it's own fork and gedit does not?
    Thanks again.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yes, like I said, pppd is a daemon.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    45
    I get it,
    thanks again Salem
    Take care

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. init adopts zombie process?
    By password636 in forum Linux Programming
    Replies: 4
    Last Post: 07-01-2009, 10:05 AM
  2. create a child process that creates a child process
    By cus in forum Linux Programming
    Replies: 9
    Last Post: 01-13-2009, 02:14 PM
  3. Replies: 3
    Last Post: 10-15-2008, 09:24 AM
  4. Problem with forking a process
    By Unitedroad in forum C Programming
    Replies: 10
    Last Post: 10-04-2007, 01:43 AM
  5. process programming
    By St0rM-MaN in forum Linux Programming
    Replies: 2
    Last Post: 09-15-2007, 07:53 AM