How many lines will the following codes print out, and why?
main() {
fork();
printf ("Hello!\n")
fork();
printf("Good Bye!\n");
} // main
What's the fork() doing here? Does it change the number of lines that will be output?
The purpose of fork() system call is to create a new process, which becomes the child process of caller, after which both, the parent and child processes, will execute the code following the fork() system call. Hence, it is important to distinguish between parent and child process. This can be done by testing the return value of fork() system call.
I'm not yet understanding how to apply that to the example above. So unds like it would be 4 lines.
And another example:
How will the following code fragment affect stdin, stdout and stderr? Can somebody please explain to me what's behind this?
int fda, fdb;
fda = open("alpha", O_RDWR | O_CREAT, 0640);
fdb = open("beta" , O_RDWR | O_CREAT, 0640);
close (2);
dup(fda);
close(0);
dup(fdb);
Help is appreciated!