/* Klient oparty na kolejkach FIFO */ #include #include #include #include #include static const char *katalog = "/tmp/FIFO-TMP-SERWER"; int main() { pid_t numer_procesu; int f, f1, f2; char bufor[1000], *pom = bufor; numer_procesu = getpid(); if ((f = open(katalog, 1)) == -1) { perror("Klient: Nie moge ustanowic polaczenia z serwerem. [1]\n"); return -1; } else { strcpy(pom, "/tmp/FIFO-TMP-1-"); pom += strlen(pom); sprintf(pom, "%d", (int)numer_procesu); if (mknod(bufor, S_IFIFO | 0600, 0) == -1) /* kolejka do czytania */ { perror("Klient: Blad tworzenia kolejki FIFO. [2]\n"); close(f); return -1; } else { *(pom - 2) = '2'; if (mknod(bufor, S_IFIFO | 0600, 0) == -1) /* kolajka do pisania */ { perror("Klient: Blad tworzenia kolejki FIFO. [3]\n"); close(f2); close(f); return -1; } write(f, &numer_procesu, sizeof(pid_t)); close(f); if ((f1 = open(bufor, O_WRONLY)) == -1) /* kolejka do pisania */ { perror("Klient: Blad otwarcia kolejki FIFO. [4]\n"); close(f); return -1; } else { *(pom - 2) = '1'; if ((f2 = open(bufor, O_RDONLY)) == -1) /* kolajka do czytania */ { perror("Klient: Blad otwarcia kolejki FIFO. [5]\n"); close(f1); close(f); return -1; } else { double a, b; printf("Podaj liczbe: "); scanf("%lf", &a); write(f1, &a, sizeof(double)); read(f2, &b, sizeof(double)); printf("Wynik obliczen %lf^2 = %lf.\n", a, b); close(f1); close(f2); close(f); unlink(bufor); *(pom - 2) = '2'; unlink(bufor); return 0; } } } } }