TD Appels Systeme Corrigé
TD Appels Systeme Corrigé
TD Appels Systeme Corrigé
TD2
Les appels systèmes
P1 P2 P3
rép :
Solution 1 : fork() retourne un boolean
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/wait.h>
int main()
{
fork()&&fork()&&fork();
while(wait(0)>0);
}
R. Beltaifa GLSI1 2020/2021
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/wait.h>
int main()
{
int i;
pid_t pid;
printf("je suis le processus main, mon pid = %d\n",getpid());
for(i=0;i<3;i++)
{
pid = fork();
if (pid == 0)
{printf("mon pid = %d, mon pere a le pid = %d\n", getpid(), getppid());exit(0);
printf("\n");};
}
while(wait(0)>0);
}
Exercice 2 :
P1
P2 P3 P4
P6 P5
Réponse 1 :
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
R. Beltaifa GLSI1 2020/2021
#include<sys/types.h>
#include<sys/wait.h>
int main()
{
printf("\nje suis le processus main, mon pid = %d\n\n",getpid());
fork()&&(fork()||fork())&&fork();
printf("mon pid = %d, mon pere a le pid = %d\n", getpid(), getppid());
printf("\n");
while(wait(0)>0);
}
Réponse V2 :
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/wait.h>
int main()
{
pid1 = fork();
if(pid1==0){
printf("je suis le fils de pid = %d, mon pere a le pid = %d\n", getpid(), getppid());
printf("\n");
exit(0);}
pid2=fork();
if(pid2==0){
R. Beltaifa GLSI1 2020/2021
printf("je suis le fils de pid = %d, mon pere a le pid = %d\n", getpid(), getppid());
printf("\n");
pid21=fork();
if(pid21>0) {
printf("je suis le fils de pid = %d, mon pere a le pid = %d\n", pid21,getpid());
printf("\n");
pid22=fork();
if(pid22>0) {
printf("je suis le fils de pid = %d, mon pere a le pid = %d\n", pid22,getpid());
printf("\n");}
}
exit(0);}
pid3=fork();
if (pid3==0) {
printf("je suis le fils de pid = %d, mon pere a le pid = %d\n", getpid(), getppid());
printf("\n");}
while(wait(0)>0);
Exercice 3 :
Correction :
a.
#include<stdio.h>
#include<unistd.h>
R. Beltaifa GLSI1 2020/2021
#include<stdlib.h>
#include<sys/types.h>
#include<sys/wait.h>
int main()
{
int pid,i;
printf("je suis le processus main, mon pid = %d\n\n",getpid());
for(i=1;i<51;i++) printf("pere : %d ",i);
pid=fork();
if (pid==0){
printf("mon pid = %d, mon pere a le pid = %d\n", getpid(), getppid());
for(i=51;i<101;i++) printf("fils : %d ",i);
printf("\n");}
/* else
{
printf("mon pid = %d, mon pere a le pid = %d\n", getpid(), getppid());
for(i=1;i<51;i++) printf("pere : %d ",i);
printf("\n");} */
while(wait(0)>0);
b.
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/wait.h>
int main()
{
int pid,i;
printf("je suis le processus main, mon pid = %d\n\n",getpid());
pid=fork();
if (pid==0){
printf("mon pid = %d, mon pere a le pid = %d\n", getpid(), getppid());
for(i=1;i<51;i++) printf("fils : %d ",i);
printf("\n");}
R. Beltaifa GLSI1 2020/2021
else
{
wait(NULL);
printf("mon pid = %d, mon pere a le pid = %d\n", getpid(), getppid());
for(i=51;i<101;i++) printf("pere : %d ",i);
printf("\n");}
while(wait(0)>0);