Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //filozof-----------------------------------
- #include <stdio.h>
- #include <sys/shm.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- #include <stdlib.h>
- struct message{
- int mtype;
- int mvalue;
- };
- void wypisz_widelce(int *w){
- int i;
- for(i = 0;i<5;i++){
- printf("Widelec[%d] = %d\n", i+1, w[i]);
- }
- }
- int main(int argc, char* argv[])
- {
- //const char *names[5] = {"Kant","Sokrates","Lao Tse","Platon","Tomasz z Akwinu"};
- key_t key_message, key_memory;
- int F = 5;
- int msgID, shmID;
- int i;
- int numer; //numer filozofa
- int *widelec; //tablica widelcow
- struct message kom;
- if( (key_message = ftok(".",'A')) == -1){ //tworzenie kolejki
- printf("Ftok key_message error(filozof)\n");
- exit(1);
- }
- if( (msgID = msgget(key_message,IPC_CREAT|0666)) == -1){
- printf("Msget error (filozof)\n");
- exit(1);
- }
- if( (key_memory = ftok(".",'B')) == -1){ //klucz shared memory
- printf("Ftok key_memory error(filozof)\n");
- exit(1);
- }
- if( (shmID = shmget(key_memory,5 * sizeof(int),IPC_CREAT|0666)) == -1){
- printf("Shmget error (filozof)\n");
- exit(1);
- }
- fflush(stdout);
- widelec = (int*)shmat(shmID,0,0); //przylaczenie pam.dziel;
- numer = atoi(argv[1]); //pobieranie numeru filozofa
- for(i = 0;i < 1; i++){
- printf("Filozof %d mysli\n", numer+1);
- if(msgrcv(msgID,&kom,sizeof(kom.mvalue),numer+1,0) == -1){
- printf("blad odebrania\n");
- exit(1);
- }
- if(msgrcv(msgID,&kom,sizeof(kom.mvalue),(numer+1)%5 +1,0) == -1){
- printf("blad odebrania\n");
- exit(1);
- }
- widelec[numer] = numer+1; //podniesienie widelcow
- widelec[(numer + 1)%5] = numer + 1;
- printf("Filozof %d je\n",numer + 1); //jedzenie
- wypisz_widelce(widelec);
- widelec[numer] = 0;
- widelec[(numer+1)%5] = 0;//zwolnienie widelcow
- kom.mtype = numer +1;
- if(msgsnd(msgID,&kom,sizeof(kom.mvalue),0) == -1){
- printf("Blad wyslania \n");
- exit(1);
- }
- kom.mtype = (numer+1)%5 + 1;
- if(msgsnd(msgID,&kom,sizeof(kom.mvalue),0) == -1){
- printf("Blad wyslania\n");
- exit(1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement