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);
}