Need help with fork()

This is a discussion on Need help with fork() within the C Programming forums, part of the General Programming Boards category; Hi all, This looks like the place for a noob like me to recieve some guidance from the seasoned C ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    4

    Need help with fork()

    Hi all,

    This looks like the place for a noob like me to recieve some guidance from the seasoned C vets

    It's been a loooong time since I used C (or did any programming for that matter..) and I'm required to write a C program for school which uses fork().

    The general idea is to write a simple program which spawns a child process which keeps writing a character while the parent process also keeps writing a character to observe what sort of display the program produces. The problem is I have no idea where to start!? Alot of the examples of code I have found look very cryptic and it would be really nice to see some more basic examples..

    Can anyone offer any advice or useful resources to aid me in my task?

    Thanks.

  2. #2
    Prying open my third eye.
    Join Date
    Jun 2005
    Posts
    45
    The following shows a basic way I have used it. Any code within the if block is the child process. It is good to take in multiple connections in a client/server app.

    Code:
    pid_t child;
    
    .......
    
    
    if ( (child = fork() ) == 0) {
    /************
    /*This is the child process
    /*
    /***********/
    }
    "So you're one of those condescending UNIX computer users?"

    "Here's a nickel, kid. Get yourself a better computer."

  3. #3
    B26354 Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    fork() returns the process identifier of the child to the parent and zero to the child itself. fork() will return -1 if an error occurs, which is always good to know about. Call fork() and test the return value to decide what character the process should display. Be sure to handle error conditions.

    Come on back if you're still stuck and post the code you've written so far.
    Jason Deckard

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    134
    May be this will help

    A fork() Primer

    Thanks,

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    4
    Thanks alot for your replies guys.

    I'll have a crack at it and let you know how I went..

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    4
    Ok,

    So I've modified the example code nkhambal posted for my own selfish purposes .

    It is as follows:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    
    main()
    {
        pid_t pid;
        int rv;
    
        while(1){
          switch(pid=fork()) {
            case -1:
                perror("fork");  /* something went wrong */
                exit(1);         /* parent exits */
    
            case 0:
    	    printf("C");
           
            default:
                printf("P");
          }
        }
    }
    I suspect this is not the tidiest way of doing things and am very open to any suggestions and criticisms anyone may have.

    Thanks

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,673
    Well it's a good way of hosing your machine I suppose.
    Not to mention the lack of break statements in your cases.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    .
    Join Date
    Nov 2003
    Posts
    307
    The while(1) statement will create an endless stream of new processes which will crash your box.

    each block in a switch() statement usually needs a break; at the end of it.

  9. #9
    Registered User
    Join Date
    Aug 2005
    Posts
    4
    Ok so I've added break; at the end of each block.

    Can you please suggest an alternative to the while(1) statement? Perhaps a 'for' statement?

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,673
    Why does it need any kind of infinite loop at all?
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Registered User
    Join Date
    Apr 2005
    Posts
    134
    Quote Originally Posted by Salem
    Why does it need any kind of infinite loop at all?
    I think because as per OP he needs to write a program to create processes and write some stuff in the child process and write some stuff in parent.

    But you need to make sure that once your job of writing a character in child process is complete, you need to exit the child process with exit(0) which means "exit with success".

    Code:
    for (;;) {
          switch(pid=fork()) {
            case -1:
                perror("fork");  /* something went wrong */
                exit(1);         /* parent exits */
    
            case 0:
    	    printf("C");
                exit(0);  /*Important to exit here */
           
            default:
                printf("P");
          }
        }
    This way, you will not hose your machine by flooding it with processes which you are not going to use eventually. If you don't, then they will be left int hanging state ("Zombies") after parent exits.
    Last edited by nkhambal; 08-24-2005 at 02:35 AM.

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,673
    I think you're going to get zombies anyway - where are the wait() calls which get the exit status of a child?
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 01:49 PM
  4. fork(), exit() - few questions!
    By s3t3c in forum C Programming
    Replies: 10
    Last Post: 11-30-2004, 05: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21