1

I am new to C , So i only know simple functions.(ex: wait(NULL) ).

Here is my main question :

Modify the program so that only the parent process creates 3 child processes, and each new created process calls a function CPU(). In addition, make the parent process wait for each child’s termination.

I know this one is the correct answer

#include<stdio.h>

int main(void)
{
    int i ;

    for( i = 0; i < 3; i++)
    {
        int ret = fork();
        if(ret == 0){
            printf("My process ID is %d\n", getpid());
            return 0;
        }

    }

    for (i = 0; i < 3; i++) //Line B
        wait(NULL);
}

but my problems are

  1. why below code is wrong when wait is executed by the parent in the loop

    #include <stdio.h>
    
    int main(void)
    {
        int i ;
    
        for( i = 0; i < 3; i++)
        {
            int ret = fork();
    
            if(ret == 0) {
                printf("My process ID is %d\n", getpid());
                return 0;
            }
            else
                wait(NULL);
        }
    }
    
  2. in very first code why do we write wait(NULL) inside a for loop? can't we write it without a for loop

  3. if there is no "return 0" in child process, should the for loop be changed to

    for (i = 0; i <7; i++)
        wait(NULL);
    
  4. I don't know how to write the CPU function

Zeal
  • 19
  • 5
  • 1
    It is not a C question, since `wait` is not mentioned in the C11 standard [n1570](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf). It could be a [POSIX](https://en.wikipedia.org/wiki/POSIX) question, a Unix one, or a Linux one. So **edit your question** to improve it and give some [MCVE]. BTW, StackOverflow is *not* a do-my-homework site. Read [ALP](http://www.cse.hcmut.edu.vn/~hungnq/courses/nap/alp.pdf) or something newer related to Unix or Linux or POSIX programming. Read also [fork(2)](http://man7.org/linux/man-pages/man2/fork.2.html) – Basile Starynkevitch May 07 '18 at 04:51
  • 1
    ... and read of course [wait(2)](http://man7.org/linux/man-pages/man2/wait.2.html) – Basile Starynkevitch May 07 '18 at 04:53
  • At last, compile with all warnings & debug info: `gcc -Wall -Wextra -g` with [GCC](http://gcc.gnu.org/) and [use the `gdb` debugger](https://sourceware.org/gdb/current/onlinedocs/gdb/) to understand the behavior of your program – Basile Starynkevitch May 07 '18 at 04:56
  • @BasileStarynkevitch I don't think this is as straight "do my homework problem", but more like some questions whose answer does not become apparent until you've written a *meaningful* child process. – Antti Haapala -- Слава Україні May 07 '18 at 05:07
  • 1
    I gave several links to help the OP – Basile Starynkevitch May 07 '18 at 05:17
  • @paulsm4 but it isn't, this question has 4 subitems, none of which is addressed in that question - **2 of the answers there even advices writing the incorrect code that OP asks about in #1!** – Antti Haapala -- Слава Україні May 07 '18 at 05:30

1 Answers1

6
  1. This code would not execute the child processes in parallel but execute each one sequentially. The difference does not show until the child processes do something beyond printing one message and exiting (you could add sleep(1)).

  2. wait function will wait for the termination of one child process (whichever finishes first). If you've got 3 child processes, you must call wait successfully at least that 3 times to ensure that each one of them has terminated. Perhaps easier would be to call wait until it sets errno to ECHILD, meaning that there are no children left:

    while (1) {
        errno = 0;
        if (wait(NULL) == -1) {
            if (errno != ECHILD) {
                perror("Unexpected error from wait");
            }
            break;
        }       
    }
    
  3. Each parent process should wait for its own children (or set the SIGCHLD disposition to SIG_IGN). Instead of counting the number of waits, use the code above

  4. This means just: "call a function by the name CPU", i.e.

    void CPU(void) {
    }
    

    in the child process.


BTW, your code is missing the necessary headers for a couple functions -

#include <sys/types.h>
#include <sys/wait.h>

for wait,

#include <unistd.h>

for fork and additionally #include <errno.h> for my errno addition.