lkml.org 
[lkml]   [1999]   [May]   [5]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
    /
    Date
    From
    SubjectRe: fork() Problem?
    On Wed, 5 May 1999, Yavuz Selim Komur wrote:

    > On Tue, 4 May 1999, David Miller wrote:
    >
    > >
    > > You do not initialize the "status" variable passed to wait(), this is
    > > probably why it crashes on SunOS. "status" could contain anything,
    > > any value, and probably it contains garbage or a wild pointer.
    >
    > Execuse me. I didn't well defined my problem. SegFault? ignore it.
    >
    > Problem is sequence error.
    >
    > if (pid = fork())
    ^^^^________ logical test of an assignment? This will always
    be true!
    if((pid = fork()) != 0) or
    if((pid = fork()))
    You need to make the assignment first, then
    test for non-zero.


    > printf("Main Program");
    > else
    > printf("Child Program");
    >
    > it's clear?

    No, also if you want a defined order in which stuff is printed either
    do fprintf(stderr, "whatever"); or printf("whatever"); fflush(stdout);

    Standard output is buffered.

    This works fine, and in the correct order.

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

    int main(void);
    int main()
    {
    pid_t pid;
    int status;

    switch((pid = fork()))
    {
    case 0: /* Child */
    fprintf(stderr,"Child (%ld)\n", getpid());
    exit(123);
    case -1:
    fprintf(stderr, "Error with fork()\n");
    exit(2);
    default: /* Parent */
    fprintf(stderr, "Parent waits for child (%ld) status\n", pid);
    wait(&status);
    fprintf(stderr, "Child's exit status was %d\n", status >> 8);
    }
    return 0;
    }


    Cheers,
    Dick Johnson
    ***** FILE SYSTEM WAS MODIFIED *****
    Penguin : Linux version 2.2.6 on an i686 machine (400.59 BogoMips).
    Warning : It's hard to remain at the trailing edge of technology.


    -
    To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
    the body of a message to majordomo@vger.rutgers.edu
    Please read the FAQ at http://www.tux.org/lkml/

    \
     
     \ /
      Last update: 2005-03-22 13:51    [W:0.021 / U:68.864 seconds]
    ©2003-2016 Jasper Spaans. hosted at Digital OceanAdvertise on this site