Problem with fork inside a thread and execv()

Discussion in 'C Programming' started by CMorgan, Jan 2, 2008.

  1. CMorgan

    CMorgan Guest

    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!!!
    :-o

    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_thread,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, Jan 2, 2008
    #1
    1. Advertising

  2. In article <>,
    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
     
    Walter Roberson, Jan 2, 2008
    #2
    1. Advertising

  3. In article <flfgdn$otb$>,
    Walter Roberson <-cnrc.gc.ca> wrote:
    >In article <>,
    >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
     
    Kenny McCormack, Jan 2, 2008
    #3
  4. On Jan 2, 2:20 am, CMorgan <> 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!!!
    > :-o
    >
    > 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_thread,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
     
    suresh shenoy, Jan 2, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. M Herriott
    Replies:
    3
    Views:
    1,694
    M Herriott
    Feb 26, 2004
  2. Rotem

    import in execv after fork

    Rotem, May 9, 2006, in forum: Python
    Replies:
    1
    Views:
    483
    Rotem
    May 9, 2006
  3. Mike - EMAIL IGNORED

    fork, execv & state variables

    Mike - EMAIL IGNORED, May 21, 2006, in forum: C++
    Replies:
    2
    Views:
    394
    Jonathan Mcdougall
    May 21, 2006
  4. Replies:
    1
    Views:
    297
    Diez B. Roggisch
    Aug 29, 2008
  5. Eric Snow

    os.fork and pty.fork

    Eric Snow, Jan 8, 2009, in forum: Python
    Replies:
    0
    Views:
    573
    Eric Snow
    Jan 8, 2009
Loading...

Share This Page