raphui
raphui

Reputation: 117

Fork: child does not execute code

I try to launch a new process via fork, but sometimes the child does not execute his code. When I launch a ps I can see the new child.

It's like if the child process is stopped or waiting before evaluate if( stream_pid == 0 ).

How can I solve this random problem ?

I execute this code on ARM SoC at 150MHz (don't know if it can help :p )

static void process_thread_func( void )
{
    const char *ret = NULL;
    char *argv[2];
    char *status = NULL;
    int stream_pid;
    int flag = 0;
    int err = 0;

    stream_pid = fork();

    if( stream_pid == 0 )
    {

        argv[0] = "/usr/bin/mplayer";
        argv[1] = "tv:// -tv driver=v4l2:device=/dev/video0:outfmt=y8:width=320:height=240";
        argv[2] = NULL;

        err = execv("/usr/bin/mplayer" , argv );

        if( err < 0 )
            printf("execv failed, reason: %d\n", errno);

    }
    else if( stream_pid < 0 )
        printf("Cannot create mplayer child, reason: %d\n", errno);
    else
    {
        ( void )waitpid( stream_pid, &flag, 0 );
    }
}

Upvotes: 1

Views: 94

Answers (1)

Barmar
Barmar

Reputation: 780724

You're writing outside the bounds of the argv array, because you didn't declare it large enough. It should be:

char *argv[3];

You could use execl instead, since the arguments are hard-coded in the program.

err = execl("/usr/bin/mplayer", "/usr/bin/mplayer", "tv:// -tv driver=v4l2:device=/dev/video0:outfmt=y8:width=320:height=240", (char*) NULL);

Or you could let the array size itself automatically from the initializer:

char *argv[] = {"/usr/bin/mplayer", "tv:// -tv driver=v4l2:device=/dev/video0:outfmt=y8:width=320:height=240", NULL};

Upvotes: 4

Related Questions