-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThread.c
More file actions
145 lines (126 loc) · 4.23 KB
/
Thread.c
File metadata and controls
145 lines (126 loc) · 4.23 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <stddef.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "Init.h"
int ThreadCount = 0;
int ThreadNext = 0;
int NextThreadID = 0;
Thread *ThreadQueue[MAX_THREADS] = {NULL};
// Local Scope declaration
Thread *CreateThread(op *Code,char * Name);
void InsertThread(Thread *thread);
char *DecodeStatus(int);
// Definition
void ThreadInit()
{
static op Idle[] = {1,opLoop,opInc,opEndLoop,opHalt};
static op prog1[] = {opStartStr,'\n','1','-','d','a','e','r','h','T',' ',opPrintStr,'9',opEmit, opHalt};
static op prog2[] = {23, opLoop, '2', opEmit, opEndLoop, opHalt};
static op prog3[] = {18, opLoop, '3', opEmit, opEndLoop, opHalt};
InsertThread(CreateThread(Idle,"Idle"));
InsertThread(CreateThread(prog1,"Thread 1"));
InsertThread(CreateThread(prog2,"Thread 2"));
InsertThread(CreateThread(prog3,"Thread 3"));
ThreadDetails();
ThreadNext = 0;
}
Thread *CreateThread(op *Code,char * Name)
{
Thread *New = (Thread*) malloc (sizeof(Thread));
if(New != NULL)
{
New->name = strdup(Name);
New->id = NextThreadID;
New->state = BIRTH_STATE;
New->code = Code;
New->ip = 0;
New->sp = 0;
New->Stat.create_time = GetCycle();
New->Stat.ready_start_time = 0;
New->Stat.ready_wait_time = 0;
New->Stat.num_cpu_bursts = 0;
NextThreadID = NextThreadID + 1 ;
return New;
}
else
{
Raise("There is no memory to create thread\n");
}
}
void MakeThreadReady(Thread *thread)
{
thread->Stat.ready_start_time = GetCycle();
thread->state = READY_STATE;
}
void SwitchToThread(Thread *thread)
{
thread->Stat.ready_wait_time = thread->Stat.ready_wait_time + (GetCycle() - thread->Stat.ready_start_time);
thread->state = RUNNING_STATE;
ContextSwitching(thread);
thread->Stat.num_cpu_bursts = thread->Stat.num_cpu_bursts + 1;
}
void InsertThread(Thread *thread)
{
if(ThreadCount == MAX_THREADS)
{
Raise("Sorry all the threads are being used\n");
}
MakeThreadReady(thread);
ThreadQueue[ThreadNext] = thread;
ThreadNext = ThreadNext + 1;
if(ThreadNext >= MAX_THREADS)
{
ThreadNext = 0;
}
ThreadCount = ThreadCount + 1;
}
void RemoveThread(Thread *thread)
{
int i;
for(i = 0; i<MAX_THREADS;i++)
{
Thread *th = ThreadQueue[i];
if(th && (thread->id==th->id))
{
ThreadQueue[i] = NULL;
ThreadCount = ThreadCount - 1;
break;
}
}
}
char *DecodeStatus(int ID)
{
switch (ID)
{
case BIRTH_STATE:
return "BIRTH";
case READY_STATE:
return "Ready";
case RUNNING_STATE:
return "Running";
case DEATH_STATE:
return "Died";
default:
return "None";
}
}
void ThreadDetails()
{
printf("\n");
printf("\n Thread initilization table\n");
printf("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
printf("+ Thread Name ID State Code Instruction Pointer Stack Stack Pointer");
printf(" C.Time S.Time W.Time Burst +\n");
printf("+==================================================================================================================================================================+\n");
for(int i =0; i<MAX_THREADS;i++)
{
if(ThreadQueue[i] != NULL)
{
printf("+ %-20s%-7d%-10s%-20p%-24d%-20p%-20d",ThreadQueue[i]->name,ThreadQueue[i]->id,DecodeStatus(ThreadQueue[i]->state),ThreadQueue[i]->code,ThreadQueue[i]->ip,ThreadQueue[i]->stack,ThreadQueue[i]->sp);
printf("%-10d%-10d%-10d%-9d +\n",ThreadQueue[i]->Stat.create_time,ThreadQueue[i]->Stat.ready_start_time,ThreadQueue[i]->Stat.ready_wait_time,ThreadQueue[i]->Stat.num_cpu_bursts);
}
}
printf("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
printf("\n");
}