Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- #include <sys/shm.h>
- #include <sys/wait.h>
- #include <sys/signal.h>
- #define LICZBA_FILOZOFOW 5 //liczba filozofow
- #define IL 3 //ilosc papu filozofow (ile razy jedza)
- struct bufor
- {
- long mtype;
- int mvalue;
- };
- void koniec(int sig);
- int msgID,shmID;
- int main(int argc,char *argv[])
- {
- key_t kluczm, klucz;
- int *pam;
- char bufor[3];
- int i;
- struct bufor komunikat;
- struct sigaction act;
- if ((kluczm = ftok(".", 'A')) == -1)
- {
- printf("MAIN: Blad kluczs\n");
- exit(1);
- }
- shmID=shmget(kluczm, LICZBA_FILOZOFOW * sizeof(int), IPC_CREAT | IPC_EXCL | 0666);
- if(shmID==-1){
- printf("Blad shmget");
- exit(1);
- }
- if ((klucz = ftok(".", 'B')) == -1)
- {
- printf("Blad ftok (main)\n");
- exit(1);
- }
- msgID = msgget(klucz, IPC_CREAT | IPC_EXCL | 0666);
- if (msgID == -1)
- {
- printf("blad kolejki komunikatow\n");
- exit(1);
- }
- pam=(int*)shmat(shmID,NULL,0);
- if(*pam==-1){
- printf("Problem z przydzieleniem adresu.\n");
- exit(1);
- }
- for (i = 0; i < LICZBA_FILOZOFOW; i++)
- pam[i]=0;
- for (i = 0; i < LICZBA_FILOZOFOW; i++)
- {
- komunikat.mtype = i + 1;//nie mog byc zera w kk, bo potem wystapi problem przy odbiorze
- if (msgsnd(msgID, &komunikat, sizeof(komunikat.mvalue), 0) == -1) //wyslanie komunikatu
- {
- printf("blad wyslania kom. pustego\n");
- exit(1);
- };
- printf("wyslany pusty komunikat %d\n", i);
- }
- for (i = 0; i < IL*LICZBA_FILOZOFOW; i++){
- switch (fork())
- {
- case -1:
- perror("MAIN: Blad fork");
- exit(2);
- case 0:
- sprintf(bufor,"%d",i%5);
- execl("./filozof", "filozof",bufor, NULL);
- }
- }
- act.sa_handler=koniec;
- sigemptyset(&act.sa_mask);
- act.sa_flags=0;
- sigaction(SIGINT,&act,0);
- for (i = 0; i < IL*LICZBA_FILOZOFOW; i++)//czekanie na zakonczenie watkow
- wait(NULL);
- msgctl(msgID, IPC_RMID, NULL);
- shmctl(shmID, IPC_RMID, NULL);
- printf("MAIN: Koniec programu\n");
- }
- void koniec(int sig)
- {
- msgctl(msgID,IPC_RMID,NULL);
- shmctl(shmID,IPC_RMID, NULL);
- printf("MAIN - funkcja koniec sygnal %d: Koniec.\n",sig);
- exit(1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement