473,396 Members | 2,057 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,396 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 2248
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.