made it so that zemaphore is used on mac, semaphore on linux, and other clean up

This commit is contained in:
Remzi Arpaci-Dusseau
2019-05-16 16:07:33 -05:00
parent bb639b166c
commit 3faa28e520
12 changed files with 146 additions and 35 deletions

View File

@@ -1,5 +1,11 @@
CC := gcc
CFLAGS := -Wall -Werror -I../include
CFLAGS := -Wall -Werror -I../include -pthread
OS := $(shell uname -s)
LIBS :=
ifeq ($(OS),Linux)
LIBS += -pthread
endif
SRCS := dining_philosophers_deadlock.c \
dining_philosophers_deadlock_print.c \
@@ -9,7 +15,8 @@ SRCS := dining_philosophers_deadlock.c \
binary.c \
producer_consumer_works.c \
rwlock.c \
zemaphore.c
zemaphore.c \
throttle.c
OBJS := ${SRCS:c=o}
PROGS := ${SRCS:.c=}
@@ -18,7 +25,7 @@ PROGS := ${SRCS:.c=}
all: ${PROGS}
${PROGS} : % : %.o Makefile
${CC} $< -o $@ -pthread
${CC} $< -o $@ ${LIBS}
clean:
rm -f ${PROGS} ${OBJS}

View File

@@ -6,6 +6,12 @@
#include "common.h"
#include "common_threads.h"
#ifdef linux
#include <semaphore.h>
#elif __APPLE__
#include "zemaphore.h"
#endif
sem_t mutex;
volatile int counter = 0;
@@ -26,7 +32,7 @@ int main(int argc, char *argv[]) {
Pthread_create(&c2, NULL, child, NULL);
Pthread_join(c1, NULL);
Pthread_join(c2, NULL);
printf("result: %d\n", counter);
printf("result: %d (should be 20000000)\n", counter);
return 0;
}

View File

@@ -5,6 +5,12 @@
#include "common.h"
#include "common_threads.h"
#ifdef linux
#include <semaphore.h>
#elif __APPLE__
#include "zemaphore.h"
#endif
typedef struct {
int num_loops;
int thread_id;

View File

@@ -5,6 +5,12 @@
#include "common.h"
#include "common_threads.h"
#ifdef linux
#include <semaphore.h>
#elif __APPLE__
#include "zemaphore.h"
#endif
typedef struct {
int num_loops;
int thread_id;

View File

@@ -5,6 +5,12 @@
#include "common.h"
#include "common_threads.h"
#ifdef linux
#include <semaphore.h>
#elif __APPLE__
#include "zemaphore.h"
#endif
typedef struct {
int num_loops;
int thread_id;

View File

@@ -5,6 +5,12 @@
#include "common.h"
#include "common_threads.h"
#ifdef linux
#include <semaphore.h>
#elif __APPLE__
#include "zemaphore.h"
#endif
typedef struct {
int num_loops;
int thread_id;

View File

@@ -6,10 +6,16 @@
#include "common.h"
#include "common_threads.h"
#ifdef linux
#include <semaphore.h>
#elif __APPLE__
#include "zemaphore.h"
#endif
sem_t s;
void *child(void *arg) {
sleep(4);
sleep(2);
printf("child\n");
Sem_post(&s); // signal here: child is done
return NULL;

View File

@@ -3,11 +3,16 @@
#include <assert.h>
#include <pthread.h>
#include <stdlib.h>
#include <semaphore.h>
#include "common.h"
#include "common_threads.h"
#ifdef linux
#include <semaphore.h>
#elif __APPLE__
#include "zemaphore.h"
#endif
int max;
int loops;
int *buffer;

View File

@@ -1,12 +1,17 @@
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#include "common.h"
#include "common_threads.h"
#ifdef linux
#include <semaphore.h>
#elif __APPLE__
#include "zemaphore.h"
#endif
typedef struct _rwlock_t {
sem_t writelock;
sem_t lock;

48
threads-sema/throttle.c Normal file
View File

@@ -0,0 +1,48 @@
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include "common.h"
#include "common_threads.h"
#ifdef linux
#include <semaphore.h>
#elif __APPLE__
#include "zemaphore.h"
#endif
sem_t s;
void *child(void *arg) {
Sem_wait(&s);
printf("child %lld\n", (long long int) arg);
sleep(1);
Sem_post(&s);
return NULL;
}
int main(int argc, char *argv[]) {
if (argc != 3) {
fprintf(stderr, "usage: throttle <num_threads> <sem_value>\n");
exit(1);
}
int num_threads = atoi(argv[1]);
int sem_value = atoi(argv[2]);
Sem_init(&s, sem_value);
printf("parent: begin\n");
pthread_t c[num_threads];
int i;
for (i = 0; i < num_threads; i++)
Pthread_create(&c[i], NULL, child, (void *) (long long int)i);
for (i = 0; i < num_threads; i++)
Pthread_join(c[i], NULL);
printf("parent: end\n");
return 0;
}

View File

@@ -5,34 +5,7 @@
#include "common.h"
#include "common_threads.h"
typedef struct __Zem_t {
int value;
pthread_cond_t cond;
pthread_mutex_t lock;
} Zem_t;
void Zem_init(Zem_t *z, int value) {
z->value = value;
Cond_init(&z->cond);
Mutex_init(&z->lock);
}
void Zem_wait(Zem_t *z) {
Mutex_lock(&z->lock);
while (z->value <= 0)
Cond_wait(&z->cond, &z->lock);
z->value--;
Mutex_unlock(&z->lock);
}
void Zem_post(Zem_t *z) {
Mutex_lock(&z->lock);
z->value++;
Cond_signal(&z->cond);
Mutex_unlock(&z->lock);
}
#include "zemaphore.h"
Zem_t s;

37
threads-sema/zemaphore.h Normal file
View File

@@ -0,0 +1,37 @@
#ifndef __zemaphore_h__
#define __zemaphore_h__
typedef struct __Zem_t {
int value;
pthread_cond_t cond;
pthread_mutex_t lock;
} Zem_t;
void Zem_init(Zem_t *z, int value) {
z->value = value;
Cond_init(&z->cond);
Mutex_init(&z->lock);
}
void Zem_wait(Zem_t *z) {
Mutex_lock(&z->lock);
while (z->value <= 0)
Cond_wait(&z->cond, &z->lock);
z->value--;
Mutex_unlock(&z->lock);
}
void Zem_post(Zem_t *z) {
Mutex_lock(&z->lock);
z->value++;
Cond_signal(&z->cond);
Mutex_unlock(&z->lock);
}
typedef Zem_t sem_t;
#define Sem_wait(s) Zem_wait(s)
#define Sem_post(s) Zem_post(s)
#define Sem_init(s, v) Zem_init(s, v)
#endif // __zemaphore_h__