SCR
This commit is contained in:
12
TP3/exo1/ex1-stdio.c
Normal file
12
TP3/exo1/ex1-stdio.c
Normal file
@@ -0,0 +1,12 @@
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
printf("NON");
|
||||
if (fork()) {
|
||||
printf("OUI\n");
|
||||
}
|
||||
}
|
14
TP3/exo1/ex1-syscall.c
Normal file
14
TP3/exo1/ex1-syscall.c
Normal file
@@ -0,0 +1,14 @@
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
//printf("NON");
|
||||
write(STDOUT_FILENO,"NON",3);
|
||||
if (fork()) {
|
||||
//printf("OUI\n");
|
||||
write(STDOUT_FILENO,"OUI\n",4);
|
||||
}
|
||||
}
|
9
TP3/exo1/rep ex1.txt
Normal file
9
TP3/exo1/rep ex1.txt
Normal file
@@ -0,0 +1,9 @@
|
||||
ce que fait grossièrement le fichier ex1-stdio.c est:
|
||||
-Il affiche d'abord le résultat du printf (NON) -> l'enregistre dans le buffer
|
||||
-puis il lance une condition du fork (la fonction qui va gérer les processus)
|
||||
-Donc le processus duplique le père (printf("NON')) et le fils dans le buffer
|
||||
-Le père écrit NON à la suite du OUI (fils)
|
||||
Donc le résultat sera:
|
||||
NONOUI
|
||||
NON
|
||||
et les 2 sont vidés à la fin dans le terminal.
|
0
TP3/exo1/rep ex1.txt~
Normal file
0
TP3/exo1/rep ex1.txt~
Normal file
BIN
TP3/exo1/stdio
Executable file
BIN
TP3/exo1/stdio
Executable file
Binary file not shown.
BIN
TP3/exo1/syscall
Executable file
BIN
TP3/exo1/syscall
Executable file
Binary file not shown.
1
TP3/exo2/#exo2-rep.txt#
Normal file
1
TP3/exo2/#exo2-rep.txt#
Normal file
@@ -0,0 +1 @@
|
||||
pour l'exo 2,
|
45
TP3/exo2/copy1byte.c
Normal file
45
TP3/exo2/copy1byte.c
Normal file
@@ -0,0 +1,45 @@
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include<stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include<assert.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
|
||||
#define msg1 "je suis le pere"
|
||||
#define msg2 "je suis le fils !!!"
|
||||
|
||||
int main(int argc,char * argv[]){
|
||||
|
||||
int infd,outfd;
|
||||
ssize_t nbread;
|
||||
char buf[1];
|
||||
pid_t p;
|
||||
|
||||
if (argc != 3){
|
||||
|
||||
printf("%s infile outfile\n",argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
infd = open(argv[1],O_RDONLY);
|
||||
assert(infd >= 0);
|
||||
outfd = open(argv[2],O_WRONLY|O_CREAT|O_TRUNC,0644);
|
||||
assert(outfd >= 0);
|
||||
|
||||
//p=fork(); // <- decommentez cette ligne
|
||||
|
||||
while(1){
|
||||
|
||||
nbread=read(infd,buf,sizeof(buf));
|
||||
if (nbread <=0 ) break;
|
||||
write(outfd,buf,sizeof(buf));
|
||||
|
||||
}
|
||||
|
||||
|
||||
close(infd);
|
||||
close(outfd);
|
||||
}
|
42
TP3/exo2/fork_and_fd1.c
Normal file
42
TP3/exo2/fork_and_fd1.c
Normal file
@@ -0,0 +1,42 @@
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include<stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include<assert.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
|
||||
#define msg1 "je suis le pere"
|
||||
#define msg2 "je suis le fils !!!"
|
||||
|
||||
int main(int argc,char * argv[]){
|
||||
|
||||
int outfd;
|
||||
pid_t p;
|
||||
|
||||
if (argc != 2){
|
||||
|
||||
printf("%s file\n",argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
//p=fork();
|
||||
outfd = open(argv[1],O_WRONLY|O_CREAT,0644);
|
||||
assert(outfd >= 0);
|
||||
p=fork();
|
||||
switch(p){
|
||||
case (pid_t)-1 :
|
||||
perror(NULL);
|
||||
exit(2);
|
||||
|
||||
case (pid_t)0 :
|
||||
write(outfd,msg2,strlen(msg2));
|
||||
break;
|
||||
default :
|
||||
write (outfd,msg1,strlen(msg1));
|
||||
break;
|
||||
}
|
||||
close(outfd);
|
||||
}
|
42
TP3/exo2/fork_and_fd2.c
Normal file
42
TP3/exo2/fork_and_fd2.c
Normal file
@@ -0,0 +1,42 @@
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include<stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include<assert.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
|
||||
#define msg1 "je suis le pere"
|
||||
#define msg2 "je suis le fils !!!"
|
||||
|
||||
int main(int argc,char * argv[]){
|
||||
|
||||
int outfd;
|
||||
pid_t p;
|
||||
|
||||
if (argc != 2){
|
||||
|
||||
printf("%s file\n",argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
p=fork();
|
||||
outfd = open(argv[1],O_WRONLY|O_CREAT,0644);
|
||||
assert(outfd >= 0);
|
||||
//p=fork();
|
||||
switch(p){
|
||||
case (pid_t)-1 :
|
||||
perror(NULL);
|
||||
exit(2);
|
||||
|
||||
case (pid_t)0 :
|
||||
write(outfd,msg2,strlen(msg2));
|
||||
break;
|
||||
default :
|
||||
write (outfd,msg1,strlen(msg1));
|
||||
break;
|
||||
}
|
||||
close(outfd);
|
||||
}
|
Reference in New Issue
Block a user