made it so that zemaphore is used on mac, semaphore on linux, and other clean up
This commit is contained in:
@@ -1,5 +1,11 @@
|
|||||||
CC := gcc
|
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 \
|
SRCS := dining_philosophers_deadlock.c \
|
||||||
dining_philosophers_deadlock_print.c \
|
dining_philosophers_deadlock_print.c \
|
||||||
@@ -9,7 +15,8 @@ SRCS := dining_philosophers_deadlock.c \
|
|||||||
binary.c \
|
binary.c \
|
||||||
producer_consumer_works.c \
|
producer_consumer_works.c \
|
||||||
rwlock.c \
|
rwlock.c \
|
||||||
zemaphore.c
|
zemaphore.c \
|
||||||
|
throttle.c
|
||||||
|
|
||||||
OBJS := ${SRCS:c=o}
|
OBJS := ${SRCS:c=o}
|
||||||
PROGS := ${SRCS:.c=}
|
PROGS := ${SRCS:.c=}
|
||||||
@@ -18,7 +25,7 @@ PROGS := ${SRCS:.c=}
|
|||||||
all: ${PROGS}
|
all: ${PROGS}
|
||||||
|
|
||||||
${PROGS} : % : %.o Makefile
|
${PROGS} : % : %.o Makefile
|
||||||
${CC} $< -o $@ -pthread
|
${CC} $< -o $@ ${LIBS}
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f ${PROGS} ${OBJS}
|
rm -f ${PROGS} ${OBJS}
|
||||||
|
|||||||
@@ -6,6 +6,12 @@
|
|||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "common_threads.h"
|
#include "common_threads.h"
|
||||||
|
|
||||||
|
#ifdef linux
|
||||||
|
#include <semaphore.h>
|
||||||
|
#elif __APPLE__
|
||||||
|
#include "zemaphore.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
sem_t mutex;
|
sem_t mutex;
|
||||||
volatile int counter = 0;
|
volatile int counter = 0;
|
||||||
|
|
||||||
@@ -26,7 +32,7 @@ int main(int argc, char *argv[]) {
|
|||||||
Pthread_create(&c2, NULL, child, NULL);
|
Pthread_create(&c2, NULL, child, NULL);
|
||||||
Pthread_join(c1, NULL);
|
Pthread_join(c1, NULL);
|
||||||
Pthread_join(c2, NULL);
|
Pthread_join(c2, NULL);
|
||||||
printf("result: %d\n", counter);
|
printf("result: %d (should be 20000000)\n", counter);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,12 @@
|
|||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "common_threads.h"
|
#include "common_threads.h"
|
||||||
|
|
||||||
|
#ifdef linux
|
||||||
|
#include <semaphore.h>
|
||||||
|
#elif __APPLE__
|
||||||
|
#include "zemaphore.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int num_loops;
|
int num_loops;
|
||||||
int thread_id;
|
int thread_id;
|
||||||
|
|||||||
@@ -5,6 +5,12 @@
|
|||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "common_threads.h"
|
#include "common_threads.h"
|
||||||
|
|
||||||
|
#ifdef linux
|
||||||
|
#include <semaphore.h>
|
||||||
|
#elif __APPLE__
|
||||||
|
#include "zemaphore.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int num_loops;
|
int num_loops;
|
||||||
int thread_id;
|
int thread_id;
|
||||||
|
|||||||
@@ -5,6 +5,12 @@
|
|||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "common_threads.h"
|
#include "common_threads.h"
|
||||||
|
|
||||||
|
#ifdef linux
|
||||||
|
#include <semaphore.h>
|
||||||
|
#elif __APPLE__
|
||||||
|
#include "zemaphore.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int num_loops;
|
int num_loops;
|
||||||
int thread_id;
|
int thread_id;
|
||||||
|
|||||||
@@ -5,6 +5,12 @@
|
|||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "common_threads.h"
|
#include "common_threads.h"
|
||||||
|
|
||||||
|
#ifdef linux
|
||||||
|
#include <semaphore.h>
|
||||||
|
#elif __APPLE__
|
||||||
|
#include "zemaphore.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int num_loops;
|
int num_loops;
|
||||||
int thread_id;
|
int thread_id;
|
||||||
|
|||||||
@@ -6,10 +6,16 @@
|
|||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "common_threads.h"
|
#include "common_threads.h"
|
||||||
|
|
||||||
|
#ifdef linux
|
||||||
|
#include <semaphore.h>
|
||||||
|
#elif __APPLE__
|
||||||
|
#include "zemaphore.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
sem_t s;
|
sem_t s;
|
||||||
|
|
||||||
void *child(void *arg) {
|
void *child(void *arg) {
|
||||||
sleep(4);
|
sleep(2);
|
||||||
printf("child\n");
|
printf("child\n");
|
||||||
Sem_post(&s); // signal here: child is done
|
Sem_post(&s); // signal here: child is done
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|||||||
@@ -3,11 +3,16 @@
|
|||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <semaphore.h>
|
|
||||||
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "common_threads.h"
|
#include "common_threads.h"
|
||||||
|
|
||||||
|
#ifdef linux
|
||||||
|
#include <semaphore.h>
|
||||||
|
#elif __APPLE__
|
||||||
|
#include "zemaphore.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
int max;
|
int max;
|
||||||
int loops;
|
int loops;
|
||||||
int *buffer;
|
int *buffer;
|
||||||
|
|||||||
@@ -1,12 +1,17 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <semaphore.h>
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "common_threads.h"
|
#include "common_threads.h"
|
||||||
|
|
||||||
|
#ifdef linux
|
||||||
|
#include <semaphore.h>
|
||||||
|
#elif __APPLE__
|
||||||
|
#include "zemaphore.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
typedef struct _rwlock_t {
|
typedef struct _rwlock_t {
|
||||||
sem_t writelock;
|
sem_t writelock;
|
||||||
sem_t lock;
|
sem_t lock;
|
||||||
|
|||||||
48
threads-sema/throttle.c
Normal file
48
threads-sema/throttle.c
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
@@ -5,34 +5,7 @@
|
|||||||
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "common_threads.h"
|
#include "common_threads.h"
|
||||||
|
#include "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);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Zem_t s;
|
Zem_t s;
|
||||||
|
|
||||||
|
|||||||
37
threads-sema/zemaphore.h
Normal file
37
threads-sema/zemaphore.h
Normal 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__
|
||||||
Reference in New Issue
Block a user