Hi, I have some questions about multi pipe. First of all, What I should do is making 6 child processes and each of child process reduces a global value (by 1) which is saved in malloc concurrently until the value reaches 0. All I could find from google was about read and write a message in buffer between a parent and a child processes. How do I have to implement it? Where do I have to write such code as "global_value--;"?
And some examples used "command" like linked list (next=command->next) what does linked commands do in multi pipe environment?

please help!

typedef struct {
    
    int buffer1[10];
    int buffer2[10];
    int index;
} agent_buffer;



int main(){
    execute_Pipe();
    
}

int execute_Pipe() 
{
    agent_buffer *buffer;
    if (( buffer = (agent_buffer *) malloc( sizeof(agent_buffer) ) ) == NULL) {
        printf("ERROR ALLOCATING mybuffer\n");
        exit;
    }


    int fd[6][2];
    int status;
    int isFirstPipe = 1;
    int pid[6] = {-1};
    int count = 3;
    int i,j;
    enum PIPES {READ, WRITE};


    while (1)
//    for (j=0;j<10;j++)
    {
        printf("%d\n",j);
        
        if( pipe(fd[count])<0 )
        {
            printf("\nError in Pipe\n");
            return -1;
        }
        pid[count] = fork();
        if (pid[count] < 0)
        {
            printf("\nError in fork");
            return -1;
        }
        else if (pid[count])  //father
        {
            printf("father\n");
            for (i = 0; i <= count; i++)
            {
                close(fd[i][READ]);
                close(fd[i][WRITE]);

            }
            int buf_index = buffer->index;
            for (i = 0; i <= buf_index-1; i++)
            {
                printf("%d\n",buffer->buffer1[buf_index]);

            }

            return 0;
        }
        else //child
        {
            printf("child\n");
            for (i = 0; i <= count; i++)
            {
                close(fd[i][READ]);
                close(fd[i][WRITE]);

                int buf_index = buffer->index;
                buffer->buffer1[buf_index]=i;
                buffer->index++;
            }
            return -1;

        }
    }
    
    free(buffer);
}

Here's a pipe program that forks 5 times, with each child adding a row of the array longarray and then communicating the sum back to the parent...

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>

#define ARR_ROW 5
#define ELEMENTS_PER_ROW 3

unsigned long longarray[ARR_ROW][ELEMENTS_PER_ROW] = 	{
								{1, 2, 3},
								{4, 5, 6},
								{7, 8, 9},
								{10, 11, 12},
								{13, 14, 15}
							};

enum PIPES {READ, WRITE};

int main(int argc, char**argv)
{
	unsigned long i = 0, tot = 0; 
	int hpipe[2];
	char ch[10];

	signal(SIGCHLD, SIG_IGN);
	pipe(hpipe);

	for (i = 0; i < ARR_ROW; ++i)
	{
		if (!fork())
		{
			int j = 0; 
			unsigned long ans = 0;
			
			close(hpipe[READ]);
			dup2(hpipe[WRITE], 1);
			close(hpipe[WRITE]);
			
			for (j = 0; j < ELEMENTS_PER_ROW; ++j)
				ans += longarray[i][j];
				
			fprintf(stdout, "%lu\n", ans);

			exit(EXIT_SUCCESS);
		}
	}

	close(hpipe[WRITE]);
	dup2(hpipe[READ], 0);
	close(hpipe[READ]);

	for (i = 0; i < ARR_ROW; ++i)
	{
		fgets(ch, 10, stdin);
		tot += strtol(ch, NULL, 10);
	}

	fprintf(stdout, "parent received a total of->%lu\n", tot);
	exit(EXIT_SUCCESS);
}
This article has been dead for over six months. Start a new discussion instead.