Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/msg.h>
- #include <stdlib.h>
- #include <sys/sem.h>
- #include <signal.h>
- #include <sys/shm.h>
- #include <time.h>
- int *bufor;
- key_t key1, key2; // zmienne na klucze
- int semid;
- int shmid;
- int pid1, pid2;
- struct sembuf buf;
- void podnies(int semid, int semnum) //podniesienie semafora
- {
- buf.sem_num = semnum;
- buf.sem_op = 1;
- buf.sem_flg = 0;
- if (semop(semid, &buf, 1) == -1)
- {
- perror("Podnoszenie semafora");
- exit(1);
- }
- }
- void opusc(int semid, int semnum) //opuszczenie semafora
- {
- buf.sem_num = semnum;
- buf.sem_op = -1;
- buf.sem_flg = 0;
- if (semop(semid, &buf, 1) == -1)
- {
- perror("Opuszczenie semafora");
- exit(1);
- }
- }
- void proces1() //funkcja pierwszego procesu potomnego
- {
- pid1=getpid();
- srand(time(0));
- while(1)
- {
- usleep(rand()%100);
- opusc(semid, 2);
- printf("K1, odczytuje: %d\n", bufor[0]);
- podnies(semid, 0);
- }
- }
- void proces2() //funkcja drugiego procesu potomnego
- {
- pid2=getpid();
- srand(time(0));
- while(1)
- {
- usleep(rand()%100);
- opusc(semid, 3);
- printf("K2, odczytuje: %d\n", bufor[0]);
- podnies(semid, 1);
- }
- }
- void proces3() //funkcja dla procesu macierzystego
- {
- int i;
- for(i=0; i<101; i++)
- {
- opusc(semid, 0);
- opusc(semid, 1);
- bufor[0]=i;
- printf("Wpisano do shared memory. %d\n", i);
- podnies(semid, 2);
- podnies(semid, 3);
- }
- }
- int main()
- {
- /* Tworzymy klucz dla semaforów */
- if ((key1 = ftok(".", 'A')) == -1)
- errx(1, "Blad tworzenia klucza!");
- ////////Deklarujemy tablice 2 semaforow
- semid = semget(key1, 4, IPC_CREAT|0600);
- //Nadajemy wartości semaforom
- if (semctl(semid, 0, SETVAL, (int)1) == -1){
- perror("Nadanie wartosci semaforowi 0");
- exit(1);
- }
- if (semctl(semid, 1, SETVAL, (int)1) == -1){
- perror("Nadanie wartosci semaforowi 1");
- exit(1);
- }
- if (semctl(semid, 2, SETVAL, (int)0) == -1){
- perror("Nadanie wartosci semaforowi 2");
- exit(1);
- }
- if (semctl(semid, 3, SETVAL, (int)0) == -1){
- perror("Nadanie wartosci semaforowi 3");
- exit(1);
- }
- /* Tworzymy klucz dla pamieci wspoldzielonej */
- if ((key2 = ftok(".", 'B')) == -1)
- errx(1, "Blad tworzenia klucza!");
- ////////Deklarujemy pamiec wspoldzielona
- shmid = shmget(key2, sizeof(int), IPC_CREAT|0600);
- if (shmid == -1){
- perror("Utworzenie segmentu pamieci wspoldzielonej");
- exit(1);
- }
- //Pobieramy wskaznik na adres pamieci wspoldzielonej
- bufor = shmat(shmid, NULL, 0);
- if (bufor == NULL){
- perror("Przylaczenie segmentu pamieci wspoldzielonej");
- exit(1);
- }
- if(fork()==0) //powolanie pierwszego potomka
- {
- int choice;
- printf("Wybierz typ inputu:\n");
- printf("1. Z klawiatury.\n");
- printf("2. Z wybranego pliku.\n");
- printf("3. Z pliku dev/urandom.\n");
- scanf(" %d", &choice);
- if(choice==1)
- {
- while(1)
- {
- }
- }
- if(choice==2)
- {
- while(1)
- {
- }
- }if(choice==3)
- {
- while(1)
- {
- }
- }
- }
- if(fork()==0) //powolanie drugiego potomka
- {
- czytaj2();
- }
- producent();
- sleep(20);
- kill(pid1, SIGKILL); //posprzatanie po stworzonych procesach
- kill(pid2, SIGKILL);
- kill(getpid(), SIGKILL);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement