Thread: How to use fork()!

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    4

    Smile How to use fork()!

    Hi, I'm rather new in C programming.

    I'm now studying about Operating Systems and have a 3 weeks mini project about "PROCESS".

    I don't know how to start it!!

    This is a detail :

    "Write a C program on Linux(and Windows) to fork() a child process. And have both perform some programming as you like,

    but that must demonstrate the benefits of having many processes."

    I really really don't know how to program.

    Could anyone please explains & suggests or shows some examples about fork() and the detail above.


    Thank you very much

    PS. Sorry for my bad ENG. ^^

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Were any examples listed in the class?
    Were you listening (or there)?

    Is it just fork(), or are you supposed to use pipe() as well?

    Because fork() by itself is just the basics for writing a very simple command line shell.
    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.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Fork() allows you to have two isolated actions occurring at (more or less) the same time. Fork is an extremely simple method of doing this, which indicates to me that you haven't put any work into this at all. The members of the board are almost always happy to help debug certain programs and point you in the right direction - but don't expect anyone to actually write the whole program for you.

    Do a google search for examples of fork() - they're abundant.

  4. #4
    pwning noobs Zlatko's Avatar
    Join Date
    Jun 2009
    Location
    The Great White North
    Posts
    132
    Hello.
    Please be sure to read the fork man page by typing man fork at your command prompt.

    Fork makes a copy of your process. When fork returns, there are two identical processes running, and fork returns in each process. In the parent process, fork returns the process ID of the child. In the child process, fork returns 0.

    If fork fails, then it returns -1 in the parent process, and there is no child process.

    Here is an example:
    Code:
    pid_t child = fork();
    if (child < 0)
    {
       /* fork failed, handle it */
    }
    if (child > 0)
    {
       /* this is the parent process */
    }
    if (child == 0)
    {
       /* this is the child process */
    }
    Each process has its own copy of its data. Its as if you started two identical prgrams from the command line. Certain things are not inherited by the child. Check the man page.

    That's the easy part.
    Now you have to demonstrate usefulness, which can be difficult if you have no programming experience because for a parent and child to work together you need some kind of interprocess communication. Maybe you could fork a couple of child processes, and have each try to read messages from a message queue. The parent would send work requests to some message queue, and the child processes could process requests in parallel. You'd need to know how to use message queues. The man pages for those are man msgget, msgsnd, and msgrcv. Be proactive, read up on unix system programming.

    Good luck.

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    4

    Smile

    Quote Originally Posted by Salem View Post
    Were any examples listed in the class?
    Were you listening (or there)?

    Is it just fork(), or are you supposed to use pipe() as well?

    Because fork() by itself is just the basics for writing a very simple command line shell.
    Dear Salem,

    Thanks very much ^^

    but I'm sorry to say that, there is no lesson about C programming or its example in OS class.

    as I said, I'm rather new with C, just a simple loop, input/output, array & some simple functions.

    moreover, this is my first time to program in linux, so, everything seem to be new and challenge for me.

    by the way, thank you for your suggestion sir ^^

    Ps. just have a simple C programming book for windows in my language. T_T

    & Sorry for my English again sir.
    Last edited by ioat; 06-26-2009 at 10:59 AM.

  6. #6
    Registered User
    Join Date
    Jun 2009
    Posts
    4

    Smile

    Thank you for all suggestion

  7. #7
    pwning noobs Zlatko's Avatar
    Join Date
    Jun 2009
    Location
    The Great White North
    Posts
    132

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    BTW, Windows doesn't have a fork() function, so when you're doing it on Windows you'll have to use something else like CreateProcess().
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  9. #9
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Did you take the required programming language prerequisites for this course?
    That's odd that they would give an assignment like that and expect students to know C.

  10. #10
    Registered User
    Join Date
    Jun 2009
    Posts
    4
    Quote Originally Posted by itCbitC View Post
    Did you take the required programming language prerequisites for this course?
    That's odd that they would give an assignment like that and expect students to know C.
    Yes, I did. But Computer Programming I for Windows just teaches us only the basical level
    (Data types, Variables,Expression,input/output,Control Statement, Subprogram, Array, and Pointer).
    Another programming courses are VB and JAVA (Basic - Advance).

    In fact, there is no prerequisites programming language required for OS course!
    I don't know why the lecturer assumes that all students in the class know C for UNIX in
    Advance level, so, he gives this assignment to us.

    I can say, "I'm now walking with no direction"

    BTW, thank you for all suggestion!

  11. #11
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    This is certainly out of the realm of beginner programming. It would usually be a third or 4th year computer science/engineering course, where the prof can of course assume everyone has quite a bit of programming experience, even if no pre-requisite has been listed explicitly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fork(), pause(), and storing PID's in a linked list
    By vital101 in forum C Programming
    Replies: 10
    Last Post: 09-28-2007, 02:16 AM
  2. Fork() not working.
    By BENCHMARKMAN in forum C++ Programming
    Replies: 3
    Last Post: 08-01-2007, 12:28 AM
  3. Fork - unpredictable?
    By fredkwok in forum Linux Programming
    Replies: 4
    Last Post: 03-26-2006, 02:49 PM
  4. fork(), exit() - few questions!
    By s3t3c in forum C Programming
    Replies: 10
    Last Post: 11-30-2004, 06:58 AM
  5. Daemon programming: allocated memory vs. fork()
    By twisgabak in forum Linux Programming
    Replies: 2
    Last Post: 09-25-2003, 02:53 PM