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