Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/msg.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <signal.h>
- #include <errno.h>
- #include <string.h>
- #include <pthread.h>
- #include <ctype.h>
- #include <unistd.h>
- #define MAX 50
- key_t key;
- int ID_kolejki;
- char komunikatTekst[MAX];
- typedef struct Komunikat {
- long type;
- long pid_klienta;
- char mtext[MAX];
- } Komunikat;
- Komunikat kom_odebrany;
- Komunikat kom_wyslany;
- void *wyslij() {
- while(1) {
- kom_wyslany.type = 1;
- kom_wyslany.pid_klienta = getpid();
- sleep(1);
- printf("\nKlient %d: Wysyłanie komunikatu. Podaj tekst do wyslania: ", getpid());
- int i = 0;
- while(i < MAX) {
- komunikatTekst[i] = getchar();
- if(komunikatTekst[i] == '\n') {
- komunikatTekst[i] = '\0';
- break;
- }
- i++;
- }
- strcpy(kom_wyslany.mtext, komunikatTekst);
- if((msgsnd(ID_kolejki, (Komunikat *)&kom_wyslany, strlen(kom_wyslany.mtext)+sizeof(long), 0)) == -1) {
- printf("Klient %d: Blad przy wysylaniu komunikatu. ERRNO %i (%s)\n", getpid(), errno, strerror(errno));
- exit(2);
- } else
- printf("Klient %d: Klient wyslal wiadomosc do serwera. Tresc wiadomosci: %s\n", getpid(), kom_wyslany.mtext);
- }
- pthread_exit((void *) 0);
- }
- void *odbierz() {
- while(1) {
- kom_odebrany.type = getpid();
- int i;
- for(i = 0; i < MAX; i++) {
- kom_odebrany.mtext[i] = '\0';
- }
- if((msgrcv(ID_kolejki, (Komunikat *)&kom_odebrany, MAX+sizeof(long), kom_odebrany.type, 0)) == -1) {
- if (errno == 43) {
- printf("Klient %d: Serwer zakonczyl prace! Kolejka zostala usunieta\n", getpid());
- exit(2);
- } else if (errno == 22) {
- printf("Klient %d: Blad przy pobieraniu komunikatu od serwera! Serwer zakonczyl prace.\n", getpid());
- exit(2);
- } else {
- printf("Klient %d: Blad przy pobieraniu komunikatu od serwera! ERRNO %i (%s)\n", getpid(), errno, strerror(errno));
- exit(2);
- }
- } else
- printf("Klient %d: Klient odebral wiadomosc. Tresc: %s\n", getpid(), kom_odebrany.mtext);
- }
- pthread_exit((void *) 0);
- }
- int main() {
- //dwa watki - do odbeirania i wysylania, dzialaja niezaleznie
- pthread_t TID1, TID2;
- //klucz do kolejki
- if((key = ftok(".", 808182)) == -1) {
- printf("Klient %d: Blad tworzenia klucza!\n", getpid());
- exit(-1);
- } else
- printf("Klient %d: Klucz zostal utworzony\n", getpid());
- //tworzenie kolejki - 0600 bo odczytywanie i pisanie
- if((ID_kolejki = msgget(808182, 0666 | IPC_CREAT)) == -1 ) {
- printf("Klient %d: Blad przy tworzeniu kolejki komunikatow! ERRNO %i (%s)\n", getpid(), errno, strerror(errno));
- exit(-2);
- }
- printf("Klient %d: Utworzono kolejke komunikatow\n", getpid());
- if(pthread_create(&TID1, NULL, odbierz, NULL) == -1) {
- printf("Klient %d: Blad tworzenia watku do odbierania wiadomosci! ERRNO %i (%s)\n", getpid(), errno, strerror(errno));
- exit(-1);
- }
- if(pthread_create(&TID2, NULL, wyslij, NULL) == -1) {
- printf("Klient %d: Blad tworzenia watku do wysylania wiadomosci! ERRNO %i (%s)\n", getpid(), errno, strerror(errno));
- exit(-1);
- }
- if(pthread_join(TID1, NULL) == -1) {
- printf("Klient %d: Blad przylaczenia watku do odbierania wiadomosci! ERRNO %i (%s)\n", getpid(), errno, strerror(errno));
- exit(-1);
- }
- if(pthread_join(TID2, NULL) == -1) {
- printf("Klient %d: Blad przylaczenia watku do wysylania wiadomosci! ERRNO %i (%s)\n", getpid(), errno, strerror(errno));
- exit(-1);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement