-
Notifications
You must be signed in to change notification settings - Fork 495
Processes in C- Farhan #376
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
base: master
Are you sure you want to change the base?
Conversation
codejoncode
left a comment
There was a problem hiding this 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;
}
codejoncode
left a comment
There was a problem hiding this 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"
|
The biggest difference is we could replace |
|
Overall your code on ex.5 works and is producing the output as it should here is some tips moving forward: if (rc < 0) { |
Thanks Jonathan will add that in! |
No description provided.