Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <fcntl.h>
  6. #include <math.h>
  7. #include <sys/ipc.h>
  8. #include <sys/sem.h>
  9. #include <err.h>
  10. #include <sys/types.h>
  11. #include <sys/shm.h>
  12. #include <sys/stat.h>
  13.  
  14. #define SHM_SIZE 20
  15.  
  16. union semun
  17. {
  18. int val;
  19. struct semid_ds *buf;
  20. unsigned short int *array;
  21. struct seminfo *__buf;
  22. };
  23.  
  24.  
  25. int semlock(int semid)
  26. {
  27. struct sembuf opr;
  28.  
  29. opr.sem_num = 0;
  30. opr.sem_op = -1; // blokowanie
  31. opr.sem_flg = 0; // operacja blokujaca
  32.  
  33. if (semop(semid, &opr, 1) == -1)
  34. {
  35. warn("Blad blokowania semafora!");
  36. return 0;
  37. }
  38. else
  39. {
  40. return 1;
  41. }
  42. }
  43.  
  44. int semunlock(int semid)
  45. {
  46. struct sembuf opr;
  47.  
  48. opr.sem_num = 0;
  49. opr.sem_op = 1;
  50. opr.sem_flg = 0;
  51.  
  52. if (semop(semid, &opr, 1) == -1)
  53. {
  54. warn("Blad odblokowania semafora!");
  55. return 0;
  56. }else return 1;
  57. }
  58.  
  59. int verification(FILE *fl, char fileName[20])
  60. {
  61. char temp='a';
  62. int number, line=-1, space=0;
  63.  
  64. if (access(fileName, R_OK)!=0)
  65. {
  66. printf("Can not read data from file.\n");
  67. fclose(fl);
  68. return 0;
  69. }
  70.  
  71. fscanf(fl, "%d", &number);
  72.  
  73. while(!feof(fl))
  74. {
  75. fscanf(fl, "%c", &temp);
  76. if (temp == '\n') line++;
  77. if (temp == ' ') space++;
  78. }
  79.  
  80. if (line != number+1 || space != number*2)
  81. {
  82. printf("The program structure is not correct.\n");
  83.  
  84. fclose(fl);
  85. return 0;
  86. }
  87.  
  88. printf("Plik sprawdzony.\n");
  89. fclose(fl);
  90. return 1;
  91. }
  92.  
  93. int main(void)
  94. {
  95. FILE *f = NULL;
  96. FILE *fOut;
  97. char fileName[20];
  98. int number, i, pdes[2], shmid, semid1, semid2;
  99. union semun ctl;
  100. float A, B, C, delta, x1, x2;
  101. float *shm, *s;
  102. key_t key, key1, key2;
  103.  
  104. if ((key = ftok(".", 'A')) == -1) errx(1, "Blad tworzenia klucza!");
  105. if ((shmid = shmget(key, SHM_SIZE, IPC_CREAT | 0666)) < 0) errx(2, "Blad tworzenia segmentu pamieci dzielonej!");
  106. if ((shm = shmat(shmid, NULL, 0)) == (float *) -1) errx(3, "Blad przylaczania pamieci dzielonej!");
  107.  
  108. if ((key1 = ftok(".", 'A')) == -1) errx(1, "Blad tworzenia klucza!");
  109. if ((semid1 = semget(key1, 1, IPC_CREAT | 0600)) == -1) errx(2, "Blad tworzenia semafora!");
  110. ctl.val = 1;
  111. if (semctl(semid1, 0, SETVAL, ctl) == -1) errx(3, "Blad ustawiania semafora!");
  112.  
  113. if ((key2 = ftok(".", 'B')) == -1) errx(1, "Blad tworzenia klucza!");
  114. if ((semid2 = semget(key2, 1, IPC_CREAT | 0600)) == -1) errx(2, "Blad tworzenia semafora!");
  115. ctl.val = 1;
  116. if (semctl(semid2, 0, SETVAL, ctl) == -1) errx(3, "Blad ustawiania semafora!");
  117.  
  118. semlock(semid2);
  119.  
  120. printf("Enter the name of the input file: ");
  121.  
  122. while(f==NULL)
  123. {
  124. scanf("%s", fileName);
  125. f = fopen(fileName, "r");
  126.  
  127. if (f == NULL) printf("Nie ma takiego pliku!\n\nSprobuj ponownie: ");
  128. }
  129.  
  130. if (!verification(f, fileName)) return 0;
  131.  
  132. f = fopen(fileName, "r");
  133. fOut = fopen("wynik.txt", "w");
  134.  
  135. fscanf(f, "%d", &number);
  136.  
  137. pipe(pdes);
  138.  
  139. if(fork())
  140. {
  141. s=shm;
  142. for (i=0; i<number; i++)
  143. {
  144. semlock(semid1);
  145. sleep(1);
  146. fscanf(f,"%f", &A);
  147. fscanf(f,"%f", &B);
  148. fscanf(f,"%f", &C);
  149.  
  150. *s++ = A;
  151. *s++ = B;
  152. *s++ = C;
  153. semunlock(semid2);
  154. }
  155. while(*s != '*') sleep(1);
  156.  
  157. shmdt(shm);
  158. shmctl(shmid, IPC_RMID, NULL);
  159. }else
  160. {
  161. s=shm;
  162. for (i=0; i<number; i++)
  163. {
  164. semlock(semid2);
  165. A = *s++;
  166. B = *s++;
  167. C = *s++;
  168.  
  169. delta = B*B - 4*A*C;
  170.  
  171. if (delta < 0)
  172. {
  173. fprintf(fOut, "(%.1f)x^2 + (%.1f)x + (%.1f) = 0 - brak rozwiazan\n", A, B, C);
  174. }
  175. else if (delta == 0)
  176. {
  177. x1 = -B/2*A;
  178. fprintf(fOut, "(%.1f)x^2 + (%.1f)x + (%.1f) = 0 - x0=%.1f\n", A, B, C, x1);
  179. }
  180. else
  181. {
  182. x1 = (-B-sqrt(delta))/(2*A);
  183. x2 = (-B+sqrt(delta))/(2*A);
  184. fprintf(fOut, "(%.1f)x^2 + (%.1f)x + (%.1f) = 0 - x1=%.1f x2=%.1f\n", A, B, C, x1, x2);
  185. }
  186. semunlock(semid1);
  187. }
  188. *s = '*';
  189. shmdt(shm);
  190. printf("Wypisano dane do wynik.txt\n\n");
  191. }
  192. sleep(1);
  193. fclose(f);
  194. fclose(fOut);
  195.  
  196. return 0;
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement