Witam,
Mam do napisania program o treści: Na podstawie przykładów dotyczących pamięci dzielonej stwórz dwa programy:
shm_text_send.c - program powinien umożliwiać wpisywanie tekstu z klawiatury i zapisywania go w pamięci dzielonej. Dodatkowo powinno istnieć zabezpieczenie przed nadpisaniem danych do czasu aż program shm_text_receive.c nie odbierze wiadomości. Wysłanie wiadomości "quit" powinno zakończyć działanie programu.
shm_text_receive.c - program powinien oczekiwać na dane wysyłane przez pamięć współdzieloną z programu shm_text_send.c. Po otrzymaniu danych powinien wyświetlić otrzymany tekst na ekranie. Jeżeli program otrzymał tekst "quit" powinien usunąć pamięć współdzieloną i zakończyć swoje działanie.
Przykładowe programy:
Przykład programu shm_write.c tworzącego pamięć współdzieloną. Program czeka 10 sekund po utworzeniu pamięci dzielonej (eng. shared memory) i zapisuje tam "jakis tekst".
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <errno.h>
#define TEXT_SIZE 512
struct shared_data { /* (A) */
int data;
char some_text[TEXT_SIZE];
};
int main() {
struct shared_data *ptr;
const char *shm_name = "s123456";
int shm_fd;
shm_fd = shm_open(shm_name, O_CREAT | O_RDWR, 0666); /* (B) */
ftruncate(shm_fd, sizeof(struct shared_data)); /* (C) */
ptr = mmap(0, sizeof(struct shared_data), PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0); /* (D) */
if (ptr == MAP_FAILED) {
printf("mmap - failed.\n");
return -1;
}
ptr->data = 0; /* (E) */
sleep(10);
sprintf(ptr->some_text,"Jakis tekst");
ptr->data = 1;
sleep(10);
if (munmap(ptr, sizeof(struct shared_data)) == -1) { /* (F) */
printf("unmap failed: %s\n", strerror(errno));
exit(1);
}
if (close(shm_fd)) {
printf("close failed: %s", strerror(errno));
exit(1);
}
return 0;
}
Przykład programu shm_read.c odczytującego dane z pamięci dzielonej utworzonej za pomocą Programu wyżej.
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <errno.h>
#define TEXT_SIZE 512
struct shared_data { /* (A) */
int data;
char some_text[TEXT_SIZE];
};
int main(void)
{
struct shared_data *ptr;
const char *shm_name = "s123456";
int shm_fd;
shm_fd = shm_open(shm_name, O_RDONLY, 0666); /* (B) */
ptr = mmap(0, sizeof(struct shared_data), PROT_READ , MAP_SHARED, shm_fd, 0); /* (C) */
if (ptr == MAP_FAILED) {
printf("mmap - failed.\n");
return -1;
}
//wait for data
while(ptr->data==0){ /* (D) */
sleep(1);
};
printf("Text: %s\n", ptr->some_text);
if (munmap(ptr, sizeof(struct shared_data)) == -1) { /* (E) */
printf("unmap failed: %s\n", strerror(errno));
exit(1);
}
if (close(shm_fd)) { /* (F) */
printf("close failed: %s", strerror(errno));
exit(1);
}
/* remove the shared memory segment from the file system */
if (shm_unlink(shm_name) == -1) { /* (G) */
printf("cons: Error removing %s: %s\n", shm_name, strerror(errno));
exit(1);
}
return 0;
}
Każda pomoc będzie na wagę złota.