/
preemtive_test.bak.c
100 lines (69 loc) · 1.91 KB
/
preemtive_test.bak.c
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include "mythreads.h"
#define BIG 1000
#define TIMER_INTERVAL_US 10
ucontext_t main_context, thread_context;
void timer_handler (int signum)
{
if (!interruptsAreDisabled)
{
printf(" interrupting\n");
threadYield();
}
}
//setup the timer
void set_timer()
{
struct sigaction sa;
struct itimerval timer;
/* Install timer_handler as the signal handler for SIGVTALRM. */
memset (&sa, 0, sizeof (sa));
sa.sa_handler = &timer_handler;
sigaction (SIGALRM, &sa, NULL);
/* Configure the timer to expire after TIMER_INTERVAL_US usec */
timer.it_value.tv_sec = 0;
timer.it_value.tv_usec = TIMER_INTERVAL_US;
/* and every TIMER_INTERVAL_US usec after that. */
timer.it_interval.tv_sec = 0;
timer.it_interval.tv_usec = TIMER_INTERVAL_US;
/* Start a virtual timer. It counts down whenever this process is executing. */
if (0 != setitimer (ITIMER_REAL, &timer, NULL))
{
perror("setitimer error");
}
}
int main(void)
{
int id1, id2;
int p1;
int p2;
p1 = 23;
p2 = 2;
int *result1, *result2;
//initialize the threading library. DON'T call this more than once!
threadInit();
//always start the timer after threadInit
set_timer();
id1 = threadCreate(/*some func*/,(void*)&p1);
printf("created thread 1.\n");
id2 = threadCreate(/*some func*/,(void*)&p2);
printf("created thread 2.\n");
threadJoin(id1, (void*)&result1);
printf("joined #1 --> %d.\n",*result1);
}
extern void threadInit() {
NODE *front,*rear,*temp,*front1;
}
extern int threadCreate(thFuncPtr funcPtr, void *argPtr) {
char * myStack = malloc(STACK_SIZE);
getcontext(&thread_context);
thread_context.uc_stack.ss_sp = myStack;
thread_context.uc_stack.ss_size = STACK_SIZE;
thread_context.uc_link = &main_context;
makecontext(&thread_context, (void (*)(void) funcPtr, 1, *argPtr);
}