Ajout Exo1
This commit is contained in:
19
ANALE-1/EXO2/SUJET/Makefile
Normal file
19
ANALE-1/EXO2/SUJET/Makefile
Normal 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
|
||||
|
||||
4
ANALE-1/EXO2/SUJET/commun.h
Normal file
4
ANALE-1/EXO2/SUJET/commun.h
Normal file
@@ -0,0 +1,4 @@
|
||||
#ifndef COMMUN_H
|
||||
#define COMMUN_H
|
||||
|
||||
#endif
|
||||
57
ANALE-1/EXO2/SUJET/dijkstra.c
Normal file
57
ANALE-1/EXO2/SUJET/dijkstra.c
Normal 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");
|
||||
}
|
||||
27
ANALE-1/EXO2/SUJET/dijkstra.h
Normal file
27
ANALE-1/EXO2/SUJET/dijkstra.h
Normal 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
|
||||
27
ANALE-1/EXO2/SUJET/launcher.c
Normal file
27
ANALE-1/EXO2/SUJET/launcher.c
Normal 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;
|
||||
}
|
||||
26
ANALE-1/EXO2/SUJET/p1.c
Normal file
26
ANALE-1/EXO2/SUJET/p1.c
Normal file
@@ -0,0 +1,26 @@
|
||||
#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 () {
|
||||
t1();
|
||||
t2();
|
||||
return 0;
|
||||
}
|
||||
18
ANALE-1/EXO2/SUJET/p2.c
Normal file
18
ANALE-1/EXO2/SUJET/p2.c
Normal file
@@ -0,0 +1,18 @@
|
||||
#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 () {
|
||||
t3();
|
||||
return 0;
|
||||
}
|
||||
18
ANALE-1/EXO2/SUJET/p3.c
Normal file
18
ANALE-1/EXO2/SUJET/p3.c
Normal file
@@ -0,0 +1,18 @@
|
||||
#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 () {
|
||||
t4();
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user