Thread: Using fork on command line arguments to count number of lines in a given file

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

    Question Using fork on command line arguments to count number of lines in a given file

    I know I shouldn't post my whole problem here, but I'm just really stuck on even a starting point. I've done a fair bit of research but can't really come up with a plan for this, being very new to C programming.
    So I'm just wondering if someone could point me in the right direction or give me a few tips for this program I'm trying to build.

    For each command-line argument, I want to use fork() to create a child process to handle that argument. Each child process should determine if the command-line argument it is processing is a valid filename. If not, or if the child process cannot open or read the file, the child process should return an exit status of -1. If the child process can read the file, it should count the number of lines in the file and return an exit status of 0 if there are an even number of lines, or 1 if there are an odd number of lines in the file. The parent process should cause all the child processes to execute in parallel and examine their return values to determine whether, overall, there is an even or odd number of lines in all the files (files that did not exist/could not be read should be excluded). The parent process should print (to standard output) "Even" if there were an even number of lines, or "Odd" if there were an odd number of lines. It should then exit with a status of 0 if the result was even, or 1 if the result was odd.

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,687
    If you need any solid examples on how the function fork() works this may be helpful.

    Using fork() in C/C++ – a minimum working example << timmurphy.org

    If your really stuck, attempt some code then post it here with further questions. Welcome to Cboard
    Double Helix STL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 09-25-2011, 12:37 AM
  2. Count number of lines in a text file
    By eXeCuTeR in forum C# Programming
    Replies: 3
    Last Post: 04-30-2008, 01:46 AM
  3. reading in command line arguments from a file?
    By g1i7ch in forum C Programming
    Replies: 30
    Last Post: 06-22-2006, 01:35 PM
  4. Command line arguments and file redirection
    By Abdi in forum C++ Programming
    Replies: 3
    Last Post: 04-22-2002, 12:44 AM
  5. Command line arguments for file
    By gagig in forum C Programming
    Replies: 7
    Last Post: 02-05-2002, 02:06 AM

Tags for this Thread