#include #include #include #include #include #include #include #define db 0 void debug(int desc, char * tekst, char * pid) { char *MSG= (char *) malloc((2 + strlen(pid) + strlen(tekst))); strcpy(MSG, pid); strcat(MSG, "\n"); strcat(MSG, tekst); write(desc, MSG, (int)strlen(MSG)); } int main(int argc, char *argv[]) { pid_t parent = getppid(); char parent_str[20]; sprintf(parent_str, "%d", parent); pid_t pid = getpid(); char pid_str[20]; sprintf(pid_str, "%d", pid); int inst; if(db == 1) inst = open("test.txt", O_CREAT | O_WRONLY | O_APPEND , S_IRUSR | S_IWUSR); int fd; if((mkfifo(parent_str, S_IRWXU | S_IRWXG | S_IRWXO)) != 0) { if(errno == EEXIST) { if(db == 1) debug(inst, " READER. Fifo DUZE juz stworzone. Otwieram do odczytu.\n", pid_str); if((fd = open(parent_str, O_RDONLY)) == -1) exit(1); } else { exit(1); } } else { if(db == 1) debug(inst, " READER. Fifo DUZE nie stworzone. TworzÄ™ i otwieram do odczytu.\n", pid_str); if((fd = open(parent_str, O_RDONLY)) == -1) exit(1); } char buf[20]; if(db == 1) debug(inst, " READER. Za chwile zaczne czytanie z fifo.\n", pid_str); while (read(fd, &buf, 20) > 0) { int p = 0; while(buf[p] != ' ') p++; buf[p] = '\n'; write(1, buf, p + 1); } if(db == 1) debug(inst, " READER. Skonczylem czytac z duzego fifo bo dostalem eof.\n", pid_str); if(db == 1) close(inst); close(fd); return 0; }