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 <fcntl.h>
- #include <math.h>
- #include <sys/ipc.h>
- #include <sys/sem.h>
- #include <err.h>
- #include <sys/types.h>
- #include <sys/shm.h>
- #include <sys/stat.h>
- #define SHM_SIZE 20
- union semun
- {
- int val;
- struct semid_ds *buf;
- unsigned short int *array;
- struct seminfo *__buf;
- };
- int semlock(int semid)
- {
- struct sembuf opr;
- opr.sem_num = 0;
- opr.sem_op = -1; // blokowanie
- opr.sem_flg = 0; // operacja blokujaca
- if (semop(semid, &opr, 1) == -1)
- {
- warn("Blad blokowania semafora!");
- return 0;
- }
- else
- {
- return 1;
- }
- }
- int semunlock(int semid)
- {
- struct sembuf opr;
- opr.sem_num = 0;
- opr.sem_op = 1;
- opr.sem_flg = 0;
- if (semop(semid, &opr, 1) == -1)
- {
- warn("Blad odblokowania semafora!");
- return 0;
- }else return 1;
- }
- int verification(FILE *fl, char fileName[20])
- {
- char temp='a';
- int number, line=-1, space=0;
- if (access(fileName, R_OK)!=0)
- {
- printf("Can not read data from file.\n");
- fclose(fl);
- return 0;
- }
- fscanf(fl, "%d", &number);
- while(!feof(fl))
- {
- fscanf(fl, "%c", &temp);
- if (temp == '\n') line++;
- if (temp == ' ') space++;
- }
- if (line != number+1 || space != number*2)
- {
- printf("The program structure is not correct.\n");
- fclose(fl);
- return 0;
- }
- printf("Plik sprawdzony.\n");
- fclose(fl);
- return 1;
- }
- int main(void)
- {
- FILE *f = NULL;
- FILE *fOut;
- char fileName[20];
- int number, i, pdes[2], shmid, semid1, semid2;
- union semun ctl;
- float A, B, C, delta, x1, x2;
- float *shm, *s;
- key_t key, key1, key2;
- if ((key = ftok(".", 'A')) == -1) errx(1, "Blad tworzenia klucza!");
- if ((shmid = shmget(key, SHM_SIZE, IPC_CREAT | 0666)) < 0) errx(2, "Blad tworzenia segmentu pamieci dzielonej!");
- if ((shm = shmat(shmid, NULL, 0)) == (float *) -1) errx(3, "Blad przylaczania pamieci dzielonej!");
- if ((key1 = ftok(".", 'A')) == -1) errx(1, "Blad tworzenia klucza!");
- if ((semid1 = semget(key1, 1, IPC_CREAT | 0600)) == -1) errx(2, "Blad tworzenia semafora!");
- ctl.val = 1;
- if (semctl(semid1, 0, SETVAL, ctl) == -1) errx(3, "Blad ustawiania semafora!");
- if ((key2 = ftok(".", 'B')) == -1) errx(1, "Blad tworzenia klucza!");
- if ((semid2 = semget(key2, 1, IPC_CREAT | 0600)) == -1) errx(2, "Blad tworzenia semafora!");
- ctl.val = 1;
- if (semctl(semid2, 0, SETVAL, ctl) == -1) errx(3, "Blad ustawiania semafora!");
- semlock(semid2);
- printf("Enter the name of the input file: ");
- while(f==NULL)
- {
- scanf("%s", fileName);
- f = fopen(fileName, "r");
- if (f == NULL) printf("Nie ma takiego pliku!\n\nSprobuj ponownie: ");
- }
- if (!verification(f, fileName)) return 0;
- f = fopen(fileName, "r");
- fOut = fopen("wynik.txt", "w");
- fscanf(f, "%d", &number);
- pipe(pdes);
- if(fork())
- {
- s=shm;
- for (i=0; i<number; i++)
- {
- semlock(semid1);
- sleep(1);
- fscanf(f,"%f", &A);
- fscanf(f,"%f", &B);
- fscanf(f,"%f", &C);
- *s++ = A;
- *s++ = B;
- *s++ = C;
- semunlock(semid2);
- }
- while(*s != '*') sleep(1);
- shmdt(shm);
- shmctl(shmid, IPC_RMID, NULL);
- }else
- {
- s=shm;
- for (i=0; i<number; i++)
- {
- semlock(semid2);
- A = *s++;
- B = *s++;
- C = *s++;
- delta = B*B - 4*A*C;
- if (delta < 0)
- {
- fprintf(fOut, "(%.1f)x^2 + (%.1f)x + (%.1f) = 0 - brak rozwiazan\n", A, B, C);
- }
- else if (delta == 0)
- {
- x1 = -B/2*A;
- fprintf(fOut, "(%.1f)x^2 + (%.1f)x + (%.1f) = 0 - x0=%.1f\n", A, B, C, x1);
- }
- else
- {
- x1 = (-B-sqrt(delta))/(2*A);
- x2 = (-B+sqrt(delta))/(2*A);
- fprintf(fOut, "(%.1f)x^2 + (%.1f)x + (%.1f) = 0 - x1=%.1f x2=%.1f\n", A, B, C, x1, x2);
- }
- semunlock(semid1);
- }
- *s = '*';
- shmdt(shm);
- printf("Wypisano dane do wynik.txt\n\n");
- }
- sleep(1);
- fclose(f);
- fclose(fOut);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement