Thread: Odd/Even Lines in given file - fork() child process

  1. #1
    Registered User
    Join Date
    Sep 2016
    Posts
    3

    Question Odd/Even Lines in given file - fork() child process

    I've used fork() to create child processes for each command line argument. Within this, I want the child process to check if the argument is a valid filename/can read the file, if not it should return exist status -1. But if it can, it should count the lines in the file and return an exit status of 0 if even number or 1 if odd. I want the parent process to execute these child processes in parallel and use their exit status' to determine whether overall there is an even or odd number of lines in all of the files. Print odd or even to standard output and exit with 0 if even, 1 if odd.

    Below is the code I've written so far to create the child processes. I was hoping someone could give me some idea as to how to get going on the childs and parents job sections. Any tips are appreciated.

    Thanks!

    Code:
     
    #include <unistd.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main() {
      int   i;
      pid_t pid;
    
    
      for(i = 0; i < n; i ++)
      {
        pid = fork();
    
    
        if(pid == 0) break;
      }
    
    
      if(pid == 0)
      {
        // Do child's job
      }
      else
      {
        // Do parent's job
      }
    
    
    }

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You haven't use command line parameters at all. And your for loop will only create one child process and then break.

  3. #3
    Registered User
    Join Date
    Sep 2016
    Posts
    3
    yeah my bad. Does this look a bit better at all..?

    Code:
    #include <unistd.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(int argc, char* argv[]) 
    {
      pid_t pid;
      FILE *fp;
      int count = 0;
      int no_lines = 0;
      
      for (int i=0; i < argc; i++) {
        pid = fork();
    
    
        if(pid == 0)
        {
          if (access(pid, R_OK) != -1) {
            fp = fopen(pid, "r");
            for (c=getc(fp); c != EOF; c=getc(fp)) {
              if (c == '\n') {
                count = count + 1;
              }
            }
            fclose(fp);
            if ((count % 2) == 0) {
              exit(0)
            }
            else {
              exit(1)
            }
          }
          else {
            exit(-1)
          }
        }
        else
        {
          // Do parent's job
        }
      }
    }
    Last edited by britty4; 09-19-2016 at 05:16 AM.

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You should loop from i=1, not from i=0 (the 0th element of argv is the program name).

    You should check if fork returned -1 (error).

    You don't need to use "access". You should instead check if the return value of fopen is NULL.

    You don't want to open the pid (!). You want to open one of the files from the command line, argv[i].
    Code:
    fp = fopen(argv[i], "r");
    if (fp == NULL)
        exit(-1);
    
    // count newlines ...
    
    fclose(fp);
    exit(count % 2);
    Where you say "Do parent's job", the parent's job at that point is to keep spawning child processes, one for each file on the command line. So just get rid of the last "else" and let the loop do its thing.

    Then after the loop, the parent will have to go into another loop and "wait" for each child to exit and return its status.
    Code:
    int status;
    for (int i = 1; i < argc; i++) {
        wait(&status);  // read about "wait" to see how to deal with status
        // ...
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 09-12-2016, 03:21 AM
  2. Spawn Multiple child using fork()
    By anirban in forum Linux Programming
    Replies: 2
    Last Post: 08-08-2010, 08:00 AM
  3. create a child process that creates a child process
    By cus in forum Linux Programming
    Replies: 9
    Last Post: 01-13-2009, 02:14 PM
  4. Fork() child process question
    By CrackDown in forum Linux Programming
    Replies: 0
    Last Post: 04-17-2003, 11:58 AM
  5. Child Process & Parent Process Data :: Win32
    By kuphryn in forum Windows Programming
    Replies: 5
    Last Post: 09-11-2002, 12:19 PM

Tags for this Thread