Вот код:
Читатель:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
readline(int fd, char *str) {
int n;
do {
n = read(fd, str, 1);
if(n == -1){
perror("Error reading:");
}
}
while(n > 0 && (str++) != NULL);
return(n > 0);
}
main (int argc, char *argv[]) {
int fd, mkn;
char message[100];
if(unlink("aPipe") == -1) {
perror("Error unlinking:");
}
if((mkn = mknod("aPipe", S_IFIFO, 0)) < 0){
perror("Error mknod:");
}
if(chmod("aPipe", 0660)) {
perror("Error chmod:");
}
if(fd = open("aPipe", O_RDONLY) < 0) {
perror("Error abriendo el PIPE");
}
printf("going to read..\n");
while(readline(fd,message))
printf("%s\n", message);
close(fd);
}
Писатель:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
main (int argc, char *argv[]) {
int fd, messagelen,i;
char message[100];
sprintf(message, "Hello from PID %d", getpid());
messagelen = strlen(message) + 1;
do {
fd = open("aPipe", O_WRONLY|O_NDELAY);
if (fd == -1) {
perror("opening aPipe:");
sleep(1);
}
}
while(fd == -1);
for (i = 1; i < 4; i++) {
if(write(fd, message, messagelen) == -1) {
perror("Error writing:");
}
sleep(3);
}
close(fd);
}
Мне тоже нужно выучить makefifo, но после того, как я это пойму.
Большое спасибо за вашу ценную помощь!