分叉代码编译但在执行期间出错

时间:2016-09-30 05:37:54

标签: c linux process

代码编译但不运行并在第7行和第10行给出错误。我无法纠正错误,所以请帮助我。 以下是shell中的错误序列:

./mylab3.c: line 7: char: command not found
./mylab3.c: line 10: syntax error near unexpected token `('
./mylab3.c: line 10: `int main( )

代码:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>


char  *env_init[]= { "USER=unknown", "PATH=/tmp",NULL};


int main( )
{
    pid_t pid;
    int flag, status;
// Create child process #1
    pid = fork();
    if (pid < 0 )
    {
        perror( "fork error" );
    }
    else if (pid == 0)
    {
// Child process – replace with different program
        flag = execle( "/bin/Is", "Is", "-IF",NULL,env_init);
        if (flag < 0)
        {
            perror( "execle error" );
        }
    }
    else
    {

        if (wait( &status ) != pid)
        {
            perror( "wait error" );
        }
    }
//Create child process #2
    pid = fork();
    if (pid < 0)
    {
        perror ( "fork error" );
    }
    else if (pid == 0)
    {

        flag = execlp( "./lab03script","lab03.script","file1", "file2", NULL);
        if (flag < 0)
        {
            perror( "execlp error" );
        }

    }
    else
    {

        if (wait( &status ) != pid)
        {
            perror( "wait error") ;
        }
    }
    exit ( 0 );
}

1 个答案:

答案 0 :(得分:-2)

更改

char  *env_init[]= { "USER=unknown", "PATH=/tmp",NULL}; 

const char  *env_init[]= { "USER=unknown", "PATH=/tmp",NULL};

不推荐将字符串转换为char *。