Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fork().c #24

Open
shilianzhu opened this issue Sep 20, 2014 · 0 comments
Open

fork().c #24

shilianzhu opened this issue Sep 20, 2014 · 0 comments

Comments

@shilianzhu
Copy link

include <stdio.h>

include <stdlib.h>

include <unistd.h>

// lost wait.h in in orgional function;

include <sys/wait.h>

define SUCCESS 1

define FAILURE 0 /// declare failure to 0

int main( int argc,char *argv[]) // in orgional function, it lost the variable int argc,and char *argv[]
{

// in orgional function argc is !=0, in this should be !=1
if (argc != 1) {

  // the printf should be stderr
    fprintf(stderr, "fork failed.\n");

   // in orgional function is FAILURE, but i change to exit(1);
   exit(1);
}

printf("Hi stranger! I'm (pid:%d)\n", (int) getpid());

// create a new fork(), not knife()
int rc = fork(); //slice off another process

// we delcare failure is 0, so we change to 0 in rc<0
if (rc < 0) {

    // Could not cut another process
    fprintf(stdout, "OS too hard, could not cut.\n");
    exit(0);

// check
} else if (rc == 0) {
fprintf(stderr, "Child can't talk to strangers.\n");
exit(1);

// printf can not print, because above code exit(1) has exit, so from printf to sleep, it does not work in here;

/* printf("Hello, I am child (pid:%d)\n", (int) rc);
sleep(1) ; */

     } else if (rc >0) {

    // we should wait child to end, not parent, it is wait()
    int wc = wait(NULL); 

    printf("Please leave my child alone, I am %d (wc:%d) (pid:%d)\n",
       (int) getpid(), wc,rc);
       // sholude be int getpid(),and rc
}

// return to 0, not success.
return 0;
}

in this function , the code stystle is not good,
missunderstand the fork() libary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant