Skip to content
Snippets Groups Projects
Commit c95ff500 authored by atheesha.vigneswa's avatar atheesha.vigneswa
Browse files

first step

parent e039fe16
Branches
No related tags found
No related merge requests found
{
"configurations": [
{
"name": "linux-gcc-x64",
"includePath": [
"${workspaceFolder}/**"
],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++11",
"intelliSenseMode": "linux-gcc-x64",
"compilerArgs": [
""
]
}
],
"version": 4
}
\ No newline at end of file
{
"version": "0.2.0",
"configurations": [
{
"name": "C/C++ Runner: Debug Session",
"type": "cppdbg",
"request": "launch",
"args": [],
"stopAtEntry": false,
"externalConsole": false,
"cwd": "/home/atheeshan/Documents/S6/ProgConcu2/tp-prog-conc-2-bykes",
"program": "/home/atheeshan/Documents/S6/ProgConcu2/tp-prog-conc-2-bykes/build/Debug/outDebug",
"MIMode": "gdb",
"miDebuggerPath": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
\ No newline at end of file
{
"C_Cpp_Runner.cCompilerPath": "gcc",
"C_Cpp_Runner.cppCompilerPath": "g++",
"C_Cpp_Runner.debuggerPath": "gdb",
"C_Cpp_Runner.cStandard": "c11",
"C_Cpp_Runner.cppStandard": "c++11",
"C_Cpp_Runner.msvcBatchPath": "",
"C_Cpp_Runner.useMsvc": false,
"C_Cpp_Runner.warnings": [
"-Wall",
"-Wextra",
"-Wpedantic",
"-Wshadow",
"-Wformat=2",
"-Wcast-align",
"-Wconversion",
"-Wsign-conversion",
"-Wnull-dereference"
],
"C_Cpp_Runner.msvcWarnings": [
"/W4",
"/permissive-",
"/w14242",
"/w14287",
"/w14296",
"/w14311",
"/w14826",
"/w44062",
"/w44242",
"/w14905",
"/w14906",
"/w14263",
"/w44265",
"/w14928"
],
"C_Cpp_Runner.enableWarnings": true,
"C_Cpp_Runner.warningsAsError": false,
"C_Cpp_Runner.compilerArgs": [],
"C_Cpp_Runner.linkerArgs": [],
"C_Cpp_Runner.includePaths": [],
"C_Cpp_Runner.includeSearch": [
"*",
"**/*"
],
"C_Cpp_Runner.excludeSearch": [
"**/build",
"**/build/**",
"**/.*",
"**/.*/**",
"**/.vscode",
"**/.vscode/**"
],
"C_Cpp_Runner.useAddressSanitizer": false,
"C_Cpp_Runner.useUndefinedSanitizer": false,
"C_Cpp_Runner.useLeakSanitizer": false,
"C_Cpp_Runner.showCompilationTime": false,
"C_Cpp_Runner.useLinkTimeOptimization": false,
"C_Cpp_Runner.msvcSecureNoWarnings": false
}
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <semaphore.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#include <time.h>
#define NB_SITES 10
#define NB_BORNES 10
// Constants
#define MAX_SITES 20
#define MAX_HABITANTS 200
#define MAX_CAMION 4
#define NB_HABITANT 100 // sera le nb threads
#define NB_TRAJET 10
// Structures
typedef struct {
int nb_velos;
int nb_bornes;
int personnes_bloquees;
pthread_mutex_t mutex;
sem_t sem_velos;
sem_t sem_places;
} Site;
// chaque site a NB_BORNES - 2 velos au debut
// boucle M (nb trajet)
// Global Variables
Site sites[MAX_SITES];
int S, H, M, N;
int depot = 0;
int camion = 2;
pthread_mutex_t mutex_depot = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex_camion = PTHREAD_MUTEX_INITIALIZER;
volatile int habitants_termines = 0;
pthread_mutex_t mutex_habitants_termines = PTHREAD_MUTEX_INITIALIZER;
// random delay for each transaction 1000-1999 microseconds
// Habitant behavior
void* thread_habitant(void* arg) {
int id = *(int*)arg;
int site = rand() % S;
// tournée camion, distribution vélos sur la carte
// routine camion
// délai chargement 100-199 microsecondes
// 2-4 velos
for (int i = 0; i < M; i++) {
// Get a bike
sem_wait(&sites[site].sem_velos);
pthread_mutex_lock(&sites[site].mutex);
sites[site].nb_velos--;
printf("(GET) person %d starts from terminal %d (%d bykes)\n", id, site, sites[site].nb_velos);
pthread_mutex_unlock(&sites[site].mutex);
// #ifdef DISABLE_SLEEP
// #define uspleep(x) // does nothing when disabled
// #else
// #define uspleep(x) usleep(x) // normal delay otherwise
// #endif
// Travel time
usleep(1000 + rand() % 1000);
typedef struct _site_t
{
int habitant;
int velo;
// Choose a different destination
int dest;
do { dest = rand() % S; } while (dest == site);
} site_t;
// Wait for a free slot at destination
sem_wait(&sites[dest].sem_places);
pthread_mutex_lock(&sites[dest].mutex);
sites[dest].nb_velos++;
printf("(PUT) person %d arrives at terminal %d (%d bykes)\n", id, dest, sites[dest].nb_velos);
pthread_mutex_unlock(&sites[dest].mutex);
int routine_habitant(){
// Activity time
usleep(1000 + rand() % 1000);
site = dest;
}
return 0;
// End
pthread_mutex_lock(&mutex_habitants_termines);
habitants_termines++;
pthread_mutex_unlock(&mutex_habitants_termines);
printf("person %d stops\n", id);
return NULL;
}
int routine_camion(){
return 0;
// Maintenance team behavior
void* thread_maintenance(void* arg) {
while (1) {
pthread_mutex_lock(&mutex_habitants_termines);
if (habitants_termines == H) {
pthread_mutex_unlock(&mutex_habitants_termines);
break;
}
pthread_mutex_unlock(&mutex_habitants_termines);
for (int i = 0; i < S; i++) {
pthread_mutex_lock(&sites[i].mutex);
// Remove bikes if too many
while (sites[i].nb_velos > sites[i].nb_bornes - 2 && camion < MAX_CAMION) {
sites[i].nb_velos--;
camion++;
sem_post(&sites[i].sem_places);
printf("Truck removes a bike at terminal %d (bikes: %d)\n", i, sites[i].nb_velos);
}
// Add bikes if too few
while (sites[i].nb_velos < 2 && camion > 0) {
sites[i].nb_velos++;
camion--;
sem_post(&sites[i].sem_velos);
printf("Truck adds a bike at terminal %d (bikes: %d)\n", i, sites[i].nb_velos);
}
pthread_mutex_unlock(&sites[i].mutex);
}
// Manage depot to balance truck load
pthread_mutex_lock(&mutex_depot);
if (camion > 2) {
depot += camion - 2;
camion = 2;
} else if (camion < 2 && depot > 0) {
int to_add = (depot < 2 - camion) ? depot : (2 - camion);
camion += to_add;
depot -= to_add;
}
pthread_mutex_unlock(&mutex_depot);
usleep(100 + rand() % 100);
}
printf("***************** CYCLING TERMINATED ***************\n");
return NULL;
}
int main(){
int camion = 2;
site_t sites[NB_SITES];
for (int i = 0; i < NB_SITES; i++)
{
sites[i].habitant = 0;
sites[i].velo = NB_BORNES - 2;
int main(int argc, char* argv[]) {
if (argc != 5) {
printf("Usage: %s <S> <H> <M> <N>\n", argv[0]);
return EXIT_FAILURE;
}
for (int i = 0; i < NB_HABITANT; i++)
{
int random_site = rand() % NB_SITES;
sites[random_site].habitant++;
// Read arguments
S = atoi(argv[1]);
H = atoi(argv[2]);
M = atoi(argv[3]);
N = atoi(argv[4]);
if (S <= 2 || H <= 2 || M <= 0 || N <= 4) {
printf("Invalid parameters. Ensure S>2, H>2, M>0, N>4\n");
return EXIT_FAILURE;
}
// test if 100 habitants
int test = 0;
for (int i = 0; i < NB_SITES; i++)
{
test += sites[i].habitant;
srand(time(NULL));
// Initialize sites
for (int i = 0; i < S; i++) {
sites[i].nb_bornes = N;
sites[i].nb_velos = N - 2;
pthread_mutex_init(&sites[i].mutex, NULL);
sem_init(&sites[i].sem_velos, 0, sites[i].nb_velos);
sem_init(&sites[i].sem_places, 0, N - sites[i].nb_velos);
}
pthread_t habitants[H], maintenance;
int ids[H];
// Start maintenance thread
pthread_create(&maintenance, NULL, thread_maintenance, NULL);
// Start habitants
for (int i = 0; i < H; i++) {
ids[i] = i;
pthread_create(&habitants[i], NULL, thread_habitant, &ids[i]);
}
// Wait for all habitants
for (int i = 0; i < H; i++) pthread_join(habitants[i], NULL);
// Wait for maintenance
pthread_join(maintenance, NULL);
// Final count
int total = depot + camion;
for (int i = 0; i < S; i++) total += sites[i].nb_velos;
printf("\n------ FINAL STATE ------\n");
for (int i = 0; i < S; i++) {
printf("Terminal %d contains %d bykes\n", i, sites[i].nb_velos);
}
printf("test: %d\n", test);
printf("Depot: %d bikes\nTruck: %d bikes\nTotal bikes: %d (expected %d)\n",
depot, camion, total, S * (N - 2) + 2);
return 0;
return EXIT_SUCCESS;
}
tp3_bykes 0 → 100755
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment