TP3
This commit is contained in:
@@ -0,0 +1 @@
|
||||
je suis le pereje suis le fils !!!
|
||||
@@ -0,0 +1 @@
|
||||
je suis le fils !!!
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
printf("NON");
|
||||
if (fork()) {
|
||||
printf("OUI\n");
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
La voie lactée, les gros yeux, le bonheur d'une impératrice
|
||||
@@ -0,0 +1 @@
|
||||
La voie lactée, les gros yeux, le bonheur dune' impératrice
|
||||
@@ -0,0 +1,18 @@
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
if (fork()){
|
||||
printf("session = %d\n",getsid(getpid()));
|
||||
while(1);
|
||||
} else {
|
||||
setsid();
|
||||
printf("session = %d\n",getsid(getpid()));
|
||||
while(1);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
Exercice 1)
|
||||
|
||||
Avec stdio, le contenu du printf est mis en cache donc lors de la création du fils vu que les données sont partagées cela fait que le non est affiché deux fois
|
||||
Or que l'appel système write envoie directement à la sortie standard
|
||||
|
||||
Exercice 2)
|
||||
Premier code : Le fork est après l'ouverture donc le père et le fils se partagent le fd et si l'un écrit l'offset se met à jour pour que les contenus des deux processus soient enregistrés
|
||||
Deuxième code : Le fork est avant l'ouverture donc chaque processus à son propre file descriptor, l'offset n'est pas partagé donc le contenu du fichier sera celui du dernier processus qui a écrit uniquement
|
||||
|
||||
Exercice 3)
|
||||
Le code copie le contenu d'une fichier vers un autre
|
||||
En décommentant le fork
|
||||
Reference in New Issue
Block a user