Subversion Repositories shark

Rev

Rev 1449 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1085 pj 1
/*
2
 * Project: S.Ha.R.K.
3
 *
4
 * Coordinators:
5
 *   Giorgio Buttazzo    <giorgio@sssup.it>
6
 *   Paolo Gai           <pj@gandalf.sssup.it>
7
 *
8
 * Authors     :
9
 *   Paolo Gai           <pj@gandalf.sssup.it>
10
 *   (see the web pages for full authors list)
11
 *
12
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
13
 *
14
 * http://www.sssup.it
15
 * http://retis.sssup.it
16
 * http://shark.sssup.it
17
 */
18
 
19
/**
20
 ------------
1450 giacomo 21
 CVS :        $Id: ptest4.c,v 1.3 2004-05-23 09:07:29 giacomo Exp $
1085 pj 22
 
23
 File:        $File$
1450 giacomo 24
 Revision:    $Revision: 1.3 $
25
 Last update: $Date: 2004-05-23 09:07:29 $
1085 pj 26
 ------------
27
 
28
 Posix test 4:
29
 
30
   the main task create 3 tasks, J1, J2, J3
31
     at t = 1 sec. it raise a signal to J1
32
     at t = 2 sec. it kills J2
33
 
34
   J1,J2,J3: it simply calls nanosleep
35
 
36
 non standard function used:
37
   cprintf
38
   sys_gettime
39
   keyboard stuffs
40
   sys_end
41
 
42
**/
43
 
44
/*
45
 * Copyright (C) 2000 Paolo Gai
46
 *
47
 * This program is free software; you can redistribute it and/or modify
48
 * it under the terms of the GNU General Public License as published by
49
 * the Free Software Foundation; either version 2 of the License, or
50
 * (at your option) any later version.
51
 *
52
 * This program is distributed in the hope that it will be useful,
53
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
54
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
55
 * GNU General Public License for more details.
56
 *
57
 * You should have received a copy of the GNU General Public License
58
 * along with this program; if not, write to the Free Software
59
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
60
 *
61
 */
62
 
63
#include <sys/types.h>
64
#include <pthread.h>
65
#include <signal.h>
66
#include <time.h>
67
 
68
#include <kernel/kern.h>
69
 
70
void uscitaJ(void *arg)
71
{
72
  cprintf("J: (pid=%d) AAAARRRRGGGHHH!!! killed by someone...\n", exec_shadow);
73
}
74
 
75
void *J(void *arg)
76
{
77
  struct timespec t1, t2;
78
  int err;
79
 
80
  cprintf("J (pid=%d) starts and call nanosleep\n",exec_shadow);
81
 
82
  t1.tv_sec = 3;
83
  t1.tv_nsec = 0;
84
  NULL_TIMESPEC(&t2);
85
  pthread_cleanup_push(uscitaJ,NULL);
86
  err = nanosleep(&t1, &t2);
87
  pthread_cleanup_pop(0);
88
 
89
  cprintf("J (pid=%d) ending, nanosleep returns errno=%d, t2=%ld.%ld\n",
90
              exec_shadow, err, t2.tv_sec, t2.tv_nsec/1000);
91
 
92
  return 0;
93
}
94
 
95
void signal_handler(int signo, siginfo_t *info, void *extra)
96
{
97
  cprintf("SIGNAL HANDLER: pid=%d\n",exec_shadow);
98
}
99
 
100
 
101
int main(int argc, char **argv)
102
{
103
  int err;
104
  pthread_t j1, j2, j3;
105
  struct sigaction sig_act;
106
 
107
  sig_act.sa_sigaction = (void *) signal_handler;
108
  sig_act.sa_flags = SA_SIGINFO;
109
  sigemptyset(&sig_act.sa_mask);
110
 
111
  sigaction(31, &sig_act, NULL);
112
 
113
  cprintf("main: creating J1\n");
114
  err = pthread_create(&j1, NULL, J, NULL);
115
  if (err) cprintf("Error creating J1\n");
116
 
117
  cprintf("main: creating J2\n");
118
  err = pthread_create(&j2, NULL, J, NULL);
119
  if (err) cprintf("Error creating J2\n");
120
 
121
  cprintf("main: creating J3\n");
122
  err = pthread_create(&j3, NULL, J, NULL);
123
  if (err) cprintf("Error creating J3\n");
124
 
125
  cprintf("main: waiting 1 sec\n");
126
  while (sys_gettime(NULL) < 1000000);
127
 
128
  cprintf("main: pthread_kill on j1, then wait until t=2 sec \n");
129
  pthread_kill(j1, 31);
130
 
131
  while (sys_gettime(NULL) < 2000000);
132
  cprintf("main: pthread_cancel(J2)\n");
133
  pthread_cancel(j2);
134
 
135
  cprintf("main: ending...\n");
136
 
137
  return 0;
138
}