chap4
This commit is contained in:
		
							
								
								
									
										27
									
								
								bac2/os/chap4/Makefile
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								bac2/os/chap4/Makefile
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,27 @@
 | 
			
		||||
.PHONY: clean, mrproper, run
 | 
			
		||||
CC = gcc
 | 
			
		||||
CFLAGS = -g -Wall
 | 
			
		||||
 | 
			
		||||
exos = ex4 group
 | 
			
		||||
 | 
			
		||||
all: $(exos)
 | 
			
		||||
 | 
			
		||||
%.o: %.c
 | 
			
		||||
	$(CC) $(CFLAGS) -c -o $@ $<
 | 
			
		||||
 | 
			
		||||
ex1: ex1.o RingBuffer.o
 | 
			
		||||
	$(CC) $(CFLAGS) -o $@ $+
 | 
			
		||||
 | 
			
		||||
ex%: ex%.o
 | 
			
		||||
	$(CC) $(CFLAGS) -o $@ $+
 | 
			
		||||
 | 
			
		||||
group: group.o
 | 
			
		||||
	$(CC) $(CFLAGS) -o $@ $+
 | 
			
		||||
 | 
			
		||||
clean:
 | 
			
		||||
	rm -f *.o core.*
 | 
			
		||||
	rm -f out group 
 | 
			
		||||
	rm -f ${exos}
 | 
			
		||||
 | 
			
		||||
run: group 
 | 
			
		||||
	./$<
 | 
			
		||||
							
								
								
									
										20
									
								
								bac2/os/chap4/RingBuffer.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								bac2/os/chap4/RingBuffer.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,20 @@
 | 
			
		||||
#include "RingBuffer.h"
 | 
			
		||||
 | 
			
		||||
void rb_push(rb_t* rb, int el){
 | 
			
		||||
	if(rb->filled)
 | 
			
		||||
		return;
 | 
			
		||||
	rb->data[rb->w_pos] = el; 
 | 
			
		||||
	rb->w_pos = (rb->w_pos + 1) % rb->size;
 | 
			
		||||
	if(rb->w_pos == rb->r_pos)
 | 
			
		||||
		rb->filled = true;
 | 
			
		||||
}
 | 
			
		||||
int rb_pop(rb_t* rb){
 | 
			
		||||
	if(rb->w_pos == rb->r_pos && !rb->filled){
 | 
			
		||||
		rb->error = true;
 | 
			
		||||
		return 0;
 | 
			
		||||
	}
 | 
			
		||||
	int ret = rb->data[rb->r_pos];
 | 
			
		||||
	rb->r_pos = (rb->r_pos + 1) % rb->size;
 | 
			
		||||
	rb->filled = false;
 | 
			
		||||
	return ret;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										27
									
								
								bac2/os/chap4/RingBuffer.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								bac2/os/chap4/RingBuffer.h
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,27 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
#include <pthread.h>
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#include <stdbool.h>
 | 
			
		||||
#include <stdarg.h>
 | 
			
		||||
 | 
			
		||||
#define rb_init(size, type)\
 | 
			
		||||
	{(size),\
 | 
			
		||||
	0, 0, false, false,\
 | 
			
		||||
	type,\
 | 
			
		||||
	malloc(sizeof(int)*(size)),\
 | 
			
		||||
	PTHREAD_MUTEX_INITIALIZER};
 | 
			
		||||
 | 
			
		||||
#define rb_free(rb) free(rb.data)
 | 
			
		||||
 | 
			
		||||
typedef struct rb{
 | 
			
		||||
	size_t size;
 | 
			
		||||
	size_t w_pos, r_pos;
 | 
			
		||||
	bool filled;
 | 
			
		||||
	bool error;
 | 
			
		||||
	enum {DROP, WAIT} type;
 | 
			
		||||
	int* data;
 | 
			
		||||
	pthread_mutex_t m;
 | 
			
		||||
} rb_t;
 | 
			
		||||
 | 
			
		||||
void rb_push(rb_t*, int);
 | 
			
		||||
int rb_pop(rb_t*);
 | 
			
		||||
							
								
								
									
										46
									
								
								bac2/os/chap4/ex1.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								bac2/os/chap4/ex1.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,46 @@
 | 
			
		||||
#include "RingBuffer.h"
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <pthread.h>
 | 
			
		||||
#include <time.h>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
 | 
			
		||||
#define THREADS 10
 | 
			
		||||
 | 
			
		||||
void* process(void* args);
 | 
			
		||||
 | 
			
		||||
int main(void)
 | 
			
		||||
{
 | 
			
		||||
	srand(time(NULL));
 | 
			
		||||
	rb_t rb = rb_init(50, DROP);
 | 
			
		||||
	pthread_t threads[THREADS];
 | 
			
		||||
 | 
			
		||||
	int even, odd = {0};
 | 
			
		||||
 | 
			
		||||
	for (int i = 0; i < THREADS; ++i) {
 | 
			
		||||
		pthread_create(threads+i, NULL, process, (void*) &rb);
 | 
			
		||||
	}
 | 
			
		||||
	int val;
 | 
			
		||||
	while(true){
 | 
			
		||||
		usleep(250);
 | 
			
		||||
		val = rb_pop(&rb);
 | 
			
		||||
		printf("got -> %d\n", val);
 | 
			
		||||
 | 
			
		||||
		if(val % 2)
 | 
			
		||||
			odd++;
 | 
			
		||||
		else
 | 
			
		||||
			even++;
 | 
			
		||||
		
 | 
			
		||||
		printf("%d nombres pairs et %d nombre impairs\n", even, odd);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	rb_free(rb);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void* process(void* args){
 | 
			
		||||
	rb_t *rb = (rb_t*) args;
 | 
			
		||||
 | 
			
		||||
	while(true){
 | 
			
		||||
		usleep((rand() % 4500) + 500);
 | 
			
		||||
		rb_push(rb, rand());
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										46
									
								
								bac2/os/chap4/ex2.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								bac2/os/chap4/ex2.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,46 @@
 | 
			
		||||
#include "RingBuffer.h"
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <pthread.h>
 | 
			
		||||
#include <time.h>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
 | 
			
		||||
#define THREADS 10
 | 
			
		||||
 | 
			
		||||
void* process(void* args);
 | 
			
		||||
 | 
			
		||||
int main(void)
 | 
			
		||||
{
 | 
			
		||||
	srand(time(NULL));
 | 
			
		||||
	rb_t rb = rb_init(50, WAIT);
 | 
			
		||||
	pthread_t threads[THREADS];
 | 
			
		||||
 | 
			
		||||
	int even, odd = {0};
 | 
			
		||||
 | 
			
		||||
	for (int i = 0; i < THREADS; ++i) {
 | 
			
		||||
		pthread_create(threads+i, NULL, process, (void*) &rb);
 | 
			
		||||
	}
 | 
			
		||||
	int val;
 | 
			
		||||
	while(true){
 | 
			
		||||
		usleep(250);
 | 
			
		||||
		val = rb_pop(&rb);
 | 
			
		||||
		printf("got -> %d\n", val);
 | 
			
		||||
 | 
			
		||||
		if(val % 2)
 | 
			
		||||
			odd++;
 | 
			
		||||
		else
 | 
			
		||||
			even++;
 | 
			
		||||
		
 | 
			
		||||
		printf("%d nombres pairs et %d nombre impairs\n", even, odd);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	rb_free(rb);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void* process(void* args){
 | 
			
		||||
	rb_t *rb = (rb_t*) args;
 | 
			
		||||
 | 
			
		||||
	while(true){
 | 
			
		||||
		usleep((rand() % 4500) + 500);
 | 
			
		||||
		rb_push(rb, rand());
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										34
									
								
								bac2/os/chap4/ex4.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								bac2/os/chap4/ex4.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,34 @@
 | 
			
		||||
#include <pthread.h>
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
 | 
			
		||||
#define SIZE 32768
 | 
			
		||||
 | 
			
		||||
pthread_t child_thread;
 | 
			
		||||
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
 | 
			
		||||
 | 
			
		||||
int table[SIZE];
 | 
			
		||||
 | 
			
		||||
int cmp(const void* a1, const void* a2){
 | 
			
		||||
	return *((int*)a2) - *((int*)a1);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void* child(void* args){
 | 
			
		||||
	pthread_mutex_lock(&mutex);
 | 
			
		||||
	for (int i = 0; i < SIZE; ++i) {
 | 
			
		||||
		table[i] = rand();
 | 
			
		||||
	}
 | 
			
		||||
	pthread_mutex_unlock(&mutex);
 | 
			
		||||
	sleep(1);
 | 
			
		||||
	pthread_mutex_lock(&mutex);
 | 
			
		||||
	return NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int main(void)
 | 
			
		||||
{
 | 
			
		||||
	pthread_create(&child_thread, NULL, child, NULL);
 | 
			
		||||
 | 
			
		||||
	pthread_mutex_lock(&mutex);
 | 
			
		||||
	qsort(table, SIZE, sizeof(int), cmp);
 | 
			
		||||
	pthread_mutex_unlock(&mutex);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										45
									
								
								bac2/os/chap4/group.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								bac2/os/chap4/group.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,45 @@
 | 
			
		||||
/***************************************************************************
 | 
			
		||||
*  Le dinner des philosophes 											   * 
 | 
			
		||||
*  Two phase locking: 													   *
 | 
			
		||||
*  - array de baguettes													   * 
 | 
			
		||||
*  - manger(): tente l'acquisition des deux. attente passive si non dispo  *
 | 
			
		||||
****************************************************************************/
 | 
			
		||||
#include <pthread.h>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#define TABLE_SIZE 5
 | 
			
		||||
 | 
			
		||||
#define RIGHT(x) ((x)) % TABLE_SIZE
 | 
			
		||||
#define LEFT(x) ((x)+1) % TABLE_SIZE
 | 
			
		||||
 | 
			
		||||
typedef unsigned int PHILOSOPHE; // philosophe's id in the table
 | 
			
		||||
 | 
			
		||||
pthread_mutex_t baguettes[TABLE_SIZE], tableLock;
 | 
			
		||||
int main(void)
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void penser(){
 | 
			
		||||
	usleep(100 + rand() % 500);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void manger(){
 | 
			
		||||
	usleep(100 + rand() % 500);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void prendre(){
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
void deposer(){
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void* thread_philosophe(void* arg){
 | 
			
		||||
	while(42){
 | 
			
		||||
		penser();
 | 
			
		||||
		prendre();
 | 
			
		||||
		manger();
 | 
			
		||||
		deposer();
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										11
									
								
								bac2/os/chap4/philosophes.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								bac2/os/chap4/philosophes.h
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,11 @@
 | 
			
		||||
#ifndef PHILOSOPHES_H
 | 
			
		||||
#define PHILOSOPHES_H
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#ifndef PHILOSPHES_IMPLEMENTATION
 | 
			
		||||
 | 
			
		||||
#endif /* end of implementation */
 | 
			
		||||
 | 
			
		||||
#endif /* end of include guard: PHILOSOPHES_H */
 | 
			
		||||
		Reference in New Issue
	
	Block a user