?? philosem2.c
字號:
/* check fifth fork...output is oppikable*/#include<stdio.h>#include<stdlib.h>#include<pthread.h>#include<sys/shm.h>#include<unistd.h>#include<sys/types.h>#include<sys/ipc.h>#include<sys/sem.h>#define N 5#define THINKING 1#define HUNGRY 2#define EATING 3#define uniqkey ftok("." , 'A') union semun { int val; struct semid_ds *buf; ushort *array; };int state[N];int semid,ret=0; static struct sembuf acquire={0,-1,SEM_UNDO}; static struct sembuf release={0,1, SEM_UNDO}; static ushort initial[6]={6,5,4,3,2,1}; //its giving some error other wayunion semun arg; void initialize (void){ int ky=0; for(ky;ky<N;ky++) { state[ky]=THINKING; }}int left (int k) { return ((k+N-1)%N); }int right (int k) { return ((k+1)%N); }int randomizeme (int d) //not used{ int lret; ret=(ret +1)%5; lret=ret*d - (ret+d); if(lret<=0) return 5; else return lret;}void printstate (void){ int ku=0; for(ku;ku<5;ku++) { printf(" P%d --",ku); switch (state[ku]) { case 1 : printf("THINKING ## ");break; case 2 : printf("HUNGRY ## ");break; case 3 : printf("EATING ## ");break; default : printf("invalid choice \n"); } printf("\n"); }}int test (int ph){ if( (state[left(ph)]!=EATING) && (state[right(ph)]!=EATING) && (state[ph]==HUNGRY) ) { if(ph==0) { acquire.sem_num=5; if(semop(semid,&acquire,1)==-1) { printf("error in acquiring %d \n",5); } } else { acquire.sem_num=ph; if(semop(semid,&acquire,1)==-1) { printf("error in acquiring %d \n",ph); } } acquire.sem_num=(ph+1)%N; if(semop(semid,&acquire,1)==-1) { printf("error in acquiring %d \n",(ph+1)); } state[ph]=EATING; return 1; } else {return 0;}}void takeforks (int p){ int t; if((p%2)==0) { state[p]=HUNGRY; if(test(p)) { //left right sleep(2); //eating time } else { sleep(2); takeforks(p); } } else { state[p]=HUNGRY; //sleep(1); //randomness urapppilla if(test(p)) { //right left// acquire.sem_num=right(p);// if(semop(semid,&acquire,1)==-1)// {// printf("error in acquiring %d \n",right(p));// }// acquire.sem_num=left(p);// if(semop(semid,&acquire,1)==-1)// {// printf("error in acquiring %d \n",left(p));// }// state[p]=EATING; sleep(2); //eating time } else { sleep(3); takeforks(p); } } //putdownforks(p);}void putdownforks (int r){ if(r==0) { release.sem_num=(5); if(semop(semid,&release,1)==-1) { //printf("error in releasing %d \n",5); } } else { release.sem_num=(r); if(semop(semid,&release,1)==-1) { printf("error in releasing %d \n",r); } } release.sem_num=(r+1); if(semop(semid,&release,1)==-1) { printf("error in releasing %d \n",(r+1)); } state[r]=THINKING;// if (test(r))// {// takeforks(left(r));putdownforks(r);// takeforks(right(r));putdownforks(r);// }}void * philosopher (void *num){ int n,ch=6; n=(int) num; printf("***%d \n",n); if(n!=N) { if(semget(uniqkey,6,0)==-1) //diff process should first access semaphore(no need 4 creation) { printf("error in accessing semaphores \n"); } if((n%2)!=0) {sleep(1);} while(ch) {// printf("[ %d #### %d ] \n",n,state[n]); ch--; //printf() takeforks(n); putdownforks(n); sleep(4); } } else {//for printing ch=25; while(ch) { printf("=====================================================\n"); sleep(1); printstate(); ch--; } } pthread_exit(NULL);}int main(){printf("ok\n");printf(" eshwara ithu work cheyyane \n");//printf("$$%d$$",left(0));//printf("$%d$",right(0));initialize(); //all philosophers are made to thinkarg.array=initial; //not able to do globally! :(semid=semget(uniqkey,6,IPC_CREAT|IPC_EXCL|0666); //6 bcz i am using from 1 to 5...5 is enoughif(semid==-1) { printf("error in making semaphore \n"); }if(semctl(semid,0,SETALL,arg) == -1) { printf("error in initialization \n"); }//int po=555;pthread_t phil ;int thread;int i=0;for(i;i<=N;i++) { thread=pthread_create(&phil,NULL,philosopher,(void *) i); if(thread) { printf("error in creating threads \n"); } }for(i=0;i<=N;i++) { pthread_join(phil,NULL); }if(semctl(semid,0,IPC_RMID,0) ==-1) { printf("error in removing semaphores \n"); } printf("=======================THE END======================\n"); return 0;}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -