The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
Google UNIX.COM


UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Inter-process communication:pipes,doors,etc. adderek UNIX for Advanced & Expert Users 2 01-18-2008 01:39 PM
Interprocess communication status jerardfjay UNIX for Advanced & Expert Users 4 07-15-2005 08:49 AM
Pipe, interprocess communication scmay UNIX for Advanced & Expert Users 2 10-23-2004 05:41 AM
cd using pipes Sinbad Shell Programming and Scripting 2 09-09-2004 07:05 AM
PIPEs and Named PIPEs (FIFO) Buffer size Jus Filesystems, Disks and Memory 1 08-20-2004 07:14 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-14-2004
Registered User
 

Join Date: Apr 2004
Location: Melbourne
Posts: 15
Unhappy Interprocess communication using pipes and fork

I'm very worried. I have an assignment that is due in 3 weeks, and also tute exercises which I can't seem to understand and work out.

Okay, the question:
The parent process will convert the command arguments into integer values using atoi() and store them into an integer array which you will create using malloc() ONLY and is to be a local variable declared in main(). The parent process will then send the values of the array to Process B by writing to pipe1's output stream.

My question here is when i pass it through the pipe to Process B the output is always incorrect, gives me some rubbish. The examples which I've found also always pass string thru the pipe. Can I pass the pointer of the array to Process B and ask Process B to read the input from there?

I don't know why I find this so difficult to understand and have been looking around the internet for notes or examples to ponder on, but seems like all the examples are passing strings thru pipes.

Really really appreciate any sort of help here.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 10-14-2004
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,465
What do your read() / write() calls look like?
Reply With Quote
  #3 (permalink)  
Old 10-14-2004
Registered User
 

Join Date: Apr 2004
Location: Melbourne
Posts: 15
solution

I wrote the solution, but I don't understand how it really works.

#include <sys/types.h>
#include <unistd.h>
#define MSGSIZE 16
main(int argc, char *argv[])
{
pid_t pid;
int p1[2];
int number,i,a;
int *ip, *head;
int inbuf[MSGSIZE];
//A opens pipe
if(pipe(p1)==-1)
{
perror("main err");
exit(1);
}
//A forks B
switch(pid=fork())
{
case -1:
perror("error");
exit(2);
case 0:
close(p1[1]);
for(a=1;a<argc;a++)
{
read(p1[0], inbuf, MSGSIZE);
printf("%d", inbuf[0]);
//why must i specify inbuf[0]? why can't i just write inbuf?

}
break;
default:
ip=(int *)malloc(10*sizeof(int));
head=ip;
close(p1[0]);

for(i=1;i<argc;i++)
{
*ip = atoi(argv[i]);
write(p1[1], ip, MSGSIZE);
ip++;
}

}
}
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 07:32 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0