Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include<sys/ipc.h>
- #include<sys/msg.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <sys/wait.h>
- #include <sys/sem.h>
- #include <sys/shm.h>
- #include <fcntl.h>
- #include "operacje.c"
- #include "operacje.h"
- #define P 50 // liczba procesow prod i kons
- #define MAX 10 //rozmiar buforu
- #define MAX2 12 // + dwa pola na indeksy zapis/odczyt
- #define PUSTY 1 //typ komunikatu
- #define PELNY 2 //typ komunikatu
- #define N 2
- //struktura komunikatu
- struct bufor{
- long mtype;
- int mvalue;
- };
- // funkcja do obsługi sygnału 2 SIGINT
- int main()
- {
- key_t klucz, kluczm, kluczs; //klucze do kk, pam. dz. i sem
- int msgID; //id kolejki kom.
- int shmID; //id pamieci dzielonej
- int semID;
- int i;
- struct bufor komunikat;
- //****************************************************************************************
- //tworzenie klucza do kolejki komunikatow i uzyskiwanie dostepu do niej
- if ( (klucz = ftok(".", 'A')) == -1 )
- {
- printf("Blad ftok (a)\n");
- exit(-2);
- }
- msgID=msgget(klucz,IPC_CREAT|IPC_EXCL|0666); //tworzenie kk
- if (msgID==-1)
- {printf("blad kolejki komunikatow\n"); exit(1);}
- //****************************************************************************************
- //tworzenie klucza do pamieci dzielonej i uzyskiwanie dostepu do niej
- if((kluczm=ftok(".",'B'))==-1){
- printf("blad ftok (b)\n");
- exit(-2);
- }
- shmID=shmget(kluczm, MAX2*sizeof(int), IPC_CREAT|IPC_EXCL|0666);//tworzenie pam. dz.
- //****************************************************************************************
- //tworzenie klucza do semaforu, alokacja oraz inicjalizacja
- if((kluczs=ftok(".", 'C'))==-1){
- printf("blad ftok (c)\n");
- exit(-2); }
- semID=semget(kluczs, N, IPC_CREAT|IPC_EXCL|0666);//alokacja semaforów
- if(semID==-1){
- printf("blad semget\n");
- exit(1);
- }
- //semctl(semID, 0, SETVAL, 0);????????????????
- semctl(semID, PUSTY, SETVAL, 1);
- semctl(semID, PELNY, SETVAL, 1);
- printf("Semafory gotowe!\n");
- //****************************************************************************************
- //wysylanie komunikatow, uruchamianie konumentow i producentow
- komunikat.mtype=PUSTY;
- komunikat.mvalue=0; // wazny jest typ komunikatu, tresc - dowolna
- for(i=0;i<MAX;i++)
- {
- if( msgsnd(msgID,&komunikat,sizeof(komunikat.mvalue),0)==-1) //wyslanie komunikatu typu pusty
- {
- printf("blad wyslania kom. pustego\n");
- exit(1);
- };
- printf("wyslany pusty komunikat %d\n",i);
- }
- for (i = 0; i < P; i++)
- switch (fork())
- {
- case -1:
- perror("Blad fork (mainprog)");
- exit(2);
- case 0:
- execl("./prod","prod", NULL);
- }
- for(i=0;i<P;i++)
- switch (fork())
- {
- case -1:
- printf("Blad fork (mainprog)\n");
- exit(2);
- case 0:
- execl("./kons","kons",NULL);
- }
- /*
- if (fork()==0)
- execl("./prod","prod", NULL);
- else
- execl("./kons","kons",NULL);
- */
- for(i=0;i<2*P;i++)
- wait(NULL);
- //zwalnianie zasobow
- msgctl(msgID,IPC_RMID,NULL);
- shmctl(shmID,IPC_RMID, NULL);
- semctl(semID, N, IPC_RMID, NULL); printf("MAIN: Koniec.\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement