Ajout Exo1
This commit is contained in:
17
ANALE-1/EXO1/SUJET/Makefile
Normal file
17
ANALE-1/EXO1/SUJET/Makefile
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
CFLAGS=-Wall -Wextra -g
|
||||||
|
OBJS=prod.o conso.o dijkstra.o launcher.o
|
||||||
|
all: conso prod launcher
|
||||||
|
|
||||||
|
prod: prod.o dijkstra.o
|
||||||
|
conso: conso.o dijkstra.o
|
||||||
|
launcher: launcher.o dijkstra.o
|
||||||
|
|
||||||
|
conso.o: conso.c dijkstra.h commun.h
|
||||||
|
prod.o: prod.c dijkstra.h commun.h
|
||||||
|
launcher.o: launcher.c dijkstra.h commun.h
|
||||||
|
dijkstra.o: dijkstra.c dijkstra.h
|
||||||
|
|
||||||
|
.PHONY: clean
|
||||||
|
clean:
|
||||||
|
rm $(OBJS) conso prod launcher
|
||||||
|
|
||||||
11
ANALE-1/EXO1/SUJET/commun.h
Normal file
11
ANALE-1/EXO1/SUJET/commun.h
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
#ifndef COMMUN_H
|
||||||
|
#define COMMUN_H
|
||||||
|
|
||||||
|
#define MEM 3
|
||||||
|
#define SEM 5
|
||||||
|
|
||||||
|
#define CLE_SEM_PROD 100
|
||||||
|
#define CLE_SEM_CONSO 200
|
||||||
|
#define CLE_MEM 100
|
||||||
|
|
||||||
|
#endif
|
||||||
23
ANALE-1/EXO1/SUJET/conso.c
Normal file
23
ANALE-1/EXO1/SUJET/conso.c
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <sys/shm.h>
|
||||||
|
#include "dijkstra.h"
|
||||||
|
#include "commun.h"
|
||||||
|
|
||||||
|
int main () {
|
||||||
|
int sem_prod = sem_create (CLE_SEM_PROD ,SEM);
|
||||||
|
int sem_conso = sem_create (CLE_SEM_CONSO ,0);
|
||||||
|
int shmid = shmget(CLE_MEM,MEM*sizeof(int),0644);
|
||||||
|
int* shmem = shmat(shmid,NULL,0);
|
||||||
|
for (int i = 0; i < 26; i++)
|
||||||
|
{
|
||||||
|
P(sem_conso);
|
||||||
|
char c = shmem[i%MEM];
|
||||||
|
V(sem_prod);
|
||||||
|
printf("%c",c);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
shmdt(shmem);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
57
ANALE-1/EXO1/SUJET/dijkstra.c
Normal file
57
ANALE-1/EXO1/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/EXO1/SUJET/dijkstra.h
Normal file
27
ANALE-1/EXO1/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
|
||||||
36
ANALE-1/EXO1/SUJET/launcher.c
Normal file
36
ANALE-1/EXO1/SUJET/launcher.c
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
#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 () {
|
||||||
|
int sem_prod = sem_create (CLE_SEM_PROD ,SEM);
|
||||||
|
int sem_conso = sem_create (CLE_SEM_CONSO ,0);
|
||||||
|
int shmid = shmget(CLE_MEM,MEM*sizeof(int),IPC_CREAT|IPC_EXCL|0644);
|
||||||
|
if (shmid == -1)
|
||||||
|
shmid = shmget(CLE_MEM,MEM*sizeof(int),0644);
|
||||||
|
int* shmem = shmat(shmid,NULL,0);
|
||||||
|
memset(shmem,0,MEM*sizeof(int));
|
||||||
|
if (fork() == 0)
|
||||||
|
{
|
||||||
|
puts("consommateur");
|
||||||
|
execlp("./conso","./conso",NULL);
|
||||||
|
}
|
||||||
|
if (fork() == 0)
|
||||||
|
{
|
||||||
|
puts("producteur");
|
||||||
|
sleep(3);
|
||||||
|
execlp("./prod","./prod",NULL);
|
||||||
|
}
|
||||||
|
wait(NULL);
|
||||||
|
wait(NULL);
|
||||||
|
shmdt(shmem);
|
||||||
|
shmctl(shmid,IPC_RMID,NULL);
|
||||||
|
sem_delete(sem_prod);
|
||||||
|
sem_delete(sem_conso);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
21
ANALE-1/EXO1/SUJET/prod.c
Normal file
21
ANALE-1/EXO1/SUJET/prod.c
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <sys/shm.h>
|
||||||
|
#include "dijkstra.h"
|
||||||
|
#include "commun.h"
|
||||||
|
|
||||||
|
int main () {
|
||||||
|
int sem_prod = sem_create (CLE_SEM_PROD ,SEM);
|
||||||
|
int sem_conso = sem_create (CLE_SEM_CONSO ,0);
|
||||||
|
int shmid = shmget(CLE_MEM,MEM*sizeof(int),0644);
|
||||||
|
int* shmem = shmat(shmid,NULL,0);
|
||||||
|
for (int i = 0; i < 26; i++)
|
||||||
|
{
|
||||||
|
P(sem_prod);
|
||||||
|
shmem[i%MEM] = 'a'+i;
|
||||||
|
V(sem_conso);
|
||||||
|
}
|
||||||
|
shmdt(shmem);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user