Skip to content

Conversation

@farhanf
Copy link

@farhanf farhanf commented Feb 20, 2019

No description provided.

Copy link

@codejoncode codejoncode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For your ex1.c file it seems to work as it should. There is the condition for if fork () returns a negative value that you are not currently prepared for also a different look :

int main(void)
{
    int x = 100;
    printf("hello world (pid: %d)\n", (int) getpid());

    int rc = fork();

    if (rc < 0) {
        fprintf(stderr, "fork failed\n");
        exit(1);
    } else if (rc == 0) {
        printf("hello, I am child (pid: %d) and x is: %d\n", (int) getpid(), x);
        x++;
        printf("child again, x is now: %d\n", x);
    } else {
        printf("hello, I am parent of %d (pid: %d) and x is: %d\n", rc, (int) getpid(), x);
        x--;
        printf("parent again, x is now: %d\n", x);
    }

    return 0;
}

Copy link

@codejoncode codejoncode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For ex4 we want to set up the main to accept argc and argv[] this would be entered in on the command line and read into the program and then worked with accordingly. Your code may infact generate the correct output but you may see functions where main will be prepared to accept arguments so I wanted to show you this.

int main(int argc, char* argv[])
{
    printf("Parent process here\n");
    int rc = fork();

    if (rc < 0) {
        fprintf(stderr, "fork failed\n");
        exit(1);
    } else if (rc == 0) {
        printf("Child process here\n");

        // execl("/bin/ls", "ls", "-l" (char *) NULL);
        
        // char *args[] = {"ls", "-l", NULL};
        // execv("/bin/ls", args);

        //execlp("ls", "ls", "-l", (char *) NULL);

        char *args[] = {"ls", "-l", NULL};
        execvp("ls", args);
    } else {
        int wc = waitpid(rc, NULL, 0);
    }

    return 0;
}

This way we would then pass "/bin/ls"

@codejoncode
Copy link

The biggest difference is we could replace char *args[] = {"ls", "-l", NULL}; with your char * argv[] main argument (ex4)

@codejoncode
Copy link

Overall your code on ex.5 works and is producing the output as it should here is some tips moving forward:

if (rc < 0) {
fprintf(stderr, "fork failed\n");
exit(1);
}

currently, right now you are not checking the fork is proper it will be for this small program but there could be an issue for something more complex.  Also before you begin to read I would `close(p[1]);` just a best practice. But not going to be something that will affect a program of this size if you don't. 

@farhanf
Copy link
Author

farhanf commented Feb 21, 2019

Overall your code on ex.5 works and is producing the output as it should here is some tips moving forward:

if (rc < 0) {
fprintf(stderr, "fork failed\n");
exit(1);
}

currently, right now you are not checking the fork is proper it will be for this small program but there could be an issue for something more complex.  Also before you begin to read I would `close(p[1]);` just a best practice. But not going to be something that will affect a program of this size if you don't. 

Thanks Jonathan will add that in!

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

Successfully merging this pull request may close these issues.

2 participants