Need some assistance

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stayit
    New Member
    • Jun 2009
    • 14

    Need some assistance

    Expand the code so that the parent creates two children and then waits until both children and then waits until both children exit.

    Note: The code might have something to do in UNIX.

    Expand your solution further so the program accepts an integer as a command line argument. The program then creates that number of child processes and assigns each child a ranodm number of seconds to sleep. Finally, the parent processes report as each child exits.

    Code:
    #include 		<stdio.h>
    #define DELAY 	2
    
    main ()
    {
    	int newpid;
    	void child_code(), parent_code();
    	printf("before: mypid is %d\n", getpid());
    	if ( (newpid = fork()) == -1 )
    			perror("fork");
    	else if ( newpid == 0 )
    			child_code(DELAY);
    	else
    			parent_code(newpid);
    }
    void child_code(int delay)
    {
    	printf("child %d here. will sleep for %d seconds\n", getpid(), 
    		delay);
    		sleep(delay);
    		printf("child done. about to exit\n");
    		exit(17);
    }
    void parent_code(int childpid)
    {
    	int wait_rv;
    	wait_rv = wait(NULL);
    	printf("done waiting for %d. Wait returned:%d\n", childpid,
    		wait_rv);
    }
    Note: The line printf("before: mypid is %d\n", getpid()); has an error: implicit declaration of function 'int wait(...)'

    Please advise.

    Thanks.
    Last edited by Banfa; Jul 2 '09, 08:16 AM. Reason: A
  • Jibran
    New Member
    • Oct 2008
    • 30

    #2
    You need to declare your function prototypes outside the main function.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      Originally posted by stayit
      The line printf("before: mypid is %d\n", getpid()); has an error: implicit declaration of function 'int wait(...)'
      This is the same error message that was explained in your other thread: A little help with this code!

      Comment

      • stayit
        New Member
        • Jun 2009
        • 14

        #4
        Re: Assistance

        Originally posted by Jibran
        You need to declare your function prototypes outside the main function.
        How would I do that and what should I include? Just a little confused.

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          wait is a posix function (the UNIX clue tells us this). If you google "posix wait" then you will get the help pages describing the function, how to use it and what headers you need to include.

          From the question I would say you will need to read an understand most of the man (help) page on that function.

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            How did you come up with line 7? It contains a pair of old-style (pre-ANSI) function declarations. This kind of function declaration went out of style in 1990. I suggest you read up on "function prototypes".

            I notice that you only include <stdio.h>; but I see the following functions being called: getpid, fork, sleep, exit, wait. None of these functions are declared in <stdio.h>; however you only report a compiler error for 'wait'. Did you include other headers or did you get additional errors?

            Functions getpid, fork, sleep, and wait are not part of the Standard C Library. You must have learned how to use them from some environment-specific documentation. That documentation should tell you everything about how to use them correctly.

            It is not uncommon for similar functions to be provided by several environments. If you don't tell us what environment you're using then we have to guess. If we guess wrong then our advice is wrong. You refer to "UNIX", but a clear declarative sentence ("My program runs on the UNIX operating system") would have been clearer.

            Comment

            • stayit
              New Member
              • Jun 2009
              • 14

              #7
              Apologies. I am using the C-Free 4 IDE.

              Comment

              Working...