Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Problem with fork inside a thread and execv()

Reply
Thread Tools

Problem with fork inside a thread and execv()

 
 
CMorgan
Guest
Posts: n/a
 
      01-02-2008
Hi everybody,
I am experiencing an annoying problem with fork() and execv().
In my program I need to launch the "pppd" from a thread, so, I create
a new process with fork and then in the child process I launch with
execv the pppd to connect an embedded system to internet.
The problem is that pppd fails.

I have made two test program just to discard bugs (my
applications is really big) and the result is that when
I call execv from a child process created from a thread the pppd
script fails!!!!
On the other hand when I call execv from a process created from a fork
(without threads) the code works!!!


Here I paste my two test program, the test that fails, the test that
success:

------- TEST PROGRAM THAT FAILS -------
#include <pthread.h>
#include <sys/time.h>
#include <stdbool.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

static bool ActivarGPRS (void);
static void* macapi_gprs_connect_thread(void *param);

int main(int argc,char** argv)
{
pthread_t thread;

pthread_create(&thread,NULL,&macapi_gprs_connect_t hread,NULL);

while(1)
{
sleep(100);
}
return 0;

}

static void* macapi_gprs_connect_thread(void *param)
{
if( true == ActivarGPRS() )
printf("GPRS activado\n");
else
printf("No se puede activar el GPRS\n");

}

static bool ActivarGPRS (void)
{
int pid;
struct timeval T0, T1;
FILE *fp = NULL;

//************ GPRS *************
printf ("ActivarGPRS\n");
if ((pid = fork ()) < 0)
{
printf ("Error fork\n");
return false;
}
// Tiempo de arranque
gettimeofday (&T0, NULL);
if (pid == 0)
{
char* argv[] = {"/usr/sbin/pppd","call","gprs",NULL};
// Proceso hijo
//system ("/usr/sbin/pppd call gprs > /home/sattoll/
StatusGPRS");
execv(argv[0],argv);
printf("execv failure:%s\n",strerror(errno));
system ("rm -f /home/sattoll/StatusGPRS");
exit (0);
}
else
{
// Proceso padre
while ((fp = fopen ("/home/sattoll/GPRSOK", "r")) == NULL)
{
// Si pasa mucho tiempo sin conexion (1 minutos), se
sale y se reintentera mas tarde
gettimeofday (&T1, NULL);
if ((T1.tv_sec - T0.tv_sec) > 40)
{
printf ("Timeout\n");
system ("killall -2 pppd");
// hay que esperar porque si no se ha conectado al
CE
system ("sleep 3");
system ("rm -f /home/sattoll/StatusGPRS");
return false;
}
}
return true;
}
}

------- END OF TEST PROGRAM THAT FAILS -------
------- TEST PROGRAM THAT WORKS -------------
#include <sys/time.h>
#include <stdbool.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

static bool ActivarGPRS (void);

int main(int argc,char** argv)
{
if( true == ActivarGPRS() )
printf("GPRS activado\n");
else
printf("No se puede activar el GPRS\n");

}

static bool ActivarGPRS (void)
{
int pid;
struct timeval T0, T1;
FILE *fp = NULL;

//************ GPRS *************
printf ("ActivarGPRS\n");
if ((pid = fork ()) < 0)
{
printf ("Error fork\n");
return false;
}
// Tiempo de arranque
gettimeofday (&T0, NULL);
if (pid == 0)
{
char* argv[] = {"/usr/sbin/pppd","call","gprs",NULL};
// Proceso hijo
//system ("/usr/sbin/pppd call gprs > /home/sattoll/
StatusGPRS");
execv(argv[0],argv);
printf("execv failure:%s\n",strerror(errno));
system ("rm -f /home/sattoll/StatusGPRS");
exit (0);
}
else
{
// Proceso padre
while ((fp = fopen ("/home/sattoll/GPRSOK", "r")) == NULL)
{
// Si pasa mucho tiempo sin conexion (1 minutos), se
sale y se reintentera mas tarde
gettimeofday (&T1, NULL);
if ((T1.tv_sec - T0.tv_sec) > 40)
{
printf ("Timeout\n");
system ("killall -2 pppd");
// hay que esperar porque si no se ha conectado al
CE
system ("sleep 3");
system ("rm -f /home/sattoll/StatusGPRS");
return false;
}
}
return true;
}
}

----------- END OF TEST PROGRAM THAT WORKS
 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      01-02-2008
In article <942ad53f-8229-4fa4-b859->,
CMorgan <> wrote:

>I am experiencing an annoying problem with fork() and execv().


Neither fork() nor execv() nor threads are part of the C language.
Please consult a newsgroup that deals with whatever extensions
or external library that you are using that is providing fork()
and execv() for you. Possibly comp.unix.programmer or
comp.programming.threads (or group names similar to those.)
--
"All is vanity." -- Ecclesiastes
 
Reply With Quote
 
 
 
 
Kenny McCormack
Guest
Posts: n/a
 
      01-02-2008
In article <flfgdn$otb$>,
Walter Roberson <> wrote:
>In article <942ad53f-8229-4fa4-b859->,
>CMorgan <> wrote:
>
>>I am experiencing an annoying problem with fork() and execv().

>
>Neither fork() nor execv() nor threads are part of the C language.
>Please consult a newsgroup that deals with whatever extensions
>or external library that you are using that is providing fork()
>and execv() for you. Possibly comp.unix.programmer or
>comp.programming.threads (or group names similar to those.)
>--
> "All is vanity." -- Ecclesiastes


IOW:

Off topic. Not portable. Cant discuss it here. Blah, blah, blah.

--
Useful clc-related links:

http://en.wikipedia.org/wiki/Aspergers
http://en.wikipedia.org/wiki/Clique
http://en.wikipedia.org/wiki/C_programming_language

 
Reply With Quote
 
suresh shenoy
Guest
Posts: n/a
 
      01-02-2008
On Jan 2, 2:20*am, CMorgan <uesteba...@gmail.com> wrote:
> Hi everybody,
> I am experiencing an annoying problem with fork() and execv().
> In my program I need to launch the "pppd" from a thread, so, I create
> a new process with fork and then in the child process I launch with
> execv the pppd to connect an embedded system to internet.
> The problem is that pppd fails.
>
> I have made two test program just to discard bugs (my
> applications is really big) and the result is that when
> I call execv from a child process created from a thread the pppd
> script fails!!!!
> On the other hand when I call execv from a process created from a fork
> (without threads) the code works!!!
>
>
> Here I paste my two test program, the test that fails, the test that
> success:
>
> ------- TEST PROGRAM THAT FAILS -------
> #include <pthread.h>
> #include <sys/time.h>
> #include <stdbool.h>
> #include <stdio.h>
> #include <unistd.h>
> #include <string.h>
> #include <errno.h>
>
> static bool ActivarGPRS (void);
> static void* macapi_gprs_connect_thread(void *param);
>
> int main(int argc,char** argv)
> {
> * * pthread_t thread;
>
> * * pthread_create(&thread,NULL,&macapi_gprs_connect_t hread,NULL);
>
> * * while(1)
> * * {
> * * * * sleep(100);
> * * }
> * * return 0;
>
> }
>
> static void* macapi_gprs_connect_thread(void *param)
> {
> * * if( true == ActivarGPRS() )
> * * * * printf("GPRS activado\n");
> * * else
> * * * * printf("No se puede activar el GPRS\n");
>
> }
>
> static bool ActivarGPRS (void)
> {
> int pid;
> struct timeval T0, T1;
> FILE *fp = NULL;
>
> * * * * //************ GPRS *************
> * * * * printf ("ActivarGPRS\n");
> * * * * if ((pid = fork ()) < 0)
> * * * * {
> * * * * * * printf ("Error fork\n");
> * * * * * * return false;
> * * * * }
> * * * * // Tiempo de arranque
> * * * * gettimeofday (&T0, NULL);
> * * * * if (pid == 0)
> * * * * {
> * * * * * * char* argv[] = {"/usr/sbin/pppd","call","gprs",NULL};
> * * * * * * // Proceso hijo
> * * * * * * //system ("/usr/sbin/pppd call gprs > /home/sattoll/
> StatusGPRS");
> * * * * * * execv(argv[0],argv);
> * * * * * * printf("execv failure:%s\n",strerror(errno));
> * * * * * * system ("rm -f /home/sattoll/StatusGPRS");
> * * * * * * exit (0);
> * * * * }
> * * * * else
> * * * * {
> * * * * * * // Proceso padre
> * * * * * * while ((fp = fopen ("/home/sattoll/GPRSOK", "r")) == NULL)
> * * * * * * {
> * * * * * * * * // Si pasa mucho tiempo sin conexion (1 minutos), se
> sale y se reintentera mas tarde
> * * * * * * * * gettimeofday (&T1, NULL);
> * * * * * * * * if ((T1.tv_sec - T0.tv_sec) > 40)
> * * * * * * * * * * {
> * * * * * * * * * * printf ("Timeout\n");
> * * * * * * * * * * system ("killall -2 pppd");
> * * * * * * * * * * // hay que esperar porque si no se ha conectado al
> CE
> * * * * * * * * * * system ("sleep 3");
> * * * * * * * * * * system ("rm -f /home/sattoll/StatusGPRS");
> * * * * * * * * * * return false;
> * * * * * * * * * * }
> * * * * * * }
> * * * * * * return true;
> * * * * }
>
> }
>
> ------- END OF TEST PROGRAM THAT FAILS -------
> ------- TEST PROGRAM THAT WORKS -------------
> #include <sys/time.h>
> #include <stdbool.h>
> #include <stdio.h>
> #include <unistd.h>
> #include <string.h>
> #include <errno.h>
>
> static bool ActivarGPRS (void);
>
> int main(int argc,char** argv)
> {
> * * if( true == ActivarGPRS() )
> * * * * printf("GPRS activado\n");
> * * else
> * * * * printf("No se puede activar el GPRS\n");
>
> }
>
> static bool ActivarGPRS (void)
> {
> int pid;
> struct timeval T0, T1;
> FILE *fp = NULL;
>
> * * * * //************ GPRS *************
> * * * * printf ("ActivarGPRS\n");
> * * * * if ((pid = fork ()) < 0)
> * * * * {
> * * * * * * printf ("Error fork\n");
> * * * * * * return false;
> * * * * }
> * * * * // Tiempo de arranque
> * * * * gettimeofday (&T0, NULL);
> * * * * if (pid == 0)
> * * * * {
> * * * * * * char* argv[] = {"/usr/sbin/pppd","call","gprs",NULL};
> * * * * * * // Proceso hijo
> * * * * * * //system ("/usr/sbin/pppd call gprs > /home/sattoll/
> StatusGPRS");
> * * * * * * execv(argv[0],argv);
> * * * * * * printf("execv failure:%s\n",strerror(errno));
> * * * * * * system ("rm -f /home/sattoll/StatusGPRS");
> * * * * * * exit (0);
> * * * * }
> * * * * else
> * * * * {
> * * * * * * // Proceso padre
> * * * * * * while ((fp = fopen ("/home/sattoll/GPRSOK", "r")) == NULL)
> * * * * * * {
> * * * * * * * * // Si pasa mucho tiempo sin conexion (1 minutos), se
> sale y se reintentera mas tarde
> * * * * * * * * gettimeofday (&T1, NULL);
> * * * * * * * * if ((T1.tv_sec - T0.tv_sec) > 40)
> * * * * * * * * * * {
> * * * * * * * * * * printf ("Timeout\n");
> * * * * * * * * * * system ("killall -2 pppd");
> * * * * * * * * * * // hay que esperar porque si no se ha conectado al
> CE
> * * * * * * * * * * system ("sleep 3");
> * * * * * * * * * * system ("rm -f /home/sattoll/StatusGPRS");
> * * * * * * * * * * return false;
> * * * * * * * * * * }
> * * * * * * }
> * * * * * * return true;
> * * * * }
>
> }
>
> ----------- END OF TEST PROGRAM THAT WORKS


CMorgan,

Firstly, I would recommend you to perform error checking, this
will help you resolve any sutle issues at the earliest. Also, i think
you should compare the pid of the thread and not fix it to 0 since you
have spawned a thread. You should get that information from the
'thread' instance created in pthread_create(..);

Suresh M. Shenoy
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
os.fork and pty.fork Eric Snow Python 0 01-08-2009 06:32 AM
thread death callback to be called after fork ara.t.howard@noaa.gov Ruby 2 11-10-2006 09:45 PM
"warning: fork terminates thread" Hal Fulton Ruby 2 06-21-2004 02:08 PM
Thread + fork warning Ariff Abdullah Ruby 4 10-17-2003 03:24 PM
fork and modifying variables inside the new process Kurt M. Dresner Ruby 3 08-16-2003 11:13 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57