Advertisement
Guest User

Untitled

a guest
Jan 15th, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.26 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <fcntl.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <math.h>
  6. #include <sys/types.h>
  7. #include <sys/ipc.h>
  8. #include <sys/msg.h>
  9. #include <errno.h>
  10.  
  11. void rozwiaz(int a, int b, int c, FILE *wynik)
  12. {
  13. printf("a-%d b-%d c-%d \n", a, b, c);
  14. int delta;
  15. float root1, root2;
  16. delta = (b * b) - (4 * a * c);
  17. fprintf(wynik, "%dx^2 + %dx + %d, delta=%d - ", a, b, c, delta);
  18.  
  19. if (delta < 0)
  20. //printf("Rownanie nie ma rozwiazan.\n");
  21. fprintf(wynik, "Rownanie nie ma rozwiazan.\n");
  22. if (delta == 0)
  23. {
  24. root1 = -b / (2 * a);
  25. //printf("x0 = %d\n", root1);
  26. fprintf(wynik, "x0 = %d\n", root1);
  27. }
  28. if (delta > 0)
  29. {
  30. root1 = (-b + sqrt(delta)) / (2 * a);
  31. root2 = (-b - sqrt(delta)) / (2 * a);
  32. //printf("x1 = %f, x2 = %f\n", root2, root1);
  33. fprintf(wynik, "x1 = %f, x2 = %f\n", root2, root1);
  34. }
  35. }
  36.  
  37. struct mymsgbuf {
  38. long mtype; /* typ wiadomości */
  39. int request; /* numer żądania danego działania */
  40. int i;
  41. } msg;
  42.  
  43. struct mymsgbuf buf;
  44.  
  45. int open_queue( key_t keyval ) {
  46. int qid;
  47.  
  48. if((qid = msgget( keyval, IPC_CREAT | 0660 )) == -1)
  49. return(-1);
  50.  
  51. return(qid);
  52. }
  53.  
  54. int send_message( int qid, struct mymsgbuf *qbuf ){
  55. int result, length;
  56.  
  57. /* lenght jest rozmiarem struktury minus sizeof(mtype) */
  58. length = sizeof(struct mymsgbuf) - sizeof(long);
  59.  
  60. if((result = msgsnd( qid, qbuf, length, 0)) == -1)
  61. return(-1);
  62.  
  63. return(result);
  64. }
  65.  
  66. int remove_queue( int qid ){
  67. if( msgctl( qid, IPC_RMID, 0) == -1)
  68. return(-1);
  69.  
  70. return(0);
  71. }
  72.  
  73. int read_message( int qid, long type, struct mymsgbuf *qbuf ){
  74. int result, length;
  75.  
  76. /* lenght jest rozmiarem struktury minus sizeof(mtype) */
  77. length = sizeof(struct mymsgbuf) - sizeof(long);
  78.  
  79. if((result = msgrcv( qid, qbuf, length, type, 0)) == -1)
  80. return(-1);
  81.  
  82. return(result);
  83. }
  84.  
  85.  
  86. int main()
  87. {
  88. char cwd[64], nazwa[64] = "liczby.txt", znak;
  89. int n = 0, count = 0, i =0, czekfuk = 0;
  90. int czekaj = 0;
  91.  
  92. int qid;
  93. key_t msgkey;
  94.  
  95. msgkey = ftok(".", 'm');
  96.  
  97. /* otwieramy/tworzymy kolejkę */
  98. if(( qid = open_queue( msgkey)) == -1) {
  99. perror("Otwieranie_kolejki");
  100. exit(1);
  101. }
  102.  
  103. msg.mtype = 1; /* typ wiadomości musi być dodatni */
  104. msg.request = 1;
  105.  
  106. if (fork() == 0)
  107. {
  108. int tab[64];
  109.  
  110. printf("Podaj nazwe pliku:\n");;
  111. gets(nazwa);
  112. if (access(nazwa, R_OK) == -1)
  113. {
  114. printf("brak dostepu do pliku");
  115. }
  116.  
  117. FILE *plikLiczby = fopen(nazwa, "r");
  118.  
  119. if (plikLiczby == NULL)
  120. {
  121. printf("blad odczytu pliku\n");
  122. }
  123.  
  124. fscanf(plikLiczby, "%d", &n);
  125.  
  126. for (znak = getc(plikLiczby); znak != EOF; znak = getc(plikLiczby))
  127. {
  128. if (znak == '\n')
  129. count = count + 1;
  130. }
  131. if(count != n)
  132. {
  133. printf("Niepoprawna liczba rownan");
  134. }
  135. fseek(plikLiczby, 0, 0);
  136.  
  137. for (i = 0; !feof(plikLiczby); i++)
  138. {
  139. fscanf(plikLiczby, "%d", &tab[i]);
  140. msg.i = tab[i];
  141. printf("wyslano %d\n", tab[i]);
  142. if((send_message( qid, &msg )) == -1) {
  143. perror("Wysylanie");
  144. exit(1);
  145. }
  146. sleep(1);
  147. }
  148. printf("\n");
  149.  
  150. fclose(plikLiczby);
  151. }
  152.  
  153. if (fork() == 0)
  154. {
  155. int a = 0, b = 0, c = 0, d = 0, ni = 0, index = 0, tab[64], i = 0, liczbarownan = 0;
  156. char nazwaWynik[] = "wynik.txt";
  157.  
  158.  
  159.  
  160. buf.mtype = 1; /* typ wiadomości musi być dodatni */
  161. buf.request = 1;
  162.  
  163. read_message(qid, buf.mtype, &buf);
  164. liczbarownan = buf.i*3;
  165. printf("%d\n", liczbarownan);
  166.  
  167. //odczytanie msg
  168. while(liczbarownan > ni)
  169. {
  170. read_message(qid, buf.mtype, &buf);
  171. printf("Zawartosc: %d\n", buf.i);
  172. tab[ni] = buf.i;
  173. ni++;
  174. printf("%d\n",ni);
  175. sleep(0.5);
  176. }
  177.  
  178. FILE *plikWynik = fopen(nazwaWynik, "w");
  179.  
  180. if(plikWynik == NULL)
  181. {
  182. printf("blad odczytu pliku\n");
  183. }
  184.  
  185. for(i = 0; i < liczbarownan; i=i+3)
  186. {
  187. a = tab[i];
  188. b = tab[i+1];
  189. c = tab[i+2];
  190. rozwiaz(a,b,c,plikWynik);
  191. }
  192. sleep(1);
  193. fclose(plikWynik);
  194. getcwd(cwd, sizeof(cwd));
  195. printf("Wyniki zapisano w %s/%s\n", cwd, nazwaWynik);
  196. czekaj = 0;
  197. exit(0);
  198. }
  199.  
  200. while(czekaj != 2)
  201. pause();
  202.  
  203. return 0;
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement