Open System Services Programmer's Guide
nrd[i] = -1; /* set end-of-file marker */
}
sumread += totread;
} while(totread);
printf("Total bytes read from files = %d\n", sumread);
return(EXIT_SUCCESS); /* write of all fifos done */
}
/* Execute parent program to read the named pipes
using the select() function and non-blocking I/O
*/
printf("Parent process code started.\n");
maxfdp1 = 3; /* Set highest descriptor number */
FD_ZERO(&m_rfds);
for(i = 0; i < nfifo; i++) /* create output files */
if((rd[i] = open(ofile[i], O_WRONLY | O_CREAT, 0644)) < 0) {
fprintf(stderr, "Can't create %s\n", ofile[i]);
exit(EXIT_FAILURE);
}
for(i = 0; i < nfifo; i++) {
if((fd[i] = open(fifo[i], O_RDONLY)) < 0) {
fprintf(stderr, "Can't open fifo %s.\n", fifo[i]);
exit(EXIT_FAILURE);
}
if(fcntl(fd[i], F_SETFL, O_NONBLOCK) < 0) {
fprintf(stderr, "fcntl() error.\n");
exit(EXIT_FAILURE) ;
}
if(fd[i] > maxfdp1)
maxfdp1 = fd[i];
FD_SET(fd[i], &m_frds);
}
maxfdp1++; /* one more than max. file descriptor number */
sumread = 0; /* initialize number of bytes read */
cnt = nfifo; /* number of fifos still open */
while(cnt) {
printf("select starting\n");
s_rfds = m_rfds;
if(select(maxfdp1, &s_rfds, NULL, NULL, NULL) <= 0) {
fprintf(stderr, "select() error %d.\n", errno);
exit(EXIT_FAILURE) ;
}
for(i = 0; i < nfifo; i++) {
nread = 0;
if(FD_ISSET(fd[i], &s_rfds)) {
if((nread = read( fd[i], buf[i], 512)) < 0) {
fprintf(stderr, "read fifo error %d\n", errno);
exit(EXIT_FAILURE);
}
if(nread > 0) { /* some data read */
write(rd[i], buf[i], nread);
sumread += nread;
} else { /* end of file */
FD_CLR(fd[i], &m_rfds ); /* stop reading from this file */
close(fd[i]);
--cnt; /* number of fifos still open */
}
}
} /* end of for loop */
} /* end of while loop */
printf("Total bytes read from fifo(s) = %d\n", sumread);
return(EXIT_SUCCESS);
} /* end of main */
Performance Considerations
The following actions improve performance when you use OSS file system functions:
86 Managing Files