File: sched_test.c

package info (click to toggle)
dmtcp 2.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,496 kB
  • sloc: cpp: 33,592; ansic: 28,099; sh: 6,735; makefile: 1,950; perl: 1,690; python: 1,241; asm: 138; java: 13
file content (63 lines) | stat: -rw-r--r-- 1,493 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#define _GNU_SOURCE
#include <sched.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
#include <signal.h>
#include <sys/prctl.h>
#include <sys/types.h>
#include <sys/wait.h>

int
main()
{
  cpu_set_t cset;
  int i = 0;
  int numOfCPUs = sysconf(_SC_NPROCESSORS_ONLN);

  pid_t ret = fork();
  if (ret < 0) {
    perror("Error forking a child: ");
    return -1;
  } else if (ret == 0) {
    prctl(PR_SET_PDEATHSIG, SIGHUP);
    while (1) {
      printf("Child: %d\n", i);
      sleep(1);
      i++;
    }
  } else {
    while (1) {
      i = i % numOfCPUs;

      CPU_ZERO(&cset);
      CPU_SET(i, &cset);
      printf("Trying to set CPU affinity for (%d) to (%d)\n", ret, i);
      if (sched_setaffinity(ret, sizeof(cset), &cset) < 0) {
        perror("Error setting CPU affinity for child");
        //return -1;  Turning this off for Travis containers
      } else {
        printf("CPU affinity for (%d) set to (%d)\n", ret, i);
      }
      sleep(1);

      CPU_ZERO(&cset);
      printf("Trying to read CPU affinity for (%d)\n", ret);
      if (sched_getaffinity(ret, sizeof(cset), &cset) < 0) {
        perror("Error getting CPU affinity for child");
        //return -1;
      } else {
        // Turning this off for Travis containers
        //assert(CPU_ISSET(i, &cset) && CPU_COUNT(&cset) == 1);
        printf("CPU affinity for (%d) is (%d)\n", ret, i);
      }
      sleep(1);

      i++;
    }
    wait(&i);
  }
  return 0;
}