Tfe

Ongi etorri tfe-ren webgunera...

Old stuff/ecole_etude_fac_de_pau/licence_3/systeme-d-exploitation/tp3/2.c

(Deskargatu)
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <errno.h>
#include <signal.h>


int etat=0;


void cont(int n)
{
  etat = 1;
}


int main(int argc, char** argv)
{
  pid_t pid;
  struct sigaction action;

  pid =fork();
  if (pid == -1)
    {
      perror("fork error");
      exit(-1);
    }


  else if(pid == 0)
    {
      // fils
      sleep(1);
      action.sa_handler = cont;
      sigemptyset(&action.sa_mask);
      if(sigaction(SIGCONT, &action, NULL) == -1)
	{
	  perror("sig action");
	  exit(-1);
	}
      printf("Fils 1\n");
      etat = 0;
      kill(getppid(),SIGCONT);
      while(etat == 0);
      printf("Fils 2\n");
      kill(getppid(), SIGCONT);
    }


  else 
    {
      action.sa_handler = cont;
      sigemptyset(&action.sa_mask);
      if(sigaction(SIGCONT, &action, NULL) == -1)
	{
	  perror("sig action");
	  exit(-1);
	}
      while(etat == 0);
      printf("Pere 1\n");
      etat = 0;
      kill(pid,SIGCONT);
      while(etat == 0);
      printf("Pere 2\n");
      etat = 0;
      kill(pid,SIGCONT);

      while(wait() != -1);
    }

  return 0;
}