Search code examples
cpipechild-processinter-process-communicat

Interprocess Communication in C, one character at a time


First off, this IS homework, I am not asking for an answer, however I am confused about something.

I have a homework assignment for a programming class, and I am a little confused about how to write the code the specific way that the instructor is asking.

The program first creates a child process, and then proceeds to send command line arguments from the parent process, through a pipe, ONE CHARACTER at a time to the child process, and then read them into the child process ONE CHARACTER at a time, incrementing the character count in the child process each time a character is read in.

I think I accomplished sending the data through the pipe one character at a time, but I have no idea how to "go" to the child process every time a character is sent, read it, increment the number of characters, and then go back to the parent process and repeat.

Here is my code, It works and gives accurate answers, but any tips on how to accomplish what my instructor is asking would be appreciated, thank you!!

// Characters from command line arguments are sent to child process
// from parent process one at a time through pipe.
//
// Child process counts number of characters sent through pipe.
//
// Child process returns number of characters counted to parent process.
//
// Parent process prints number of characters counted by child process.

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>

int main(int argc, char **argv)
{
// set up pipe

    int    pA[2];
char   buff[50];

pipe(pA);
// call fork()

pid_t  childId = fork();

if (childId == 0) {
    // -- running in child process --
    int     nChars = 0;

    // close the output side of pipe
    close(pA[1]);

    // Receive characters from parent process via pipe
    // one at a time, and count them.

    nChars = read(pA[0], buff, sizeof(buff)); //this line of code is what i need to change to be reading characters in one at a time

    // Return number of characters counted to parent process.
    return nChars;
    }
else {
    // -- running in parent process --
    int     nChars = 0;
    int     size = 0;
    printf("CS201 - Assignment 3 - Timothy Jensen\n");

    // close the input side of the pipe
    close(pA[0]);

    // Send characters from command line arguments starting with
    // argv[1] one at a time through pipe to child process.

    for (int i = 1; i < argc; i++)
      {
        size = strlen(argv[i]);
        for (int z = 0; z < size; z++)
          {
        write(pA[1], &argv[i][z], 1);
          }
      }

    // Wait for child process to return. Reap child process.
    // Receive number of characters counted via the value
    // returned when the child process is reaped.

    wait(&nChars);

    printf("child counted %d chars\n", nChars/256);
    return 0;
    }
}

Solution

  • You need to make the following changes:

    1. Make the last argument 1 in the call to read.

      read(pA[0], buff, 1);
      
    2. Put the above call in a while loop and increment nChar for every successful attempt at read.

         while ( read(pA[0], buff, 1) == 1 )
         {
            ++nChars;
         }
      
    3. Close the file descriptor from the parent process once you are done writing to it.

    Here's a working version of main.

    int main(int argc, char **argv)
    {
       // set up pipe
    
       int    pA[2];
       char   buff[50];
    
       pipe(pA);
       // call fork()
    
       pid_t  childId = fork();
    
       if (childId == 0) {
           // -- running in child process --
           int     nChars = 0;
    
           // close the output side of pipe
           close(pA[1]);
    
           // Receive characters from parent process via pipe
           // one at a time, and count them.
           while ( read(pA[0], buff, 1) == 1 )
           {
              ++nChars;
           }
    
           return nChars;
       }
       else {
           // -- running in parent process --
           int     nChars = 0;
           int     size = 0;
           printf("CS201 - Assignment 3 - Timothy Jensen\n");
    
           // close the input side of the pipe
           close(pA[0]);
    
           // Send characters from command line arguments starting with
           // argv[1] one at a time through pipe to child process.
    
           for (int i = 1; i < argc; i++)
           {
              size = strlen(argv[i]);
              for (int z = 0; z < size; z++)
              {
                 write(pA[1], &argv[i][z], 1);
              }
           }
    
           close(pA[1]);
    
           // Wait for child process to return. Reap child process.
           // Receive number of characters counted via the value
           // returned when the child process is reaped.
    
           wait(&nChars);
    
           printf("child counted %d chars\n", nChars/256);
           return 0;
       }
    }