Newsgroups: comp.unix.questions Path: utzoo!utgpu!news-server.csri.toronto.edu!torsqnt!geac!maccs!maccs.dcss.mcmaster.ca!ashraf From: ashraf@maccs.dcss.mcmaster.ca (Ashraf Mahmoud) Subject: Questions regarding PIPES and SHARED MEMORY. Message-ID: <285926A1.13114@maccs.dcss.mcmaster.ca> Sender: ashraf@maccs.dcss.mcmaster.ca (Ashraf Mahmoud) Organization: McMaster University, Hamilton, Ontario, Canada Date: Fri, 14 Jun 91 20:27:13 GMT 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 #include #include #include #include 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.