/*
* File: main.c
* Author: norte
*
* Created on December 7, 2014, 11:38 AM
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#define FILE_NAME "Input.txt"
#define N 5
int main(int argc, char** argv) {
int A[N][N],i,j;
int sum[N]={};
int check;
FILE *f;
int buffer;
if ((f=fopen(FILE_NAME,"r"))==NULL)
{
printf("File '%s' didn't found",FILE_NAME);
return 1;
}
int fd[2];
int p[N],l;
int buf,temp;
for (i=0;i<N*N;++i)
{
fscanf(f,"%d",&A[0][i]);
}
if (pipe(fd)<0) // Make pipe
{
printf("Error pipe()\n");
return 1;
}
check=getpid(); // remember father PID
for (i=0; i<5; ++i)
{
if (getpid()==check){ // Real father?
if ((p[i]=fork())<0) // Make son
{
printf("Error fork()\n");
return 1;
}
else
if (p[i]!=0)
{
wait(NULL);
}
else
{
printf("Son process: %d, Father: %d ",getpid(),getppid());
for (j=0;j<N;++j)
{
sum[i]+=A[i][j];
printf("%d ",A[i][j]);
}
close(fd[0]);
printf("%d ",sum[i]);
buffer=sum[i];
write(fd[1],&buffer,1);
}
}
}
printf("\n");
if (getpid()==check)
{
printf("From main process: ");
for (i=0; i<5; i++)
{
close(fd[1]);
read(fd[0],&temp,1);
printf("|%d| ",temp);
}
}
return (EXIT_SUCCESS);
}