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

Processes - Evan Carlstrom #374

Open
wants to merge 24 commits into
base: master
Choose a base branch
from

Conversation

ecarlstrom
Copy link

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.

You have some good looking stuff here. I would only recommend the following if statement,

if (rc < 0) {
        fprintf(stderr, "fork failed\n");
        exit(1);
    }
/*stderr is the standard error stream the default destination for error messages and other diagnostic warnings. Like stdout 
      it is usually also directed by default to the text console(generally, on the screen). 
      */
exit   so its prototype looks like this 
      void exit(int status);
      Returns nothing   but indicates whehter the program terminated normally.  
      exit_success   successful termination 
      0    successful termination 
      exit_failure   unsuccessful termination 

      exit will terminate the program and everything that comes after it will not show up. 

This is for ex1.c however, you will want a check for if you were to ever receive a error or interruption after calling fork()

…x changes to learn more about process ordering and the different versions of exec() from ex4.
@ecarlstrom
Copy link
Author

Thanks, will try and add some error handling into the forked processes from now on.

I looked up a bit about "rc"; its function is to represent whatever is returned, correct? So this would basically mean that (rc < 0) indicates an unsuccessful return that will lead to an unsuccessful termination.

@codejoncode
Copy link

Sorry int rc = fork(); in this case.

@ecarlstrom
Copy link
Author

Okay, got it then. This would just be another 'if' case to add to the existing checks.

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"

@ecarlstrom
Copy link
Author

Thanks, I'll take a look at that code when I'm done with the repo today and see how well I understand it.

@codejoncode
Copy link

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

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