Arjun Suresh (talk | contribs) (Created page with " //Dining philosopher using threads #include<stdio.h> #include<pthread.h> void* reader(void*); pthread_mutex_t mut[5]; int n; int main() { pthread_t t1[5]; int i,j=0,k; ...") |
Arjun Suresh (talk | contribs) |
||
(10 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
− | + | <metadesc>C implementation of dining philosophers problem with threads</metadesc> | |
+ | <syntaxhighlight lang="c" name="diningphilosopher_thread"> | ||
//Dining philosopher using threads | //Dining philosopher using threads | ||
#include<stdio.h> | #include<stdio.h> | ||
#include<pthread.h> | #include<pthread.h> | ||
+ | |||
void* reader(void*); | void* reader(void*); | ||
pthread_mutex_t mut[5]; | pthread_mutex_t mut[5]; | ||
int n; | int n; | ||
+ | |||
int main() | int main() | ||
{ | { | ||
pthread_t t1[5]; | pthread_t t1[5]; | ||
− | int i,j=0,k; | + | int i,j = 0,k; |
− | + | n = 5; | |
− | for(i=0;i<n;i++) | + | for(i = 0; i < n; i++) |
{ | { | ||
j=i; | j=i; | ||
− | pthread_create(&t1[i],NULL,reader,&j); | + | pthread_create(&t1[i], NULL, reader, &j); |
} | } | ||
− | for(j=0;j<n;j++) | + | for(j = 0; j < n; j++) |
− | pthread_join(t1[j],NULL); | + | pthread_join(t1[j], NULL); |
return 1; | return 1; | ||
} | } | ||
+ | |||
void *reader(void* arg) | void *reader(void* arg) | ||
{ | { | ||
− | int val=*(int*)arg; | + | int val = *(int*)arg; |
− | printf("%d Thinking\n",val+1); | + | printf("%d Thinking\n", val + 1); |
− | pthread_mutex_lock(&mut[val%n]); | + | pthread_mutex_lock(&mut[val % n]); |
− | pthread_mutex_lock(&mut[(val+1)%n]); | + | pthread_mutex_lock(&mut[(val + 1) % n]); |
− | printf("%d Eating\n",val+1); | + | printf("%d Eating\n", val + 1); |
sleep(3); | sleep(3); | ||
− | pthread_mutex_unlock(&mut[val%n]); | + | pthread_mutex_unlock(&mut[val % n]); |
− | pthread_mutex_unlock(&mut[(val+1)%n]); | + | pthread_mutex_unlock(&mut[(val + 1) % n]); |
− | printf("%d Finished Eating\n",val+1); | + | printf("%d Finished Eating\n",val + 1); |
+ | return NULL: | ||
} | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | |||
+ | {{Template:FBD}} | ||
− | [[Operating System Codes]] | + | [[Category: Operating System Codes]] |
<syntaxhighlight lang="c" name="diningphilosopher_thread"> //Dining philosopher using threads
void* reader(void*); pthread_mutex_t mut[5]; int n;
int main() {
pthread_t t1[5]; int i,j = 0,k; n = 5; for(i = 0; i < n; i++) { j=i; pthread_create(&t1[i], NULL, reader, &j); } for(j = 0; j < n; j++) pthread_join(t1[j], NULL); return 1;
}
void *reader(void* arg) {
int val = *(int*)arg; printf("%d Thinking\n", val + 1); pthread_mutex_lock(&mut[val % n]); pthread_mutex_lock(&mut[(val + 1) % n]); printf("%d Eating\n", val + 1); sleep(3); pthread_mutex_unlock(&mut[val % n]); pthread_mutex_unlock(&mut[(val + 1) % n]); printf("%d Finished Eating\n",val + 1); return NULL:
} </syntaxhighlight>
//Dining philosopher using threads
void* reader(void*); pthread_mutex_t mut[5]; int n; int main() {
pthread_t t1[5]; int i,j=0,k; n = 5; for(i=0;i<n;i++) { j=i; pthread_create(&t1[i],NULL,reader,&j); } for(j=0;j<n;j++) pthread_join(t1[j],NULL); return 1;
} void *reader(void* arg) {
int val=*(int*)arg; printf("%d Thinking\n",val+1); pthread_mutex_lock(&mut[val%n]); pthread_mutex_lock(&mut[(val+1)%n]); printf("%d Eating\n",val+1); sleep(3); pthread_mutex_unlock(&mut[val%n]); pthread_mutex_unlock(&mut[(val+1)%n]); printf("%d Finished Eating\n",val+1);
}