"debut TD0"
This commit is contained in:
5
2019-2020/S3T_TP_06_IPC/.vscode/settings.json
vendored
Normal file
5
2019-2020/S3T_TP_06_IPC/.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"files.associations": {
|
||||
"unistd.h": "c"
|
||||
}
|
||||
}
|
BIN
2019-2020/S3T_TP_06_IPC/Demonstrations/demoShm
Executable file
BIN
2019-2020/S3T_TP_06_IPC/Demonstrations/demoShm
Executable file
Binary file not shown.
75
2019-2020/S3T_TP_06_IPC/Demonstrations/demo_sem.c
Normal file
75
2019-2020/S3T_TP_06_IPC/Demonstrations/demo_sem.c
Normal file
@@ -0,0 +1,75 @@
|
||||
///*****************************************************************/
|
||||
///* IUT NICE-COTE D'AZUR - Departement INFORMATIQUE - R. CHIGNOLI */
|
||||
///* Module DUT M311 Theme IPC POSIX */
|
||||
///*****************************************************************/
|
||||
/// demo_sem.c : Demonstration d'utilisation d'un sémaphore
|
||||
/// usage : commande [-creation] cle
|
||||
/// premier acces : utiliser l'option "-create"
|
||||
/// Depuis bash, utiliser la commande ls -l /dev/shm
|
||||
/// Depuis bash, utiliser la commande rm /dev/shm/*
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdio_ext.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
#include <string.h>
|
||||
#include <semaphore.h>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
char reponse;
|
||||
int mode, encore = 1;
|
||||
sem_t *sem;
|
||||
|
||||
if (argc < 3) {
|
||||
fprintf(stderr, "Usage: %s [ -create | -use ] nom_sem\n",
|
||||
argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
if ((argc == 3) && (strcmp (argv[1], "-create") == 0)) {
|
||||
mode = O_RDWR | O_CREAT | O_EXCL;
|
||||
printf ("\nMode creation\n\n");
|
||||
} else if ((argc == 3) && (strcmp (argv[1], "-use") == 0)) {
|
||||
mode = O_RDWR;
|
||||
printf ("\nMode utilisation\n\n");
|
||||
} else {
|
||||
fprintf(stderr, "Usage: %s [ -create | -use ] nom\n", argv[0]);
|
||||
exit(2);
|
||||
}
|
||||
|
||||
sem = sem_open(argv[2], mode, 0600, 1);
|
||||
if (sem == SEM_FAILED) {
|
||||
perror ("open");
|
||||
exit (3);
|
||||
}
|
||||
while (encore) {
|
||||
printf("W,P,X,Q ? ");
|
||||
reponse = getchar();
|
||||
__fpurge(stdin); ///... pour bonne gestion du buffer clavier ...
|
||||
switch (reponse) {
|
||||
case 'P':
|
||||
sem_wait (sem);
|
||||
printf("... W OK.\n");
|
||||
break;
|
||||
case 'V':
|
||||
sem_post (sem);
|
||||
printf("... P OK.\n");
|
||||
break;
|
||||
case 'X':
|
||||
sem_close (sem);
|
||||
chdir ("/dev/shm");
|
||||
sem_unlink (argv[2]);
|
||||
printf("\n... Semaphore /dev/shm/sem.%s detruit\n", argv[2]);
|
||||
encore = 0;
|
||||
break;
|
||||
case 'Q':
|
||||
encore = 0;
|
||||
break;
|
||||
default:
|
||||
printf("... Commande incorrecte\n");
|
||||
}
|
||||
}
|
||||
printf("\n... Bye\n\n");
|
||||
}
|
80
2019-2020/S3T_TP_06_IPC/Demonstrations/demo_shm.c
Normal file
80
2019-2020/S3T_TP_06_IPC/Demonstrations/demo_shm.c
Normal file
@@ -0,0 +1,80 @@
|
||||
///******************************************************************/
|
||||
///* IUT NICE-COTE D'AZUR - Departement INFORMATIQUE - R. CHIGNOLI */
|
||||
///* Module M311 Theme IPC POSIX */
|
||||
///******************************************************************/
|
||||
///* demo_shm.c : Demonstration d'utilisation d'un segment */
|
||||
///* de memoire partagee */
|
||||
///* UTILISATION : commande ( -create | -use ) */
|
||||
///* Utiliser ls, etc ... pour gerer les ressources */
|
||||
///******************************************************************/
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/time.h>
|
||||
#include <time.h>
|
||||
|
||||
/// Type de la zone mémoire
|
||||
typedef struct memoire
|
||||
{
|
||||
int valeur;
|
||||
int date;
|
||||
} Memoire;
|
||||
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
struct memoire *zone;
|
||||
int mem_fd;
|
||||
|
||||
if (argc != 2)
|
||||
{
|
||||
fprintf (stderr, "\nSyntaxe : %s ( -create | -use )\n\n", argv[0]);
|
||||
exit (1);
|
||||
}
|
||||
mem_fd = shm_open ("demo_mem", O_CREAT |O_RDWR, 0777);
|
||||
if (mem_fd == -1)
|
||||
{
|
||||
perror("shm_open");
|
||||
exit (1);
|
||||
}
|
||||
if (ftruncate(mem_fd, sizeof(struct memoire)) != 0)
|
||||
{
|
||||
perror("ftruncate");
|
||||
exit(2);
|
||||
}
|
||||
zone = mmap (NULL, 100, PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd, 0);
|
||||
if (zone == (void *) -1)
|
||||
{
|
||||
perror("mmap");
|
||||
exit (3);
|
||||
}
|
||||
if ((strcmp (argv[1], "-create")!= 0) && (strcmp (argv[1], "-use")!= 0))
|
||||
{
|
||||
fprintf (stderr, "\nSyntaxe : %s ( -create | -use )\n\n", argv[0]);
|
||||
exit (2);
|
||||
}
|
||||
if (strcmp (argv[1], "-create")== 0)
|
||||
{
|
||||
zone->valeur = 0;
|
||||
zone->date =time(NULL);
|
||||
printf("\nCREATE : VALEURS en memoire : valeur = %d - date = %s\n",
|
||||
zone->valeur, ctime ((time_t*)&zone->date));
|
||||
}
|
||||
else /// -use
|
||||
{
|
||||
printf("\nUSE : ANCIENNES VALEURS en memoire : valeur = %d - date = %s \n",
|
||||
zone->valeur, ctime ((time_t*)&zone->date));
|
||||
zone->valeur++;
|
||||
zone->date =time(NULL);
|
||||
printf("USE : NOUVELLES VALEURS en memoire : valeur = %d - date = %s \n",
|
||||
zone->valeur, ctime ((time_t*)&zone->date));
|
||||
}
|
||||
|
||||
|
||||
close(mem_fd);
|
||||
return 0;
|
||||
}
|
35
2019-2020/S3T_TP_06_IPC/Squelettes/imprimer.c
Normal file
35
2019-2020/S3T_TP_06_IPC/Squelettes/imprimer.c
Normal file
@@ -0,0 +1,35 @@
|
||||
///************************************************/
|
||||
///* imprimer : Ce programme illustre le principe */
|
||||
///* du partage de temps de LINUX ... */
|
||||
/**************************************************/
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
void imprimer (int unNombre, char unCar)
|
||||
{
|
||||
int i;
|
||||
for (i=0; i<unNombre; i++) printf ("%c", unCar);
|
||||
}
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int nb, res;
|
||||
assert (argc == 2);
|
||||
nb = atoi (argv[1]);
|
||||
res = fork();
|
||||
if (res == -1)
|
||||
{
|
||||
perror ("fork");
|
||||
exit (1);
|
||||
}
|
||||
if (res == 0)
|
||||
{
|
||||
imprimer (nb, '/');
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
imprimer (nb, '*');
|
||||
return 0;
|
||||
}
|
||||
}
|
37
2019-2020/S3T_TP_06_IPC/Squelettes/s_critique.c
Normal file
37
2019-2020/S3T_TP_06_IPC/Squelettes/s_critique.c
Normal file
@@ -0,0 +1,37 @@
|
||||
///***************************************************/
|
||||
///* s_critique : Ce programme illustre le blocage */
|
||||
///* du partage de temps de LINUX avec un semaphore. */
|
||||
///***************************************************/
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
void imprimer (int unNombre, char unCar)
|
||||
{
|
||||
int i;
|
||||
for (i=0; i<unNombre; i++) printf ("%c", unCar);
|
||||
}
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
/// FINIR ///
|
||||
|
||||
int nb, res;
|
||||
assert (argc == 2);
|
||||
nb = atoi (argv[1]);
|
||||
res = fork();
|
||||
if (res == -1)
|
||||
{
|
||||
perror ("fork");
|
||||
exit (1);
|
||||
}
|
||||
if (res == 0)
|
||||
{
|
||||
imprimer (nb, '/');
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
imprimer (nb, '*');
|
||||
return 0;
|
||||
}
|
||||
}
|
28
2019-2020/S3T_TP_06_IPC/Squelettes/shm_add.c
Normal file
28
2019-2020/S3T_TP_06_IPC/Squelettes/shm_add.c
Normal file
@@ -0,0 +1,28 @@
|
||||
///******************************************************************/
|
||||
///* IUT NICE-COTE D'AZUR - Departement INFORMATIQUE - R. CHIGNOLI */
|
||||
///* Module M311 Theme IPC POSIX */
|
||||
///******************************************************************/
|
||||
///* shm_add.c : voir enonce */
|
||||
///* UTILISATION : commande nom_segment */
|
||||
///* Utiliser ls -l /dev/shm et rm /dev/shm/... */
|
||||
///******************************************************************/
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/mman.h>
|
||||
|
||||
int main (int argc, char * argv[]) {
|
||||
int fd;
|
||||
int * compteur;
|
||||
|
||||
if (argc != 2) {
|
||||
fprintf(stderr, "Syntaxe : %s nom_segment\n", argv[0]);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
///
|
||||
/// A TERMINER ...
|
||||
///
|
||||
return EXIT_SUCCESS;
|
||||
}
|
41
2019-2020/S3T_TP_06_IPC/Squelettes/shm_create.c
Normal file
41
2019-2020/S3T_TP_06_IPC/Squelettes/shm_create.c
Normal file
@@ -0,0 +1,41 @@
|
||||
//******************************************************************/
|
||||
///* IUT NICE-COTE D'AZUR - Departement INFORMATIQUE - R. CHIGNOLI */
|
||||
///* Module M311 Theme IPC POSIX */
|
||||
///******************************************************************/
|
||||
///* shm_create.c : creation d'un nouveau segment de memoire de la */
|
||||
///* taille d'un entier et initialisation a zero */
|
||||
///* UTILISATION : commande nom_segment */
|
||||
///* Utiliser ls -l /dev/shm et rm /dev/shm/... */
|
||||
///******************************************************************/
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/mman.h>
|
||||
|
||||
int main (int argc, char * argv[])
|
||||
{
|
||||
int fd;
|
||||
int * compteur;
|
||||
|
||||
if (argc != 2) {
|
||||
fprintf(stderr, "Syntaxe : %s nom_segment\n", argv[0]);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
fd = shm_open("applejack", O_CREAT | O_RDWR, 0777);
|
||||
|
||||
if(fd == -1) {
|
||||
perror("shm_open");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
///
|
||||
/// A TERMINER
|
||||
///
|
||||
return EXIT_SUCCESS;
|
||||
}
|
28
2019-2020/S3T_TP_06_IPC/Squelettes/shm_excl.c
Normal file
28
2019-2020/S3T_TP_06_IPC/Squelettes/shm_excl.c
Normal file
@@ -0,0 +1,28 @@
|
||||
//******************************************************************/
|
||||
///* IUT NICE-COTE D'AZUR - Departement INFORMATIQUE - R. CHIGNOLI */
|
||||
///* Module M311 Theme IPC POSIX */
|
||||
///******************************************************************/
|
||||
///* shm_excl.c : voir enonce */
|
||||
///* UTILISATION : commande nom_segment */
|
||||
///* Utiliser ls -l /dev/shm et rm /dev/shm/... */
|
||||
///******************************************************************/
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/mman.h>
|
||||
|
||||
int main (int argc, char * argv[]) {
|
||||
int fd;
|
||||
int * compteur;
|
||||
|
||||
if (argc != 2) {
|
||||
fprintf(stderr, "Syntaxe : %s nom_segment\n", argv[0]);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
///
|
||||
/// A TERMINER
|
||||
///
|
||||
return EXIT_SUCCESS;
|
||||
}
|
43
2019-2020/S3T_TP_06_IPC/Squelettes/shm_sem11.c
Normal file
43
2019-2020/S3T_TP_06_IPC/Squelettes/shm_sem11.c
Normal file
@@ -0,0 +1,43 @@
|
||||
///*************************************************************/
|
||||
///* shm_sem : memoire partagee et semaphores */
|
||||
///* SYNTAXE shm_sem */
|
||||
///* */
|
||||
///* a) le p<>re cr<63>e un fils avec fork */
|
||||
///* b) le fils cr<63>e une nouvelle m<>moire partag<61>e pouvant */
|
||||
///* contenir 10 caract<63>res */
|
||||
///* c) le p<>re <20>crit <20> BONJOUR <20> dans cette m<>moire partag<61>e */
|
||||
///* d) le fils affiche le contenu de la m<>moire partag<61>e */
|
||||
///* */
|
||||
///* Version initiale : avec shm mais sans sem */ */
|
||||
///* Version finale : avec shm et avec sem */
|
||||
///* */
|
||||
///* NOTE : POUR SIMPLIFIER, le choix des noms pour les IPC */
|
||||
///* est libre ...Les conflits eventuels doivent <20>tre d<>tectes */
|
||||
///* et donne lieu <20> un message et l'arret du programme */
|
||||
///*************************************************************/
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
int main(int argc)
|
||||
{
|
||||
/// FINIR ///
|
||||
int res;
|
||||
assert (argc == 1);
|
||||
res = fork();
|
||||
if (res == -1)
|
||||
{
|
||||
perror ("fork");
|
||||
exit (1);
|
||||
}
|
||||
if (res == 0)
|
||||
{
|
||||
/// FINIR ///
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
/// FINIR ///
|
||||
return 0;
|
||||
}
|
||||
}
|
2
2019-2020/S3T_TP_06_IPC/instructionsGCCLRT.txt
Normal file
2
2019-2020/S3T_TP_06_IPC/instructionsGCCLRT.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
gcc -c ./demo_shm.c -o ./demo_shm.o
|
||||
gcc ./demo_shm.o -o ./demoShm -lrt
|
BIN
2019-2020/S3T_TP_06_IPC/~$11_TP_06-SIGNAUX.docx
Normal file
BIN
2019-2020/S3T_TP_06_IPC/~$11_TP_06-SIGNAUX.docx
Normal file
Binary file not shown.
Reference in New Issue
Block a user