// SERVER #include #include #include #include #include #include #include #include #include #include typedef struct sembuf semafory; char *path = "/tmp/semafory3"; int IDpamiec, IDsemaforw; struct wiadomosc { int a; char test[1024]; }; typedef struct wiadomosc msg; msg *pamiec; int main(int argc, char *argv[]) { unlink(path); key_t kluczPamiec, kluczSemaforw; int plik; semafory sem; if ((plik = open(path, 0600 | O_CREAT)) == -1) { perror("Blad tworzenia pliku\n"); return -1; } close(plik); if ((kluczPamiec = ftok(path, 1)) == -1) { perror("Blad tworzenia klucza pamieci\n"); return -1; } if ((kluczSemaforw = ftok(path, 2)) == -1) { perror("Blad tworzenia klucza semaforow\n"); return -1; } if ((IDpamiec = shmget(kluczPamiec, sizeof(msg) * 1024, 0600 | IPC_CREAT)) == -1) { perror("Blad tworzenia ID pamieci\n"); return -1; } pamiec = shmat(IDpamiec, (msg *)0, 0); if ((IDsemaforw = semget(kluczSemaforw, 2, 0600 | IPC_CREAT)) == -1) { perror("Blad tworzenia ID semaforw\n"); shmdt(pamiec); shmctl(IDpamiec, IPC_RMID, NULL); return -1; } u_short ustawSemafory[] = {1, 0}; if (semctl(IDsemaforw, 0, SETALL, ustawSemafory) == -1) { perror("Blad ustawienia wartosci poczatkowych\n"); shmdt(pamiec); shmctl(IDpamiec, IPC_RMID, NULL); return -1; } while (1) { sem.sem_num = 1; sem.sem_op = -1; sem.sem_flg = 0; if (semop(IDsemaforw, &sem, 1) == -1) { perror("Blad inicjalizacji semaforow\n"); shmdt(pamiec); shmctl(IDpamiec, IPC_RMID, NULL); semctl(IDsemaforw, 0, IPC_RMID, NULL); return -1; } msg *pomoc; pomoc = malloc(sizeof(msg) * 1024); memcpy(pomoc, pamiec, sizeof(msg)); printf("Wiadomosc otrzymałem: %d, %s\n", pomoc->a, pomoc->test); char *pomoc2; pomoc2 = pomoc->test; pomoc2 += strlen(pomoc2); sprintf(pomoc2, "%d", pomoc->a); memcpy(pamiec, pomoc, sizeof(msg)); sem.sem_num = 0; sem.sem_op = 1; sem.sem_flg = 0; if (semop(IDsemaforw, &sem, 1) == -1) { perror("Blad inicjalizacji semaforow\n"); shmdt(pamiec); shmctl(IDpamiec, IPC_RMID, NULL); semctl(IDsemaforw, 0, IPC_RMID, NULL); return -1; } sem.sem_num = 1; sem.sem_op = -1; sem.sem_flg = 0; if (semop(IDsemaforw, &sem, 1) == -1) { perror("Blad inicjalizacji semaforow\n"); shmdt(pamiec); shmctl(IDpamiec, IPC_RMID, NULL); semctl(IDsemaforw, 0, IPC_RMID, NULL); return -1; } memcpy(pomoc, pamiec, sizeof(msg)); printf("Ostateczna wiadomosc: %s\n", pomoc->test); sem.sem_num = 0; sem.sem_op = 1; sem.sem_flg = 0; if (semop(IDsemaforw, &sem, 1) == -1) { perror("Blad inicjalizacji semaforow\n"); shmdt(pamiec); shmctl(IDpamiec, IPC_RMID, NULL); semctl(IDsemaforw, 0, IPC_RMID, NULL); return -1; } memset(pamiec, 0, sizeof(msg)); } } // KLIENT #include #include #include #include #include #include #include #include #include #include typedef struct sembuf semafory; char *path = "/tmp/semafory3"; struct wiadomosc { int a; char test[1024]; }; typedef struct wiadomosc msg; int main(int argc, char *argv[]) { int IDpamiec, IDsemaforw; key_t kluczPamiec, kluczSemaforw; int plik; msg *pamiec; semafory sem; if ((kluczPamiec = ftok(path, 1)) == -1) { perror("Blad tworzenia klucza pamieci\n"); return -1; } if ((kluczSemaforw = ftok(path, 2)) == -1) { perror("Blad tworzenia klucza semaforow\n"); return -1; } if ((IDsemaforw = semget(kluczSemaforw, 2, 0600 | IPC_CREAT)) == -1) { perror("Blad tworzenia ID semaforw\n"); return -1; } sem.sem_num = 0; sem.sem_op = -1; sem.sem_flg = 0; if (semop(IDsemaforw, &sem, 1) == -1) { perror("Blad inicjalizacji semaforow\n"); semctl(IDsemaforw, 0, IPC_RMID, NULL); return -1; } if ((IDpamiec = shmget(kluczPamiec, sizeof(msg) * 1024, 0600 | IPC_CREAT)) == -1) { perror("Blad tworzenia ID pamieci\n"); return -1; } pamiec = shmat(IDpamiec, (msg *)0, 0); msg *pomoc; pomoc = malloc(sizeof(msg) * 1024); pomoc->a = 5; char *pomoc2; pomoc2 = pomoc->test; strcpy(pomoc2, "Jakis tekst"); printf("Wiadomosc to: %d, %s\n", pomoc->a, pomoc->test); memcpy(pamiec, pomoc, sizeof(msg)); sem.sem_num = 1; sem.sem_op = 1; sem.sem_flg = 0; if (semop(IDsemaforw, &sem, 1) == -1) { perror("Blad inicjalizacji semaforow\n"); shmdt(pamiec); shmctl(IDpamiec, IPC_RMID, NULL); semctl(IDsemaforw, 0, IPC_RMID, NULL); return -1; } printf("Wysłałem wiadomosc\n"); sem.sem_num = 0; sem.sem_op = -1; sem.sem_flg = 0; if (semop(IDsemaforw, &sem, 1) == -1) { perror("Blad inicjalizacji semaforow\n"); shmdt(pamiec); shmctl(IDpamiec, IPC_RMID, NULL); semctl(IDsemaforw, 0, IPC_RMID, NULL); return -1; } memcpy(pomoc, pamiec, sizeof(msg)); printf("Otrzymalem sklejona wiadomosc %s\n", pomoc->test); pomoc2 = pomoc->test; pomoc2 += strlen(pomoc2); strcpy(pomoc2, " doklejam cos 2 raz"); memcpy(pamiec, pomoc, sizeof(msg)); sem.sem_num = 1; sem.sem_op = 1; sem.sem_flg = 0; if (semop(IDsemaforw, &sem, 1) == -1) { perror("Blad inicjalizacji semaforow\n"); shmdt(pamiec); shmctl(IDpamiec, IPC_RMID, NULL); semctl(IDsemaforw, 0, IPC_RMID, NULL); return -1; } shmdt(pamiec); }