/*
* ######
* ## ps_exec.c
* ## ~~~~~~~~~
* ## 04.02.1995: Creation TL
* ## 07.05.2018:
* ##
* ## Au lancement de ce programme, il est possible de passer
* ## des arguments.
* ##
* ## Verifier ce qui est obtenu avec:
* ##
* ## ./ps_exec un deux trois
* ##
* ## Le 1er argument (un) est-il le numero 0 ou 1 de argv ?
* ## Comment recupere-ton le nom de la commande ?
* ## Obtient-on le filename et le pathname de la commande ?
* ######
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main(argc, argv)
int argc; char **argv;
{
printf( " \n" );
printf( " ps_exec> Debut execution\n" );
printf( " ps_exec> Numero PID = %d\n", getpid() );
printf( " ps_exec> Nb arguments = %d\n", argc );
int KP;
for ( KP = 0; KP < argc; KP++ ) {
printf( " ps_exec> argv[ %d ] = %s\n", KP, argv[ KP ] );
}
printf(" \n ");
exit( 0 );
}
/*-- fin ps_exec.c --*/
/*
* #######
* ## ps_fork.c
* ## =========
* ## 04.02.1995: Creation TL
* ## 07.05.2018:
* ##
* ## Ce programme se divise en 2 processus par FORK
* ##
* ######
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main()
{
int pidp, pidf, pidk, pidw;
printf( " \n" );
pidp = getpid();
printf( " ps_fork > Debut execution : pid = %d\n", pidp );
/*
* Creation du processus fils par FORK
* Les 2 processsus executent les memes instructions
* Il sdifferent par leutr PID et la valeur retournee par le fork
* et stokee dans la variable pidp
*/
pidk = fork();
if ( pidk == -1 ) {
printf( " ps_fork > Creation de processus impossible\n" );
exit(1);
}
printf( " ps_fork > Code execute par les 2 ! mais : getpid() = %d\n ", getpid());
if ( pidk == 0 ) {
/* code execute par le processus FILS */
printf( " ps_fork(f)> Valeur retournee par le fork = %d\n", pidk );
pidf=getpid();
printf( " ps_fork(f)> PID( avant exec ) = %d\n", pidf );
sleep(5);
(void) execl("ps_exec", "liste des arguments", NULL );
printf( " ps_fork(f)> Erreur ps_exec\n" );
} else {
/* code execute par le processus PERE */
printf( " PS_FORK(p)> Valeur retournee par le fork = %d\n", pidk );
/* attente de fin d'execution du (d'un) fils */
pidw = wait( 0 );
printf( " PS_FORK(p)> PID du fils se terminant = %d\n", pidw );
}
printf( " ps_fork > Fin Processus : %d\n", getpid() );
exit( 0 );
}
/*-- fin ps_fork.c --*/