Tfe

Ongi etorri tfe-ren webgunera...

Old stuff/ecole_etude_fac_de_pau/licence_3/systeme-d-exploitation/tp2/5.c

(Deskargatu)
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>

int main()
{
 char commande[100];
 pid_t pid;
 printf("\033[31m$ ") ;
 scanf("%s",commande);
 printf("\033[0m\n");
 while(strcmp("exit",commande))

    {
      pid = vfork();
      if (pid== -1) { 
	perror("fork error"); 
	exit(-1); 
      }
      else if(pid == 0) 
	{ 
	  if (execlp(commande,commande, '\0')==-1)
	    {
	  	    perror("erreur execlp"); 
		    exit(-1); 
	    }
	  
	}
      else
	{
	  if (wait(0) == -1)
	    {
	      perror("Erreur wait"); 
	      exit(-1);
	    }
	  printf("\033[31m$ ");
	  scanf("%s",commande); 
	  printf("\033[0m\n");
	}
    }
}