Article View: fr.comp.lang.c
Article #15030probleme avec les fonctions exec...
From: Thierry B
Date: Sat, 15 Oct 2005 15:46
Date: Sat, 15 Oct 2005 15:46
159 lines
5677 bytes
5677 bytes
Bonjour,
j'ai un problème avec un fichier source que j'ai ecrit:
------------------------------------------------------------------------------------
#define _POSIX_SOURCE 1
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
int main(int argc, char **argv){
pid_t pid_fils;
int i;
/* Structure pour récupérer les informations de terminaison des
fils */
struct fils {
int fpid;
int retour;
};
struct fils fils[6];
for (i=0; i < 6; i++){
pid_fils = fork();
fils[i].fpid = pid_fils;
if (pid_fils == 0) {
switch (i){
case 0:{
printf("FILS %d va faire un ls -l\n", i+1);
execl("/bin/ls","ls","-l",NULL);
perror("execl");
return 0;
break;
}
case 1:{
printf("FILS %d va faire un ls -l\n", i+1);
execlp("ls","ls","-l",NULL);
perror("execlp");
return 0;
break;
}
case 2:{
printf("FILS %d va faire un echo $PATH\n", i+1);
execle("/bin/echo","echo","$PATH", NULL, NULL);
perror("execle");
return 0;
break;
}
case 3:{
char *tab[3] = {"ls", "-l", NULL};
printf("FILS %d va faire un ls -l\n", i+1);
execv("/bin/ls",tab);
perror("execv");
return 0;
break;
}
case 4:{
char *tab[3] = {"ls", "-l",NULL};
printf("FILS %d va faire un ls -l\n", i+1);
execvp("ls",tab);
perror("execvp");
return 0;
break;
}
case 5:{
char *tab[3] = {"echo","$PATH",NULL};
printf("FILS %d va faire un echo $PATH\n", i+1);
execve("/bin/echo",tab,NULL);
perror("execve");
return 0;
break;
}
}
exit(0);
}
}
/* attente de la terminaison des fils et récupération des
informations sur leurs terminaisons */
for (i = 0; i < 6; i++){
waitpid(fils[i].fpid, &fils[i].retour, WNOHANG);
}
/* Affichage des informations sur la terminaison des fils */
for (i = 0; i < 6; i++){
if ((WIFEXITED(fils[i].retour)) != 0) {
printf("Terminaison du fils %d correcte.\n", i+1);
}
printf("Code de retour du fils %d : %d\n", i+1,
WEXITSTATUS(fils[i].retour));
}
return 0;
}
-------------------------------------------------------------------------------------------
Ca me donne ceci:
thierry@debian:~/Master/Posix/Semaine2/Partie2$ ./bin/question2
FILS 1 va faire un ls -l
total 20
drwxr-xr-x 2 thierry thierry 4096 2005-10-15 17:40 bin
drwxr-xr-x 2 thierry thierry 4096 2005-10-15 17:12 include
-rw-r--r-- 1 thierry thierry 469 2005-10-15 17:12 makefile
drwxr-xr-x 2 thierry thierry 4096 2005-10-15 17:40 obj
drwxr-xr-x 2 thierry thierry 4096 2005-10-15 17:40 src
FILS 2 va faire un ls -l
FILS 3 va faire un echo $PATH
$PATH
FILS 4 va faire un ls -l
total 20
drwxr-xr-x 2 thierry thierry 4096 2005-10-15 17:40 bin
drwxr-xr-x 2 thierry thierry 4096 2005-10-15 17:12 include
-rw-r--r-- 1 thierry thierry 469 2005-10-15 17:12 makefile
drwxr-xr-x 2 thierry thierry 4096 2005-10-15 17:40 obj
drwxr-xr-x 2 thierry thierry 4096 2005-10-15 17:40 src
total 20
drwxr-xr-x 2 thierry thierry 4096 2005-10-15 17:40 bin
drwxr-xr-x 2 thierry thierry 4096 2005-10-15 17:12 include
-rw-r--r-- 1 thierry thierry 469 2005-10-15 17:12 makefile
drwxr-xr-x 2 thierry thierry 4096 2005-10-15 17:40 obj
drwxr-xr-x 2 thierry thierry 4096 2005-10-15 17:40 src
FILS 5 va faire un ls -l
FILS 6 va faire un echo $PATH
$PATH
Terminaison du fils 1 correcte.
Code de retour du fils 1 : 0
Terminaison du fils 2 correcte.
Code de retour du fils 2 : 0
Terminaison du fils 3 correcte.
Code de retour du fils 3 : 0
Terminaison du fils 4 correcte.
Code de retour du fils 4 : 0
Code de retour du fils 5 : 136
Terminaison du fils 6 correcte.
Code de retour du fils 6 : 0
thierry@debian:~/Master/Posix/Semaine2/Partie2$ total 20
drwxr-xr-x 2 thierry thierry 4096 2005-10-15 17:40 bin
drwxr-xr-x 2 thierry thierry 4096 2005-10-15 17:12 include
-rw-r--r-- 1 thierry thierry 469 2005-10-15 17:12 makefile
drwxr-xr-x 2 thierry thierry 4096 2005-10-15 17:40 obj
drwxr-xr-x 2 thierry thierry 4096 2005-10-15 17:40 src
thierry@debian:~/Master/Posix/Semaine2/Partie2$
Je remarque que le fils 5 (donc le case 4 ne se termine pas) et aussi
que le fils 3 et 6 (donc case 2 et 5) qui sont censé faire echo $PATH
affichent $PATH, mais pourtant un "echo $PATH" dans un shell affiche le
contenu de la variable PATH, ce qui n'est pas le cas dans mon
programme.
Merci :-)
A+
Technical Details
Message-ID:
<1129391183.461767.154610@z14g2000cwz.googlegroups.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!postnews.google.com!z14g2000cwz.googlegroups.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Thread: f9822,f48dca767454f981,start X-Google-Attributes: gidf9822,public X-Google-Language: FRENCH,ASCII Path: arc1-ca.pugleaf.net!g2news1.google.com!postnews.google.com!z14g2000cwz.googlegroups.com!not-for-mail From: "Thierry B" <thierry.bouhnik@gmail.com> Newsgroups: fr.comp.lang.c Subject: probleme avec les fonctions exec... Date: 15 Oct 2005 08:46:23 -0700 Organization: http://groups.google.com Lines: 160 Message-ID: <1129391183.461767.154610@z14g2000cwz.googlegroups.com> NNTP-Posting-Host: 82.224.47.63 Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1129391188 11538 127.0.0.1 (15 Oct 2005 15:46:28 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Sat, 15 Oct 2005 15:46:28 +0000 (UTC) User-Agent: G2/0.2 X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux i686; fr-FR; rv:1.7.12) Gecko/20050920 Firefox/1.0.7,gzip(gfe),gzip(gfe) Complaints-To: groups-abuse@google.com Injection-Info: z14g2000cwz.googlegroups.com; posting-host=82.224.47.63; posting-account=8zpqDA0AAACpm8oU5_TKS1gqRyX3CEVa