LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-15-2011, 02:40 AM   #1
tarunchawla
Member
 
Registered: Mar 2010
Location: New Delhi,India
Distribution: Ubuntu 14.04
Posts: 117

Rep: Reputation: 3
how fork works


#include<iostream>
using namespace std;
void main1();
void main2();
int main()

{
cout<<"tarun";
main1();
main2();
}
void main1()
{
cout<<"parent"<<endl;
pid_t pid;
pid=fork();
if(pid==0)
{
cout<<"child";
}
}
void main2()
{
cout<<"parent2"<<endl;
pid_t pid;
pid=fork();
if(pid==0)
{
cout<<"child2";
}
}

please tell me the output and how fork works?
 
Old 10-15-2011, 03:31 AM   #2
macemoneta
Senior Member
 
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 344Reputation: 344Reputation: 344Reputation: 344
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.
 
Old 10-15-2011, 11:55 AM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.

--- rod.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] sed: alone [ /pattern/!d ] works; alone [ s ] works; together =don't work klenot Programming 7 10-23-2010 09:45 AM
Strangest FTP error on RHEL5. gFTP works. RHEL4 works too. GuitsBoy Linux - Software 1 04-10-2008 08:29 AM
Mounting works, playing music works, reading tags doesn't Celettu Linux - Newbie 7 08-23-2006 12:27 PM
pppoeconf do not works on notebook while it works on desktop ...same configuration sarajevo Debian 2 06-12-2006 02:57 PM
XAWTV works, videodog works, motion works but how to code my own? rylan76 Linux - Hardware 0 01-06-2006 06:30 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration