Created
January 12, 2014 12:57
-
-
Save mukeshkdangi/8384286 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<conio.h> | |
char state[10],self[10],spoon[10]; | |
void test(int k) | |
{ | |
if((state[(k+4)%5]!='e')&&(state[k]=='h')&&(state[(k=1)]!='e')) | |
{ | |
state[k]='e'; | |
self[k]='s'; | |
spoon[k]='n'; | |
spoon[(k+4)%5]='n'; | |
} | |
} | |
void pickup(int i) | |
{ | |
state[i]='h'; | |
test(i); | |
if(state[i]=='h') | |
self[i]='w'; | |
} | |
void putdown(int i) | |
{ | |
state[i]='t'; | |
spoon[i]='s'; | |
spoon[i-1]='s'; | |
test((i+4)%5); | |
test((i+1)%5); | |
} | |
main() | |
{ | |
int ch,a,n,i; | |
printf("\n Dining Philosopher's Problem:"); | |
printf("\n...........................................\n"); | |
for(i=0;i<5;i++) | |
{ | |
state[i]='t'; | |
self[i]='s'; | |
spoon[i]='s'; | |
} | |
printf("\n Initial state of each philososphers:"); | |
printf("\n Phil No : \t Think/Eat \t Status \t\t Spoon"); | |
for(i=0;i<5;i++) | |
printf("\n %d \t\t %c \t\t %c \t\t %c",i+1,state[i],self[i],spoon[i]); | |
printf("\n 1.Hungry \n 2.Thinking\n3.Exit"); | |
printf("\n Enter your choice :"); | |
scanf("%d",&ch); | |
while(1) | |
{ | |
switch(ch) | |
{ | |
case 3: | |
return 0; | |
case 1: | |
printf("\n Enter wchich philosophers is Hungry :"); | |
scanf("%d",&n); | |
pickup(n-1); | |
break; | |
case 2: | |
printf("\n Enter which Philosopher is Thinking :"); | |
scanf("%d",n); | |
putdown(n-1); | |
break; | |
} | |
printf("\n State of each Philosepher :"); | |
printf("\n Phil no: \t Think/Eat \t Status \t\t Spoon"); | |
for(i=0;i<5;i++) | |
printf("\n %d \t\t %c \t\t %c \t\t %c",i+1,state[i],self[i],spoon[i]); | |
printf("\n Exit \n 2.Hungry \n 3. Thinking"); | |
scanf("%d",&ch); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<cmath> | |
#include<conio.h> | |
#include<stdlib.h> | |
void fcfs(int noq, int qu[10], int st) | |
{ | |
int i,s=0; | |
for(i=0;i<noq;i++) | |
{ | |
s+=abs(st-qu[i]); | |
st=qu[i]; | |
} | |
printf("\n Total seek time :%d",s); | |
} | |
void sstf(int noq, int qu[10], int st, int visit[10]) | |
{ | |
int min,s=0,p,i; | |
while(1) | |
{ | |
min=999; | |
for(i=0;i<noq;i++) | |
if (visit[i] == 0) | |
{ | |
if(min > abs(st - qu[i])) | |
{ | |
min = abs(st-qu[i]); | |
p = i; | |
} | |
} | |
if(min == 999) | |
break; | |
visit[p]=1; | |
s=s + min; | |
st = qu[p]; | |
} | |
printf("\n Total seek time is: %d",s); | |
} | |
void scan(int noq, int qu[10], int st, int ch) | |
{ | |
int i,j,s=0; | |
for(i=0;i<noq;i++) | |
{ | |
if(st < qu[i]) | |
{ | |
for(j=i-1; j>= 0;j--) | |
{ | |
s=s+abs(st - qu[j]); | |
st = qu[j]; | |
} | |
if(ch == 3) | |
{ | |
s = s + abs(st - 0); | |
st = 0; | |
} | |
for(j = 1;j < noq;j++) | |
{ | |
s= s + abs(st - qu[j]); | |
st = qu[j]; | |
} | |
break; | |
} | |
} | |
printf("\n Total seek time : %d",s); | |
} | |
int main() | |
{ | |
int n,qu[20],st,i,j,t,noq,ch,visit[20]; | |
printf("\n Enter the maximum number of cylinders : "); | |
scanf("%d",&n); | |
printf("enter number of queue elements"); | |
scanf("%d",&noq); | |
printf("\n Enter the work queue"); | |
for(i=0;i<noq;i++) | |
{ | |
scanf("%d",&qu[i]); | |
visit[i] = 0; | |
} | |
printf("\n Enter the disk head starting posision: \n"); | |
scanf("%d",&st); | |
while(1) | |
{ | |
printf("\n\n\t\t MENU \n"); | |
printf("\n\n\t\t 1. FCFS \n"); | |
printf("\n\n\t\t 2. SSTF \n"); | |
printf("\n\n\t\t 3. SCAN \n"); | |
printf("\n\n\t\t 4. EXIT \n"); | |
printf("\nEnter your choice: "); | |
scanf("%d",&ch); | |
if(ch > 2) | |
{ | |
for(i=0;i<noq;i++) | |
for(j=i+1;j<noq;j++) | |
if(qu[i]>qu[j]) | |
{ | |
t=qu[i]; | |
qu[i] = qu[j]; | |
qu[j] = t; | |
} | |
} | |
switch(ch) | |
{ | |
case 1: printf("\n FCFS \n"); | |
printf("\n*****\n"); | |
fcfs(noq,qu,st); | |
break; | |
case 2: printf("\n SSTF \n"); | |
printf("\n*****\n"); | |
sstf(noq,qu,st,visit); | |
break; | |
case 3: printf("\n SCAN \n"); | |
printf("\n*****\n"); | |
scan(noq,qu,st,ch); | |
break; | |
case 4: | |
exit(0); | |
} | |
} | |
return 0; | |
getch(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<conio.h> | |
#include<math.h> | |
int main(){ | |
int a[8][8],b[8][8],n,t; | |
printf("\n\tEnter number of vertices(where n>=3 & n is odd number ):\t"); | |
scanf("%d",&n); | |
printf("\n\tYou can send %d messages by this graph:\t ",(n-1)/2); | |
printf("\n\tEnter values of adjacency matrix :\n"); | |
printf("\t\n"); | |
printf("\nEnter the time when receiver receives the all messages:\t "); | |
scanf("%d",&t); | |
for(int i=0;i<n;i++){ | |
printf("\t"); | |
for(int j=0;j<n;j++){ | |
printf("\t"); | |
scanf("%d",&a[i][j]); | |
printf("\t"); | |
} | |
printf("\n"); | |
} | |
printf("\n\tEntered Matrix is:"); | |
for(int i=0;i<n;i++){ | |
printf("\n"); | |
for(int j=0;j<n;j++){ | |
printf("%d",a[i][j]); | |
printf("\t"); | |
} | |
} | |
printf("\nReceiver receives all the messages at \t t=%d ",t); | |
for(int l=t-1;l>=0;l--){ | |
printf("\n\n New Decrypted data at t=%d:\n\n",l); | |
for(int i=0;i<n;i++){ | |
printf("\n"); | |
for(int j=0;j<n;j++){ | |
b[i][j]= ((a[i][j])-1)/(2*(l+1)); | |
a[i][j]=b[i][j]; | |
printf("%d",a[i][j]); | |
printf("\t"); | |
} | |
} | |
} | |
return 0; | |
getch(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<pthread.h> | |
#include<semaphore.h> | |
int buf[5],f,r; | |
sem_t mutex,full,empty; | |
void *produce(void *arg) | |
{ | |
int i; | |
for(i=0;i<10;i++) | |
{ | |
sem_wait(&empty); | |
sem_wait(&mutex); | |
printf("produced item is %d\n",i); | |
buf[(++r)%5]=i; | |
sleep(1); | |
sem_post(&mutex); | |
sem_post(&full); | |
printf("full %u\n",full); | |
} | |
} | |
void *consume(void *arg) | |
{ | |
int item,i; | |
for(i=0;i<10;i++) | |
{ | |
sem_wait(&full); | |
printf("full %u\n",full); | |
sem_wait(&mutex); | |
item=buf[(++f)%5]; | |
printf("consumed item is %d\n",item); | |
sleep(1); | |
sem_post(&mutex); | |
sem_post(&empty); | |
} | |
} | |
main() | |
{ | |
pthread_t tid1,tid2; | |
sem_init(&mutex,0,1); | |
sem_init(&full,0,1); | |
sem_init(&empty,0,5); | |
pthread_create(&tid1,NULL,produce,NULL); | |
pthread_create(&tid2,NULL,consume,NULL); | |
pthread_join(tid1,NULL); | |
pthread_join(tid2,NULL); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment