创建新进程时的分段错误(核心转储)

时间:2013-11-29 23:07:08

标签: c segmentation-fault

首先,我发布的另一个问题是lil shady我要求移除该线程,并且有点想用这个替换它...

我可以先解释一下我的程序应该做什么: 该程序应该创建几个运行printenv,grep [inputarray],sort,less的进程,然后这些应该使用管道传输数据,如下所示: printenv - > pipe1 - > grep - > pipe2 - >排序 - > pipe3 - >少 - >标准输出

现在我通过调用函数“createProcesswithPipe”以递归方式完成了这个操作。 我相信该计划背后的理论是正确的。

现在,如果您要查看代码,您会看到

printf("point: %d" , pointer); 
每次输入函数时,

都会打印出指针的值。现在的问题是,如果程序按原样运行,它将永远不打印出来。有什么想法吗?

但如果我要进一步删除递归调用:

createProcesswithPipe(++pointer, executes, execute);

程序在得到相同错误之前打印出“point:0”:分段错误(核心转储)

也不要介意瑞典语中的评论!如果你有任何问题请求!我有点失落了:)

/* digenv.c - dig (ta reda på) env (enviroment variablar) */
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define PIPE_READ_SIDE ( 0 )
#define PIPE_WRITE_SIDE ( 1 )

pid_t childpid; /* för child-processens PID vid fork() */


void createProcesswithPipe(int pointer, int executes, char* execute[])
{
    printf("point: %d" , pointer);

    int pipe_filedesc[ 2 ]; /* för fildeskriptorer från pipe(2) */
    int return_value; /* för returvärden från systemanrop */

    return_value = pipe( pipe_filedesc ); /* skapa en pipe */
    if( -1 == return_value ) {perror( "Cannot create pipe" ); exit( 1 );} /* om pipe()     misslyckades */

    childpid = fork(); /* skapa första child-processen */

    if( 0 == childpid )
    {
        return_value = dup2( pipe_filedesc[ PIPE_READ_SIDE], STDIN_FILENO );
        if( -1 == return_value){perror( "Cannot dup" ); exit( 1 );}

        /* TA BORT GAMLA REFERENSEN TILL PIPE BEHÖVER INTE HA 2st!!!!!!! */
        return_value = close( pipe_filedesc[PIPE_READ_SIDE] );
        if( -1 == return_value ){perror( "Cannot close read end" ); exit( 1 );}

        /* THIS PIPE SHOULD ONLY READ */
        return_value = close( pipe_filedesc[PIPE_WRITE_SIDE] );
        if( -1 == return_value ){perror( "Cannot close write end" ); exit( 1 );}

        if(pointer <= 3){
            createProcesswithPipe(++pointer, executes, execute);
        }
        else if(3 == pointer && executes < 4){
            return_value = dup2( STDOUT_FILENO, pipe_filedesc[ PIPE_WRITE_SIDE] );
            if( -1 == return_value){perror( "Cannot dup" ); exit( 1 );}

            /* TA BORT GAMLA REFERENSEN TILL PIPE BEHÖVER INTE HA 2st!!!!!!! */
            return_value = close( pipe_filedesc[ PIPE_WRITE_SIDE ] );
            if( -1 == return_value ){perror( "Cannot close read end" ); exit( 1 );}

            (void) execlp( execute[executes], execute[executes], (char *) 0 );
        }
        else
        {
            return_value = dup2( STDOUT_FILENO, pipe_filedesc[ PIPE_WRITE_SIDE] );
            if( -1 == return_value){perror( "Cannot dup" ); exit( 1 );}

            /* TA BORT GAMLA REFERENSEN TILL PIPE BEHÖVER INTE HA 2st!!!!!!! */
            return_value = close( pipe_filedesc[ PIPE_WRITE_SIDE ] );
            if( -1 == return_value ){perror( "Cannot close read end" ); exit( 1 );}

            char* execute2[executes-4];
            int i;
            for(i = 1;i<executes-4;i++){
                execute2[i] = execute[i+3];
            }
            (void) execlp( execute[executes], execute[executes], (char *) execute2 );
        }
    }
    else
    {

        if( -1 == childpid ){   perror( "Cannot fork()" );  exit( 1 );}  /* fork() misslyckades */

        return_value = dup2( pipe_filedesc[ PIPE_WRITE_SIDE], STDOUT_FILENO );
        if( -1 == return_value){perror( "Cannot dup" ); exit( 1 );}

        /* Kommer vi hit i koden så är vi i parent-processen
         och fork() har fungerat bra. */

        /* THIS PIPE SHOULD ONLY WRITE */
        return_value = close( pipe_filedesc[ PIPE_READ_SIDE ] );
        if( -1 == return_value ){perror( "Cannot close read end" ); exit( 1 );}

        /* TA BORT GAMLA REFERENSEN TILL PIPE BEHÖVER INTE HA 2st!!!!!!! */
        return_value = close( pipe_filedesc[ PIPE_WRITE_SIDE ] );
        if( -1 == return_value ){perror( "Cannot close read end" ); exit( 1 );}

        (void) execlp( execute[executes], execute[executes], (char *) 0 );

        /* exec returnerar bara om något fel har uppstått
         och om exec returnerar så är returvärdet alltid -1 */
        perror( "Cannot exec &execute[executes]" );
        exit( 1 );

    }
}




int main( int argc, char * argv[] )
{
    char* execute[3+argc-1];

    execute[0] = "printenv";
    execute[3+argc-3] = "sort";
    execute[3+argc-2] = "less";

    int i;
    for(i = 3;i<(3+argc-1);i++)
    {
        if(!(strcmp(argv[i-3], "more"))){
            execute[3+argc-2] = "more";
        }
        else{
            execute[i] = argv[i-3];
        }
    }

    createProcesswithPipe(0, (3+argc-1), execute);

    exit( 0 ); /* Avsluta parent-processen på normalt sätt */
}

0 个答案:

没有答案