Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
95
Task/Dining-philosophers/C/dining-philosophers-1.c
Normal file
95
Task/Dining-philosophers/C/dining-philosophers-1.c
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#define N 5
|
||||
const char *names[N] = { "Aristotle", "Kant", "Spinoza", "Marx", "Russell" };
|
||||
pthread_mutex_t forks[N];
|
||||
|
||||
#define M 5 /* think bubbles */
|
||||
const char *topic[M] = { "Spaghetti!", "Life", "Universe", "Everything", "Bathroom" };
|
||||
|
||||
#define lock pthread_mutex_lock
|
||||
#define unlock pthread_mutex_unlock
|
||||
#define xy(x, y) printf("\033[%d;%dH", x, y)
|
||||
#define clear_eol(x) print(x, 12, "\033[K")
|
||||
void print(int y, int x, const char *fmt, ...)
|
||||
{
|
||||
static pthread_mutex_t screen = PTHREAD_MUTEX_INITIALIZER;
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
|
||||
lock(&screen);
|
||||
xy(y + 1, x), vprintf(fmt, ap);
|
||||
xy(N + 1, 1), fflush(stdout);
|
||||
unlock(&screen);
|
||||
}
|
||||
|
||||
void eat(int id)
|
||||
{
|
||||
int f[2], ration, i; /* forks */
|
||||
f[0] = f[1] = id;
|
||||
|
||||
/* make some (but not all) philosophers leftie.
|
||||
could have been f[!id] = (id + 1) %N; for example */
|
||||
f[id & 1] = (id + 1) % N;
|
||||
|
||||
clear_eol(id);
|
||||
print(id, 12, "..oO (forks, need forks)");
|
||||
|
||||
for (i = 0; i < 2; i++) {
|
||||
lock(forks + f[i]);
|
||||
if (!i) clear_eol(id);
|
||||
|
||||
print(id, 12 + (f[i] != id) * 6, "fork%d", f[i]);
|
||||
/* delay 1 sec to clearly show the order of fork acquisition */
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
for (i = 0, ration = 3 + rand() % 8; i < ration; i++)
|
||||
print(id, 24 + i * 4, "nom"), sleep(1);
|
||||
|
||||
/* done nomming, give up forks (order doesn't matter) */
|
||||
for (i = 0; i < 2; i++) unlock(forks + f[i]);
|
||||
}
|
||||
|
||||
void think(int id)
|
||||
{
|
||||
int i, t;
|
||||
char buf[64] = {0};
|
||||
|
||||
do {
|
||||
clear_eol(id);
|
||||
sprintf(buf, "..oO (%s)", topic[t = rand() % M]);
|
||||
|
||||
for (i = 0; buf[i]; i++) {
|
||||
print(id, i+12, "%c", buf[i]);
|
||||
if (i < 5) usleep(200000);
|
||||
}
|
||||
usleep(500000 + rand() % 1000000);
|
||||
} while (t);
|
||||
}
|
||||
|
||||
void* philosophize(void *a)
|
||||
{
|
||||
int id = *(int*)a;
|
||||
print(id, 1, "%10s", names[id]);
|
||||
while(1) think(id), eat(id);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int i, id[N];
|
||||
pthread_t tid[N];
|
||||
|
||||
for (i = 0; i < N; i++)
|
||||
pthread_mutex_init(forks + (id[i] = i), 0);
|
||||
|
||||
for (i = 0; i < N; i++)
|
||||
pthread_create(tid + i, 0, philosophize, id + i);
|
||||
|
||||
/* wait forever: the threads don't actually stop */
|
||||
return pthread_join(tid[0], 0);
|
||||
}
|
||||
94
Task/Dining-philosophers/C/dining-philosophers-2.c
Normal file
94
Task/Dining-philosophers/C/dining-philosophers-2.c
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
typedef struct philData {
|
||||
pthread_mutex_t *fork_lft, *fork_rgt;
|
||||
const char *name;
|
||||
pthread_t thread;
|
||||
int fail;
|
||||
} Philosopher;
|
||||
|
||||
int running = 1;
|
||||
|
||||
void *PhilPhunction(void *p) {
|
||||
Philosopher *phil = (Philosopher*)p;
|
||||
int failed;
|
||||
int tries_left;
|
||||
pthread_mutex_t *fork_lft, *fork_rgt, *fork_tmp;
|
||||
|
||||
while (running) {
|
||||
printf("%s is sleeping --er thinking\n", phil->name);
|
||||
sleep( 1+ rand()%8);
|
||||
|
||||
fork_lft = phil->fork_lft;
|
||||
fork_rgt = phil->fork_rgt;
|
||||
printf("%s is hungry\n", phil->name);
|
||||
tries_left = 2; /* try twice before being forceful */
|
||||
do {
|
||||
failed = pthread_mutex_lock( fork_lft);
|
||||
failed = (tries_left>0)? pthread_mutex_trylock( fork_rgt )
|
||||
: pthread_mutex_lock(fork_rgt);
|
||||
if (failed) {
|
||||
pthread_mutex_unlock( fork_lft);
|
||||
fork_tmp = fork_lft;
|
||||
fork_lft = fork_rgt;
|
||||
fork_rgt = fork_tmp;
|
||||
tries_left -= 1;
|
||||
}
|
||||
} while(failed && running);
|
||||
|
||||
if (!failed) {
|
||||
printf("%s is eating\n", phil->name);
|
||||
sleep( 1+ rand() % 8);
|
||||
pthread_mutex_unlock( fork_rgt);
|
||||
pthread_mutex_unlock( fork_lft);
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void Ponder()
|
||||
{
|
||||
const char *nameList[] = { "Kant", "Guatma", "Russel", "Aristotle", "Bart" };
|
||||
pthread_mutex_t forks[5];
|
||||
Philosopher philosophers[5];
|
||||
Philosopher *phil;
|
||||
int i;
|
||||
int failed;
|
||||
|
||||
for (i=0;i<5; i++) {
|
||||
failed = pthread_mutex_init(&forks[i], NULL);
|
||||
if (failed) {
|
||||
printf("Failed to initialize mutexes.");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
for (i=0;i<5; i++) {
|
||||
phil = &philosophers[i];
|
||||
phil->name = nameList[i];
|
||||
phil->fork_lft = &forks[i];
|
||||
phil->fork_rgt = &forks[(i+1)%5];
|
||||
phil->fail = pthread_create( &phil->thread, NULL, PhilPhunction, phil);
|
||||
}
|
||||
|
||||
sleep(40);
|
||||
running = 0;
|
||||
printf("cleanup time\n");
|
||||
|
||||
for(i=0; i<5; i++) {
|
||||
phil = &philosophers[i];
|
||||
if ( !phil->fail && pthread_join( phil->thread, NULL) ) {
|
||||
printf("error joining thread for %s", phil->name);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
Ponder();
|
||||
return 0;
|
||||
}
|
||||
60
Task/Dining-philosophers/C/dining-philosophers-3.c
Normal file
60
Task/Dining-philosophers/C/dining-philosophers-3.c
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
#include <stdio.h>
|
||||
#include <threads.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define NUM_THREADS 5
|
||||
|
||||
struct timespec time1;
|
||||
mtx_t forks[NUM_THREADS];
|
||||
|
||||
typedef struct {
|
||||
char *name;
|
||||
int left;
|
||||
int right;
|
||||
} Philosopher;
|
||||
|
||||
Philosopher *create(char *nam, int lef, int righ) {
|
||||
Philosopher *x = malloc(sizeof(Philosopher));
|
||||
x->name = nam;
|
||||
x->left = lef;
|
||||
x->right = righ;
|
||||
return x;
|
||||
}
|
||||
|
||||
int eat(void *data) {
|
||||
time1.tv_sec = 1;
|
||||
Philosopher *foo = (Philosopher *) data;
|
||||
mtx_lock(&forks[foo->left]);
|
||||
mtx_lock(&forks[foo->right]);
|
||||
printf("%s is eating\n", foo->name);
|
||||
thrd_sleep(&time1, NULL);
|
||||
printf("%s is done eating\n", foo->name);
|
||||
mtx_unlock(&forks[foo->left]);
|
||||
mtx_unlock(&forks[foo->right]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
thrd_t threadId[NUM_THREADS];
|
||||
Philosopher *all[NUM_THREADS] = {create("Teral", 0 ,1),
|
||||
create("Billy", 1, 2),
|
||||
create("Daniel", 2,3),
|
||||
create("Philip", 3, 4),
|
||||
create("Bennet", 0, 4)};
|
||||
for (int i = 0; i < NUM_THREADS; i++){
|
||||
if (mtx_init(&forks[i], mtx_plain) != thrd_success){
|
||||
puts("FAILED IN MUTEX INIT!");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
for (int i=0; i < NUM_THREADS; ++i) {
|
||||
if (thrd_create(threadId+i, eat, all[i]) != thrd_success) {
|
||||
printf("%d-th thread create error\n", i);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i=0; i < NUM_THREADS; ++i)
|
||||
thrd_join(threadId[i], NULL);
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue