Ajout Résolution Anale 1

This commit is contained in:
2025-08-08 08:17:23 +00:00
parent 1c993ece2d
commit 1bb411b001
17 changed files with 212 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
CFLAGS=-Wall -Wextra -g
OBJS=p1.o p2.o p3.o dijkstra.o launcher.o
all: p1 p2 p3 launcher
p1: p1.o dijkstra.o
p2: p2.o dijkstra.o
p3: p3.o dijkstra.o
launcher: launcher.o dijkstra.o
p1.o: p1.c dijkstra.h commun.h
p2.o: p2.c dijkstra.h commun.h
p3.o: p3.c dijkstra.h commun.h
launcher.o: launcher.c dijkstra.h commun.h
dijkstra.o: dijkstra.c dijkstra.h
.PHONY: clean
clean:
rm $(OBJS) p1 p2 p3 launcher

View File

@@ -0,0 +1,8 @@
#ifndef COMMUN_H
#define COMMUN_H
#define CLE_SEM_T1 10
#define CLE_SEM_T2 20
#define CLE_SEM_T3 30
#endif

View File

@@ -0,0 +1,57 @@
#include <sys/sem.h>
#include <stdlib.h>
#include <stdio.h>
#include "dijkstra.h"
int sem_create(key_t cle, int initval) {
int semid;
union semun { int val; struct semid_ds *buf; ushort *array; } arg_ctl;
semid = semget(cle, 1 , IPC_CREAT|IPC_EXCL|0666);
if (semid == -1)
{
semid = semget(cle,1, 0666);
if (semid == -1)
{
perror("Erreur semget()");
exit(1);
}
}
else
{
arg_ctl.val = initval;
if (semctl(semid, 0, SETVAL, arg_ctl) == -1)
{
perror("Erreur initialisation sémaphore");
exit(1);
}
}
return(semid) ;
}
void P(int semid)
{
struct sembuf sempar;
sempar.sem_num = 0;
sempar.sem_op = -1;
sempar.sem_flg = 0;
if (semop(semid, &sempar, 1) == -1)
perror("Erreur operation P");
}
void V(int semid)
{
struct sembuf sempar;
sempar.sem_num = 0;
sempar.sem_op = 1;
sempar.sem_flg = 0;
if (semop(semid, &sempar, 1) == -1)
perror("Erreur opération V");
}
void sem_delete(int semid)
{
if (semctl(semid,0,IPC_RMID,0) == -1)
perror("Erreur dans destruction sémaphore");
}

View File

@@ -0,0 +1,27 @@
#ifndef DIJKSTRA_H
#define DIJKSTRA_H
#include <sys/sem.h>
/**
* return the semaphore associated to the key cle
* if the semaphore doesn't already exist the function create it with initval token then return it
*/
int sem_create(key_t cle, int initval);
/**
* take a token on the semaphore semid or wait if none available
*/
void P(int semid);
/**
* put a token on the semaphore semid
*/
void V(int semid);
/**
* delete the semaphore semid
*/
void sem_delete(int semid);
#endif

Binary file not shown.

BIN
ANALE-1/RESOLUTION/launcher Executable file

Binary file not shown.

View File

@@ -0,0 +1,27 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/shm.h>
#include <sys/wait.h>
#include <string.h>
#include "dijkstra.h"
#include "commun.h"
int main () {
if (fork() == 0)
{
execlp("./p1","./p2",NULL);
}
if (fork() == 0)
{
execlp("./p2","./p2",NULL);
}
if (fork() == 0)
{
execlp("./p3","./p3",NULL);
}
wait(NULL);
wait(NULL);
wait(NULL);
return 0;
}

Binary file not shown.

BIN
ANALE-1/RESOLUTION/p1 Executable file

Binary file not shown.

30
ANALE-1/RESOLUTION/p1.c Normal file
View File

@@ -0,0 +1,30 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/shm.h>
#include "dijkstra.h"
#include "commun.h"
void t1()
{
puts("t1");
sleep(10);
puts("end t1");
}
void t2()
{
puts("t2");
sleep(2);
puts("end t2");
}
int main () {
int sem_t1 = sem_create (CLE_SEM_T1,0);
int sem_t2 = sem_create (CLE_SEM_T2,0);
t1();
V(sem_t1);
t2();
V(sem_t2);
return 0;
}

BIN
ANALE-1/RESOLUTION/p1.o Normal file

Binary file not shown.

BIN
ANALE-1/RESOLUTION/p2 Executable file

Binary file not shown.

22
ANALE-1/RESOLUTION/p2.c Normal file
View File

@@ -0,0 +1,22 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/shm.h>
#include "dijkstra.h"
#include "commun.h"
void t3()
{
puts("t3");
sleep(7);
puts("end t3");
}
int main () {
int sem_t1 = sem_create (CLE_SEM_T1,0);
int sem_t3 = sem_create (CLE_SEM_T3,0);
P(sem_t1);
t3();
V(sem_t3);
return 0;
}

BIN
ANALE-1/RESOLUTION/p2.o Normal file

Binary file not shown.

BIN
ANALE-1/RESOLUTION/p3 Executable file

Binary file not shown.

22
ANALE-1/RESOLUTION/p3.c Normal file
View File

@@ -0,0 +1,22 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/shm.h>
#include "dijkstra.h"
#include "commun.h"
void t4()
{
puts("t4");
sleep(5);
puts("end t4");
}
int main () {
int sem_t2 = sem_create (CLE_SEM_T2,0);
int sem_t3 = sem_create (CLE_SEM_T3,0);
P(sem_t2);
P(sem_t3);
t4();
return 0;
}

BIN
ANALE-1/RESOLUTION/p3.o Normal file

Binary file not shown.