0

I am creating 10 children to one parent. I want all the children to write into a pipe appending the pipe, so I can then read the collective data later in the parent. So first child writes into the pipe "9-6" then the second child writes "9-6" making the contents inside the pipe "9-6 9-6" but what I have discovered here is that Every child when open the pipes for writing inside it. It truncates the pipe. Is there a way I can just keep on adding content into the pipe and eventually in the end just read it.

#include  <fcntl.h>                              //
#include  <stdio.h>                              //
#include  <stdlib.h>                             //
#include  <string.h>                             //
#include  <sys/types.h>                          //
#include  <sys/wait.h>                           //
#include  <sys/stat.h>                           //
#include  <termios.h>                            //
#include  <unistd.h> 
#include <iostream> 

using namespace std;


int main()
{
    pid_t pids[10]; //10 children
    int i;
    int n = 10;
    

    int f1 = mkfifo("p", 0666);   //making named pipe                                   
    
    if (f1 < 0)
        std::cerr << "Pipe not created";

    char str[256] = "9-6";   //character array that every child writes in the pipe
    char read_char[256] = "";                                            
    /* Start children. */
    for (i = 0; i < 10; ++i) {
        if ((pids[i] = fork()) < 0) {
            perror("fork");
            abort();
        }
        else if (pids[i] == 0) {\

            int fifo_write = open("p", O_WRONLY); //open the pipe for writing
            if (fifo_write < 0)
            {
                std::cerr << "Pipe could not be created";
                return 0;
            }
            else
            {
               
                write(fifo_write, str, sizeof(str)); //write char str[] and close the pipe
                close(fifo_write);
                exit(0); 
            }
            
            
        }

    }

    
    

    /* Wait for children to exit. */
   
    int count(0); 
    int fifo_read = open("p", O_RDONLY);
    char str1[256] = ""; 
    if (fifo_read < 0)
    {
        std::cerr << "Pipe could not be created";
    }
    else
    {
            
        read(fifo_read, str1, sizeof(str));
        cout << str1 << endl;
        count++; 
        close(fifo_read);
    }
    cout << "the count is " << count << endl; 
    unlink("p");
}

I have also tried first reading the pipe and appending what is read from the pipe with "9-6" and then writing in the pipe again. The implementation of that looks like this

#include  <fcntl.h>                              //
#include  <stdio.h>                              //
#include  <stdlib.h>                             //
#include  <string.h>                             //
#include  <sys/types.h>                          //
#include  <sys/wait.h>                           //
#include  <sys/stat.h>                           //
#include  <termios.h>                            //
#include  <unistd.h> 
#include <iostream> 

using namespace std;





int main()
{
    pid_t pids[10];
    int i;
    int n = 10;
    

    int f1 = mkfifo("p", 0666);                                         //Making named pipe
    
    if (f1 < 0)
        std::cerr << "Pipe not created";

    char str[256] = "9-6";   
    char read_char[256] = "";                                            
    /* Start children. */
    for (i = 0; i < 10; ++i) {
        if ((pids[i] = fork()) < 0) {
            perror("fork");
            abort();
        }
        else if (pids[i] == 0) {
//here I open the pipe for read so that I can read what inside and concatenate it with 9-6 //so every time it is concatenated with 9-6 and written back in the pipe
            int fifo_read = open("p", O_RDONLY); 
            if (fifo_read < 0)
            {
                std::cerr << "Pipe could not be created";
                return 0;
            }
            else
            {
                read(fifo_read, read_char, sizeof(read_char)); 
                strcat(str, read_char); 
                close(fifo_read); 

            }
            int fifo_write = open("p", O_WRONLY);
            if (fifo_write < 0)
            {
                std::cerr << "Pipe could not be created";
                return 0;
            }
            else
            {
               
                write(fifo_write, str, sizeof(str));
                close(fifo_write);
                exit(0); 
            }
            
            
        }

    }

    
    

    /* Wait for children to exit. */
   
    int count(0); 
    int fifo_read = open("p", O_RDONLY);
    char str1[256] = ""; 
    if (fifo_read < 0)
    {
        std::cerr << "Pipe could not be created";
    }
    else
    {
            
        read(fifo_read, str1, sizeof(str));
        cout << str1 << endl;
        count++; 
        close(fifo_read);
    }
    cout << "the count is " << count << endl; 
    unlink("p");
}

But when I do this mkfifo fails and "pipe not created" is printed

  • 1
    Instead of `open("p", O_WRONLY)`, try `open("p", O_APPEND)`. Calling your pipe something as minimal as `p` is a bit adventurous, but perhaps that's just for the sake of your [mre]. – Paul Sanders Apr 17 '22 at 22:29
  • @PaulSanders Paul, that gives me error in the creation of pipe (where I do mkfifo). On changing from O_WRONLY to O_APPEND the pipe fails to create. Thank you for your answer! – Abdullah Chaudhry Apr 17 '22 at 22:36
  • @PaulSanders I tried debugging the code after making the change of "O_APPEND". And it turns out the program hangs on the 8th last line (read(fifo_read, str, sizeof(str))). If any idea why I will be grateful. – Abdullah Chaudhry Apr 17 '22 at 22:44
  • Now that I look at the code more carefully, I realise that I don't understand what you are trying to do. Your child processes seem to be both reading from and writing to the pipe, I think you need to talk to your [rubber duck](https://en.m.wikipedia.org/wiki/Rubber_duck_debugging). – Paul Sanders Apr 18 '22 at 10:21

0 Answers0