簡體   English   中英

分叉代碼編譯但在執行過程中出錯

[英]Forking code compiles but gives error during execution

代碼編譯但不運行並在第 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 );
}

改變

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

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

不推薦將字符串轉換為 char*。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM