Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- #include <errno.h>
- void rozwiaz(int a, int b, int c, FILE *wynik)
- {
- printf("a-%d b-%d c-%d \n", a, b, c);
- int delta;
- float root1, root2;
- delta = (b * b) - (4 * a * c);
- fprintf(wynik, "%dx^2 + %dx + %d, delta=%d - ", a, b, c, delta);
- if (delta < 0)
- //printf("Rownanie nie ma rozwiazan.\n");
- fprintf(wynik, "Rownanie nie ma rozwiazan.\n");
- if (delta == 0)
- {
- root1 = -b / (2 * a);
- //printf("x0 = %d\n", root1);
- fprintf(wynik, "x0 = %d\n", root1);
- }
- if (delta > 0)
- {
- root1 = (-b + sqrt(delta)) / (2 * a);
- root2 = (-b - sqrt(delta)) / (2 * a);
- //printf("x1 = %f, x2 = %f\n", root2, root1);
- fprintf(wynik, "x1 = %f, x2 = %f\n", root2, root1);
- }
- }
- struct mymsgbuf {
- long mtype; /* typ wiadomości */
- int request; /* numer żądania danego działania */
- int i;
- } msg;
- struct mymsgbuf buf;
- int open_queue( key_t keyval ) {
- int qid;
- if((qid = msgget( keyval, IPC_CREAT | 0660 )) == -1)
- return(-1);
- return(qid);
- }
- int send_message( int qid, struct mymsgbuf *qbuf ){
- int result, length;
- /* lenght jest rozmiarem struktury minus sizeof(mtype) */
- length = sizeof(struct mymsgbuf) - sizeof(long);
- if((result = msgsnd( qid, qbuf, length, 0)) == -1)
- return(-1);
- return(result);
- }
- int remove_queue( int qid ){
- if( msgctl( qid, IPC_RMID, 0) == -1)
- return(-1);
- return(0);
- }
- int read_message( int qid, long type, struct mymsgbuf *qbuf ){
- int result, length;
- /* lenght jest rozmiarem struktury minus sizeof(mtype) */
- length = sizeof(struct mymsgbuf) - sizeof(long);
- if((result = msgrcv( qid, qbuf, length, type, 0)) == -1)
- return(-1);
- return(result);
- }
- int main()
- {
- char cwd[64], nazwa[64] = "liczby.txt", znak;
- int n = 0, count = 0, i =0, czekfuk = 0;
- int czekaj = 0;
- int qid;
- key_t msgkey;
- msgkey = ftok(".", 'm');
- /* otwieramy/tworzymy kolejkę */
- if(( qid = open_queue( msgkey)) == -1) {
- perror("Otwieranie_kolejki");
- exit(1);
- }
- msg.mtype = 1; /* typ wiadomości musi być dodatni */
- msg.request = 1;
- if (fork() == 0)
- {
- int tab[64];
- printf("Podaj nazwe pliku:\n");;
- gets(nazwa);
- if (access(nazwa, R_OK) == -1)
- {
- printf("brak dostepu do pliku");
- }
- FILE *plikLiczby = fopen(nazwa, "r");
- if (plikLiczby == NULL)
- {
- printf("blad odczytu pliku\n");
- }
- fscanf(plikLiczby, "%d", &n);
- for (znak = getc(plikLiczby); znak != EOF; znak = getc(plikLiczby))
- {
- if (znak == '\n')
- count = count + 1;
- }
- if(count != n)
- {
- printf("Niepoprawna liczba rownan");
- }
- fseek(plikLiczby, 0, 0);
- for (i = 0; !feof(plikLiczby); i++)
- {
- fscanf(plikLiczby, "%d", &tab[i]);
- msg.i = tab[i];
- printf("wyslano %d\n", tab[i]);
- if((send_message( qid, &msg )) == -1) {
- perror("Wysylanie");
- exit(1);
- }
- sleep(1);
- }
- printf("\n");
- fclose(plikLiczby);
- }
- if (fork() == 0)
- {
- int a = 0, b = 0, c = 0, d = 0, ni = 0, index = 0, tab[64], i = 0, liczbarownan = 0;
- char nazwaWynik[] = "wynik.txt";
- buf.mtype = 1; /* typ wiadomości musi być dodatni */
- buf.request = 1;
- read_message(qid, buf.mtype, &buf);
- liczbarownan = buf.i*3;
- printf("%d\n", liczbarownan);
- //odczytanie msg
- while(liczbarownan > ni)
- {
- read_message(qid, buf.mtype, &buf);
- printf("Zawartosc: %d\n", buf.i);
- tab[ni] = buf.i;
- ni++;
- printf("%d\n",ni);
- sleep(0.5);
- }
- FILE *plikWynik = fopen(nazwaWynik, "w");
- if(plikWynik == NULL)
- {
- printf("blad odczytu pliku\n");
- }
- for(i = 0; i < liczbarownan; i=i+3)
- {
- a = tab[i];
- b = tab[i+1];
- c = tab[i+2];
- rozwiaz(a,b,c,plikWynik);
- }
- sleep(1);
- fclose(plikWynik);
- getcwd(cwd, sizeof(cwd));
- printf("Wyniki zapisano w %s/%s\n", cwd, nazwaWynik);
- czekaj = 0;
- exit(0);
- }
- while(czekaj != 2)
- pause();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement