Project

General

Profile

Statistics
| Revision:

root / branches / simulator / projects / simulator / libsim / libsim.c @ 996

History | View | Annotate | Download (1.04 KB)

1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <sys/time.h>
4
#include <signal.h>
5
#include <unistd.h>
6
#include <stdlib.h>
7
#include <sys/shm.h>
8
#include <sys/ipc.h>
9
#include <robot_shared.h>
10
#include <string.h>
11

    
12

    
13
RobotShared* shared_state;
14

    
15
void *tick(int sig)
16
{
17
  printf("robot process paused. suspending\n"); 
18

    
19
  if(raise(SIGTSTP)<0)
20
    printf("could not kill self!\n");
21

    
22
  printf("robot resumed\n");
23

    
24
  return NULL;
25
}
26

    
27

    
28
void dragonfly_init(int config)
29
{
30
  struct itimerval iv;
31
  int ret;
32

    
33
  shared_state = shmat(atoi(getenv("memory_id")), NULL, 0);
34

    
35
  if(shared_state < 0)
36
  {
37
    printf("unable to get shared memory region\n");
38
    return 1;
39
  }
40

    
41
                                             
42
  printf("hello. I am a robot w/ memory_id %s\n", getenv("memory_id"));
43

    
44

    
45
  iv.it_interval.tv_sec = 1;
46
  iv.it_interval.tv_usec = 0;
47
  iv.it_value.tv_sec = 3;
48
  iv.it_value.tv_usec = 0;
49

    
50
  signal(SIGVTALRM, tick);
51

    
52
  ret = setitimer(ITIMER_VIRTUAL, &iv, NULL);
53

    
54
  printf("setitimer returned %d.\n waiting...\n", ret);
55
  fflush(stdout);
56

    
57
  //TODO: clean up code??
58

    
59
}
60