I am having a nightmare with this program. When I run it through a test it says following:
Error:
valgrind has detected an error in the program
==22165== Syscall param write(buf) points to uninitialised byte(s)
==22165== at 0x4EE5930: write (in /lib/libc-2.5.so)
==22165== by 0x400B4E: main (in /home/vajeacc/npipe)
==22165== Address 0x7FF000434 is on thread 1's stack
==22169== Syscall param write(buf) points to uninitialised byte(s)
==22169== at 0x4EE5930: write (in /lib/libc-2.5.so)
==22169== by 0x400CA4: main (in /home/vajeacc/npipe)
==22169== Address 0x7FF000434 is on thread 1's stack
Code:
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char** argv)
{
char temp_in[16]="";
char temp_out[50][16];
int pipa[2];
int pipa2[2];
int pipa3[2];
int prices[50];
int price = 0;
int i = 0;
pid_t pid;
if (argc==1)
{
printf("Add an element!\n");
}
else
{
int products=argc-1;
if(pipe(pipa) == -1)
{
perror("pipe call error");
_exit(1);
}
if(pipe(pipa2) == -1)
{
perror("pipe call error");
_exit(1);
}
if(pipe(pipa2) == -1)
{
perror("pipe call error");
_exit(1);
}
pid=fork();
if(pid==0)
{
for( i=0;i<products;i++)
{
price=atoi((argv[i+1]));
printf("Price %d at manufactor: %d\n", i+1, price);
prices[i+1]=price;
close(pipa[0]);
write(pipa[1], argv[i+1], 16);
}
exit(EXIT_SUCCESS);
}
else /*distributer*/
{
wait(&pid);
pid=fork();
if(pid==0)
{
for(i=0;i<products;i++)
{
close(pipa[1]);
read(pipa[0], temp_in,16);
prices[i]=atoi(temp_in);
prices[i]+=(prices[i]*0.2);
printf("Price %d at distributer: %d\n", i+1, prices[i]);
}
for(i=0; i<products;i++)
{
sprintf(temp_out[i], "%d", prices[i]);
close(pipa[0]);
write(pipa2[1], temp_out[i], 16);
}
close(pipa[1]);
exit(EXIT_SUCCESS);
}
else
{
wait(&pid);
pid=fork();
if(pid==0)
{
for(i=0; i<products;i++)
{
close(pipa2[1]);
read(pipa2[0], temp_in, 16);
prices[i]=atoi(temp_in);
prices[i]+=(prices[i]*0.3);
printf("Price %d at dealer: %d\n", i+1, prices[i]);
}
for(i=0; i<products;i++)
{
sprintf(temp_out[i], "%d", prices[i]);
close(pipa2[0]);
write(pipa[1], temp_out[i], 16);
}
exit(EXIT_SUCCESS);
}
else
{
wait(&pid);
pid=fork();
if(pid==0)
{
for(i=0; i<products;i++)
{
close(pipa[1]);
read(pipa[0], temp_in, 16);
prices[i]= atoi(temp_in);
printf("Price %d at consumer: %d\n", i+1, prices[i]);
}
exit(EXIT_SUCCESS);
}
}
}
}
}
return EXIT_SUCCESS;
}
If anyone could shed some light upon my problem I would be grateful!