Project

General

Profile

Revision 981

Added signal handling code to core simulator

added a robot_test directory which contiains code to run the timer and deal with the robot side signaling. To compile it you need to run gcc
robot_test/robot.c -o robot_test/robot. This whole file is temporary. It is just proof of concept until it gets added to the simulator library

View differences:

branches/simulator/projects/simulator/simulator/robot_test/robot.c
1
#include <stdio.h>
2
#include <sys/time.h>
3
#include <signal.h>
4
#include <unistd.h>
5
#include <stdlib.h>
6

  
7
volatile int i;
8

  
9
void *tick(int sig)
10
{
11
  printf("robot process paused. suspending\n");
12

  
13
  if(raise(SIGTSTP)<0)
14
    printf("could not kill self!\n");
15

  
16
  printf("robot resumed\n");
17

  
18
  return NULL;
19
}
20

  
21
int main(int argc, char *argp[], char *envp[])
22
{
23
  int ret;
24
  struct itimerval iv;
25

  
26
  printf("hello. I am a robot w/ env[0] %s\n", envp[0]);
27

  
28
  iv.it_interval.tv_sec = 1;
29
  iv.it_interval.tv_usec = 0;
30
  iv.it_value.tv_sec = 3;
31
  iv.it_value.tv_usec = 0;
32

  
33
  signal(SIGVTALRM, tick);
34

  
35
  ret = setitimer(ITIMER_VIRTUAL, &iv, NULL);
36

  
37
  printf("setitimer returned %d.\n waiting...\n", ret);
38
  fflush(stdout);
39

  
40
  i=0;
41

  
42
  while(1)
43
    i++;
44

  
45
  return 0;
46
}
branches/simulator/projects/simulator/simulator/core/main.c
1 1
#include "gtk_gui.h"
2
#include <stdio.h>
3
#include <unistd.h>
4
#include <stdlib.h>
5
#include <signal.h>
6
#include <sys/wait.h>
2 7

  
8
int create_robot(char *execname, int id)
9
{
10
  /* do shared memory stuff here */
11

  
12
  int child;
13

  
14
  if((child = fork())<0){
15
    printf("error with fork!\n");
16
    return -1;
17
  }
18

  
19
  char *envp[] = {"test=1", NULL};
20

  
21
  if(!child){
22
    /* restore default sigchld handler */
23
    signal(SIGCHLD, SIG_DFL);
24

  
25
    execve(execname, NULL, envp);  //TODO: keep the other env. stuff around
26
    printf ("error! exec failed\n");
27
    exit(-1);
28
  }
29

  
30
  return child;
31
}
32

  
33
void *sig_chld_handler(int sig)
34
{
35
  pid_t pid;
36
  int cstat;
37

  
38
  printf("SIGCHLD: parent waiting\n");
39

  
40
  pid = waitpid(0, &cstat, WUNTRACED);
41

  
42
  printf("got %d from %d\n", cstat, pid);
43

  
44
  kill(pid, SIGCONT);
45

  
46
  return NULL;
47
}
48

  
3 49
int main(int argc, char** argv)
4 50
{
5
	return gtk_gui_run(argc, argv);
51
  signal(SIGCHLD, sig_chld_handler);
52

  
53
  create_robot("robot_test/robot", 0);
54

  
55
  printf("returned to parent\n");
56

  
57

  
58
  gtk_gui_run(argc, argv);
59

  
60
  printf("parent exiting.\n");
61

  
62
  return 0;
6 63
}
7 64

  

Also available in: Unified diff