Systémové programovanie domáca č. 3 - riesenie

Created: 2010-05-08 - 18:34

Zadanie

Uloha bola porovnat rychlost FIFO a zdielanej pamäte pri odosielanie a prijmani sprav... Cize tak ako v predchadzajucich zadaniach som si forkol dva procesy, ktore navzajom komunikovali... posielali si 100 000x spravu:

Namerane hodnoty

time ./shm

real    0m3.472s
user    0m1.236s
sys     0m0.002s
time ./fifo

real    0m4.520s
user    0m0.589s
sys     0m2.207s

Zdrojaky

fifo.c
==================================================================
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <limits.h>


#define BUFFER_SIZE PIPE_BUF
#define FIFO_NAME_RODIC "/home/rajzak/rodic"
#define FIFO_NAME_DIETA "/home/rajzak/dieta"

int main() {
        pid_t pid;
        char buffer[BUFFER_SIZE];
        int i;
        int res;
        FILE *write;
        FILE *read;

        //fifo rodic -> dieta
        if (access(FIFO_NAME_RODIC, F_OK) == -1) {
                res = mkfifo(FIFO_NAME_RODIC, 0777);
                if (res != 0) {
                        fprintf(stderr, "Could not create fifo %s\n", FIFO_NAME_RODIC);
                        exit(EXIT_FAILURE);
                }
        }
        //fifo dieta -> rodic
        if (access(FIFO_NAME_DIETA, F_OK) == -1) {
                res = mkfifo(FIFO_NAME_DIETA, 0777);
                if (res != 0) {
                        fprintf(stderr, "Could not create fifo %s\n", FIFO_NAME_DIETA);
                        exit(EXIT_FAILURE);
                }
        }


        pid = fork();

        //dieta
        if (pid == (pid_t) 0) {
                read = fopen(FIFO_NAME_RODIC, "r");
                write = fopen(FIFO_NAME_DIETA, "w");

                while (!feof(read) && !ferror(read) && fgets(buffer, BUFFER_SIZE, read) != NULL) {
                        fprintf(write, "Dostal som spravu.\n");
                        fflush(write);
                }
                fclose(read);
                fclose(write);
        //dieta
        } else {
                write = fopen(FIFO_NAME_RODIC, "w");
                read = fopen(FIFO_NAME_DIETA, "r");

                for (i = 0; i < 100000; ++i) {
                        fprintf(write, "Posielam spravu.\n");
                        fflush(write);
                        if (feof(read) || ferror(read) || fgets(buffer, BUFFER_SIZE, read) == NULL) {
                                break;
                        }
                }

                fclose(write);
                fclose(read);

                waitpid(pid, NULL, 0);

                // zmazeme FIFO z disku
                unlink(FIFO_NAME_RODIC);
                unlink(FIFO_NAME_DIETA);
        }
        return 0;
}

shm.c

==================================================================

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>


#include "shm_com.h" //vid SPR_8_semafory_fronty_sprav_a_zdielana_pamat.doc

# define VELKOST_PAMATE 0x1000
# define UID 2204 //moje UID zistene cez cat /etc/passwd | grep "Rajzak"

int main() {
        pid_t pid;
        int cislo_segmentu;
        char* zdielana_pamat;
        struct shared_use_st *zdielana_struktura;

        pid = fork();

        //dieta
        if(pid == (pid_t) 0) {
                cislo_segmentu = shmget((key_t)UID, VELKOST_PAMATE,0666 | IPC_CREAT);
                zdielana_pamat = shmat(cislo_segmentu, 0, 0);
                zdielana_struktura = (struct shared_use_st *) zdielana_pamat;
                int j = 0;
                int running = 1;
                zdielana_struktura->written_by_you=0;
                //v slucke caka na spravy, az kym posledna nie je end
                while(running){
                        if(zdielana_struktura->written_by_you == 1){
                                strncpy(zdielana_struktura->some_text,"Prijal som spravu.", TEXT_SZ);
                                //printf("Som brat jezisa"); //testovaci string prijmatela
                                zdielana_struktura->written_by_you = 0;
                                if(strncmp(zdielana_struktura->some_text,"end",3)==0)
                                        running=0;
                        }
                }
                shmdt(zdielana_pamat);
                shmctl(cislo_segmentu,IPC_RMID,0);

        //rodic
        } else {
                cislo_segmentu = shmget((key_t)UID, VELKOST_PAMATE,0666 | IPC_CREAT);
                zdielana_pamat = shmat(cislo_segmentu, 0, 0);
                zdielana_struktura = (struct shared_use_st *) zdielana_pamat;
                int j = 0;
                while(++j < 100000){
                        while(zdielana_struktura->written_by_you == 1){}
                        strncpy(zdielana_struktura->some_text,"Posielam spravu.",TEXT_SZ);
                        //printf("Jezis odoslany"); //testovaci string odosielatela
                        zdielana_struktura->written_by_you = 1;
                }
                strncpy(zdielana_struktura->some_text,"end",3);
                shmdt(zdielana_pamat);
                shmctl(cislo_segmentu,IPC_RMID,0);
        }

        return 0;
}