Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <sys/stat.h>
- #define FIFO1 "my_fifo1"
- #define FIFO2 "my_fif02"
- int silnia(int n)
- {
- int out=1;
- int i;
- for(i=n;i>1;i--)
- {
- out*=i;
- }
- return out;
- }
- void client1(const char *IN){
- FILE *fp;
- FILE *fp_out;
- char string[100];
- printf("IN1: server - > client1 : %s\n", IN);
- printf("OUT2: client1 -> client2 : %s\n", FIFO2);
- /* W petli pobieramy dane wpisane przez uzytkownika */
- while(1){
- fp = fopen(FIFO1, "r");
- fp_out = fopen(FIFO2, "w");
- fgets(string, 100, fp);
- int data = atoi(string);
- data = silnia(data);
- sprintf(string,"%d",data);
- fputs(string, fp_out);
- fclose(fp);
- fclose(fp_out);
- }
- }
- void client2(const char *IN){
- FILE *fp;
- char string[100];
- printf("IN2: client1 -> client2 %s\n", IN);
- /* W petli pobieramy dane wpisane przez uzytkownika */
- while(1){
- fp = fopen(FIFO2, "r");
- fgets(string, 100, fp);
- int data = atoi(string);
- printf("Silnia wynosi: %d\n\n",data);
- fclose(fp);
- }
- }
- void server(const char *OUT){
- FILE *fp;
- char string[100];
- printf("OUT1: server -> client1 : %s\n", OUT);
- /* W petli pobieramy dane od uzytkownika */
- while(1){
- fp = fopen(FIFO1, "w");
- fprintf(stdout, "Podaj liczbe: ");
- scanf("%s", string);
- /* Zapisujemy je do otwartego poprzednio deskryptora */
- fputs(string, fp);
- fclose(fp);
- sleep(1);
- }
- }
- int main(void){
- /* UWAGA! Na prawa dostepu tworzonego pliku ma wplyw UMASK */
- umask(0);
- mkfifo(FIFO1, 0666); //0 z przodu oznacza notacje ósemkowa
- mkfifo(FIFO2, 0666); //0 z przodu oznacza notacje ósemkowa
- /* rozdwajamy proces */
- if(fork())
- {
- client1(FIFO1);
- }
- if(fork())
- {
- client2(FIFO2);
- }
- else
- {
- server(FIFO1);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement