Processes in C- Farhan#376
Processes in C- Farhan#376farhanf wants to merge 4 commits intobloominstituteoftechnology:masterfrom farhanf:master
Conversation
codejoncode
left a comment
There was a problem hiding this comment.
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.
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.