Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <string.h>
- #include <stdlib.h>
- #include <signal.h>
- #include <sys/types.h>
- #include <sys/msg.h>
- #include <sys/wait.h>
- #define MAX_TEXT_LENGTH 500
- #define READ 0
- #define WRITE 1
- // znak oddzielajacy liczby w paczce
- const char *znak = "|";
- int pdes1[2];
- int pdes2[2];
- int flaga = 0;
- int qid;
- // tablica pidow
- __pid_t pidy[3];
- #define PLIK_Z_PIDAMI "pidy.txt"
- struct msgbuf {
- long mtype;
- int komunikat;
- };
- int wyslij_komunikat(int qid, struct msgbuf *data)
- {
- return msgsnd(qid, data, sizeof(struct msgbuf) - sizeof(long), 0);
- }
- int odczytaj_komunikat(int qid, long type, struct msgbuf *output)
- {
- return msgrcv(qid, output, sizeof(struct msgbuf) - sizeof(long), type, 0);
- }
- void usun_kolejke(int qid)
- {
- msgctl(qid, IPC_RMID, 0);
- }
- void wczytaj_pidy()
- {
- int i;
- FILE *f = fopen(PLIK_Z_PIDAMI, "r");
- for (i = 0; i < 3; i++)
- fscanf(f, "%d", &pidy[i]);
- fclose(f);
- }
- int moze_byc_liczba(char *str, int n)
- {
- int i;
- for (i = 0; i < n; i++)
- {
- if (!((str[i] <= '9' && str[i] >= '0') || ( i == 0 && str[i] == '-')))
- return 0;
- }
- return 1;
- }
- void wypelnij_tablice_zerami(char tab[], int n)
- {
- int i;
- for (i = 0; i < n; i++)
- tab[i] = 0;
- }
- void sygnaly_dalsze_dzialanie(){
- struct msgbuf xd;
- odczytaj_komunikat(qid, getpid(), &xd);
- int wartosc = xd.komunikat;
- if(wartosc == SIGUSR2){
- flaga = 0; //Wznowienie dzialania
- }
- else if(wartosc == SIGUSR1){
- flaga = 1; //Wstrzymanie dzialania
- }
- else if(wartosc == SIGTERM){
- exit(0); //Zakonczenie dzialania procesu
- }
- }
- void obsluga_sygnalu(int wartosc){
- wczytaj_pidy();
- struct msgbuf xd; //tworze strukture xd do wysylania komunikatow
- xd.komunikat = wartosc; //wartosc to nr sygnalu
- int i;
- for (i = 0; i < 3; i++)
- {
- xd.mtype = pidy[i];
- wyslij_komunikat(qid, &xd); //wysylamy komunikat ze otrzymalismy sygnal
- }
- for (i = 0; i < 3; i++)
- {
- if(getpid() != pidy[i]) //Wyslij sygnal4 ktory powiadamioa ze mozliwy jest odczyt z kolejki
- kill(pidy[i], SIGCONT);
- }
- sygnaly_dalsze_dzialanie();
- }
- int main()
- {
- pipe(pdes1);
- pipe(pdes2);
- signal(SIGUSR1, obsluga_sygnalu);
- signal(SIGUSR2, obsluga_sygnalu);
- signal(SIGTERM, obsluga_sygnalu);
- signal(SIGCONT, obsluga_sygnalu);
- __key_t klucz_kolejki = ftok(".", 'C');
- qid = msgget(klucz_kolejki, IPC_CREAT | 0660); //tworze kolejke
- if ((pidy[0] = fork()) == 0)
- {
- close(pdes1[READ]);
- char text[MAX_TEXT_LENGTH] = {};
- while (1)
- {
- while (flaga);
- if (fgets(text, MAX_TEXT_LENGTH, stdin) != NULL)
- write(pdes1[WRITE], text, strlen(text) + 1); // + 1 bo 0 końca stringa
- }
- }
- if ((pidy[1] = fork()) == 0)
- {
- close(pdes1[WRITE]);
- close(pdes2[READ]);
- char text[MAX_TEXT_LENGTH] = {};
- char payload[MAX_TEXT_LENGTH] = {};
- char *token;
- int liczba;
- int index = 0;
- int n;
- int poprawne_dane = 1;
- while (1)
- {
- while (flaga);
- wypelnij_tablice_zerami(text, MAX_TEXT_LENGTH);
- wypelnij_tablice_zerami(payload, MAX_TEXT_LENGTH);
- index = 0;
- poprawne_dane = 1;
- n = read(pdes1[READ], text, MAX_TEXT_LENGTH);
- // nadpisanie znaku nowej linii
- text[n - 2] = 0;
- if (text[n - 3] < '0' || text[n - 3] > '9')
- {
- printf("Podano nieprawidlowe dane wejsciowe!\n");
- continue;
- }
- token = strtok(text, "+"); //dzili string na segment co +
- while (token != NULL)
- {
- if (!moze_byc_liczba(token, strlen(token)) || (token[0] == '0' && strlen(token) > 1))
- {
- printf("Podano nieprawidlowe dane wejsciowe!\n");
- poprawne_dane = 0;
- break;
- }
- liczba = atoi(token); //zwraca string jako int
- if (liczba == 0 && strlen(token) > 1)
- {
- printf("Podano nieprawidlowe dane wejsciowe!\n");
- poprawne_dane = 0;
- break;
- }
- n = strlen(token);
- strncpy(&payload[index], token, n);
- index += n;
- strncpy(&payload[index], znak, 1);
- index++;
- token = strtok(NULL, "+"); //wczytuje dane dopuki nie napotka plusa
- }
- if (poprawne_dane)
- write(pdes2[WRITE], payload, strlen(payload)); //wysyłamy dane do procesu 3
- }
- }
- if ((pidy[2] = fork()) == 0)
- {
- close(pdes2[WRITE]);
- int suma = 0;
- char text[MAX_TEXT_LENGTH] = {};
- char *token;
- while (1)
- {
- while (flaga);
- read(pdes2[READ], text, MAX_TEXT_LENGTH);
- token = strtok(text, znak);
- while (token != NULL)
- {
- suma += atoi(token);
- token = strtok(NULL, znak);
- }
- printf("Suma: %d\n", suma);
- wypelnij_tablice_zerami(text, MAX_TEXT_LENGTH);
- suma = 0;
- }
- }
- FILE *f = fopen(PLIK_Z_PIDAMI, "w");
- int z;
- for (z = 0; z < 3; z++)
- fprintf(f, "%d\n", pidy[z]);
- fclose(f);
- waitpid(pidy[2], NULL, 0);
- waitpid(pidy[1], NULL, 0);
- waitpid(pidy[0], NULL, 0);
- close(pdes1[READ]);
- close(pdes1[WRITE]);
- close(pdes2[READ]);
- close(pdes2[WRITE]);
- usun_kolejke(qid);
- remove(PLIK_Z_PIDAMI);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement