Thread: Process Scheduling using fork()

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    16

    Process Scheduling using fork()

    Hi I have to accomplish the following:

    Write a program that will consider the effects of process scheduling by using fork() system call. The write() system call is used instead of printf. The sleep() system call is used to prevent the process from running to completion within one slice time. Compile and run your program so it will output as follows:

    Parent
    Parent
    Child
    Parent
    Child
    Parent
    Child
    Parent
    Child
    Child

    I would appreciate any help on this, I'm not sure what the fork() system call does and how to integrate it in a program.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    Do you have any code to work with at all yet? Also, if your getting a programming assignment on a specific function, I would have guessed they had explained what it was for to start with. If your really stuck, you might want to talk to your teacher.
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    16
    It's an operating systems class there's no code that we learned whatsoever which sucks, I have no examples to reference.

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Search references on fork() on google, or try "man fork" in unix
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User
    Join Date
    Nov 2003
    Posts
    16
    I searched for some examples and I'm trying to compile one and I'm getting an error saying: "fatal error C1083: Cannot open include file: 'unistd.h': No such file or directory
    Error executing cl.exe." here's the code:

    Code:
    #include <iostream>
    using namespace std;
    #include <sys/types.h> 
    #include <unistd.h> 
    
    int main(void) { 
    
    	pid_t childpid;
    
    	childpid = fork();
    	for (int i = 0;  i < 5;  i++) {
    		cout << "This is process " << getpid() << endl;
    		/* sleep is a system call or library function that suspends
    		   this process for the indicated number of seconds */
    		sleep(2);
    	}
    
    	return 0;
    }

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    95
    You need to compile your code on some kind of unix system - Windows/MS compiler don't do fork().

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > Compile and run your program so it will output as follows:
    Unless you're prepared to sleep() for days at a time, there is no real way to ensure that two independent processes will remain synchronised such that it's guaranteed that they'll always produce the same output.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    Quote Originally Posted by Salem
    > Compile and run your program so it will output as follows:
    Unless you're prepared to sleep() for days at a time, there is no real way to ensure that two independent processes will remain synchronised such that it's guaranteed that they'll always produce the same output.
    I know with threads you can do seamophores(SP) and mutexes. You could use a pointer you declare in the main, and have it check if one has printed and the other hasn't. Like if the value the pointer points to equals anything but 0, the parent has printed its message, and if it equals 0, the child has. You can tell which is the parent and the child based on what fork returns (I believe its the pid to the parent, 0 to the child).
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

  9. #9
    Registered User
    Join Date
    Nov 2003
    Posts
    16
    yes you're correct Xipher thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Understanding fork()
    By NuNn in forum C Programming
    Replies: 8
    Last Post: 02-27-2009, 12:09 PM
  2. Problem with forking a process
    By Unitedroad in forum C Programming
    Replies: 10
    Last Post: 10-04-2007, 01:43 AM
  3. process programming
    By St0rM-MaN in forum Linux Programming
    Replies: 2
    Last Post: 09-15-2007, 07:53 AM
  4. Round Robin Scheduling using c.
    By eclipt in forum C Programming
    Replies: 8
    Last Post: 12-28-2005, 04:58 PM
  5. fork(), exit() - few questions!
    By s3t3c in forum C Programming
    Replies: 10
    Last Post: 11-30-2004, 06:58 AM