Repository navigation
Expand file tree
/
Copy pathFileContextSwitch.c
More file actions
151 lines (128 loc) · 4.23 KB
/
Copy pathFileContextSwitch.c
File metadata and controls
151 lines (128 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
146
147
148
149
150
151
/* Context Switch between 2 Threads with no interrupt handling */
#include<stdio.h> //for printf
#include<pthread.h> //for threads, mutual exclusion, conditional wait and signalling
#include<time.h> //for accurate clock time
#include<limits.h> //for min-max limits of several data types
#include<inttypes.h> //for using uint64_t data type
#include<stdlib.h> //for malloc
//#include<semaphore.h>
#include <stdbool.h> //for boolean variables
#include<string.h>
#define TIMESLICE 1000000 //nanoseconds to define milliseconds
#define BILLION 1000000000L
void *threadFunction(void *arg);
void *dispatcher(void *arg);
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
int numProcWait = 2; //No of processes running
typedef struct ThreadControlBlock {
bool blockedState;
char *filePaths[3];
} TCB;
void *threadFunction(void *arg) {
int i,instructions = INT_MAX/64;
bool locked,unlockCheck=true; // locked for synchronizing the thread withing itself
char buff[100];
uint64_t diff=0;
TCB *process = (TCB *)arg;
int fileCount = sizeof(process->filePaths)/sizeof(process->filePaths[0]);
FILE *fp[fileCount];
char *fileNames[fileCount];
printf("\nWaiting to start Thread id: %lu\n",pthread_self());
while(process->blockedState);
for(i = 0; i<fileCount; i++) {
if(process->filePaths[i]!=NULL) {
//printf("NOT NULL\n");
fileNames[i] = malloc(sizeof(char) * strlen(process->filePaths[i]));
strcpy(fileNames[i],process->filePaths[i]);
printf("FILE NAME: %s\n",fileNames[i]);
fp[i] = fopen(fileNames[i], "r");
}
else {
fp[i] = NULL;
}
}
printf("Received Signal Successful! Thread ID: %lu.\n",pthread_self());
locked = true;
char c = 0;
for (i = 0; i < instructions; i++) {
if(process->blockedState) {
while(process->blockedState);
printf("\nCurrent executing Thread ID: %lu.\n",pthread_self());
}
else {
if((c = fgetc(fp[0])) != EOF) {
//printf("%c",c);
//printf(" ");
}
else {
printf("\n\nBREAKING!\n");
break;
}
}
}
for(i = 0; i<fileCount; i++) {
if(process->filePaths[i]!=NULL) {
free(fileNames[i]);
fclose(fp[i]);
}
}
numProcWait--;
printf("EXITING THREAD: %lu\n\n",pthread_self());
printf("Number of processes waiting: %d\n\n",numProcWait);
return NULL;
}
void *dispatcher(void *arg) {
uint64_t diff = 0;
TCB *process = (TCB *) arg;
//printf("STATE1: %s\n",*blockedState?"true":"false");
//printf("STATE2: %s\n",*(blockedState+1)?"true":"false");
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
int currentThread = 0;
(process + currentThread)->blockedState = false;
while(numProcWait>0) {
clock_gettime(CLOCK_MONOTONIC, &end);
diff = BILLION * (end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec;
if(diff > TIMESLICE) {
//if(numProcWait>1) {
(process + currentThread)->blockedState = !(process + currentThread)->blockedState;
if(currentThread == 0) {
currentThread = 1;
}
else {
currentThread = 0;
}
(process + currentThread)->blockedState = !(process + currentThread)->blockedState;
//}
clock_gettime(CLOCK_MONOTONIC, &start);
}
}
}
int main() {
pthread_t tid[2];
pthread_t disp; //dispatcher
bool blockedState[2] = {true,true}; //true = blocked, false = unblocked
uint64_t diff;
struct timespec start, end;
TCB process[2];
process[0].blockedState = true;
process[1].blockedState = true;
process[0].filePaths[0] = "/home/bhavya/bhavya/Github/OS/Project/file1.txt";
process[0].filePaths[1] = NULL;
process[0].filePaths[2] = NULL;
process[1].filePaths[0] = "/home/bhavya/bhavya/Github/OS/Project/file2.txt";
process[1].filePaths[1] = NULL;
process[1].filePaths[2] = NULL;
clock_gettime(CLOCK_MONOTONIC, &start);
pthread_create(&disp, NULL, dispatcher, &process);
pthread_create(&tid[0], NULL, threadFunction, (void *)&process[0]);
pthread_create(&tid[1], NULL, threadFunction, (void *)&process[1]);
pthread_join(tid[0], NULL);
printf("Thread 1 EXITED. No. of Waiting Processes: %d\n",numProcWait);
pthread_join(tid[1], NULL);
printf("Thread 2 EXITED. No. of Waiting Processes: %d\n",numProcWait);
clock_gettime(CLOCK_MONOTONIC, &end);
diff = BILLION * (end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec;
printf("Total Run Time: %" PRIu64 " ms\n",diff/1000000);
pthread_mutex_destroy(&lock);
}