0

I have provided code for a problem that involves fork. However I am at a tough spot figuring out a few key parts to the program. The problem states for me to Check the appropriate command-line argument and output a usage message if the command line is incorrect. Initialize pr_limit from the command line. The pr_limit variable specifies the maximum number of children allowed to execute at a time.The pr_count variable holds the number of active children. Initialize it to 0. Execute the following main loop until the end-of-file is reached on standard input: If pr_count is pr_limit, wait for a child to finish and decrement pr_count. Read a line from standard input (fgets) of up to MAX_BUF characters and execute a program corresponding to that command line by forking a child and execute the file. Increment pr_count to track the number of active children. Check to see if any of the children have finished (**). Decrement pr_count for each child that has completed. After encountering end-of-file on standard input, wait for all of the remaining children to finish and then exit. For each terminated child, prints out its exit code value.

The portion that I am having trouble implementing involves reading line from standard file and executing a program corresponding to the command line by forking. As well as keeping track of the active children and checking if children has finished. Still fairly new to systems programming.

int main (int argc, char *argv[])
{
    FILE *file;
    file = fopen("testsim.c", "r+");

    pid_t childpid;
    childpid = fork();

    int len = 0;
    char *line = NULL;

    read = getline(&line, &len, file);
    MAX_BUF = read;
    char buf[MAX_BUF];

    int i, pr_limit;
    int pr_count = 0;

    if (argc != 2)
    {
            fprintf(stderr, "Usage: %s processes\n", argv[0]);
            return 1;
    }

    if (childpid == 0)
    {
            while (!feof(file))
            {
                    if (pr_count == pr_limit)
                    {
                            wait(-1, &status, WNOHANG);
                            pr_count--;
                    }

                    else
                    {
                            fgets(buf, MAX_BUF, file);
                    }
                            pr_limit = atoi(argv[1]);
                            for (i = 0; i < pr_limit; i++)
                            {
                                    if ((childpid = fork()) <= 0)
                                            break;
                            }

                    fprintf(stderr, "i: %d process ID: %ld parent ID: %ld child ID: %ld\n", i, getpid(), getppid(), childpid);
            }
    }

    if (pid < 0)
    {
            perror("Error: \n");
            return EXIT_FAILURE;
    }

    return 0;

}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • 1
    Read [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Andrew Henle May 03 '18 at 10:05
  • `if (argc != 2)` - shouldn't you be doing that *before* you fork. Seems pointless to stand up another process when neither get past that condition if its triggered and they both exit. – WhozCraig May 03 '18 at 10:14
  • @WhozCraig i thought that i called the fork after the if(argc != 2), was it done incorrectly?? –  May 03 '18 at 15:41

0 Answers0