Programmation C

sanoua -  
mamiemando Messages postés 33228 Date d'inscription   Statut Modérateur Dernière intervention   -
Bonjour,

quelqu'un pourrait me dire qu'est ce qu'il fait ce programme et quel est l'affichage possible svp ?

if (fork()){
wait(&status) ;
printf( "\n fils terminé");
exec(commande) ;
printf ("\n pere termine");
}

else {
printf("\n lancement");
exec(commande);
printf("\n fini");

}
Configuration: Linux
Firefox 3.0.14

1 réponse

  1. mamiemando Messages postés 33228 Date d'inscription   Statut Modérateur Dernière intervention   7 944
     
    Tu peux trouver des explications sur toutes les fonctions appelées avec google (en cherchant man nom_de_la_fonction), par exemple "man fork".

    Le programme dans l'état actuel ne peut pas compiler, après corrections voilà à quoi il ressemble :

    #include <stdio.h>
    #include <unistd.h>
    
    int main(){
        int status;
    
        if (fork()){
            wait(&status) ;
            printf( "fils terminé\n");
            execv("ls /",NULL) ;
            printf ("pere termine \n");
        } else {
            printf("lancement\n");
            execv("ls -l /",NULL);
            printf("fini\n");
        }
        return 0;
    }


    Moi j'écrirais plutôt ça :

    #include <stdio.h>
    #include <unistd.h>
    
    int main(){
        int status;
        FILE *fp;
    
        if (fork()){
            wait(&status) ;
            printf( "fils terminé\n");
            // Afficher le contenu de / (un fichier par ligne)
            if((fp = popen("ls -1 /","w")) != NULL){
                pclose(fp);
            }
            printf ("père termine\n");
        } else {
            printf("lancement\n");
            // Afficher le contenu de / et les droits
            if((fp = popen("ls -l /","w")) != NULL){
                pclose(fp);
            }
            printf ("fini\n");
        }
        return 0;
    }


    A l'exécution on obtient ceci :

    lancement
    total 7580
    drwxr-xr-x   2 root root    4096 janv.  9 17:08 bin
    drwxr-xr-x   3 root root    4096 janv.  9 17:15 boot
    lrwxrwxrwx   1 root root      11 juin   3  2007 cdrom -> media/cdrom
    drwxr-xr-x  14 root root    4080 janv. 10 14:27 dev
    drwxr-xr-x 127 root root   12288 janv. 10 14:27 etc
    drwxr-xr-x   9 root root    4096 janv. 16  2009 home
    drwxr-xr-x   2 root root    4096 juin   3  2007 initrd
    lrwxrwxrwx   1 root root      28 sept. 18 11:03 initrd.img -> boot/initrd.img-2.6.30-2-686
    lrwxrwxrwx   1 root root      28 août  23 15:38 initrd.img.old -> boot/initrd.img-2.6.30-1-686
    drwxr-xr-x  13 root root   12288 janv.  9 17:08 lib
    drwx------   2 root root   16384 juin   3  2007 lost+found
    drwxr-xr-x   5 root root    4096 janv. 10 14:27 media
    drwxr-xr-x  10 root root    4096 juil. 16  2008 mnt
    drwxr-xr-x   3 root root    4096 juil.  2  2008 opt
    dr-xr-xr-x 119 root root       0 janv. 10 15:27 proc
    drwxr-xr-x  24 root root    4096 janv. 10 03:39 root
    drwxr-xr-x   2 root root    4096 janv.  9 17:08 sbin
    drwxr-xr-x   2 root root    4096 mai    7  2007 selinux
    drwxr-xr-x   3 root root    4096 nov.  17  2008 srv
    drwxr-xr-x  12 root root       0 janv. 10 15:27 sys
    drwxrwxrwt  10 root root   32768 janv. 10 15:37 tmp
    drwxr-xr-x  11 root root    4096 sept. 19 01:31 usr
    drwxr-xr-x  15 root root    4096 nov.  25 20:18 var
    lrwxrwxrwx   1 root root      25 sept. 18 11:03 vmlinuz -> boot/vmlinuz-2.6.30-2-686
    lrwxrwxrwx   1 root root      25 août  23 15:38 vmlinuz.old -> boot/vmlinuz-2.6.30-1-686
    fils terminé
    fini
    bin
    boot
    cdrom
    dev
    etc
    home
    initrd
    initrd.img
    initrd.img.old
    lib
    lost+found
    media
    mnt
    opt
    proc
    root
    sbin
    selinux
    srv
    sys
    tmp
    usr
    var
    vmlinuz
    vmlinuz.old
    père termine
    


    Je m'explique. Sous linux, un processus P1 peut être lancé par un autre processus P2. Dans ce cas, on dit que P1 est le processus père de P2 (et que P2 est un processus fils de P1). Un processus peut avoir plusieurs processus fils. C'est d'ailleurs ce que tu peux observer en tapant la commande :

    ps -faux


    Quand tu lances ce programme, il commence par faire un fork : le processus se sépare en deux processus :
    - un processus père (dont le fil d'exécution reprend dans le if) P1
    - un processus fils (dont le fil d'exécution reprend dans le else) P2

    P2 est donc un processus fils de P1.

    Ensuite, l'appel à popen engendre la création d'un processus fils à P1 et P2 :
    - le processus "ls /" est fils de P1
    - le processus "ls -l /" est fils de P2

    Ici j'ai utilisé popen pour qu'on voit le résultat des deux commandes "ls", car avec un execv on ne les voit pas.

    Bonne chance
    0