-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode.txt
More file actions
767 lines (704 loc) · 22.8 KB
/
Code.txt
File metadata and controls
767 lines (704 loc) · 22.8 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
1.FCFS
import java.util.*;
public class Main {
public static void FCFS(int process[][]){
int n=process.length;
int perfMat[][]=new int[n][3];
Arrays.sort(process,(a,b)->a[1]-b[1]);
int currTime=0;
boolean done[]=new boolean[n];
Queue<Integer> q=new LinkedList<>();
int minArr=process[0][1];
int j=0;
while(j<n && process[j][1]==minArr){
q.add(j);
j++;
}
while(!q.isEmpty()){
int p=q.remove();
int pro=process[p][0];
int at=process[p][1];
int bt=process[p][2];
currTime+=bt;
done[pro]=true;
perfMat[process[p][0]][0]=currTime;
for(int i=0;i<n;i++){
if(process[i][1]<=currTime &&
!done[process[i][0]] && !q.contains(i)){
q.add(i);
}
}
}
Arrays.sort(process,(a,b)->a[0]-b[0]);
for(int i=0;i<n;i++){
perfMat[i][1]=perfMat[i][0]-process[i][1];
perfMat[i][2]=perfMat[i][1]-process[i][2];
}
System.out.println("The Performance Matrix is : ");
System.out.println("Process CT TAT WT");
for(int i=0;i<n;i++){
System.out.println(" P"+i+" "+perfMat[i][0]+"
"+perfMat[i][1]+" "+perfMat[i][2]);
}
float avgTAT=0;
float avgWT=0;
for(int i=0;i<n;i++){
avgTAT+=perfMat[i][1];
avgWT+=perfMat[i][2];
}
System.out.println("Avg TAT is : "+avgTAT/n);
System.out.println("Avg WT is : "+avgWT/n);
}
public static void main(String[] args) {
int
processTable[][]={{0,4,5},{1,0,3},{2,1,2},{3,2,3},{4,8,6}};
FCFS(processTable);
}
}
2. SJF
import java.util.*;
public class Solution{
public static void SJF(int processTable[][],int
performance[][]){
int n=processTable.length;
boolean done[]=new boolean[n];
boolean inQueue[]=new boolean[n];
Arrays.sort(processTable,(a,b)->a[1]-b[1]);
PriorityQueue<int[]> pq=new PriorityQueue<>((a,b)
>a[1]-b[1]);
int currTime=0;
int minTime=processTable[0][1];
for(int i=0;i<n;i++){
if(processTable[i][1]==minTime){
pq.add(new
int[]{processTable[i][0],processTable[i][2]});
inQueue[processTable[i][0]]=true;
}
}
while(!pq.isEmpty()){
int curr[]=pq.remove();
int process=curr[0];
System.out.print(process+" ");
int burstTime=curr[1];
currTime+=burstTime;
performance[process][0]=currTime;
done[process]=true;
for(int i=0;i<n;i++){
if(processTable[i][1]<=currTime &&
!done[processTable[i][0]] && !inQueue[processTable[i][0]]){
pq.add(new
int[]{processTable[i][0],processTable[i][2]});
inQueue[processTable[i][0]]=true;
}
}
}
System.out.println();
Arrays.sort(processTable,(a,b)->a[0]-b[0]);
}
public static void main(String args[]){
int
processTable[][]={{0,5,1},{1,1,2},{2,0,4},{3,3,5},{4,2,3}};
int performanceTable[][]=new
int[processTable.length][3];
SJF(processTable,performanceTable);
System.out.println("P CT TAT WT");
for(int i=0;i<processTable.length;i++){
performanceTable[i][1]=performanceTable[i][0]
processTable[i][1];
performanceTable[i][2]=performanceTable[i][1]
processTable[i][2];
System.out.println("P"+i+"
"+performanceTable[i][0]+" "+performanceTable[i][1]+"
"+performanceTable[i][2]);
}
double avgWT=0;
double avgTAT=0;
for(int i=0;i<processTable.length;i++){
avgTAT+=performanceTable[i][1];
avgWT+=performanceTable[i][2];
}
System.out.println("Avg TAT is
:"+avgTAT/processTable.length);
System.out.println("Avg WT is
:"+avgWT/processTable.length);
}
3. HRRN
import java.util.*;
public class Solution{
public static void HRRN(int processTable[][],int
performanceTable[][]){
int n=processTable.length;
boolean done[]=new boolean[n];
Arrays.sort(processTable,(a,b)->a[1]-b[1]);
Queue<Integer> q=new LinkedList<>();
int currTime=0;
int minTime=processTable[0][1];
for(int i=0;i<n;i++){
if(processTable[i][1]==minTime){
q.add(i);
}
}
while(!q.isEmpty()){
int curr=q.remove();
int process=processTable[curr][0];
System.out.print(process+" ");
int burstTime=processTable[curr][2];
currTime+=burstTime;
performanceTable[process][0]=currTime;
done[process]=true;
double maxResRatio=-1;
int maxProcess=-1;
for(int i=0;i<n;i++){
if(processTable[i][1]<=currTime &&
!done[processTable[i][0]]){
int waitingTime=currTime
processTable[i][1];
double
resRatio=(waitingTime+processTable[i][2])/processTable[i][2];
if(resRatio>maxResRatio){
maxResRatio=resRatio;
maxProcess=i;
}
}
}
if (maxProcess != -1) {
q.add(maxProcess);
} else {
for (int i = 0; i < n; i++) {
if (!done[processTable[i][0]]) {
currTime = processTable[i][1];
q.add(i);
break;
}
}
}
}
System.out.println();
Arrays.sort(processTable,(a,b)->a[0]-b[0]);
}
public static void main(String args[]){
int
processTable[][]={{0,5,1},{1,1,2},{2,0,4},{3,3,5},{4,2,3}};
int performanceTable[][]=new
int[processTable.length][3];
HRRN(processTable,performanceTable);
System.out.println("P CT TAT WT");
for(int i=0;i<processTable.length;i++){
performanceTable[i][1]=performanceTable[i][0]
processTable[i][1];
performanceTable[i][2]=performanceTable[i][1]
processTable[i][2];
System.out.println("P"+i+"
"+performanceTable[i][0]+" "+performanceTable[i][1]+"
"+performanceTable[i][2]);
}
double avgWT=0;
double avgTAT=0;
for(int i=0;i<processTable.length;i++){
avgTAT+=performanceTable[i][1];
avgWT+=performanceTable[i][2];
}
System.out.println("Avg TAT is
:"+avgTAT/processTable.length);
System.out.println("Avg WT is
:"+avgWT/processTable.length);
}
4. Round Robin
import java.util.*;
public class Solution{
public static void roundRobin(int processTable[][],int
timeQun){
Queue<Integer> q=new LinkedList<>();
int performanceTable[][]=new
int[processTable.length][3];
int done[]=new int[processTable.length];
for(int i=0;i<processTable.length;i++){
done[i]=processTable[i][2];
}
Arrays.sort(processTable,(a,b)->a[1]-b[1]);
q.add(processTable[0][0]);
int currTime=0;
while(!q.isEmpty()){
int process=q.remove();
currTime+=Math.min(done[process],timeQun);
done[process]-=Math.min(done[process],timeQun);
if(done[process]==0){
performanceTable[process][0]=currTime;
}
for(int i=0;i<processTable.length;i++){
int pId=processTable[i][0];
if(processTable[i][1]<=currTime &&
done[pId]!=0 && !q.contains(pId)){
q.add(pId);
}
}
if(done[process]>0){
q.add(process);
}
}
Arrays.sort(processTable,(a,b)->a[0]-b[0]);
double avgWt=0;
double avgTat=0;
System.out.println("P CT TAT WT");
for(int i=0;i<performanceTable.length;i++){
performanceTable[i][1]=performanceTable[i][0]
processTable[i][1];
performanceTable[i][2]=performanceTable[i][1]
processTable[i][2];
avgTat+=performanceTable[i][1];
avgWt+=performanceTable[i][2];
System.out.println("P"+i+"
"+performanceTable[i][0]+" "+performanceTable[i][1]+"
"+performanceTable[i][2]);
}
System.out.println("Avg TAT is :
"+avgTat/performanceTable.length);
System.out.println("Avg WT is :
"+avgWt/performanceTable.length);
}
public static void main(String args[]){
int
processTable[][]={{0,1,8},{1,0,4},{2,3,5},{3,5,2}};
int timeQun=1;
roundRobin(processTable,timeQun);
}
}
5. Shortest Remaining Time First(SRTF)
import java.util.*;
public class Solution{
public static void SRTF(int processTable[][],int
timeQun){
PriorityQueue<int[]> q=new PriorityQueue<>((a,b)
>a[1]-b[1]);
int performanceTable[][]=new
int[processTable.length][3];
int done[]=new int[processTable.length];
for(int i=0;i<processTable.length;i++){
done[i]=processTable[i][2];
}
Arrays.sort(processTable,(a,b)->a[1]-b[1]);
q.add(new
int[]{processTable[0][0],processTable[0][2]});
int currTime=0;
while(!q.isEmpty()){
int curr[]=q.remove();
int process=curr[0];
currTime+=Math.min(done[process],timeQun);
done[process]-=Math.min(done[process],timeQun);
if(done[process]==0){
performanceTable[process][0]=currTime;
}
for(int i=0;i<processTable.length;i++){
int pId=processTable[i][0];
if(processTable[i][1]<=currTime &&
done[pId]!=0 && !q.contains(pId)){
q.add(new int[]{pId,done[pId]});
}
}
if(done[process]>0){
q.add(new int[]{process,done[process]});
}
}
Arrays.sort(processTable,(a,b)->a[0]-b[0]);
double avgWt=0;
double avgTat=0;
System.out.println("P CT TAT WT");
for(int i=0;i<performanceTable.length;i++){
performanceTable[i][1]=performanceTable[i][0]
processTable[i][1];
performanceTable[i][2]=performanceTable[i][1]
processTable[i][2];
avgTat+=performanceTable[i][1];
avgWt+=performanceTable[i][2];
System.out.println("P"+i+"
"+performanceTable[i][0]+" "+performanceTable[i][1]+"
"+performanceTable[i][2]);
}
System.out.println("Avg TAT is :
"+avgTat/performanceTable.length);
System.out.println("Avg WT is :
"+avgWt/performanceTable.length);
}
public static void main(String args[]){
int
processTable[][]={{0,1,8},{1,0,4},{2,3,5},{3,5,2}};
int timeQun=1;
SRTF(processTable,timeQun);
}
}
6. Producer-Consumer
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#define BUFFER_SIZE 5
int buffer[BUFFER_SIZE];
int indexValue = 0;
pthread_mutex_t mutex;
sem_t empty, full;
void* producer(void* arg) {
int item = 1;
while (1) {
sem_wait(&empty); // Wait if buffer is
full
pthread_mutex_lock(&mutex); // Lock
buffer
buffer[indexValue++] = item;
printf("Producer produced: %d\n", item);
item++;
pthread_mutex_unlock(&mutex); // Unlock
buffer
sem_post(&full); // Increase count of
filled slots
sleep(1);
}
}
void* consumer(void* arg) {
while (1) {
sem_wait(&full); // Wait if buffer is
empty
pthread_mutex_lock(&mutex); // Lock
buffer
indexValue--;
int item = buffer[indexValue];
printf("Consumer consumed: %d\n", item);
pthread_mutex_unlock(&mutex); // Unlock
buffer
sem_post(&empty); // Increase count of
empty slots
sleep(1);
}
}
int main() {
pthread_t prodThread, consThread;
pthread_mutex_init(&mutex, NULL);
sem_init(&empty, 0, BUFFER_SIZE);
sem_init(&full, 0, 0);
pthread_create(&prodThread, NULL, producer,
NULL);
pthread_create(&consThread, NULL, consumer,
NULL);
pthread_join(prodThread, NULL);
pthread_join(consThread, NULL);
pthread_mutex_destroy(&mutex);
sem_destroy(&empty);
sem_destroy(&full);
return 0;
}
7. Dining Philospher
import java.util.*;
import java.util.concurrent.*;
public class Main {
static class Philosopher implements Runnable
{
int id;
Semaphore leftFork;
Semaphore rightFork;
public Philosopher(int id, Semaphore
leftFork, Semaphore rightFork) {
this.id = id;
this.leftFork = leftFork;
this.rightFork = rightFork;
}
@Override
public void run() {
while (true) {
think();
eat();
}
}
public void think() {
System.out.println("Philosopher " +
id + " is thinking.....");
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
public void eat() {
if (id % 2 == 0) {
if (leftFork.tryAcquire()) {
try {
System.out.println("Philosopher " + id + "
picked up leftFork");
if
(rightFork.tryAcquire()) {
try {
System.out.println("Philosopher " + id + "
picked up rightFork");
System.out.println("Philosopher " + id + " is
eating");
try {
TimeUnit.SECONDS.sleep(5);
} catch
(InterruptedException e) {
Thread.currentThread().interrupt();
}
} finally {
rightFork.release();
System.out.println("Philosopher " + id + " put
down rightFork");
}
}
} finally {
leftFork.release();
System.out.println("Philosopher " + id + " put
down leftFork");
}
}
} else {
if (rightFork.tryAcquire()) {
try {
System.out.println("Philosopher " + id + "
picked up rightFork");
if
(leftFork.tryAcquire()) {
try {
System.out.println("Philosopher " + id + "
picked up leftFork");
System.out.println("Philosopher " + id + " is
eating");
try {
TimeUnit.SECONDS.sleep(5);
} catch
(InterruptedException e) {
Thread.currentThread().interrupt();
}
} finally {
leftFork.release();
System.out.println("Philosopher " + id + " put
down leftFork");
}
}
} finally {
rightFork.release();
System.out.println("Philosopher " + id + " put
down rightFork");
}
}
}
}
}
public static void main(String[] args) {
int n = 5;
Semaphore forks[] = new Semaphore[n];
for (int i = 0; i < n; i++) {
forks[i] = new Semaphore(1);
}
Thread threads[] = new Thread[n];
for (int i = 0; i < n; i++) {
int leftForkIdx = i;
int rightForkIdx = (i + 1) % n;
threads[i] = new Thread(new
Philosopher(
i,
forks[leftForkIdx],
forks[rightForkIdx]
));
threads[i].start();
}
}
}
8. Page Replacement (FIFO & LRU)
import java.util.*;
class Main {
public static void replaceLRU(Queue<Character> q,int
i,String s,char c,int N){
HashSet<Character> set=new HashSet<>();
Queue<Character> tempQ=new LinkedList<>();
char charToremove='$';
for(char curr : q){
set.add(curr);
}
int idx=i-1;
while(idx>=0 && set.size()>1){
char curr=s.charAt(idx);
if(set.contains(curr)){
set.remove(curr);
}
idx--;
}
for(char curr : set){
charToremove=curr;
}
idx=0;
while(idx<N){
char curr=q.remove();
if(curr!=charToremove){
q.add(curr);
}
idx++;
}
q.add(c);
}
public static float FIFO(int N,String pages){
Queue<Character> q=new LinkedList<>();
float hit=0;
float miss=0;
for(int i=0;i<pages.length();i++){
char curr=pages.charAt(i);
if(!q.contains(curr)){
miss++;
if(q.size()<N){
q.add(curr);
}else{
q.remove();
q.add(curr);
}
}else{
hit++;
}
System.out.println("After inserting page :
"+curr);
System.out.print("Queue is : ");
for(char c : q){
System.out.print(c+" ");
}
System.out.println();
}
return hit/miss;
}
public static float LRU(int N,String pages){
Queue<Character> q=new LinkedList<>();
float hit=0;
float miss=0;
for(int i=0;i<pages.length();i++){
char curr=pages.charAt(i);
if(!q.contains(curr)){
miss++;
if(q.size()<N){
q.add(curr);
}else{
replaceLRU(q,i,pages,curr,N);
}
}else{
hit++;
}
System.out.println("After inserting page :
"+curr);
System.out.print("Queue is : ");
for(char c : q){
System.out.print(c+" ");
}
System.out.println();
}
return hit/miss;
}
public static void main(String[] args) {
int N=4;
String pagesReference="70120304230323";
float ans=FIFO(N,pagesReference);
System.out.println("Hit/Miss ratio is : "+ans);
}
}
9. Memory allocation algo (Fisrt fit,Best Fit,Worst Fit)
import java.util.*;
public class Main
{
public static void firstFit(int process[],int memory[]){
int n=process.length;
int m=memory.length;
int allocated[]=new int[n];
int memoryWastage[]=new int[n];
boolean used[]=new boolean[m];
for(int i=0;i<n;i++){
allocated[i]=-1;
}
for(int j=0;j<n;j++){
memoryWastage[j]=-1;
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(!used[j] && memory[j]>=process[i]){
allocated[i]=j;
memoryWastage[i]=memory[j]-process[i];
used[j]=true;
break;
}
}
}
System.out.println("Process Size Allocated Memory wastage");
for(int i=0;i<n;i++){
System.out.println("Process"+i+" "+process[i]+" "+(allocated[i]==-1 ? "Unallocated" : "Block"+allocated[i])+" "+(memoryWastage[i]==-1 ? " " : memoryWastage[i]));
}
}
public static void bestFit(int process[],int memory[]){
Arrays.sort(memory);
int n=process.length;
int m=memory.length;
ArrayList<Integer> memList=new ArrayList<>();
int allocated[]=new int[n];
int fragment[]=new int[n];
for(int i=0;i<n;i++){
allocated[i]=-1;
}
for(int j=0;j<n;j++){
fragment[j]=-1;
memList.add(memory[j]);
}
for(int i=0;i<n;i++){
for(int j=0;j<memList.size();j++){
if(memList.get(j)>=process[i]){
allocated[i]=memList.get(j);
fragment[i]=memList.get(j)-process[i];
memList.remove(j);
if(fragment[i]!=0){
memList.add(fragment[i]);
Collections.sort(memList);
}
break;
}
}
}
System.out.println("Process Size Allocated Fragment");
for(int i=0;i<n;i++){
System.out.println("Process"+i+" "+process[i]+" "+(allocated[i]==-1 ? "Unallocated" : +allocated[i])+" "+(fragment[i]==-1 ? " " : (fragment[i]==0 ? "None" : fragment[i])));
}
}
public static void worstFit(int process[],int memory[]){
int n=process.length;
int m=memory.length;
ArrayList<Integer> memList=new ArrayList<>();
int allocated[]=new int[n];
int fragment[]=new int[n];
for(int i=0;i<n;i++){
allocated[i]=-1;
fragment[i]=-1;
}
for(int j=0;j<m;j++){
memList.add(memory[j]);
}
for(int i=0;i<n;i++){
int max=Integer.MIN_VALUE;
int maxIdx=-1;
for(int j=0;j<memList.size();j++){
if(memList.get(j)>max){
max=memList.get(j);
maxIdx=j;
}
}
if(max>=process[i]){
allocated[i]=maxIdx;
fragment[i]=max-process[i];
memList.remove(maxIdx);
memList.add(maxIdx,fragment[i]);
}
}
System.out.println("Process Block Allocated Fragment");
for(int i=0;i<n;i++){
System.out.println("Process"+i+" "+process[i]+" "+(allocated[i]==-1 ? "Unallocated" : "Block"+allocated[i])+" "+(fragment[i]==-1 ? " " : (fragment[i]==0 ? "None" : fragment[i])));
}
}
public static void main(String[] args) {
//int processes[]={90,50,30,40};
//int memory[]={20,100,40,200,10};
//firstFit(processes,memory);
//int processes[]={20,200,500,50};
//int memory[]={30,50,200,700,980};
//bestFit(processes,memory);
// int processes[]={212, 417, 112, 426};
// int memory[]={100, 500, 200, 300, 600};
// worstFit(processes,memory);
}
}