Subversion Repositories shark

Rev

Rev 1086 | Rev 1450 | 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
 ------------
1449 giacomo 21
 CVS :        $Id: ptest4.c,v 1.2 2004-05-23 09:05:51 giacomo Exp $
1085 pj 22
 
23
 File:        $File$
1449 giacomo 24
 Revision:    $Revision: 1.2 $
25
 Last update: $Date: 2004-05-23 09:05:51 $
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
#include <drivers/keyb.h>
70
 
71
void uscitaJ(void *arg)
72
{
73
  cprintf("J: (pid=%d) AAAARRRRGGGHHH!!! killed by someone...\n", exec_shadow);
74
}
75
 
76
void *J(void *arg)
77
{
78
  struct timespec t1, t2;
79
  int err;
80
 
81
  cprintf("J (pid=%d) starts and call nanosleep\n",exec_shadow);
82
 
83
  t1.tv_sec = 3;
84
  t1.tv_nsec = 0;
85
  NULL_TIMESPEC(&t2);
86
  pthread_cleanup_push(uscitaJ,NULL);
87
  err = nanosleep(&t1, &t2);
88
  pthread_cleanup_pop(0);
89
 
90
  cprintf("J (pid=%d) ending, nanosleep returns errno=%d, t2=%ld.%ld\n",
91
              exec_shadow, err, t2.tv_sec, t2.tv_nsec/1000);
92
 
93
  return 0;
94
}
95
 
96
void signal_handler(int signo, siginfo_t *info, void *extra)
97
{
98
  cprintf("SIGNAL HANDLER: pid=%d\n",exec_shadow);
99
}
100
 
101
 
102
int main(int argc, char **argv)
103
{
104
  int err;
105
  pthread_t j1, j2, j3;
106
  struct sigaction sig_act;
107
 
108
  sig_act.sa_sigaction = (void *) signal_handler;
109
  sig_act.sa_flags = SA_SIGINFO;
110
  sigemptyset(&sig_act.sa_mask);
111
 
112
  sigaction(31, &sig_act, NULL);
113
 
114
  cprintf("main: creating J1\n");
115
  err = pthread_create(&j1, NULL, J, NULL);
116
  if (err) cprintf("Error creating J1\n");
117
 
118
  cprintf("main: creating J2\n");
119
  err = pthread_create(&j2, NULL, J, NULL);
120
  if (err) cprintf("Error creating J2\n");
121
 
122
  cprintf("main: creating J3\n");
123
  err = pthread_create(&j3, NULL, J, NULL);
124
  if (err) cprintf("Error creating J3\n");
125
 
126
  cprintf("main: waiting 1 sec\n");
127
  while (sys_gettime(NULL) < 1000000);
128
 
129
  cprintf("main: pthread_kill on j1, then wait until t=2 sec \n");
130
  pthread_kill(j1, 31);
131
 
132
  while (sys_gettime(NULL) < 2000000);
133
  cprintf("main: pthread_cancel(J2)\n");
134
  pthread_cancel(j2);
135
 
136
  cprintf("main: ending...\n");
137
 
138
  return 0;
139
}