Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- #define FIFO "my_fifo"
- void policz(FILE*output, float a, float b, float c)
- {
- if(a!=0)//kwadratowe
- {
- fprintf(output, "%.2fx^2 + %.2fx + %.2f = 0 - ",a,b,c);
- float delta = b*b-4*a*c;
- if(delta<0)
- fprintf(output, "Brak rozwiazan(rzeczywistych)");
- else if(delta == 0)
- fprintf(output, "x1 = x2 = %.2f", -b/(2*a) );
- else
- {
- delta = sqrtf(delta);
- fprintf(output, "x1 = %.2f x2 = %.2f", (-b-delta)/(2*a), (-b+delta)/(2*a));
- }
- }
- else if(b!=0)//liniowe
- {
- fprintf(output, "%.2fx + %.2f = 0 - ",b,c);
- fprintf(output,"x = %f", -c/b);
- }
- else
- {
- fprintf(output, "%.2f = 0 - ",c);
- if(c==0)
- fprintf(output,"x nalezy do R");
- else
- fprintf(output,"Brak rozwiazan");
- }
- fprintf(output, "\n");
- }
- void PP1(FILE*input, int ilosc)
- {
- FILE*fp;
- for(int i = 0;i<ilosc;i++)
- {
- fp = fopen(FIFO, "w");
- float a,b,c;
- fscanf(input, "%f %f %f", &a, &b, &c);
- printf("%f %f %f out from PP1\n", a,b,c);
- fflush(stdout);
- fprintf(fp, "%f %f %f", a,b,c);
- fclose(fp);
- sleep(1);
- }
- }
- void PP2(FILE*output, int ilosc)
- {
- sleep(1);
- FILE*fp;
- for(int i = 0;i<ilosc;i++)
- {
- fp = fopen(FIFO, "r");
- float a,b,c;
- fscanf(fp, "%f %f %f", &a,&b,&c);
- printf("%f %f %f in PP2\n", a,b,c);
- policz(output, a,b,c);
- fclose(fp);
- }
- }
- int main(int argc, char *argv[])
- {
- if(argc!=2)
- {
- printf("Zla ilosc argumentow!\n");
- return 1;
- }
- FILE*input = fopen(argv[1], "r+");
- if(input == NULL) {
- printf("Blad przy odczytywaniu pliku!\n");
- return 2;
- }
- else if(access ( argv[1], F_OK ) == -1)
- {
- printf("Brak dostepu!\n");
- return 3;
- }
- FILE* output = fopen("out.txt", "w+");
- if(output == NULL) {
- printf("Blad przy otwieraniu pliku do zapisu!\n");
- return 4;
- }
- umask(0);
- mkfifo(FIFO, 0666); //0 z przodu oznacza notacje ósemkowa
- int ilosc;
- fscanf(input, "%d", &ilosc);
- if(fork())
- {
- PP1(input, ilosc);
- }
- else
- {
- PP2(output, ilosc);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement