"depot M311"
This commit is contained in:
BIN
S3T_TP_06_IPC/Demonstrations/demoShm
Executable file
BIN
S3T_TP_06_IPC/Demonstrations/demoShm
Executable file
Binary file not shown.
75
S3T_TP_06_IPC/Demonstrations/demo_sem.c
Normal file
75
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
S3T_TP_06_IPC/Demonstrations/demo_shm.c
Normal file
80
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;
|
||||
}
|
Reference in New Issue
Block a user