malleabilityZombies.c 1.67 KB
Newer Older
iker_martin's avatar
iker_martin committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <mpi.h>
#include <signal.h>
#include "malleabilityZombies.h"

#define PIDS_QTY 320

void zombies_suspend();

int offset_pids, *pids = NULL;


void gestor_usr2() {}

19
void zombies_collect_suspended(MPI_Comm comm) {
iker_martin's avatar
iker_martin committed
20
  int pid = getpid();
21
22
  int *pids_counts = malloc(mall->numP * sizeof(int));
  int *pids_displs = malloc(mall->numP * sizeof(int));
iker_martin's avatar
iker_martin committed
23
24
  int i, count=1;

25
26
27
28
29
  #if USE_MAL_DEBUG > 2
    if(mall->myId == mall->root){ DEBUG_FUNC("Collecting zombies", mall->myId, mall->numP); } fflush(stdout);
  #endif

  if(mall->myId < mall->numC) {
iker_martin's avatar
iker_martin committed
30
    count = 0;
31
32
    if(mall->myId == mall->root) {
      for(i=0; i < mall->numC; i++) {
iker_martin's avatar
iker_martin committed
33
34
	pids_counts[i] = 0;
      }
35
      for(i=mall->numC; i<mall->numP; i++) {
iker_martin's avatar
iker_martin committed
36
  	pids_counts[i] = 1;
37
	pids_displs[i] = (i - mall->numC) + offset_pids;
iker_martin's avatar
iker_martin committed
38
      }
39
      offset_pids += mall->numP - mall->numC;
iker_martin's avatar
iker_martin committed
40
41
      }
  }
42
  MPI_Gatherv(&pid, count, MPI_INT, pids, pids_counts, pids_displs, MPI_INT, mall->root, comm);
iker_martin's avatar
iker_martin committed
43
44
45
  free(pids_counts);
  free(pids_displs);

46
  if(mall->myId >= mall->numC) {
iker_martin's avatar
iker_martin committed
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
    zombies_suspend();
  }
}

void zombies_service_init() {
  offset_pids = 0;
  pids = malloc(PIDS_QTY * sizeof(int));

  for(int i=0; i<PIDS_QTY; i++) {
    pids[i] = 0;
  }
}

void zombies_service_free() {
  free(pids);
}

void zombies_suspend() {
  struct sigaction act;

  sigemptyset(&act.sa_mask);
  act.sa_flags=0;
  act.sa_handler=gestor_usr2;

  sigaction(SIGUSR2, &act, NULL);

  sigset_t set;
  sigprocmask(SIG_SETMASK,NULL,&set);

  sigsuspend(&set);
}

void zombies_awake() {
  for(int i=0; i < offset_pids; i++) { // Despertar a los zombies
    kill(pids[i], SIGUSR2);
  }
}