File main Author norte Created on December 2014 11 38 AM include stdio

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
* 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);
}