"depot M311"
This commit is contained in:
parent
54abdaaeb7
commit
0a828aa477
25
.gitignore
vendored
25
.gitignore
vendored
@ -50,3 +50,28 @@ modules.order
|
|||||||
Module.symvers
|
Module.symvers
|
||||||
Mkfile.old
|
Mkfile.old
|
||||||
dkms.conf
|
dkms.conf
|
||||||
|
|
||||||
|
#others
|
||||||
|
*.pdf
|
||||||
|
*.jpg
|
||||||
|
*.png
|
||||||
|
*.db
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
9
S3T_TP_00_REVISIONS/Demo_make/bonjour.c
Executable file
9
S3T_TP_00_REVISIONS/Demo_make/bonjour.c
Executable file
@ -0,0 +1,9 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "util_afficher.h"
|
||||||
|
int main (int argc, char * argv[], char * envp[])
|
||||||
|
{
|
||||||
|
if (argc != 2) {fprintf (stderr, "\nSyntaxe : %s <chaine> \n\n"); exit (1); }
|
||||||
|
afficher_chaine(argv[1]);
|
||||||
|
}
|
||||||
|
|
9
S3T_TP_00_REVISIONS/Demo_make/util_afficher.c
Executable file
9
S3T_TP_00_REVISIONS/Demo_make/util_afficher.c
Executable file
@ -0,0 +1,9 @@
|
|||||||
|
/*********************************/
|
||||||
|
/* Specification afficher_chaine */
|
||||||
|
/*********************************/
|
||||||
|
#include <stdio.h>
|
||||||
|
#include "util_afficher.h"
|
||||||
|
void afficher_chaine (char * chaine) {
|
||||||
|
printf ("\n\n*** %s ! ***\n\n", chaine);
|
||||||
|
}
|
||||||
|
|
5
S3T_TP_00_REVISIONS/Demo_make/util_afficher.h
Executable file
5
S3T_TP_00_REVISIONS/Demo_make/util_afficher.h
Executable file
@ -0,0 +1,5 @@
|
|||||||
|
/*********************************/
|
||||||
|
/* Specification afficher_chaine */
|
||||||
|
/*********************************/
|
||||||
|
void afficher_chaine (char * chaine);
|
||||||
|
|
BIN
S3T_TP_00_REVISIONS/_DOC_Bash_Configuration.JPG
Executable file
BIN
S3T_TP_00_REVISIONS/_DOC_Bash_Configuration.JPG
Executable file
Binary file not shown.
After Width: | Height: | Size: 58 KiB |
18
S3T_TP_00_REVISIONS/crash.c
Executable file
18
S3T_TP_00_REVISIONS/crash.c
Executable file
@ -0,0 +1,18 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
int main (int argc, char * argv[], char * envp[])
|
||||||
|
{
|
||||||
|
FILE *leFichier ;
|
||||||
|
int n;
|
||||||
|
// assert (argc == 2);
|
||||||
|
if (argc != 2) {fprintf (stderr, "\nSyntaxe : %s <chaine> \n\n", argv[0]); exit (1); }
|
||||||
|
leFichier = fopen (argv[1], "w");
|
||||||
|
n = fwrite ("OK !\13\10\10\10\10", strlen ("OK !\13\10\10\10\10"), 1, leFichier);
|
||||||
|
fclose ( leFichier);
|
||||||
|
|
||||||
|
printf ("\nhello %s\n\n", argc);
|
||||||
|
printf ("\n\n\n Fin normale du programme\n\n");
|
||||||
|
}
|
||||||
|
|
11
S3T_TP_00_REVISIONS/hello.c
Executable file
11
S3T_TP_00_REVISIONS/hello.c
Executable file
@ -0,0 +1,11 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <assert.h>
|
||||||
|
int main (int argc, char * argv[], char * envp[])
|
||||||
|
{
|
||||||
|
assert (argc == 2);
|
||||||
|
//if (argc != 2) {fprintf (stderr, "\nSyntaxe : %s <chaine> \n\n", argv[0]); exit (1); }
|
||||||
|
|
||||||
|
printf ("\nhello %s\n\n", argv[1]);
|
||||||
|
}
|
||||||
|
|
15
S3T_TP_00_REVISIONS/processus_1.c
Executable file
15
S3T_TP_00_REVISIONS/processus_1.c
Executable file
@ -0,0 +1,15 @@
|
|||||||
|
/*
|
||||||
|
gcc processus_1.c
|
||||||
|
Exemple : ./a.out un deux trois
|
||||||
|
*/
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
int main(int argc, char * argv[]){
|
||||||
|
int i;
|
||||||
|
for (i=1; i< argc; i++) {
|
||||||
|
printf("argument %d: %s\n",i,argv[i]);
|
||||||
|
}
|
||||||
|
printf("\nPID = %d - PPID = %d \n\n",getpid(), getppid());
|
||||||
|
return 0;
|
||||||
|
}
|
18
S3T_TP_00_REVISIONS/processus_2.c
Executable file
18
S3T_TP_00_REVISIONS/processus_2.c
Executable file
@ -0,0 +1,18 @@
|
|||||||
|
/*
|
||||||
|
gcc processus_2.c
|
||||||
|
Exemple : ./a.out PATH
|
||||||
|
*/
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int main(int argc, char* argv[])
|
||||||
|
{
|
||||||
|
if (argc != 2)
|
||||||
|
{
|
||||||
|
fprintf (stderr, "Syntaxe : %s <nom-de-variable>\n", argv[0]);
|
||||||
|
exit (1);
|
||||||
|
}
|
||||||
|
printf ("\n%s\n\n", getenv (argv[1]));
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
|
|
10
S3T_TP_01_SGF-1/Makefile
Executable file
10
S3T_TP_01_SGF-1/Makefile
Executable file
@ -0,0 +1,10 @@
|
|||||||
|
CFLAGS = -Wall -std=gnu99 -g
|
||||||
|
|
||||||
|
EXECUTABLES = demo \
|
||||||
|
myls
|
||||||
|
|
||||||
|
all : ${EXECUTABLES}
|
||||||
|
|
||||||
|
clean :
|
||||||
|
@rm -f core *.o *.out *~
|
||||||
|
@rm -f ${EXECUTABLES}
|
19
S3T_TP_01_SGF-1/demo.c
Executable file
19
S3T_TP_01_SGF-1/demo.c
Executable file
@ -0,0 +1,19 @@
|
|||||||
|
/// M311 - Demonstration de base
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
int main (int argc, char* argv[])
|
||||||
|
{
|
||||||
|
int resultat;
|
||||||
|
if (argc != 2)
|
||||||
|
{ fprintf (stderr, "Syntaxe : %s <nom>\n\n", argv[0]); exit (1); }
|
||||||
|
|
||||||
|
if ((resultat = mkdir (argv[1], 0777)) == -1)
|
||||||
|
{ perror ("Echec mkdir"); exit (2); }
|
||||||
|
|
||||||
|
/// ...
|
||||||
|
}
|
8
S3T_TP_01_SGF-1/myls.c
Executable file
8
S3T_TP_01_SGF-1/myls.c
Executable file
@ -0,0 +1,8 @@
|
|||||||
|
/// M311 - myls
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main (int argc, char* argv[])
|
||||||
|
{
|
||||||
|
/// ...
|
||||||
|
}
|
BIN
S3T_TP_02_PROCESSUS-1-BASES/_DOC_ps_Exemples.JPG
Executable file
BIN
S3T_TP_02_PROCESSUS-1-BASES/_DOC_ps_Exemples.JPG
Executable file
Binary file not shown.
After Width: | Height: | Size: 122 KiB |
16
S3T_TP_03_PROCESSUS-2-PROG/Demonstrations/Makefile
Executable file
16
S3T_TP_03_PROCESSUS-2-PROG/Demonstrations/Makefile
Executable file
@ -0,0 +1,16 @@
|
|||||||
|
CFLAGS = -Wall -g -std=gnu99
|
||||||
|
|
||||||
|
EXECUTABLES = demo_base \
|
||||||
|
demo_exit \
|
||||||
|
demo_sleep \
|
||||||
|
demo_fork \
|
||||||
|
demo_exec_scan \
|
||||||
|
demo_exec_arg \
|
||||||
|
demo_strtok
|
||||||
|
|
||||||
|
|
||||||
|
all : ${EXECUTABLES}
|
||||||
|
|
||||||
|
clean :
|
||||||
|
@rm -f core *.o *.out *~
|
||||||
|
@rm -f ${EXECUTABLES}
|
21
S3T_TP_03_PROCESSUS-2-PROG/Demonstrations/demo_base.c
Executable file
21
S3T_TP_03_PROCESSUS-2-PROG/Demonstrations/demo_base.c
Executable file
@ -0,0 +1,21 @@
|
|||||||
|
/**************************************/
|
||||||
|
/* demo_base : demonstration basique */
|
||||||
|
/**************************************/
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
|
int main (int argc, char* argv[], char* envp[])
|
||||||
|
// argc = compteur d'arguments
|
||||||
|
// argv et envp = tableau de pointeurs de chaines
|
||||||
|
{
|
||||||
|
if (argc != 2){
|
||||||
|
fprintf(stdout, "\nSyntaxe : %s nom-de-variable\n\n", argv[0]);
|
||||||
|
exit (1);
|
||||||
|
}
|
||||||
|
printf ("\nProcessus %d (pere : %d) - 2eme mot ='%s'\n", getpid (), getppid (),
|
||||||
|
getenv(argv[1]));
|
||||||
|
return 0;
|
||||||
|
}
|
25
S3T_TP_03_PROCESSUS-2-PROG/Demonstrations/demo_exec_arg.c
Executable file
25
S3T_TP_03_PROCESSUS-2-PROG/Demonstrations/demo_exec_arg.c
Executable file
@ -0,0 +1,25 @@
|
|||||||
|
/*************************************************************/
|
||||||
|
/* demo_exec_arg : */
|
||||||
|
/* - Reservation de la place max des arguments */
|
||||||
|
/* (20 car.), */
|
||||||
|
/* - 1er paramètre = la commande a executer, */
|
||||||
|
/* - 2ème paramètre et suivant = paramètres de */
|
||||||
|
/* la commande */
|
||||||
|
/* - execution (avec prise en compte de la */
|
||||||
|
/* variable PATH). */
|
||||||
|
/*************************************************************/
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
int main (int argc, char* argv[])
|
||||||
|
{
|
||||||
|
char* argv2[argc];
|
||||||
|
int i;
|
||||||
|
printf(" argc=%i \n", argc);
|
||||||
|
for (i= 1; i <= argc; i++)
|
||||||
|
{
|
||||||
|
argv2[i-1] = argv[i];
|
||||||
|
printf(" argv[%i] = %s\n", i, argv2[i-1]);
|
||||||
|
}
|
||||||
|
execvp (argv2[0],argv2);
|
||||||
|
printf ("\nERREUR : execvp impossible\n");
|
||||||
|
}
|
22
S3T_TP_03_PROCESSUS-2-PROG/Demonstrations/demo_exec_scan.c
Executable file
22
S3T_TP_03_PROCESSUS-2-PROG/Demonstrations/demo_exec_scan.c
Executable file
@ -0,0 +1,22 @@
|
|||||||
|
/***************************************************************/
|
||||||
|
/* demo_exec_scan : */
|
||||||
|
/* - Reservation de la place max des arguments */
|
||||||
|
/* (30 car.), */
|
||||||
|
/* - lecture du nom de la commande a executer, */
|
||||||
|
/* - lecture d'un param. obligatoire, */
|
||||||
|
/* - execution (avec prise en compte de la */
|
||||||
|
/* variable PATH). */
|
||||||
|
/***************************************************************/
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
int main ()
|
||||||
|
{ char *argv[3];
|
||||||
|
argv[0] = (char*) malloc(30); argv[1] = (char*)malloc(30);
|
||||||
|
printf ("Nom de la commande a executer : "); scanf ("%s", argv[0]);
|
||||||
|
printf ("Un argument (obligatoire) : "); scanf ("%s", argv[1]);
|
||||||
|
argv[2] = (char*)0;
|
||||||
|
execvp (argv[0],argv);
|
||||||
|
printf ("\nERREUR : execvp impossible\n");
|
||||||
|
}
|
18
S3T_TP_03_PROCESSUS-2-PROG/Demonstrations/demo_exit.c
Executable file
18
S3T_TP_03_PROCESSUS-2-PROG/Demonstrations/demo_exit.c
Executable file
@ -0,0 +1,18 @@
|
|||||||
|
/*************************************************/
|
||||||
|
/* demo_exit : envoi d'un code de retour */
|
||||||
|
/*************************************************/
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
int main (int argc, char *argv[])
|
||||||
|
{
|
||||||
|
if (argc != 2) {fprintf (stderr, "Syntaxe : %s code-de-retour \n", argv[0]); exit(-1);}
|
||||||
|
exit (atoi (argv[1]));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Par exemple, tester en bash avec :
|
||||||
|
|
||||||
|
/// if ./demo_exit 3 ; then echo OK ; else echo KO ; fi
|
||||||
|
|
||||||
|
/// Puis avec .\demo_exit 0 ...
|
||||||
|
|
||||||
|
|
13
S3T_TP_03_PROCESSUS-2-PROG/Demonstrations/demo_fork.c
Executable file
13
S3T_TP_03_PROCESSUS-2-PROG/Demonstrations/demo_fork.c
Executable file
@ -0,0 +1,13 @@
|
|||||||
|
/******************************************************/
|
||||||
|
/* demo_fork : Ce programme illustre le mecanisme de */
|
||||||
|
/* duplication d'images de LINUX. */
|
||||||
|
/******************************************************/
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
int main()
|
||||||
|
{ int res;
|
||||||
|
res = fork();
|
||||||
|
if (res == -1) {perror ("demo_fork - fork"); exit (1);}
|
||||||
|
printf ("\nProcessus %d (pere : %d) - res ='%d'\n", getpid (), getppid (), res);
|
||||||
|
}
|
23
S3T_TP_03_PROCESSUS-2-PROG/Demonstrations/demo_sleep.c
Executable file
23
S3T_TP_03_PROCESSUS-2-PROG/Demonstrations/demo_sleep.c
Executable file
@ -0,0 +1,23 @@
|
|||||||
|
/*************************************************/
|
||||||
|
/* demo_sleep : Utilisation de "sleep" */
|
||||||
|
/*************************************************/
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
int main (int argc, char* argv[])
|
||||||
|
/* argc : compteur d'arguments */
|
||||||
|
/* argv : tableau de pointeurs de chaines */
|
||||||
|
{
|
||||||
|
if (argc != 2)
|
||||||
|
{
|
||||||
|
fprintf (stderr, "Syntaxe : %s duree-du-sleep\n", argv[0]);
|
||||||
|
exit (1);
|
||||||
|
}
|
||||||
|
printf("\n%s (Pid = %d): Regardez-moi avec ps ... Je m'endors %s secondes.\n",
|
||||||
|
argv[0], getpid(),argv[1]);
|
||||||
|
sleep (atoi(argv[1]));
|
||||||
|
printf("%s (Pid = %d): Je me reveille après %s secondes.\n\n",
|
||||||
|
argv[0], getpid(), argv[1]);
|
||||||
|
}
|
20
S3T_TP_03_PROCESSUS-2-PROG/Demonstrations/demo_strtok.c
Executable file
20
S3T_TP_03_PROCESSUS-2-PROG/Demonstrations/demo_strtok.c
Executable file
@ -0,0 +1,20 @@
|
|||||||
|
/// M311 - demonstration : gets, strtok
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <strings.h>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
char *mot;
|
||||||
|
char *ligne = malloc (80);
|
||||||
|
ligne = fgets (ligne, 80, stdin);
|
||||||
|
printf("===%s===\n", ligne);
|
||||||
|
mot = strtok (ligne, " ");
|
||||||
|
while (mot != NULL)
|
||||||
|
{
|
||||||
|
printf("%s\n", mot);
|
||||||
|
mot = strtok (NULL, " ");
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
23
S3T_TP_03_PROCESSUS-2-PROG/Demonstrations/p_fichier.c
Executable file
23
S3T_TP_03_PROCESSUS-2-PROG/Demonstrations/p_fichier.c
Executable file
@ -0,0 +1,23 @@
|
|||||||
|
/*******************************************************/
|
||||||
|
/* p_fichier: Ce programme crée un nouveau */
|
||||||
|
/* processus avec fork */
|
||||||
|
/* - Le processus pere ouvre un nouveau fichier en */
|
||||||
|
/* création de nom donné en argument */
|
||||||
|
/* - Le processus fils écrit une chaine donnee en */
|
||||||
|
/* argument dans le fichier puis meurt */
|
||||||
|
/* - Le processus pere affiche le fichier puis meurt */
|
||||||
|
/* */
|
||||||
|
/* syntaxe : p_fichier fichier chaine */
|
||||||
|
/*******************************************************/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main (int argc, char* argv[]) {
|
||||||
|
|
||||||
|
if (argc != 3) fprintf (stderr, "\nSyntaxe: %s <fichier> <chaine>\n\n", argv[0]);
|
||||||
|
|
||||||
|
/**********************/
|
||||||
|
/* PARTIE A COMPLETER */
|
||||||
|
/**********************/
|
||||||
|
|
||||||
|
}
|
19
S3T_TP_03_PROCESSUS-2-PROG/Demonstrations/p_fork.c
Executable file
19
S3T_TP_03_PROCESSUS-2-PROG/Demonstrations/p_fork.c
Executable file
@ -0,0 +1,19 @@
|
|||||||
|
/*******************************************************/ /* */
|
||||||
|
/* p_fork : Ce programme illustre le mecanisme de */
|
||||||
|
/* duplication de processus de LINUX. */
|
||||||
|
/* Le processus pere affiche N etoiles, alors */
|
||||||
|
/* que le processus fils affiche N slashs. */
|
||||||
|
/* La valeur de N est transmise en premier argument */
|
||||||
|
/* de la commande. */
|
||||||
|
/* */
|
||||||
|
/*******************************************************/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main (int argc, char* argv[]) {
|
||||||
|
|
||||||
|
/**********************/
|
||||||
|
/* PARTIE A COMPLETER */
|
||||||
|
/**********************/
|
||||||
|
|
||||||
|
}
|
41
S3T_TP_03_PROCESSUS-2-PROG/Demonstrations/p_fork_exec.c
Executable file
41
S3T_TP_03_PROCESSUS-2-PROG/Demonstrations/p_fork_exec.c
Executable file
@ -0,0 +1,41 @@
|
|||||||
|
/*********************************************************/
|
||||||
|
/* */
|
||||||
|
/* p_fork_exec : Prototype extremement simplifie d' une */
|
||||||
|
/* boucle de SHELL : */
|
||||||
|
/* - Le pere lit une commande a executer (fait) */
|
||||||
|
/* - Le pere se duplique et attend la fin du fils */
|
||||||
|
/* - Le pere affiche OK si le fils s'est "bien" fini */
|
||||||
|
/* (exit avec 0) et KO sinon. */
|
||||||
|
/* et KO sinon. */
|
||||||
|
/* - Le fils execute la commande lue par le pere */
|
||||||
|
/* (avec utilisation de la variable PATH). */
|
||||||
|
/* */
|
||||||
|
/*********************************************************/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int main ()
|
||||||
|
|
||||||
|
{ char *arguments[3];
|
||||||
|
|
||||||
|
arguments[0] = (char*) malloc(30);
|
||||||
|
arguments[1] = (char*) malloc(30);
|
||||||
|
|
||||||
|
printf ("\n=============================\n");
|
||||||
|
printf (" BIENVENUE SOUS p_fork_exec \n");
|
||||||
|
printf ("=============================\n\n");
|
||||||
|
|
||||||
|
printf ("? Nom de la commande a executer : "); scanf ("%s", arguments[0]);
|
||||||
|
printf ("? Un argument (obligatoire) : "); scanf ("%s", arguments[1]);
|
||||||
|
|
||||||
|
arguments[2] = (char*)0;
|
||||||
|
|
||||||
|
/**********************/
|
||||||
|
/* PARTIE A COMPLETER */
|
||||||
|
/**********************/
|
||||||
|
|
||||||
|
printf ("\n==============================\n");
|
||||||
|
printf (" p_fork_exec: FIN DE SESSION \n");
|
||||||
|
printf ("==============================\n\n");
|
||||||
|
}
|
20
S3T_TP_03_PROCESSUS-2-PROG/Demonstrations/p_fork_wait.c
Executable file
20
S3T_TP_03_PROCESSUS-2-PROG/Demonstrations/p_fork_wait.c
Executable file
@ -0,0 +1,20 @@
|
|||||||
|
/*******************************************************/ /* */
|
||||||
|
/* p_fork_wait : Ce programme illustre le mecanisme */
|
||||||
|
/* de duplication de processus de LINUX. */
|
||||||
|
/* Le processus fils affiche N slashs,pendant */
|
||||||
|
/* que le père attend la fin du fils pour afficher */
|
||||||
|
/* un message de fin. */
|
||||||
|
/* La valeur de N est transmise en premier argument */
|
||||||
|
/* de la commande. */
|
||||||
|
/* */
|
||||||
|
/*******************************************************/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main (int argc, char* argv[]) {
|
||||||
|
|
||||||
|
/**********************/
|
||||||
|
/* PARTIE A COMPLETER */
|
||||||
|
/**********************/
|
||||||
|
|
||||||
|
}
|
26
S3T_TP_03_PROCESSUS-2-PROG/Demonstrations/p_fork_wait_exit.c
Executable file
26
S3T_TP_03_PROCESSUS-2-PROG/Demonstrations/p_fork_wait_exit.c
Executable file
@ -0,0 +1,26 @@
|
|||||||
|
/*******************************************************/ /* */
|
||||||
|
/* p_fork_wait_exit : Ce programme illustre le */
|
||||||
|
/* mecanisme de duplication de processus */
|
||||||
|
/* de LINUX. */
|
||||||
|
/* Le processus pere attend le fils. */
|
||||||
|
/* Le processus fils dort quelques secondes puis ... */
|
||||||
|
/* Le processus fils affiche un message puis retourne */
|
||||||
|
/* un code de retour pris dans argv[1]. */
|
||||||
|
/* Le pere affiche "Operation reussie" si le fils */
|
||||||
|
/* s'est bien terminé, et "Echec d'operation" sinon. */
|
||||||
|
/* */
|
||||||
|
/* Convention : "bien terminé" == (code de retour == 0)*/
|
||||||
|
/* */
|
||||||
|
/* Technique : Utiliser wait (adresse) et WEXITSTATUS */
|
||||||
|
/* */
|
||||||
|
/*******************************************************/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main (int argc, char* argv[]) {
|
||||||
|
|
||||||
|
/**********************/
|
||||||
|
/* PARTIE A COMPLETER */
|
||||||
|
/**********************/
|
||||||
|
|
||||||
|
}
|
31
S3T_TP_03_PROCESSUS-2-PROG/Demonstrations/p_system.c
Executable file
31
S3T_TP_03_PROCESSUS-2-PROG/Demonstrations/p_system.c
Executable file
@ -0,0 +1,31 @@
|
|||||||
|
/*******************************************************/ /* */
|
||||||
|
/* p_system : Ce programme définit une fonction */
|
||||||
|
/* "p_system" qui reproduit le fonctionnement */
|
||||||
|
/* de la routine system(3). */
|
||||||
|
/* */
|
||||||
|
/*******************************************************/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
|
|
||||||
|
int p_system (char* commande){
|
||||||
|
printf ("Execution de p_system avec : %s\n", commande);
|
||||||
|
|
||||||
|
/**********************/
|
||||||
|
/* PARTIE A COMPLETER */
|
||||||
|
/**********************/
|
||||||
|
return 0; // A modifier ...
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char * argv[]){
|
||||||
|
int res;
|
||||||
|
if (argc != 2) {
|
||||||
|
fprintf (stderr, "\nSyntaxe : %s \"une ligne de commande\"\n\n", argv[0]); exit (1);}
|
||||||
|
|
||||||
|
res = p_system (argv[1]);
|
||||||
|
|
||||||
|
printf ("Code de retour : %d\n", res);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
13
S3T_TP_04_PROCESSUS-3-SGF-2/Demonstrations/Makefile
Executable file
13
S3T_TP_04_PROCESSUS-3-SGF-2/Demonstrations/Makefile
Executable file
@ -0,0 +1,13 @@
|
|||||||
|
CFLAGS = -Wall -std=gnu99
|
||||||
|
|
||||||
|
EXECUTABLES = demo_dup2 \
|
||||||
|
demo_pipe \
|
||||||
|
demo_pipe_fork
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
all : ${EXECUTABLES}
|
||||||
|
|
||||||
|
clean :
|
||||||
|
@rm -f core *.o *.out *~
|
||||||
|
@rm -f ${EXECUTABLES}
|
20
S3T_TP_04_PROCESSUS-3-SGF-2/Demonstrations/demo_dup2.c
Executable file
20
S3T_TP_04_PROCESSUS-3-SGF-2/Demonstrations/demo_dup2.c
Executable file
@ -0,0 +1,20 @@
|
|||||||
|
///********************/
|
||||||
|
///* demo_dup2 : ... */
|
||||||
|
///********************/
|
||||||
|
# include <stdio.h>
|
||||||
|
# include <fcntl.h>
|
||||||
|
# include <unistd.h>
|
||||||
|
|
||||||
|
# define ENTREE_STANDARD 0
|
||||||
|
int main()
|
||||||
|
{ int descr_group;
|
||||||
|
char c;
|
||||||
|
descr_group = open ("/etc/passwd", O_RDONLY);
|
||||||
|
dup2 (descr_group, ENTREE_STANDARD);/// retour a tester ...
|
||||||
|
c = getchar();
|
||||||
|
while (c != EOF)
|
||||||
|
{
|
||||||
|
putchar(c); c = getchar();
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
35
S3T_TP_04_PROCESSUS-3-SGF-2/Demonstrations/demo_fork_fork.c
Executable file
35
S3T_TP_04_PROCESSUS-3-SGF-2/Demonstrations/demo_fork_fork.c
Executable file
@ -0,0 +1,35 @@
|
|||||||
|
/********************/
|
||||||
|
/* demo_dup2 : ... */
|
||||||
|
/********************/
|
||||||
|
# include <stdio.h>
|
||||||
|
# include <fcntl.h>
|
||||||
|
# include <unistd.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{ int res;
|
||||||
|
int tp[2];
|
||||||
|
pipe (tp);
|
||||||
|
if ((res = fork()) ==-1) {perror ("fork1"); exit (1); }
|
||||||
|
else if (res != 0) // Pere
|
||||||
|
{
|
||||||
|
if ((res = res = fork()) ==-1) {perror ("fork2"); exit (1); }
|
||||||
|
else if (res != 0)
|
||||||
|
{ // pere
|
||||||
|
execlp ("who", "who", NULL);
|
||||||
|
}
|
||||||
|
else // Fils2
|
||||||
|
{
|
||||||
|
close (tp[0]);
|
||||||
|
dup2 (tp[1], 1);
|
||||||
|
execlp ("ls", "ls", "-la", NULL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else // Fils1
|
||||||
|
{
|
||||||
|
close (tp[1]);
|
||||||
|
dup2 (tp[0], 0);
|
||||||
|
execlp ("wc", "wc", "-l", NULL);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
16
S3T_TP_04_PROCESSUS-3-SGF-2/Demonstrations/demo_pipe.c
Executable file
16
S3T_TP_04_PROCESSUS-3-SGF-2/Demonstrations/demo_pipe.c
Executable file
@ -0,0 +1,16 @@
|
|||||||
|
///********************/
|
||||||
|
///* demo_pipe : ... */
|
||||||
|
///********************/
|
||||||
|
# include <stdio.h>
|
||||||
|
# include <unistd.h>
|
||||||
|
|
||||||
|
char string[] = "hello";
|
||||||
|
int main()
|
||||||
|
{ char buf[70];
|
||||||
|
int fds[2];
|
||||||
|
pipe (fds);
|
||||||
|
write(fds[1], string, 6);
|
||||||
|
read (fds[0], buf, 6);
|
||||||
|
printf ("Chaine provenant du pipe-line : %s \n", buf);
|
||||||
|
return 0;
|
||||||
|
}
|
51
S3T_TP_04_PROCESSUS-3-SGF-2/Demonstrations/demo_pipe_fork.c
Executable file
51
S3T_TP_04_PROCESSUS-3-SGF-2/Demonstrations/demo_pipe_fork.c
Executable file
@ -0,0 +1,51 @@
|
|||||||
|
|
||||||
|
/// demo_pipe_fork : ...
|
||||||
|
/// - Demande de descripteurs de pipe line,
|
||||||
|
/// - demande de fork,
|
||||||
|
/// - PERE : lit des car. au clavier, ecrit dans le pipe-line
|
||||||
|
/// (apres fermeture du descr. inutile),
|
||||||
|
/// - FILS : lit dans le pipe-line, ecrit les car. recus
|
||||||
|
/// (apres fermeture du descr. inutile),
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
# define READ 0
|
||||||
|
# define WRITE 1
|
||||||
|
int main ()
|
||||||
|
{ int pid , tabpipe[2];
|
||||||
|
FILE *stream;
|
||||||
|
char c;
|
||||||
|
char * type;
|
||||||
|
pipe (tabpipe); /// Retour à tester
|
||||||
|
pid = fork();
|
||||||
|
if (pid < 0) printf ("ERREUR FORK");
|
||||||
|
else
|
||||||
|
if (pid == 0)
|
||||||
|
{ /// FILS : LIT DANS LE PIPE-LINE
|
||||||
|
type = "r";
|
||||||
|
stream = fdopen (tabpipe[0], type);
|
||||||
|
close (tabpipe[WRITE]);
|
||||||
|
/// Lecture dans le pipe-line, ecriture sur stdout
|
||||||
|
c = fgetc(stream);
|
||||||
|
while ( c != EOF)
|
||||||
|
{ printf("Car. recu : %c \n",c); fflush (stdout);
|
||||||
|
c = fgetc(stream);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{ /// PERE : ECRIT DANS LE PIPE-LINE
|
||||||
|
printf ("Message a envoyer (ctrl.D pour finir) : ");
|
||||||
|
close ( tabpipe[READ] );
|
||||||
|
type ="w";
|
||||||
|
stream = fdopen (tabpipe[1], type);
|
||||||
|
/// Lecture sur stdin, ecriture dans le pipe-line
|
||||||
|
c=getchar();
|
||||||
|
while ( c != EOF)
|
||||||
|
{ fputc(c, stream); fflush (stream);
|
||||||
|
c=getchar();
|
||||||
|
}
|
||||||
|
/// Production de la fin de fichier
|
||||||
|
fclose (stream); }
|
||||||
|
return 0;
|
||||||
|
}
|
11
S3T_TP_04_PROCESSUS-3-SGF-2/Makefile
Executable file
11
S3T_TP_04_PROCESSUS-3-SGF-2/Makefile
Executable file
@ -0,0 +1,11 @@
|
|||||||
|
CFLAGS = -Wall -std=gnu99
|
||||||
|
|
||||||
|
EXECUTABLES = p_pipe\
|
||||||
|
p_pipe_arg
|
||||||
|
|
||||||
|
|
||||||
|
all : ${EXECUTABLES}
|
||||||
|
|
||||||
|
clean :
|
||||||
|
@rm -f core *.o *.out *~
|
||||||
|
@rm -f ${EXECUTABLES}
|
47
S3T_TP_04_PROCESSUS-3-SGF-2/POPEN_sources/m311_popen_demo.c
Executable file
47
S3T_TP_04_PROCESSUS-3-SGF-2/POPEN_sources/m311_popen_demo.c
Executable file
@ -0,0 +1,47 @@
|
|||||||
|
/**
|
||||||
|
Auteur : RC (inspiration MS)
|
||||||
|
Squelette du programme popen_somme
|
||||||
|
Cf. algorithme dans la fonction main
|
||||||
|
|
||||||
|
NB : La fonction popen() engendre un processus en créant un tube
|
||||||
|
(pipe), executant un fork(), et en invoquant le shell avec la
|
||||||
|
commande donnée dans l'argument no 1.
|
||||||
|
Comme un tube est unidirectionnel par definition,
|
||||||
|
l'argument no 2 doit indiquer seulement une lecture
|
||||||
|
ou une écriture, et non pas les deux.
|
||||||
|
Le flux correspondant sera donc ouvert en lecture seule
|
||||||
|
ou écriture seule.
|
||||||
|
Cf. man ...
|
||||||
|
*/
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <assert.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#define MAX_COMMANDE 100
|
||||||
|
|
||||||
|
int main (int argc, char *argv[])
|
||||||
|
{
|
||||||
|
assert (argc == 3);
|
||||||
|
/// Syntaxe : ./a.out <x> <n>
|
||||||
|
/// Exemple : ./a.out 2 10
|
||||||
|
/// Resultat : 2047
|
||||||
|
int x;
|
||||||
|
int n;
|
||||||
|
FILE *fPin;
|
||||||
|
char commande[MAX_COMMANDE];
|
||||||
|
int somme;
|
||||||
|
int l;
|
||||||
|
char unResultat[10];
|
||||||
|
|
||||||
|
/// "n" boucles "for" avec le compteur l
|
||||||
|
{
|
||||||
|
/// Preparation de la commande pour popen(avec sprintf)
|
||||||
|
/// Creation du fils et du pipeline et test d'erreur...
|
||||||
|
/// Avec fscanf, lecture de la valeur x^l produite par ...
|
||||||
|
/// Fermeture avec pclose
|
||||||
|
/// Incrementation de la somme
|
||||||
|
}
|
||||||
|
/// affichage du resultat
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
17
S3T_TP_04_PROCESSUS-3-SGF-2/POPEN_sources/m311_puiss.c
Executable file
17
S3T_TP_04_PROCESSUS-3-SGF-2/POPEN_sources/m311_puiss.c
Executable file
@ -0,0 +1,17 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <assert.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
int main(int argc, char* argv[])
|
||||||
|
{
|
||||||
|
/// Syntaxe ./argv[0] x n
|
||||||
|
/// Resultat sur stdout : x^n
|
||||||
|
assert (argc == 3);
|
||||||
|
int x = atoi (argv[1]);
|
||||||
|
int n = atoi (argv[2]);
|
||||||
|
|
||||||
|
printf ("%.0f\n",pow (x, n));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
22
S3T_TP_04_PROCESSUS-3-SGF-2/POPEN_sources/m311_somme_puiss.c
Executable file
22
S3T_TP_04_PROCESSUS-3-SGF-2/POPEN_sources/m311_somme_puiss.c
Executable file
@ -0,0 +1,22 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <assert.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
int main(int argc, char* argv[])
|
||||||
|
{
|
||||||
|
/// Syntaxe ./argv[0] x n
|
||||||
|
/// Resultat sur stdout : somme x^i avec i entre 0 et n
|
||||||
|
assert (argc == 3);
|
||||||
|
int x = atoi (argv[1]);
|
||||||
|
int n = atoi (argv[2]);
|
||||||
|
int somme = 0;
|
||||||
|
int i;
|
||||||
|
for (i = 0; i<=n ; i++)
|
||||||
|
{
|
||||||
|
somme+=pow(x,i);
|
||||||
|
printf ("... %d\n", somme);/// Mise au point
|
||||||
|
}
|
||||||
|
printf ("%d\n", somme);
|
||||||
|
return 0;
|
||||||
|
}
|
51
S3T_TP_04_PROCESSUS-3-SGF-2/p_pipe.c
Executable file
51
S3T_TP_04_PROCESSUS-3-SGF-2/p_pipe.c
Executable file
@ -0,0 +1,51 @@
|
|||||||
|
///***************************************************************/
|
||||||
|
///* */
|
||||||
|
///* p_pipe : - Demande de descripteurs de pipeline, */
|
||||||
|
///* - Demande de fork, */
|
||||||
|
///* - PERE : execute la commande wc-l depuis le pipeline */
|
||||||
|
///* (apres avoir ferme le descripteur inutile), */
|
||||||
|
///* - FILS : execute la commande ls -ls vers le pipeline */
|
||||||
|
///* (apres avoir ferme le descripteur inutile). */
|
||||||
|
///* */
|
||||||
|
///***************************************************************/
|
||||||
|
///* - Version 1 : utiliser dup2 et execlp */
|
||||||
|
///* - Version 2 : utiliser dup et execlp */
|
||||||
|
///* - Version 3 : utiliser dup2 et execl ET */
|
||||||
|
///* Le comportement du pere (ci-dessus) est */
|
||||||
|
///* est fait dans un deuxieme fils */
|
||||||
|
///* */
|
||||||
|
///* Ne pas utiliser d'initialisation dynamique de tableau */
|
||||||
|
///***************************************************************/
|
||||||
|
|
||||||
|
# include <stdio.h>
|
||||||
|
# include <stdlib.h>
|
||||||
|
# include <stdlib.h>
|
||||||
|
# include <unistd.h>
|
||||||
|
|
||||||
|
# define READ 0
|
||||||
|
# define WRITE 1
|
||||||
|
# define ENTREE 0
|
||||||
|
# define SORTIE 1
|
||||||
|
|
||||||
|
int main ()
|
||||||
|
{ int res;
|
||||||
|
|
||||||
|
///******************** PERE ***********************/
|
||||||
|
///* PARTIE A COMPLETER */
|
||||||
|
///*************************************************/
|
||||||
|
|
||||||
|
res = fork();
|
||||||
|
|
||||||
|
if (res < 0) { perror ("ERREUR FORK"); exit (2);}
|
||||||
|
else
|
||||||
|
if (res == 0)
|
||||||
|
{ ///******************** FILS ***********************/
|
||||||
|
///* PARTIE A COMPLETER */
|
||||||
|
///*************************************************/
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{ ///******************** PERE ***********************/
|
||||||
|
///* PARTIE A COMPLETER */
|
||||||
|
///*************************************************/
|
||||||
|
}
|
||||||
|
}
|
69
S3T_TP_04_PROCESSUS-3-SGF-2/p_pipe_arg.c
Executable file
69
S3T_TP_04_PROCESSUS-3-SGF-2/p_pipe_arg.c
Executable file
@ -0,0 +1,69 @@
|
|||||||
|
///************************************************************/
|
||||||
|
///* */
|
||||||
|
///* p_pipe_arg : - Demande de descripteurs de pipe-line, */
|
||||||
|
///* - Demande de fork, */
|
||||||
|
///* - PERE : recupere en parametres du main */
|
||||||
|
///* les N mots qui correspondent a la */
|
||||||
|
///* commande qui doit s'executer en lisant */
|
||||||
|
///* dans le pipe-line, */
|
||||||
|
///* - FILS : recupere en parametres du main */
|
||||||
|
///* les M mots suivants qui correspondent */
|
||||||
|
///* a la commande qui doit s'executer en */
|
||||||
|
///* lisant dans le pipe-line */
|
||||||
|
///* s'executer vers le pipe-line. */
|
||||||
|
///* EXEMPLE : p_pipe_arg who wc <====> who | wc */
|
||||||
|
///************************************************************/
|
||||||
|
///* - Version 1 : N = 1 et M = 1 (equivalent ls | wc) */
|
||||||
|
///* - Version 2 : N = 1 et M = 1 ET */
|
||||||
|
///* Le comportement du pere (ci-dessus) est */
|
||||||
|
///* est fait dans un deuxieme fils. */
|
||||||
|
///* - Version 3 : N = 2 et M = 2 ET */
|
||||||
|
///* Le comportement du pere (ci-dessus) est */
|
||||||
|
///* est fait dans un petit-fils. */
|
||||||
|
///* */
|
||||||
|
///* Ne pas utiliser d'initialisation dynamique de tableau */
|
||||||
|
///************************************************************/
|
||||||
|
|
||||||
|
# include <stdio.h>
|
||||||
|
# include <stdlib.h>
|
||||||
|
# include <unistd.h>
|
||||||
|
|
||||||
|
# define READ 0
|
||||||
|
# define WRITE 1
|
||||||
|
# define ENTREE 0
|
||||||
|
# define SORTIE 1
|
||||||
|
|
||||||
|
int main (int argc, char* argv[])
|
||||||
|
|
||||||
|
{
|
||||||
|
int resultat ;
|
||||||
|
|
||||||
|
if (argc != 3)
|
||||||
|
fprintf (stderr, "SYNTAXE : %s commande1 commande2\n", argv[0]);
|
||||||
|
|
||||||
|
{
|
||||||
|
///**************** PERE ******************/
|
||||||
|
///* PARTIE A COMPLETER */
|
||||||
|
///****************************************/
|
||||||
|
|
||||||
|
resultat = fork();
|
||||||
|
if (resultat < 0)
|
||||||
|
{
|
||||||
|
perror("ERREUR FORK");
|
||||||
|
exit (2) ;
|
||||||
|
}
|
||||||
|
else if (resultat == 0)
|
||||||
|
{
|
||||||
|
///**************** FILS ******************/
|
||||||
|
///* PARTIE A COMPLETER */
|
||||||
|
///****************************************/
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/**************** PERE ******************/
|
||||||
|
/* PARTIE A COMPLETER */
|
||||||
|
/****************************************/
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
7
S3T_TP_05_PROCESSUS-4-SIGNAUX/.vscode/settings.json
vendored
Executable file
7
S3T_TP_05_PROCESSUS-4-SIGNAUX/.vscode/settings.json
vendored
Executable file
@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"files.associations": {
|
||||||
|
"signal.h": "c",
|
||||||
|
"stdio.h": "c",
|
||||||
|
"stdlib.h": "c"
|
||||||
|
}
|
||||||
|
}
|
BIN
S3T_TP_05_PROCESSUS-4-SIGNAUX/Demonstrations/M311.lnk
Executable file
BIN
S3T_TP_05_PROCESSUS-4-SIGNAUX/Demonstrations/M311.lnk
Executable file
Binary file not shown.
BIN
S3T_TP_05_PROCESSUS-4-SIGNAUX/Demonstrations/demoAlarm
Executable file
BIN
S3T_TP_05_PROCESSUS-4-SIGNAUX/Demonstrations/demoAlarm
Executable file
Binary file not shown.
BIN
S3T_TP_05_PROCESSUS-4-SIGNAUX/Demonstrations/demoKill
Executable file
BIN
S3T_TP_05_PROCESSUS-4-SIGNAUX/Demonstrations/demoKill
Executable file
Binary file not shown.
25
S3T_TP_05_PROCESSUS-4-SIGNAUX/Demonstrations/demo_alarm.c
Executable file
25
S3T_TP_05_PROCESSUS-4-SIGNAUX/Demonstrations/demo_alarm.c
Executable file
@ -0,0 +1,25 @@
|
|||||||
|
/**********************************************************/
|
||||||
|
/* DUT INFORMATIQUE - M311 - R. CHIGNOLI */
|
||||||
|
/* demo_alarm.c : Utilisation du signal SIGALRM par alarm */
|
||||||
|
/**********************************************************/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <signal.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <assert.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
void rep_SIGALRM ()
|
||||||
|
{ printf ("\n*** Processus %d : Signal SIGALRM recu\n\n", getpid());
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main (int argc, char* argv[])
|
||||||
|
{
|
||||||
|
int res;
|
||||||
|
assert (argc == 2);
|
||||||
|
signal (SIGALRM, rep_SIGALRM);
|
||||||
|
printf ("\n*** Processus %d : Je m' endors ......\n", getpid());
|
||||||
|
res = alarm (atoi(argv[1]));
|
||||||
|
for (;;);
|
||||||
|
}
|
19
S3T_TP_05_PROCESSUS-4-SIGNAUX/Demonstrations/demo_kill.c
Executable file
19
S3T_TP_05_PROCESSUS-4-SIGNAUX/Demonstrations/demo_kill.c
Executable file
@ -0,0 +1,19 @@
|
|||||||
|
/************************************************/
|
||||||
|
/* */
|
||||||
|
/* demo_kill.c : Utilisation de la routine kill */
|
||||||
|
/* */
|
||||||
|
/************************************************/
|
||||||
|
# include <stdio.h>
|
||||||
|
# include <signal.h>
|
||||||
|
# include <sys/types.h>
|
||||||
|
# include <unistd.h>
|
||||||
|
|
||||||
|
int main ()
|
||||||
|
{
|
||||||
|
printf ("\nJe commence et j'attends 3 secondes...\n");
|
||||||
|
sleep (3);
|
||||||
|
printf ("\n... puis je me suicide par kill - %d - \n", getpid());
|
||||||
|
kill (getpid(), SIGKILL);
|
||||||
|
printf ("\n... je suis mort\n");
|
||||||
|
return 0;
|
||||||
|
}
|
28
S3T_TP_05_PROCESSUS-4-SIGNAUX/Demonstrations/demo_kill_grp_1.c
Executable file
28
S3T_TP_05_PROCESSUS-4-SIGNAUX/Demonstrations/demo_kill_grp_1.c
Executable file
@ -0,0 +1,28 @@
|
|||||||
|
/******************************************************/
|
||||||
|
/* DUT INFORMATIQUE - M311 - R. CHIGNOLI */
|
||||||
|
/* demo_kill_grp_1.c : Utilisation de la routine kill */
|
||||||
|
/******************************************************/
|
||||||
|
# include <stdio.h>
|
||||||
|
# include <signal.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int main ()
|
||||||
|
{ int res;
|
||||||
|
res = fork ();
|
||||||
|
if (res != 0)
|
||||||
|
{
|
||||||
|
printf ("\nPERE No %d ( GRPID = %d ) : j'attends 5 secondes...\n",
|
||||||
|
getpid(), getpgrp());
|
||||||
|
sleep (5);
|
||||||
|
printf ("\nPERE No %d ( GRPID = %d ) : ... puis je me suicide par kill\n\n", getpid(), getpgrp());
|
||||||
|
kill (0, SIGKILL);
|
||||||
|
printf ("\nPERE No %d ( GRPID = %d ) : ... je suis mort\n\n", getpid(), getpgrp());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf ("\nFILS No %d ( GRPID = %d ) : Je boucle indefiniment ...\n",
|
||||||
|
getpid(), getpgrp());
|
||||||
|
for(;;);
|
||||||
|
}
|
||||||
|
}
|
32
S3T_TP_05_PROCESSUS-4-SIGNAUX/Demonstrations/demo_kill_grp_2.c
Executable file
32
S3T_TP_05_PROCESSUS-4-SIGNAUX/Demonstrations/demo_kill_grp_2.c
Executable file
@ -0,0 +1,32 @@
|
|||||||
|
/******************************************************/
|
||||||
|
/* DUT INFORMATIQUE - M311 - R. CHIGNOLI */
|
||||||
|
/* demo_kill_grp_2.c : Utilisation de la routine kill */
|
||||||
|
/******************************************************/
|
||||||
|
# include <stdio.h>
|
||||||
|
# include <signal.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
int main () {
|
||||||
|
int res;
|
||||||
|
res = fork ();
|
||||||
|
if (res != 0){
|
||||||
|
printf ("\nPERE No %d ( GRPID = %d ) : j'attends 3 secondes...\n", getpid(), getpgrp());
|
||||||
|
sleep (3);
|
||||||
|
printf ("\nPERE No %d ( GRPID = %d ) : ... puis je me suicide par kill\n", getpid(), getpgrp());
|
||||||
|
kill (0, SIGKILL);
|
||||||
|
printf ("\nPERE No %d ( GRPID = %d ) : ... je suis mort\n", getpid(), getpgrp());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (setpgid (0, getpid()) != 0){
|
||||||
|
perror ("setgpid"); exit (1);
|
||||||
|
}
|
||||||
|
printf ("\nFILS No %d ( GRPID = %d ) : nouveau groupe ...\n", getpid(), getpgrp());
|
||||||
|
printf ("\nFILS No %d ( GRPID = %d ) : J'attends 5 secondes\n", getpid());
|
||||||
|
sleep (5);
|
||||||
|
printf ("\nFILS No %d ( GRPID = %d ) : Et je meurs naturellement !\n\n",
|
||||||
|
getpid());
|
||||||
|
exit (0);
|
||||||
|
}
|
||||||
|
}
|
17
S3T_TP_05_PROCESSUS-4-SIGNAUX/bash_trap.sh
Executable file
17
S3T_TP_05_PROCESSUS-4-SIGNAUX/bash_trap.sh
Executable file
@ -0,0 +1,17 @@
|
|||||||
|
#!/usr/bin/bash
|
||||||
|
#####################################
|
||||||
|
# M311 - CHIGNOLI #
|
||||||
|
# bash-trap.sh : demonstration de #
|
||||||
|
# de la gestion de signaux en bash #
|
||||||
|
#####################################
|
||||||
|
trap 'echo Ignore signal SIGINT' SIGINT
|
||||||
|
trap 'echo Ignore signal SIGSTOP' SIGSTOP
|
||||||
|
trap 'echo Ignore signal SIGUSR1' SIGUSR1
|
||||||
|
trap 'echo FIN ! ; exit' SIGUSR2
|
||||||
|
trap
|
||||||
|
while true
|
||||||
|
do
|
||||||
|
echo Je boucle ...
|
||||||
|
sleep 1
|
||||||
|
done
|
||||||
|
|
17
S3T_TP_05_PROCESSUS-4-SIGNAUX/bash_trap.txt
Executable file
17
S3T_TP_05_PROCESSUS-4-SIGNAUX/bash_trap.txt
Executable file
@ -0,0 +1,17 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
################################################
|
||||||
|
# DUT INFORMATIQUE - M311 - R. CHIGNOLI #
|
||||||
|
# bash_trap : traitement des signaux en bash #
|
||||||
|
################################################
|
||||||
|
|
||||||
|
echo '*******************************************'
|
||||||
|
echo '* J'ignore les signaux SIGQUIT et SIGTERM *'
|
||||||
|
echo '*******************************************'
|
||||||
|
trap 'echo Ignore signal SIGINT' SIGINT
|
||||||
|
trap 'echo Ignore signal SIGTERM' SIGTERM
|
||||||
|
while true
|
||||||
|
do
|
||||||
|
echo je boucle
|
||||||
|
sleep 1
|
||||||
|
done
|
||||||
|
|
BIN
S3T_TP_05_PROCESSUS-4-SIGNAUX/pForkSignal
Executable file
BIN
S3T_TP_05_PROCESSUS-4-SIGNAUX/pForkSignal
Executable file
Binary file not shown.
BIN
S3T_TP_05_PROCESSUS-4-SIGNAUX/pSignal
Executable file
BIN
S3T_TP_05_PROCESSUS-4-SIGNAUX/pSignal
Executable file
Binary file not shown.
59
S3T_TP_05_PROCESSUS-4-SIGNAUX/p_fork_signal.c
Executable file
59
S3T_TP_05_PROCESSUS-4-SIGNAUX/p_fork_signal.c
Executable file
@ -0,0 +1,59 @@
|
|||||||
|
/**********************************************************/
|
||||||
|
/* DUT INFORMATIQUE - M311 - R. CHIGNOLI */
|
||||||
|
/* p_fork_signal : Apr<70>s fork, */
|
||||||
|
/* - PERE : Affiche son pid et ppid, */
|
||||||
|
/* Ignore le signal SIGINT et le montre */
|
||||||
|
/* S'arrete <20> la reception du signal SIGUSR1, */
|
||||||
|
/* se met en boucle infinie. */
|
||||||
|
/* - FILS : Affiche son pid et son ppid, */
|
||||||
|
/* Envoie trois signaux SIGINT au pere, */
|
||||||
|
/* Attend un signal SIGTERM de l'utilisateur */
|
||||||
|
/* Se met en boucle infinie. */
|
||||||
|
/* Envoie le signal SIGUSR1 a son pere a */
|
||||||
|
/* l'interception du signal SIGTERM puis */
|
||||||
|
/* meurt. */
|
||||||
|
/**********************************************************/
|
||||||
|
# include <stdio.h>
|
||||||
|
# include <stdlib.h>
|
||||||
|
# include <signal.h>
|
||||||
|
# include <unistd.h>
|
||||||
|
|
||||||
|
void repSIGINT() {
|
||||||
|
printf ("\nJ'ignore le signal SIGINT (<Ctrl + \\C>)\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
int main () {
|
||||||
|
int res;
|
||||||
|
res = fork();
|
||||||
|
|
||||||
|
if (res < 0){
|
||||||
|
printf ("ERREUR FORK\n");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (res == 0)
|
||||||
|
{ /************************** FILS ***********************/
|
||||||
|
printf("Je suis le fils : ");
|
||||||
|
printf("pid : %d\n", getpid());
|
||||||
|
printf("ppid : %d\n",getppid());
|
||||||
|
for (int i = 0; i < 5; i++)
|
||||||
|
{
|
||||||
|
kill(getppid(), SIGINT);
|
||||||
|
sleep(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{ /************************** PERE ***********************/
|
||||||
|
printf("Je suis le père : ");
|
||||||
|
printf("pid : %d\n", getpid());
|
||||||
|
printf("ppid : %d\n",getppid());
|
||||||
|
|
||||||
|
signal(SIGINT, repSIGINT);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for(;;);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
53
S3T_TP_05_PROCESSUS-4-SIGNAUX/p_multi_fork.c
Executable file
53
S3T_TP_05_PROCESSUS-4-SIGNAUX/p_multi_fork.c
Executable file
@ -0,0 +1,53 @@
|
|||||||
|
/**********************************************************/
|
||||||
|
/* DUT INFORMATIQUE - M311 - R. CHIGNOLI */
|
||||||
|
/* p_multi_fork_signal : Après fork, */
|
||||||
|
/* - PERE : Affiche son pid et ppid, */
|
||||||
|
/* Ignore le signal SIGINT */
|
||||||
|
/* S'arrete à la reception du signal SIGUSR1, */
|
||||||
|
/* Se met en boucle infinie. */
|
||||||
|
/* - FILS : Affiche son pid et son ppid, */
|
||||||
|
/* Cree 3 (sous)fils */
|
||||||
|
/* Attend un signal SIGTERM de l'un des fils */
|
||||||
|
/* Envoie un signal SIGINT a son pere, */
|
||||||
|
/* Se met en boucle infinie. */
|
||||||
|
/* Envoie le signal SIGUSR1 a son pere a */
|
||||||
|
/* l'interception du signal SIGTERM puis */
|
||||||
|
/* meurt. */
|
||||||
|
/* - Chaque sous-FILS : Affiche son pid et son ppid, */
|
||||||
|
/* Se desolidarise de son pere, */
|
||||||
|
/* Attend un signal SIGUSR1 de l'utilisateur, */
|
||||||
|
/* Se met en boucle infinie. */
|
||||||
|
/* Envoie le signal SIGTERM vers son pere */
|
||||||
|
/* a l'interception du signal SIGUSR1. */
|
||||||
|
/* Se termine en recevant le signal SIGUSR2 */
|
||||||
|
/* de la part de l'utilisateur. */
|
||||||
|
/* NB : à la fin du scenario, il ne reste que les trois */
|
||||||
|
/* sous-fils arretable par envoi de signaux SIGUSR2 */
|
||||||
|
/**********************************************************/
|
||||||
|
|
||||||
|
# include <stdio.h>
|
||||||
|
# include <signal.h>
|
||||||
|
/**********************/
|
||||||
|
/* PARTIE A COMPLETER */
|
||||||
|
/**********************/
|
||||||
|
main ()
|
||||||
|
{ int res;
|
||||||
|
/**********************/
|
||||||
|
/* PARTIE A COMPLETER */
|
||||||
|
/**********************/
|
||||||
|
res = fork();
|
||||||
|
if (res < 0) printf ("ERREUR FORK");
|
||||||
|
else
|
||||||
|
if (res == 0)
|
||||||
|
{ // FILS
|
||||||
|
/**********************/
|
||||||
|
/* PARTIE A COMPLETER */
|
||||||
|
/**********************/
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{ // PERE
|
||||||
|
/**********************/
|
||||||
|
/* PARTIE A COMPLETER */
|
||||||
|
/**********************/
|
||||||
|
}
|
||||||
|
}
|
72
S3T_TP_05_PROCESSUS-4-SIGNAUX/p_signal.c
Executable file
72
S3T_TP_05_PROCESSUS-4-SIGNAUX/p_signal.c
Executable file
@ -0,0 +1,72 @@
|
|||||||
|
/**********************************************************/
|
||||||
|
/* DUT INFORMATIQUE - M311 - R. CHIGNOLI */
|
||||||
|
/* p_signal : Modification des actions associees */
|
||||||
|
/* aux signaux SIGINT, SIGSEGV et SIGQUIT. */
|
||||||
|
/**********************************************************/
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <signal.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
SIGINT 2 Term Interruption depuis le clavier.
|
||||||
|
SIGQUIT 3 Core Demande « Quitter » depuis le clavier.
|
||||||
|
SIGILL 4 Core Instruction illégale.
|
||||||
|
SIGABRT 6 Core Signal d'arrêt depuis abort(3).
|
||||||
|
SIGFPE 8 Core Erreur mathématique virgule flottante.
|
||||||
|
SIGKILL 9 Term Signal « KILL ».
|
||||||
|
SIGSEGV 11 Core Référence mémoire invalide.
|
||||||
|
SIGPIPE 13 Term Écriture dans un tube sans lecteur.
|
||||||
|
SIGALRM 14 Term Temporisation alarm(2) écoulée.
|
||||||
|
SIGTERM 15 Term Signal de fin.
|
||||||
|
SIGUSR1 30,10,16 Term Signal utilisateur 1.
|
||||||
|
SIGUSR2 31,12,17 Term Signal utilisateur 2.
|
||||||
|
SIGCHLD 20,17,18 Ign Fils arrêté ou terminé.
|
||||||
|
SIGCONT 19,18,25 Cont Continuer si arrêté.
|
||||||
|
SIGSTOP 17,19,23 Stop Arrêt du processus.
|
||||||
|
SIGTSTP 18,20,24 Stop Stop invoqué depuis tty.
|
||||||
|
SIGTTIN 21,21,26 Stop Lecture sur tty en arrière-plan.
|
||||||
|
SIGTTOU 22,22,27 Stop Écriture sur tty en arrière-plan.
|
||||||
|
|
||||||
|
SIG_DFL = action par defaut pour le signal
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
int stop = 1;
|
||||||
|
|
||||||
|
void repSIGQUIT() {
|
||||||
|
printf ("\nJ'ignore le signal SIGQUIT (<Ctrl \\>)\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void repSIGINT() {
|
||||||
|
if(stop == 0) {
|
||||||
|
kill (getpid(), SIGKILL);
|
||||||
|
} else {
|
||||||
|
stop--;
|
||||||
|
printf ("Je m'arrete au 2eme signal d'interruption (<Ctrl C>)\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void repSIGSEGV() {
|
||||||
|
printf ("Je m'arrete au premier signal SIGSEGV\n\n");
|
||||||
|
kill (getpid(), SIGKILL);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
//printf ("\nJ'ignore le signal SIGQUIT (<Ctrl \\>)\n");
|
||||||
|
//printf ("Je m'arrete au 2eme signal d'interruption (<Ctrl C>)\n");
|
||||||
|
//printf ("Je m'arrete au premier signal SIGSEGV\n\n");
|
||||||
|
printf("%d\n", getpid());
|
||||||
|
|
||||||
|
signal(SIGQUIT, repSIGQUIT);
|
||||||
|
signal(SIGINT, repSIGINT);
|
||||||
|
signal(SIGSEGV, repSIGSEGV);
|
||||||
|
|
||||||
|
/**********************/
|
||||||
|
/* PARTIE A COMPLETER */
|
||||||
|
/**********************/
|
||||||
|
|
||||||
|
/* boucle de temporisation */
|
||||||
|
for (;;);
|
||||||
|
}
|
5
S3T_TP_06_IPC/.vscode/settings.json
vendored
Normal file
5
S3T_TP_06_IPC/.vscode/settings.json
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"files.associations": {
|
||||||
|
"unistd.h": "c"
|
||||||
|
}
|
||||||
|
}
|
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;
|
||||||
|
}
|
35
S3T_TP_06_IPC/Squelettes/imprimer.c
Normal file
35
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
S3T_TP_06_IPC/Squelettes/s_critique.c
Normal file
37
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
S3T_TP_06_IPC/Squelettes/shm_add.c
Normal file
28
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
S3T_TP_06_IPC/Squelettes/shm_create.c
Normal file
41
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
S3T_TP_06_IPC/Squelettes/shm_excl.c
Normal file
28
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
S3T_TP_06_IPC/Squelettes/shm_sem11.c
Normal file
43
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ée un fils avec fork */
|
||||||
|
///* b) le fils crée une nouvelle mémoire partagée pouvant */
|
||||||
|
///* contenir 10 caractères */
|
||||||
|
///* c) le père écrit « BONJOUR » dans cette mémoire partagée */
|
||||||
|
///* d) le fils affiche le contenu de la mémoire partagé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 être détectes */
|
||||||
|
///* et donne lieu à 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
S3T_TP_06_IPC/instructionsGCCLRT.txt
Normal file
2
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
S3T_TP_06_IPC/~$11_TP_06-SIGNAUX.docx
Normal file
BIN
S3T_TP_06_IPC/~$11_TP_06-SIGNAUX.docx
Normal file
Binary file not shown.
BIN
S3T_TP_07_THREADS/18_19_M311_S3T_IE.PDF
Normal file
BIN
S3T_TP_07_THREADS/18_19_M311_S3T_IE.PDF
Normal file
Binary file not shown.
BIN
S3T_TP_07_THREADS/Demonstrations/demo_mutex
Executable file
BIN
S3T_TP_07_THREADS/Demonstrations/demo_mutex
Executable file
Binary file not shown.
57
S3T_TP_07_THREADS/Demonstrations/demo_mutex.c
Normal file
57
S3T_TP_07_THREADS/Demonstrations/demo_mutex.c
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
///********************************************************************/
|
||||||
|
///* IUT NICE-COTE D'AZUR - Departement INFORMATIQUE - R. CHIGNOLI */
|
||||||
|
///* Module DUT M311 Theme PTHREADS */
|
||||||
|
///********************************************************************/
|
||||||
|
/// demo_mutex.c : Demonstration simple d'utilisation des
|
||||||
|
/// pthreads-mutex rapides.
|
||||||
|
/// Utiliser par exemple les séquences LU0 LL0 LULU0 LUUL0
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
|
pthread_t filsA;
|
||||||
|
pthread_mutex_t fastmutex = PTHREAD_MUTEX_INITIALIZER;
|
||||||
|
char *sequence;
|
||||||
|
|
||||||
|
void* action (void* name) {
|
||||||
|
int encore = 0;
|
||||||
|
while (sequence [encore] != '0') {
|
||||||
|
switch (sequence [encore]) {
|
||||||
|
case 'L': printf("L ... ");
|
||||||
|
if (pthread_mutex_lock (&fastmutex) != 0)
|
||||||
|
printf("... L PB.\n"); else printf("... L OK.\n"); break;
|
||||||
|
case 'U': printf("U ... ");
|
||||||
|
if (pthread_mutex_unlock (&fastmutex) != 0)
|
||||||
|
printf("... U PB.\n"); else printf("... U OK.\n"); break;
|
||||||
|
case 'X': printf("X ... ");
|
||||||
|
if (pthread_mutex_destroy (&fastmutex) != 0)
|
||||||
|
printf("... X PB.\n"); else printf("... X OK.\n"); break;
|
||||||
|
default: printf("... Commande incorrecte\n");
|
||||||
|
}
|
||||||
|
encore ++;
|
||||||
|
}
|
||||||
|
printf("... Bye\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main (int argc, char* argv[]) {
|
||||||
|
pthread_t filsA;
|
||||||
|
assert (argc ==2);
|
||||||
|
sequence = argv[1];
|
||||||
|
printf ("\nEssayer avec les sequences : LU0 LL0 LULU0 LUUL0\n\n");
|
||||||
|
|
||||||
|
if (pthread_create(&filsA, NULL, action, "filsA")) {x
|
||||||
|
perror("pthread_create");
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\nPERE (Thread principal) : boucle infinie\n\n") ;
|
||||||
|
for(;;);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
BIN
S3T_TP_07_THREADS/Demonstrations/demo_pthread
Executable file
BIN
S3T_TP_07_THREADS/Demonstrations/demo_pthread
Executable file
Binary file not shown.
38
S3T_TP_07_THREADS/Demonstrations/demo_pthread.c
Normal file
38
S3T_TP_07_THREADS/Demonstrations/demo_pthread.c
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
///********************************************************************/
|
||||||
|
///* IUT NICE-COTE D'AZUR - Departement INFORMATIQUE - R. CHIGNOLI */
|
||||||
|
///* Module DUT M311 Theme PTHREADS */
|
||||||
|
///********************************************************************/
|
||||||
|
/// demo_pthread.c : Demonstration d'utilisation des threads
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
|
||||||
|
void* action (void* name) {
|
||||||
|
printf ("\n... ACTION par thread %s (pid : %d) ... Puis MORT\n\n",
|
||||||
|
(char*) name, getpid());
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main (void) {
|
||||||
|
pthread_t filsA, filsB;
|
||||||
|
|
||||||
|
printf ("\nDebut du pere ( pid : %d ; ppid : %d ) )\n\n",
|
||||||
|
getpid(), getppid());
|
||||||
|
|
||||||
|
if (pthread_create(&filsA, NULL, action, "filsA")) {
|
||||||
|
perror("pthread_create");
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pthread_create(&filsB, NULL, action, "filsB")) {
|
||||||
|
perror("pthread_create");
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
printf("\nFin du pere\n\n") ;
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
BIN
S3T_TP_07_THREADS/Demonstrations/demo_system
Executable file
BIN
S3T_TP_07_THREADS/Demonstrations/demo_system
Executable file
Binary file not shown.
22
S3T_TP_07_THREADS/Demonstrations/demo_system.c
Normal file
22
S3T_TP_07_THREADS/Demonstrations/demo_system.c
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
///********************************************************************/
|
||||||
|
///* IUT NICE-COTE D'AZUR - Departement INFORMATIQUE - R. CHIGNOLI */
|
||||||
|
///* Module DUT M311 Theme PTHREADS */
|
||||||
|
///********************************************************************/
|
||||||
|
/// demo_system : Demonstration d'utilisation de "system"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <assert.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
|
||||||
|
int main (int argc, char* argv[]) {
|
||||||
|
int i;
|
||||||
|
assert (argc == 2);
|
||||||
|
for (i=1; i<=3; i++) { /// 3 : par exemple ...
|
||||||
|
system (argv[1]);
|
||||||
|
printf (" ... Au suivant ...\n");
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
34
S3T_TP_07_THREADS/Demonstrations/ds_pthread.c
Normal file
34
S3T_TP_07_THREADS/Demonstrations/ds_pthread.c
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
|
||||||
|
|
||||||
|
//
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
|
||||||
|
int GlobV = 0;
|
||||||
|
|
||||||
|
void* action (void* increment) {
|
||||||
|
for (i=1 ; i<= NOMBRE; i++) globV = globV + *increment;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main (int argc; char* argv[]) {
|
||||||
|
pthread_t unThread;
|
||||||
|
int valeur;
|
||||||
|
assert (argc == 2);
|
||||||
|
valeur = atoi(argv[1];
|
||||||
|
|
||||||
|
if (pthread_create(&unThread, NULL, action, &valeur) {
|
||||||
|
perror("pthread_create");
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
printf("\nmain : globV = %d \n\n", globV) ;
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
13
S3T_TP_07_THREADS/Demonstrations/makefile
Normal file
13
S3T_TP_07_THREADS/Demonstrations/makefile
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
CFLAGS = -Wall -g -std=gnu99 -lpthread
|
||||||
|
CC = gcc
|
||||||
|
|
||||||
|
EXECUTABLES = demo_pthread \
|
||||||
|
demo_system \
|
||||||
|
pthread10 \
|
||||||
|
demo_mutex
|
||||||
|
|
||||||
|
all : ${EXECUTABLES}
|
||||||
|
|
||||||
|
clean :
|
||||||
|
@rm -f core *.o *.out *~
|
||||||
|
@rm -f ${EXECUTABLES}
|
BIN
S3T_TP_07_THREADS/Demonstrations/pthread10
Executable file
BIN
S3T_TP_07_THREADS/Demonstrations/pthread10
Executable file
Binary file not shown.
38
S3T_TP_07_THREADS/Demonstrations/pthread10.c
Normal file
38
S3T_TP_07_THREADS/Demonstrations/pthread10.c
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
///********************************************************************/
|
||||||
|
///* IUT NICE-COTE D'AZUR - Departement INFORMATIQUE - R. CHIGNOLI */
|
||||||
|
///* Module DUT M311 Theme PTHREADS */
|
||||||
|
///********************************************************************/
|
||||||
|
/// pthread10.c : squelette pour creation de trois threads
|
||||||
|
/// Cf enonce ...
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
|
typedef struct { int nombre; char car;} struct_p;
|
||||||
|
|
||||||
|
void imprimer (struct_p* param){
|
||||||
|
int i;
|
||||||
|
for (i=0 ; i < param->nombre; i++) printf ("%c", param->car); }
|
||||||
|
|
||||||
|
int main (int argc, char* argv[]) {
|
||||||
|
int i, valeur;
|
||||||
|
struct_p x;
|
||||||
|
char tab[3] = {'/', '*', '='};
|
||||||
|
|
||||||
|
assert (argc == 2);
|
||||||
|
valeur = atoi (argv[1]);
|
||||||
|
for (i=1; i<=3; i++) {
|
||||||
|
printf ("\nMAIN : Tour de boucle nO %d\n", i);
|
||||||
|
/// A VOUS DE JOUER
|
||||||
|
/// Creation des trois threads d'affichage ...
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
BIN
S3T_TP_07_THREADS/Demonstrations/pthread4
Executable file
BIN
S3T_TP_07_THREADS/Demonstrations/pthread4
Executable file
Binary file not shown.
41
S3T_TP_07_THREADS/Demonstrations/pthread4.c
Normal file
41
S3T_TP_07_THREADS/Demonstrations/pthread4.c
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
///********************************************************************/
|
||||||
|
///* IUT NICE-COTE D'AZUR - Departement INFORMATIQUE - R. CHIGNOLI */
|
||||||
|
///* Module DUT M311 Theme PTHREADS */
|
||||||
|
///********************************************************************/
|
||||||
|
/// demo_pthread.c : Demonstration d'utilisation des threads
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
|
||||||
|
void* action (void* name) {
|
||||||
|
printf ("\n... ACTION par thread %s (pid : %d) ... Puis MORT\n\n",
|
||||||
|
(char*) name, getpid());
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main (void) {
|
||||||
|
pthread_t filsA, filsB;
|
||||||
|
|
||||||
|
printf ("\nDebut du pere ( pid : %d ; ppid : %d ) )\n\n",
|
||||||
|
getpid(), getppid());
|
||||||
|
|
||||||
|
if (pthread_create(&filsA, NULL, action, "filsA")) {
|
||||||
|
perror("pthread_create");
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pthread_create(&filsB, NULL, action, "filsB")) {
|
||||||
|
perror("pthread_create");
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
printf("\nFin du pere\n\n") ;
|
||||||
|
|
||||||
|
for(;;) {}
|
||||||
|
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
131
S3T_TP_07_THREADS/thread_somme_tab_SOLUTION.c
Normal file
131
S3T_TP_07_THREADS/thread_somme_tab_SOLUTION.c
Normal file
@ -0,0 +1,131 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
/// Processus legers
|
||||||
|
|
||||||
|
#define TAILLE 8 /// Multiple de 4 !!!!
|
||||||
|
|
||||||
|
int SOMME; /// Variable GLOBALE en mutex (somme totale)
|
||||||
|
int NOMBRE; /// Variable GLOBALE en mutex (nbre de reponses)
|
||||||
|
int sommeFinale[4] = {-1}; /// Variable GLOBALE pour les sous-tableau
|
||||||
|
|
||||||
|
pthread_mutex_t mutexSomme = PTHREAD_MUTEX_INITIALIZER; /// mutex
|
||||||
|
|
||||||
|
typedef struct TF
|
||||||
|
{
|
||||||
|
int numero;
|
||||||
|
int *tabAdresse;
|
||||||
|
int tabNombre;
|
||||||
|
} tabParams;
|
||||||
|
|
||||||
|
void initTabAleat (int *leTableau, int leNombre);
|
||||||
|
/// Initialise <leTableau> de <leNombre> elements
|
||||||
|
/// de façon aleatoire avec des nombres tous differents.
|
||||||
|
|
||||||
|
int sommeTab (int *leTableau, int leNombre);
|
||||||
|
/// retourne la somme des <leNombre> elements de <leTableau>.
|
||||||
|
|
||||||
|
void afficheTableau (int leTableau [], int leNombre);
|
||||||
|
/// Affiche <leTableau> de <leNombre> elements sur la sortie standard.
|
||||||
|
|
||||||
|
void initTabAleat (int leTableau [], int leNombre)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int min = 0;
|
||||||
|
srand (time (NULL));
|
||||||
|
|
||||||
|
for (i=0; i<leNombre; i++)
|
||||||
|
{
|
||||||
|
leTableau [i] = (rand() % (leNombre - min + 1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int sommeTab (int leTableau [], int leNombre)
|
||||||
|
{
|
||||||
|
int i, somme = 0;
|
||||||
|
for(i=0 ; i < leNombre ; i++)
|
||||||
|
{
|
||||||
|
somme = somme + leTableau[i];
|
||||||
|
}
|
||||||
|
return somme;
|
||||||
|
}
|
||||||
|
|
||||||
|
void afficheTableau (int leTableau [], int leNombre)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
printf ("\n==> tableau : ");
|
||||||
|
for (i=0; i <leNombre; i++)
|
||||||
|
printf ("%d ", leTableau[i]);
|
||||||
|
printf ("\n\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void ajouterGlob (int unNombre)
|
||||||
|
{
|
||||||
|
pthread_mutex_lock (&mutexSomme);
|
||||||
|
SOMME = SOMME + unNombre;
|
||||||
|
NOMBRE++;
|
||||||
|
pthread_mutex_unlock (&mutexSomme);
|
||||||
|
}
|
||||||
|
|
||||||
|
int threadFonction (tabParams *param)
|
||||||
|
{
|
||||||
|
int local;
|
||||||
|
printf ("Lancement threadFonction ...\n");
|
||||||
|
local = sommeTab (param->tabAdresse, param->tabNombre);
|
||||||
|
|
||||||
|
sommeFinale [param->numero] = local;
|
||||||
|
ajouterGlob (local);
|
||||||
|
|
||||||
|
return local;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int * grandTableau;
|
||||||
|
pthread_t tache[4];
|
||||||
|
tabParams *localParam;
|
||||||
|
|
||||||
|
SOMME = 0;
|
||||||
|
NOMBRE = 0;
|
||||||
|
|
||||||
|
grandTableau = malloc (sizeof(int) * TAILLE);
|
||||||
|
|
||||||
|
initTabAleat (grandTableau, TAILLE);
|
||||||
|
afficheTableau(grandTableau, TAILLE);
|
||||||
|
|
||||||
|
for (i=0; i<4; i++)
|
||||||
|
{
|
||||||
|
localParam = malloc (sizeof (tabParams));
|
||||||
|
localParam->numero = i;
|
||||||
|
localParam->tabAdresse = grandTableau + (i * TAILLE / 4);
|
||||||
|
localParam->tabNombre = TAILLE / 4;
|
||||||
|
pthread_create(&tache[i], NULL, (void*(*)(void*)) threadFonction,
|
||||||
|
localParam);
|
||||||
|
}
|
||||||
|
/// Version 1 : avec pthread_join
|
||||||
|
pthread_join (tache[0], NULL); pthread_join (tache[1], NULL);
|
||||||
|
pthread_join (tache[2], NULL); pthread_join (tache[3], NULL);
|
||||||
|
printf ("\nVERSION 1 : OK !\n");
|
||||||
|
|
||||||
|
/// Version 2 : attente du remplissage de sommeFinale []
|
||||||
|
while ( sommeFinale [0] == 0 || sommeFinale [1] == 0 ||
|
||||||
|
sommeFinale [2] == 0 || sommeFinale [3] == 0 );
|
||||||
|
printf ("VERSION 2 : OK !\n");
|
||||||
|
|
||||||
|
/// Version 3 : attente de NOMBRE == 4
|
||||||
|
while ( NOMBRE != 4);
|
||||||
|
printf ("VERSION 3 : OK !\n");
|
||||||
|
|
||||||
|
/// Affichage toutes versions ...
|
||||||
|
SOMME = sommeFinale [0] + sommeFinale [1] +
|
||||||
|
sommeFinale [2] + sommeFinale [3] ;
|
||||||
|
|
||||||
|
printf ( "\nSOMME : %d = %d + %d + %d + %d\n", SOMME, sommeFinale [0],
|
||||||
|
sommeFinale [1],sommeFinale [2], sommeFinale [3]);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
62
S3T_TP_08_TCP-IP/DEMOS/client_tcp.c
Normal file
62
S3T_TP_08_TCP-IP/DEMOS/client_tcp.c
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
/******************************************************/
|
||||||
|
/* S3T - M311 - CHIGNOLI */
|
||||||
|
/* Client IPC/BERKELEY -- DOMAINE INET -- MODE TCP */
|
||||||
|
/* A FINIR - A FINIR -A FINIR - A FINIR - A FINIR */
|
||||||
|
/******************************************************/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <strings.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#include <netdb.h>
|
||||||
|
#include <signal.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
|
||||||
|
#define BUFSIZE 80
|
||||||
|
int sd;
|
||||||
|
|
||||||
|
int main (int argc, char* argv [])
|
||||||
|
{
|
||||||
|
char buffer [BUFSIZE];
|
||||||
|
struct hostent *host_name;
|
||||||
|
struct sockaddr_in socket_name;
|
||||||
|
|
||||||
|
if (argc != 2)
|
||||||
|
{
|
||||||
|
printf ("\nSYNTAXE : %s <machine>\n\n", argv[0]); exit (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Obtention d'un descripteur de "socket" */
|
||||||
|
if ((sd = socket (PF_INET, SOCK_DGRAM, 0)) == -1)
|
||||||
|
{ perror ("*** Echec socket ***"); exit (2); }
|
||||||
|
|
||||||
|
/* Recherche de l'adresse INTERNET de la machine du serveur */
|
||||||
|
host_name = gethostbyname (argv[1]);
|
||||||
|
if (host_name == 0)
|
||||||
|
{ fprintf (stderr, "\n*** Echec gethostbyname ***\n\n"); exit(1); }
|
||||||
|
|
||||||
|
/* Paramétrage de la "socket" */
|
||||||
|
bzero ( (char *) &socket_name, sizeof (socket_name) ); // mise à zero
|
||||||
|
socket_name.sin_family = AF_INET;
|
||||||
|
socket_name.sin_port = 6200;
|
||||||
|
bcopy (host_name -> h_addr, &socket_name.sin_addr, host_name -> h_length);
|
||||||
|
|
||||||
|
/* Affichage infos socket */
|
||||||
|
printf ("CLIENT sur machine %s (%u) - port %d\n",
|
||||||
|
inet_ntoa (socket_name.sin_addr),
|
||||||
|
socket_name.sin_addr, socket_name.sin_port);
|
||||||
|
|
||||||
|
/**************************************************/
|
||||||
|
/* A FAIRE : */
|
||||||
|
/* - demande de connexion */
|
||||||
|
/* - envoi de 80 caracteres au serveur */
|
||||||
|
/* - recuperation de 80 caracteres du serveur */
|
||||||
|
/* - affichage des 80 caractères retournés */
|
||||||
|
/**************************************************/
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
13
S3T_TP_08_TCP-IP/DEMOS/demo_dialogue.c
Normal file
13
S3T_TP_08_TCP-IP/DEMOS/demo_dialogue.c
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <strings.h>
|
||||||
|
|
||||||
|
int main (int argc, char* argv[])
|
||||||
|
{
|
||||||
|
char buffer [80];
|
||||||
|
if (argc != 2) fprintf (stderr, "\nERREUR ... Syntaxe : %s nom_login\n\n", argv[0]), exit(1);
|
||||||
|
|
||||||
|
bzero (buffer, 80); // les '\0' sur 80 caracteres !!
|
||||||
|
sprintf (buffer, "%s OUI",argv[1]);
|
||||||
|
printf ("*%s*\n", buffer);
|
||||||
|
}
|
13
S3T_TP_08_TCP-IP/DEMOS/demo_echange.c
Normal file
13
S3T_TP_08_TCP-IP/DEMOS/demo_echange.c
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <strings.h>
|
||||||
|
|
||||||
|
int main (int argc, char* argv[])
|
||||||
|
{
|
||||||
|
char buffer [80];
|
||||||
|
if (argc != 2) fprintf (stderr, "\nERREUR ... Syntaxe : %s chaine\n\n", argv[0]), exit(1);
|
||||||
|
bzero (buffer, 80);
|
||||||
|
strcpy (buffer, argv[1]);
|
||||||
|
printf ("\nBuffer a envoyer ou a recevoir : %s\n\n", buffer);
|
||||||
|
}
|
75
S3T_TP_08_TCP-IP/DEMOS/demo_serveur_tcp.c
Normal file
75
S3T_TP_08_TCP-IP/DEMOS/demo_serveur_tcp.c
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
/******************************************************/
|
||||||
|
/* S3T - M311 - CHIGNOLI */
|
||||||
|
/* Serveur IPC/BERKELEY -- DOMAINE INET -- MODE TCP */
|
||||||
|
/* A FINIR - A FINIR -A FINIR - A FINIR - A FINIR */
|
||||||
|
/******************************************************/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <strings.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#include <netdb.h>
|
||||||
|
#include <signal.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
|
||||||
|
#define BUFSIZE 80
|
||||||
|
int sd;
|
||||||
|
|
||||||
|
void arret_service()
|
||||||
|
{
|
||||||
|
printf("\n SERVEUR : SIGINT recu => Arret du SERVICE\n\n");
|
||||||
|
close(sd);
|
||||||
|
exit (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main (int argc, char* argv [])
|
||||||
|
{
|
||||||
|
char buffer [BUFSIZE];
|
||||||
|
struct hostent *host_name;
|
||||||
|
struct sockaddr_in socket_name;
|
||||||
|
|
||||||
|
if (argc != 2)
|
||||||
|
{
|
||||||
|
printf ("\nSYNTAXE : %s <machine>\n\n", argv[0]); exit (1);
|
||||||
|
}
|
||||||
|
/* Armement du signal SIGINT pour arret "propre" du serveur */
|
||||||
|
signal(SIGINT,arret_service);
|
||||||
|
|
||||||
|
/* Obtention d'un descripteur de "socket" */
|
||||||
|
if ((sd = socket (PF_INET, SOCK_DGRAM, 0)) == -1)
|
||||||
|
{ perror ("*** Echec socket ***"); exit (2); }
|
||||||
|
|
||||||
|
/* Recherche de l'adresse INTERNET de la machine du serveur */
|
||||||
|
host_name = gethostbyname (argv[1]);
|
||||||
|
if (host_name == 0)
|
||||||
|
{ fprintf (stderr, "\n*** Echec gethostbyname ***\n\n"); exit(1); }
|
||||||
|
|
||||||
|
/* Paramétrage de la "socket" */
|
||||||
|
bzero ( (char *) &socket_name, sizeof (socket_name) ); // mise à zero
|
||||||
|
socket_name.sin_family = AF_INET;
|
||||||
|
socket_name.sin_port = 6200;
|
||||||
|
bcopy (host_name -> h_addr, &socket_name.sin_addr, host_name -> h_length);
|
||||||
|
|
||||||
|
/* Affichage infos socket */
|
||||||
|
printf ("SERVEUR sur machine %s (%u) - port %d\n",
|
||||||
|
inet_ntoa (socket_name.sin_addr),
|
||||||
|
socket_name.sin_addr, socket_name.sin_port);
|
||||||
|
|
||||||
|
/**************************************************/
|
||||||
|
/* A FAIRE : Suite du parametrage du "serveur" */
|
||||||
|
/**************************************************/
|
||||||
|
|
||||||
|
/* Boucle de traitement des demandes de connexion */
|
||||||
|
while (1)
|
||||||
|
{
|
||||||
|
/**************************************************/
|
||||||
|
/* A FAIRE : Attente des demandes de connexion */
|
||||||
|
/* d'un client et traitement. */
|
||||||
|
/**************************************************/
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
13
S3T_TP_08_TCP-IP/DEMOS/demo_wait3.c
Normal file
13
S3T_TP_08_TCP-IP/DEMOS/demo_wait3.c
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
:*******************************/
|
||||||
|
/* handler de signaux SIGCHILD */
|
||||||
|
/* Comptage de fin de fils */
|
||||||
|
/*******************************/
|
||||||
|
|
||||||
|
void handler_SIGCHLD()
|
||||||
|
/* Pour eliminer les zombies */
|
||||||
|
{
|
||||||
|
while (wait3 (NULL, WNOHANG, NULL) > 0)
|
||||||
|
{
|
||||||
|
nb_fils--;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user