Skip to content

Instantly share code, notes, and snippets.

@erickedji
Created November 8, 2011 11:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erickedji/1347536 to your computer and use it in GitHub Desktop.
Save erickedji/1347536 to your computer and use it in GitHub Desktop.
Exercices de la séance 3 - TP Systèmes centralisés
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(void)
{
int n, ret;
n= fork();
if ( n == -1 )
{
fprintf(stderr,"Probleme execution fork\n");
exit(1);
}
if ( n == 0 )
{
ret= execlp("find","toto",".","-name","core","-print",NULL);
perror("erreur execution exec");
exit(2);
}
else
{
sleep(5);
ret= wait(&n);
if ( WIFEXITED(n) )
printf("fils %d mort par exit %d\n",ret,WEXITSTATUS(n));
else
printf("fils %d mort par signal %d\n",ret,WTERMSIG(n));
printf("fin\n");
}
exit(0);
}
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main(void)
{
fork(); printf("fork1\n");
fork(); printf("fork2\n");
fork(); printf("fork3\n");
return (0);
}
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
/*
Le prompt du shell peut revenir avant que toutes les sorties
ne soient affichées dans l'exercice 2.2.5.1. Il suffit pour
cela que le processus père (processus racine, créé par le
shell) se termine avant l'un des fils.
On peut introduire de la synchronisation avec wait() pour obliger
tous les processus pères à attendre leurs fils; auquel cas on a
le programme suivant (NB: la sortie devient prévisible):
*/
int main(void)
{
int a, b, c;
a = fork(); printf("fork1\n");
if ( a != 0 ) waitpid(a, NULL, 0);
b = fork(); printf("fork2\n");
if ( b != 0 ) waitpid(b, NULL, 0);
c = fork(); printf("fork3\n");
if ( c != 0 ) waitpid(c, NULL, 0);
return (0);
}
/* Time-stamp: <02 Sep 2008 11:08 queinnec@enseeiht.fr> */
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
void boucle (int unused)
{
sigset_t monset;
sigfillset(&monset);
sigprocmask(SIG_SETMASK, &monset, NULL);
while (1)
{
printf("Je boucle et personne ne peut m'arreter! hehehe!\n");
sleep(3);
}
}
int main(void)
{
struct sigaction monaction;
monaction.sa_handler = boucle;
sigaction(SIGINT, &monaction, NULL);
sleep(5);
return(0);
}
/* Time-stamp: <02 Sep 2008 11:06 queinnec@enseeiht.fr> */
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
void boucle (int unused)
{
int i;
for (i = 1; i <= NSIG; i++)
signal (i, SIG_IGN);
printf("entree dans boucle infinie\n");
for(;;){
printf("numero de processus %d\n", getpid());
sleep(5);
}
}
int main()
{
signal(SIGINT, boucle);
printf("numero de processus %d\n", getpid());
sleep(60);
return 0;
}
/* Time-stamp: <02 Sep 2008 11:11 queinnec@enseeiht.fr> */
/* Exo 4.1.8, page 10 des processus.
* timeout <temps> <cmde> <arg1> ... <argn> */
/* Rq: le fait de devoir afficher le message 'le fils a ete tue' dans
* le main et non dans le handler complique la solution: il faut
* introduire sigalrm_a_ete_recu.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <sys/wait.h>
int pid_fils;
int sigalrm_a_ete_recu = 0;
void death_to_my_son (int unused)
{
kill (pid_fils, SIGTERM);
sigalrm_a_ete_recu = 1;
}
int main (int argc, char **argv)
{
int status;
int ret;
if (argc < 3) {
fprintf (stderr, "timeout <temps> <cmde> <arg1> ... <argn>\n");
exit (1);
}
pid_fils = fork();
switch (pid_fils) {
case -1:
perror ("fork");
exit(1);
case 0: /* fils: exécute le commande */
execvp (argv[2], argv+2);
perror ("Can't exec the command");
exit (1);
default: /* pere: attend la fin du fils */
signal (SIGALRM, death_to_my_son);
alarm (atoi (argv[1]));
/* En SystemV (donc Solaris/SunOS5), wait est interrompu par le
* signal SIGALARM et n'est pas redémarré. Il faut donc tester
* la valeur de retour et errno pour recommencer si nécessaire.
* En BSD (donc SunOS4), wait est automatiquement redémarré par
* le noyau, et la boucle devient inutile. */
do {
ret = wait (&status);
} while ((ret == -1) && (errno == EINTR));
if (ret == -1) {
perror ("bad wait");
exit (1);
}
if (sigalrm_a_ete_recu)
printf ("Le fils %d a ete tue.\n", pid_fils);
else if (WIFEXITED (status))
printf ("Fils %d exit %d\n", pid_fils, WEXITSTATUS (status));
else /* WIFSIGNALED (status) */
printf ("Fils %d killed by %d\n", pid_fils, WTERMSIG (status));
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment