"depot M311"
This commit is contained in:
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;
|
||||
}
|
Reference in New Issue
Block a user