cm processus

This commit is contained in:
2026-09-16 20:46:50 +02:00
parent 5a51062c63
commit 60816fac42
17 changed files with 382 additions and 2 deletions
+45
View 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);
}
+49
View File
@@ -0,0 +1,49 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#define N 8*1024*1024
char tab[N];
int main(void)
{
for (int i = 0; i < N; i++)
tab[i] = (char)i;
printf("Avant fork : tab = %p\n", (void *)tab);
pid_t pid = fork();
if (pid < 0) {
perror("fork");
exit(EXIT_FAILURE);
}
if (pid == 0) {
printf("Fils : tab = %p, tab[5000] = %d\n",
(void *)(tab+5000), tab[5000]);
printf("FILS PID = %d\n", getpid());
getchar();
tab[5000] = 42;
printf("-> modification\n");
getchar();
printf("Fils après modification : tab[5000] = %d\n",
tab[5000]);
exit(EXIT_SUCCESS);
}
else {
sleep(1);
printf("Père : tab = %p, tab[5000] = %d\n",
(void *)tab, tab[5000]);
wait(NULL);
}
return 0;
}
+12
View 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
View 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);
}
}
+32
View File
@@ -0,0 +1,32 @@
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <assert.h>
#include <sys/wait.h>
int main(int argc, char *argv[])
{
pid_t p;
int status;
p = fork();
assert( p >= 0);
if ( p == 0){
printf("@retour de fork = %d pid = %d ppid = %d\n",p,getpid(),getppid());
sleep(4);
exit(2);
}
printf("@retour de fork = %d pid = %d ppid = %d\n",p,getpid(),getppid());
assert( wait(&status) >= 0);
if (WIFEXITED(status))
printf("code retour fils = %d\n",WEXITSTATUS(status));
execlp("ls","totototo","-l","/",NULL);
/* code ici */
assert(0);
return 0;
}
+43
View File
@@ -0,0 +1,43 @@
#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;
}
getchar();
close(outfd);
}
+43
View File
@@ -0,0 +1,43 @@
#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;
}
getchar();
close(outfd);
}
+18
View File
@@ -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;
}