LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-06-2010, 03:33 AM   #1
SuchANewb
LQ Newbie
 
Registered: Nov 2010
Posts: 7

Rep: Reputation: 0
Pipes problem


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!
 
Old 12-06-2010, 04:55 AM   #2
SuchANewb
LQ Newbie
 
Registered: Nov 2010
Posts: 7

Original Poster
Rep: Reputation: 0
char temp_in[16]="";

how do I make the same with char temp_out[50][16]; ?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Named pipes - Problem with write() and read() MarijaS Programming 2 08-10-2010 08:48 AM
problem in handling pipes concept while passing error and output in two files nagendrar Programming 1 11-06-2009 08:11 AM
Curious about open file handlers and pipes with a past problem helptonewbie Linux - Newbie 1 08-27-2008 03:49 AM
Problem executing a command with pipes DeepSeaNautilus Programming 12 08-08-2008 01:28 PM
pipes screensaver problem greengrocer Linux - Newbie 3 08-12-2006 08:36 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration