ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
1. If you want to know the output, why not run it yourself?
2. Put code in a code block to retain formatting.
3. Google and Wikipedia answers simple questions like how fork works.
In the simplest terms, fork() duplicates the entire process of the process that called fork(). This is called forking/launching a child process, and is how all userspace processes are created in Unix/Linux. The return value of fork() is the process id of the resulting child process. Since the result of calling fork() will be an exact duplicate of the parent process, the child process will execute the same logic as the parent. In doing so, it also tests the return value of fork(), and upon seeing that said return value is zero, recognizes that it is the child process. Usually, the child process logic causes it to perform some different actions, often a call to exec(), which replaces the current process with another, never to return.
Of course there is much greater detail on this subject in your handy man pages. I won't 'tell you the output', as that should be trivial for you to do for yourself, and because it would probably defeat the purpose of the homework exercise.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.