Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Napisz program, który stworzy dwa spokrewnione procesy, które prze ś l ą mi ę dzy
- sobą 10 komunikatów przez kolejkę komunikatów, a nast ę pnie stworzy dwa kolejne
- procesy, które prze ś l ą te same komunikaty za pomocą pami ę ci dzielonej. W obu
- przypadkach dokonaj pomiaru czasu przesy ł ania komunikatów i wy ś wietl otrzy-
- mane wyniki. Do pomiaru czasu u ż yj funkcji time() (man 2 time).
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/ipc.h>
- #include <sys/types.h>
- #include <sys/msg.h>
- #include <sys/shm.h>
- #include <sys/sem.h>
- #include <unistd.h>
- #include <wait.h>
- #include <time.h>
- #include <string.h>
- const char *messages[] = {
- "Pierwszy komunikat",
- "Drugi komunikat",
- "Trzeci komunikat",
- "Czwarty komunikat",
- "Piaty komunikat",
- "Szosty komunikat",
- "Siodmy komunikat",
- "Osmy komunikat",
- "Dziewiaty komunikat",
- "Dziesiaty komunikat"
- };
- #define n_messages (sizeof(messages) / sizeof(const char *))
- #define BUFFER_SIZE 64
- void error_exit(const char *msg)
- {
- perror(msg);
- exit(EXIT_FAILURE);
- }
- struct msgbuf {
- long mtype;
- char mtext[BUFFER_SIZE];
- };
- void msg_send_message(int msgid, int i)
- {
- struct msgbuf send_buffer;
- send_buffer.mtype = 1;
- memset(send_buffer.mtext, '\0', BUFFER_SIZE-1);
- strncpy(send_buffer.mtext, messages[i], BUFFER_SIZE-1);
- if(msgsnd(msgid, &send_buffer, strlen(send_buffer.mtext), 0) == -1)
- error_exit("msgsnd");
- }
- void msg_recv_message(int msgid)
- {
- struct msgbuf recv_buffer;
- memset(recv_buffer.mtext, '\0', BUFFER_SIZE-1);
- if(msgrcv(msgid, &recv_buffer, sizeof(recv_buffer.mtext), 1, 0) == -1)
- error_exit("msgrcv");
- printf("%s\n", recv_buffer.mtext);
- }
- void sem_up_and_wait(int semid)
- {
- struct sembuf up_operation = {0, 1, 0},
- wait_operation = {0, 0, 0};
- if(semop(semid, &up_operation, 1) == -1)
- error_exit("semop_up");
- if(semop(semid, &wait_operation, 1) == -1)
- error_exit("semop_wait");
- }
- void sem_down(int semid)
- {
- struct sembuf down_operation = {0, -1, 0};
- if(semop(semid, &down_operation, 1) == -1)
- error_exit("semop_down");
- }
- void shm_save_message(int shmid, int i)
- {
- char *shared_memory;
- if((shared_memory = (char *)shmat(shmid, NULL, 0)) == (void *)-1)
- error_exit("shmat_save");
- memset(shared_memory, '\0', BUFFER_SIZE);
- strncpy(shared_memory, messages[i], strlen(messages[i]));
- if(shmdt(shared_memory) == -1)
- error_exit("shmdt_save");
- }
- void shm_load_message(int shmid)
- {
- char *shared_memory;
- if((shared_memory = (char *)shmat(shmid, NULL, 0)) == (void *)-1)
- error_exit("shmat_load");
- printf("%s\n", shared_memory);
- if(shmdt(shared_memory) == -1)
- error_exit("shmdt_load");
- }
- int main(void)
- {
- pid_t pid, pid2;
- int msgid;
- int shmid;
- int semid;
- time_t start_msg, stop_msg;
- time_t start_shm, stop_shm;
- ///////////////////////////
- start_msg = clock();
- // Kolejka komunikatow
- printf("KOLEJKA KOMUMINKATOW:\n");
- if((msgid = msgget(IPC_PRIVATE, 0775 | IPC_CREAT)) == -1)
- error_exit("msgget");
- pid = fork();
- if(pid == -1)
- error_exit("fork_msg");
- else if(pid == 0){
- for(int i = 0; i < 10; i++){
- msg_send_message(msgid, i);
- usleep(100000);
- }
- return EXIT_SUCCESS;
- }
- else{
- for(int i = 0; i < 10; i++)
- msg_recv_message(msgid);
- if(msgctl(msgid, IPC_RMID, NULL) == -1)
- error_exit("msgctl");
- }
- stop_msg = clock();
- wait(NULL);
- usleep(500000);
- printf("\n");
- start_shm = clock();
- // Pamiec dzielona
- printf("PAMIEC DZIELONA:\n");
- if((shmid = shmget(IPC_PRIVATE, SHMLBA, 0775 | IPC_CREAT)) == -1)
- error_exit("shmget");
- if((semid = semget(IPC_PRIVATE, 1, 0775 | IPC_CREAT)) == -1)
- error_exit("semget");
- if(semctl(semid, 0, SETVAL, 1) == -1)
- error_exit("semctl_setval");
- pid2 = fork();
- if(pid2 == -1)
- error_exit("fork_shm");
- else if(pid2 == 0){
- sem_up_and_wait(semid);
- for(int i = 0; i < 10; i++){
- shm_save_message(shmid, i);
- usleep(100000);
- sem_up_and_wait(semid);
- }
- return EXIT_SUCCESS;
- }
- else{
- sem_down(semid);
- for(int i = 0; i < 10; i++){
- sem_down(semid);
- shm_load_message(shmid);
- }
- sem_down(semid);
- wait(NULL);
- if(semctl(semid, 0, IPC_RMID, NULL) == -1)
- error_exit("semctl_rmid");
- if(shmctl(shmid, IPC_RMID, NULL) == -1)
- error_exit("shmctl");
- }
- stop_shm = clock();
- wait(NULL);
- printf("\n");
- printf("Czas przesylania przez kolejke komunikatow: %lf\n",
- (double)(stop_msg-start_msg)/CLOCKS_PER_SEC);
- printf("Czas przesylania przez pamiec dzielona: %lf\n",
- (double)(stop_shm-start_shm)/CLOCKS_PER_SEC);
- return EXIT_SUCCESS;
- }
Add Comment
Please, Sign In to add comment