Questions regarding PIPES and SHARED MEMORY.

Ashraf Mahmoud ashraf at maccs.dcss.mcmaster.ca
Sat Jun 15 06:27:13 AEST 1991


Hello every Body,

    I would like first to thank all the unix-group readers who responded to   
my previous question about reading from pipes and how to make it nonblocking.
I have two more questions, the first is really a consequence of the solution I
have received: 
Q1. The following code makes child read from pipe in a nonblocking manner. But
    it seems, when I run the code, that not all messages sent by parent are 
    received by child. Some are lost. The child is supposed to receive five 
    messages (i=0,1,..,4). Sometimes it receives those for i = 0,2,4, and
    sometimes different messages, it differs each execution. How to fix it?
 
 Code:
   ------------------------------------------------------------
#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>


void main ()
{
 int pid1 , fdes[2];
 int FLAGMINE ;
 int status, i ;
 char buf[80];

 pipe(fdes) ;

 fcntl( fdes[0], F_GETFL, &FLAGMINE );
 FLAGMINE = O_NDELAY;
 fcntl( fdes[0], F_SETFL, &FLAGMINE );

 if ((pid1 = fork()) == 0 ) {
                           child1(fdes);
                           exit (0);
                           }
 close(fdes[0]) ;
 for(i=0; i<5; i++) 
 { 
  sprintf(buf,"Parent Calling Child %d Itr = %d",pid1,i);
  write(fdes[1],buf,strlen(buf)+1);
 }
 wait(&status) ;
}

child1( fdes ) 
int fdes[2] ;
{
 int i;
 char buf[80];
 
 close(fdes[1]) ;
 for(i=0; i<1500; i++){
     strcpy(buf,"");
     read(fdes[0],buf,80);
     printf("\n Read succeeded i = %d ",i);
     printf("\nChild> %s",buf);
    
 }
}
          ------------------------------------------------------------


Q2. Any idea about how to declare a certain area ( variables or structures ) 
    in memory where more than one process ( or agent ) can have access to it?
    What type of control ( regarding setting semiphores and other things) is
    required? And how to do it? A simple code for a small example would be    
    appreciated.

    Thank you all out there in advance. Any useful response is greatly welcomed.

    

Ashraf S. Mahmoud
McMaster U.




More information about the Comp.unix.questions mailing list