fork - Unix

This is a discussion on fork - Unix ; //fork.c #include #include #include #include int main() { pid_t pid; pid=fork(); if(pid { fprintf(stdout, "Fork Failed\n"); exit(EXIT_FAILURE); } else if(pid==0) { execlp("/bin/bash", "ls",NULL); } else { wait(NULL); printf("child complete\n"); } return 0; } I run the above code in ubuntu ...

+ Reply to Thread
Results 1 to 5 of 5

Thread: fork

  1. fork

    //fork.c
    #include
    #include
    #include
    #include

    int main()
    {
    pid_t pid;
    pid=fork();

    if(pid<0)
    {
    fprintf(stdout, "Fork Failed\n");
    exit(EXIT_FAILURE);
    }
    else if(pid==0)
    {
    execlp("/bin/bash", "ls",NULL);
    }
    else
    {
    wait(NULL);
    printf("child complete\n");
    }
    return 0;
    }

    I run the above code in ubuntu 8.04 with
    gcc -o fork fork.c
    ../fork

    But it didn't show any o/p . Can anyone tell me what's the problem???

  2. Re: fork

    asit wrote:
    > execlp("/bin/bash", "ls",NULL);


    Introduce yourself to the parameters of execlp()
    and to the command line options of bash ...

    execlp("/bin/bash","bash","-c","ls",NULL");

  3. Re: fork

    In article
    <518189f6-d7a5-461b-80c1-339e423002d9@w39g2000prb.googlegroups.com>,
    asit wrote:

    > //fork.c
    > #include
    > #include
    > #include
    > #include
    >
    > int main()
    > {
    > pid_t pid;
    > pid=fork();
    >
    > if(pid<0)
    > {
    > fprintf(stdout, "Fork Failed\n");
    > exit(EXIT_FAILURE);
    > }
    > else if(pid==0)
    > {
    > execlp("/bin/bash", "ls",NULL);
    > }
    > else
    > {
    > wait(NULL);
    > printf("child complete\n");
    > }
    > return 0;
    > }
    >
    > I run the above code in ubuntu 8.04 with
    > gcc -o fork fork.c
    > ./fork
    >
    > But it didn't show any o/p . Can anyone tell me what's the problem???


    Your program started an interactive bash shell. Didn't you get a bash
    prompt? Type "exit" and you'll see the "child complete" message.

    --
    Barry Margolin, barmar@alum.mit.edu
    Arlington, MA
    *** PLEASE post questions in newsgroups, not directly to me ***
    *** PLEASE don't copy me on replies, I'll read them in the group ***

  4. Re: fork

    asit wrote:
    > //fork.c
    > #include
    > #include
    > #include
    > #include
    >
    > int main()
    > {
    > pid_t pid;
    > pid=fork();
    >
    > if(pid<0)
    > {
    > fprintf(stdout, "Fork Failed\n");


    Better:
    perror("fork failed");

    > exit(EXIT_FAILURE);
    > }
    > else if(pid==0)
    > {
    > execlp("/bin/bash", "ls",NULL);


    Try:
    execlp("/bin/bash", "bash", "-c", "ls", (char *)0);

    The first argument is the file to exec. Remaining arguments become
    argv[] in the new process image.

    Note that you must use (char *)0 (or (char *)NULL if preferred) to
    terminate the list. This is because it is possible that the system has
    "#define NULL 0" and because execlp() is a variadic function, plain NULL
    may therefore end up being passed as an int (perhaps 32 bit) when a
    pointer is expected by the function (perhaps 64 bit).

    You should also somehow handle execlp() failing - perhaps:

    perror("/bin/bash");
    _exit(1);

    Note the use of _exit(): exit() does some cleanup which is undesirable
    in this case. As a general rule, if you fork(), at most one of those
    processes should return from main() or call exit().

    > }
    > else
    > {
    > wait(NULL);
    > printf("child complete\n");
    > }
    > return 0;
    > }


    Alex

  5. Re: fork

    On Nov 1, 11:50 pm, Alex Fraser wrote:
    > asit wrote:
    > > //fork.c
    > > #include
    > > #include
    > > #include
    > > #include

    >
    > > int main()
    > > {
    > > pid_t pid;
    > > pid=fork();

    >
    > > if(pid<0)
    > > {
    > > fprintf(stdout, "Fork Failed\n");

    >
    > Better:
    > perror("fork failed");
    >
    > > exit(EXIT_FAILURE);
    > > }
    > > else if(pid==0)
    > > {
    > > execlp("/bin/bash", "ls",NULL);

    >
    > Try:
    > execlp("/bin/bash", "bash", "-c", "ls", (char *)0);
    >
    > The first argument is the file to exec. Remaining arguments become
    > argv[] in the new process image.
    >
    > Note that you must use (char *)0 (or (char *)NULL if preferred) to
    > terminate the list. This is because it is possible that the system has
    > "#define NULL 0" and because execlp() is a variadic function, plain NULL
    > may therefore end up being passed as an int (perhaps 32 bit) when a
    > pointer is expected by the function (perhaps 64 bit).
    >
    > You should also somehow handle execlp() failing - perhaps:
    >
    > perror("/bin/bash");
    > _exit(1);
    >
    > Note the use of _exit(): exit() does some cleanup which is undesirable
    > in this case. As a general rule, if you fork(), at most one of those
    > processes should return from main() or call exit().
    >
    > > }
    > > else
    > > {
    > > wait(NULL);
    > > printf("child complete\n");
    > > }
    > > return 0;
    > > }

    >
    > Alex


    Thank you

+ Reply to Thread