473,385 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

fork() help...beginner!!!

Hi I'm in the very early stages of my OS course...We are fresh on the topic of the fork(). So, the if statement code:

// if((childpid = fork()) == 0)

does what?

As a guess, it creates childpid and checks to see if it is a child...
Feb 8 '07 #1
2 2246
michaelb
534 Expert 512MB
The fork system call creates a child process.
After that you typically have some code that executes in the child process, and some code that executes in the parent process.
The value returned from the fork is used to determine where you are.
Here's a little illustration

Expand|Select|Wrap|Line Numbers
  1.  
  2.    int pid;
  3.    pid = fork();
  4.  
  5.    if (pid == 0)
  6.    {
  7.       /* 
  8.       ** fork() returned 0, meaning we are in
  9.       ** the child process. So put your "child" code 
  10.       ** in this section.
  11.       */
  12.    }
  13.    else if (pid > 0)
  14.    { 
  15.        /* 
  16.        ** Continue with the Parent process;
  17.        ** the same process that executed the fork() call.
  18.        ** You may have more code in this section.
  19.        */
  20.    }
  21.    else
  22.    {   
  23.       /*
  24.       ** return value is negative, meaning that
  25.       ** fork() failed. Typically you would have 
  26.       ** some error-handling here.
  27.       */
  28.    }
  29.  
  30.  
Feb 8 '07 #2
Motoma
3,237 Expert 2GB
Great example MichaelB! Thanks!
Feb 9 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Kevin Cloutier | last post by:
Hi, I'm am brand new to PHP and I'm getting up to speed (slowly) running through some example tutorials. But I am having trouble getting a For Loop to work. It is supposed to return the...
2
by: Wong | last post by:
static const char assertFileName = __FILE__; Assert(); This is normally found in applications, what does this mean ? Thanks in advance.
12
by: lifeshortlivitup | last post by:
Hello all....I am brand new to this whole Java environment and have become stumped with an early homework assignment. It sounds simple enough, but after hours and hours of working on it I can't get...
8
by: sathyashrayan | last post by:
Dear group, For a log-in page I have created a mysql db and user registers with a user name and password. The password field is encrypted with $passwd = sha1($_REQUEST); I insert the...
5
by: JoeW | last post by:
Now before I go into detail I just want to say that this is purely for my own benefit and has no real world usage. I remember way back when the tool for *nix systems called forkbomb was created. I...
5
by: bd | last post by:
I keep getting this error: line(28): error C2664: 'convertString' : cannot convert parameter 1 from 'std::string ' to 'std::string' Here is my code: //...
4
by: chihwei | last post by:
can any one help me on this one ? thank you Write the definition of a class Telephone . The class has no constructors, one instance variable of type String called number , and two static...
7
by: ronaldh | last post by:
can anyone help me to set up a formmail.cig i am in over my head last to days trying and no luck i have managed to completly delette my cgi doc all together and need to start from scratch please help...
0
by: nk28 | last post by:
hey, Is there any way to share memory space with child process created using fork. Basically I have a start pointer that updates a queue as soon as a process is created with its pid. But for...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.