"p for execlp ok"
This commit is contained in:
parent
8b366a33f7
commit
038b4fe071
@ -1,21 +0,0 @@
|
||||
/**************************************/
|
||||
/* demo_base : demonstration basique */
|
||||
/**************************************/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <assert.h>
|
||||
|
||||
int main (int argc, char* argv[], char* envp[])
|
||||
// argc = compteur d'arguments
|
||||
// argv et envp = tableau de pointeurs de chaines
|
||||
{
|
||||
if (argc != 2){
|
||||
fprintf(stdout, "\nSyntaxe : %s nom-de-variable\n\n", argv[0]);
|
||||
exit (1);
|
||||
}
|
||||
printf ("\nProcessus %d (pere : %d) - 2eme mot ='%s'\n", getpid (), getppid (),
|
||||
getenv(argv[1]));
|
||||
return 0;
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
/*************************************************************/
|
||||
/* demo_exec_arg : */
|
||||
/* - Reservation de la place max des arguments */
|
||||
/* (20 car.), */
|
||||
/* - 1er paramètre = la commande a executer, */
|
||||
/* - 2ème paramètre et suivant = paramètres de */
|
||||
/* la commande */
|
||||
/* - execution (avec prise en compte de la */
|
||||
/* variable PATH). */
|
||||
/*************************************************************/
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
int main (int argc, char* argv[])
|
||||
{
|
||||
char* argv2[argc];
|
||||
int i;
|
||||
printf(" argc=%i \n", argc);
|
||||
for (i= 1; i <= argc; i++)
|
||||
{
|
||||
argv2[i-1] = argv[i];
|
||||
printf(" argv[%i] = %s\n", i, argv2[i-1]);
|
||||
}
|
||||
execvp (argv2[0],argv2);
|
||||
printf ("\nERREUR : execvp impossible\n");
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
/***************************************************************/
|
||||
/* demo_exec_scan : */
|
||||
/* - Reservation de la place max des arguments */
|
||||
/* (30 car.), */
|
||||
/* - lecture du nom de la commande a executer, */
|
||||
/* - lecture d'un param. obligatoire, */
|
||||
/* - execution (avec prise en compte de la */
|
||||
/* variable PATH). */
|
||||
/***************************************************************/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main ()
|
||||
{ char *argv[3];
|
||||
argv[0] = (char*) malloc(30); argv[1] = (char*)malloc(30);
|
||||
printf ("Nom de la commande a executer : "); scanf ("%s", argv[0]);
|
||||
printf ("Un argument (obligatoire) : "); scanf ("%s", argv[1]);
|
||||
argv[2] = (char*)0;
|
||||
execvp (argv[0],argv);
|
||||
printf ("\nERREUR : execvp impossible\n");
|
||||
}
|
@ -1,18 +0,0 @@
|
||||
/*************************************************/
|
||||
/* demo_exit : envoi d'un code de retour */
|
||||
/*************************************************/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
int main (int argc, char *argv[])
|
||||
{
|
||||
if (argc != 2) {fprintf (stderr, "Syntaxe : %s code-de-retour \n", argv[0]); exit(-1);}
|
||||
exit (atoi (argv[1]));
|
||||
}
|
||||
|
||||
/// Par exemple, tester en bash avec :
|
||||
|
||||
/// if ./demo_exit 3 ; then echo OK ; else echo KO ; fi
|
||||
|
||||
/// Puis avec .\demo_exit 0 ...
|
||||
|
||||
|
@ -1,13 +0,0 @@
|
||||
/******************************************************/
|
||||
/* demo_fork : Ce programme illustre le mecanisme de */
|
||||
/* duplication d'images de LINUX. */
|
||||
/******************************************************/
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
int main()
|
||||
{ int res;
|
||||
res = fork();
|
||||
if (res == -1) {perror ("demo_fork - fork"); exit (1);}
|
||||
printf ("\nProcessus %d (pere : %d) - res ='%d'\n", getpid (), getppid (), res);
|
||||
}
|
@ -1,23 +0,0 @@
|
||||
/*************************************************/
|
||||
/* demo_sleep : Utilisation de "sleep" */
|
||||
/*************************************************/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
int main (int argc, char* argv[])
|
||||
/* argc : compteur d'arguments */
|
||||
/* argv : tableau de pointeurs de chaines */
|
||||
{
|
||||
if (argc != 2)
|
||||
{
|
||||
fprintf (stderr, "Syntaxe : %s duree-du-sleep\n", argv[0]);
|
||||
exit (1);
|
||||
}
|
||||
printf("\n%s (Pid = %d): Regardez-moi avec ps ... Je m'endors %s secondes.\n",
|
||||
argv[0], getpid(),argv[1]);
|
||||
sleep (atoi(argv[1]));
|
||||
printf("%s (Pid = %d): Je me reveille après %s secondes.\n\n",
|
||||
argv[0], getpid(), argv[1]);
|
||||
}
|
@ -1,20 +0,0 @@
|
||||
/// M311 - demonstration : gets, strtok
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
char *mot;
|
||||
char *ligne = malloc (80);
|
||||
ligne = fgets (ligne, 80, stdin);
|
||||
printf("===%s===\n", ligne);
|
||||
mot = strtok (ligne, " ");
|
||||
while (mot != NULL)
|
||||
{
|
||||
printf("%s\n", mot);
|
||||
mot = strtok (NULL, " ");
|
||||
}
|
||||
return 0;
|
||||
}
|
@ -1,9 +1,19 @@
|
||||
/**
|
||||
* @ Author: JunkJumper
|
||||
* @ Link: https://github.com/JunkJumper
|
||||
* @ Copyright: Creative Common 4.0 (CC BY 4.0)
|
||||
* @ Create Time: 01-10-2020 10:24:44
|
||||
* @ Modified by: JunkJumper
|
||||
* @ Modified time: 01-10-2020 10:53:03
|
||||
* @ Description:
|
||||
*/
|
||||
|
||||
/*******************************************************/
|
||||
/* p_fichier: Ce programme crée un nouveau */
|
||||
/* p_fichier: Ce programme cr<EFBFBD>e un nouveau */
|
||||
/* processus avec fork */
|
||||
/* - Le processus pere ouvre un nouveau fichier en */
|
||||
/* création de nom donné en argument */
|
||||
/* - Le processus fils écrit une chaine donnee en */
|
||||
/* cr<EFBFBD>ation de nom donn<6E> en argument */
|
||||
/* - Le processus fils <EFBFBD>crit une chaine donnee en */
|
||||
/* argument dans le fichier puis meurt */
|
||||
/* - Le processus pere affiche le fichier puis meurt */
|
||||
/* */
|
||||
|
@ -1,3 +1,13 @@
|
||||
/**
|
||||
* @ Author: JunkJumper
|
||||
* @ Link: https://github.com/JunkJumper
|
||||
* @ Copyright: Creative Common 4.0 (CC BY 4.0)
|
||||
* @ Create Time: 01-10-2020 10:24:44
|
||||
* @ Modified by: JunkJumper
|
||||
* @ Modified time: 01-10-2020 10:52:58
|
||||
* @ Description:
|
||||
*/
|
||||
|
||||
/*******************************************************/ /* */
|
||||
/* p_fork : Ce programme illustre le mecanisme de */
|
||||
/* duplication de processus de LINUX. */
|
||||
|
@ -1,3 +1,13 @@
|
||||
/**
|
||||
* @ Author: JunkJumper
|
||||
* @ Link: https://github.com/JunkJumper
|
||||
* @ Copyright: Creative Common 4.0 (CC BY 4.0)
|
||||
* @ Create Time: 01-10-2020 10:24:44
|
||||
* @ Modified by: JunkJumper
|
||||
* @ Modified time: 01-10-2020 11:12:48
|
||||
* @ Description:
|
||||
*/
|
||||
|
||||
/*********************************************************/
|
||||
/* */
|
||||
/* p_fork_exec : Prototype extremement simplifie d' une */
|
||||
@ -13,7 +23,11 @@
|
||||
/*********************************************************/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
int main ()
|
||||
|
||||
@ -31,11 +45,41 @@ printf ("? Un argument (obligatoire) : "); scanf ("%s", arguments[1]);
|
||||
|
||||
arguments[2] = (char*)0;
|
||||
|
||||
/**********************/
|
||||
/* PARTIE A COMPLETER */
|
||||
/**********************/
|
||||
/**********************/
|
||||
/* PARTIE A COMPLETER */
|
||||
/**********************/
|
||||
int infosRetour;
|
||||
int res = fork();
|
||||
if (res < 0)
|
||||
{
|
||||
printf("ERREUR FORK\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
if (res == 0)
|
||||
{ /************************** FILS ***********************/
|
||||
printf("Je suis le fils :\n");
|
||||
printf("pid : %d\n", getpid());
|
||||
printf("ppid : %d\n", getppid());
|
||||
printf("je fais la commande :\n");
|
||||
|
||||
printf ("\n==============================\n");
|
||||
printf (" p_fork_exec: FIN DE SESSION \n");
|
||||
printf ("==============================\n\n");
|
||||
execlp(arguments[0], arguments[0], arguments[1], NULL);
|
||||
|
||||
printf("\n\n");
|
||||
exit(0);
|
||||
}
|
||||
else
|
||||
{ /************************** PERE ***********************/
|
||||
wait(&infosRetour);
|
||||
printf("Je suis le père :\n");
|
||||
printf("pid : %d\n", getpid());
|
||||
printf("ppid : %d\n", getppid());
|
||||
printf("%d", WEXITSTATUS(infosRetour));
|
||||
|
||||
|
||||
printf("\n\n");
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -1,3 +1,13 @@
|
||||
/**
|
||||
* @ Author: JunkJumper
|
||||
* @ Link: https://github.com/JunkJumper
|
||||
* @ Copyright: Creative Common 4.0 (CC BY 4.0)
|
||||
* @ Create Time: 01-10-2020 10:24:44
|
||||
* @ Modified by: JunkJumper
|
||||
* @ Modified time: 01-10-2020 10:53:06
|
||||
* @ Description:
|
||||
*/
|
||||
|
||||
/*******************************************************/ /* */
|
||||
/* p_fork_wait : Ce programme illustre le mecanisme */
|
||||
/* de duplication de processus de LINUX. */
|
||||
|
@ -1,3 +1,13 @@
|
||||
/**
|
||||
* @ Author: JunkJumper
|
||||
* @ Link: https://github.com/JunkJumper
|
||||
* @ Copyright: Creative Common 4.0 (CC BY 4.0)
|
||||
* @ Create Time: 01-10-2020 10:24:44
|
||||
* @ Modified by: JunkJumper
|
||||
* @ Modified time: 01-10-2020 10:52:49
|
||||
* @ Description:
|
||||
*/
|
||||
|
||||
/*******************************************************/ /* */
|
||||
/* p_fork_wait_exit : Ce programme illustre le */
|
||||
/* mecanisme de duplication de processus */
|
||||
@ -22,7 +32,6 @@
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
|
||||
@ -31,7 +40,7 @@ int main(int argc, char *argv[])
|
||||
/**********************/
|
||||
int infosRetour;
|
||||
int res = fork();
|
||||
int max=atoi(argv[1]);
|
||||
int max = atoi(argv[1]);
|
||||
if (res < 0)
|
||||
{
|
||||
printf("ERREUR FORK\n");
|
||||
@ -59,7 +68,6 @@ int main(int argc, char *argv[])
|
||||
printf("ppid : %d\n", getppid());
|
||||
printf("%d", WEXITSTATUS(infosRetour));
|
||||
|
||||
|
||||
printf("\n\n");
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user