├── Pass 2 of 2 pass Assembler ├── Length.dat ├── Optab.dat ├── Symbol.dat ├── Input.dat └── Pass2.c ├── Pass 1 of 2 pass Assembler ├── Optab.dat ├── Input.dat └── PassOne.c ├── Absolute Loader ├── Input.dat └── AbsoluteLoader.c ├── README.md ├── FCFS.c ├── SJF.c ├── Priority.c ├── Round_Robin.c ├── Single_Level_Dir_File.c ├── Bankers_Algorithm.c └── Disk_Scheduling.c /Pass 2 of 2 pass Assembler/Length.dat: -------------------------------------------------------------------------------- 1 | 25 2 | -------------------------------------------------------------------------------- /Pass 2 of 2 pass Assembler/Optab.dat: -------------------------------------------------------------------------------- 1 | LDA 00 2 | STA 23 3 | ADD 01 4 | SUB 05 5 | -------------------------------------------------------------------------------- /Pass 2 of 2 pass Assembler/Symbol.dat: -------------------------------------------------------------------------------- 1 | 1012 ALPHA 2 | 1017 ONE 3 | 1019 TWO 4 | 1022 BETA 5 | -------------------------------------------------------------------------------- /Pass 1 of 2 pass Assembler/Optab.dat: -------------------------------------------------------------------------------- 1 | START ** 2 | LDA 03 3 | STA 0f 4 | LDCH 53 5 | STCH 57 6 | END ** 7 | -------------------------------------------------------------------------------- /Absolute Loader/Input.dat: -------------------------------------------------------------------------------- 1 | H 1000 232 2 | T 1000 142033 483039 102036 3 | T 2000 298300 230000 282030 302015 4 | E 5 | -------------------------------------------------------------------------------- /Pass 1 of 2 pass Assembler/Input.dat: -------------------------------------------------------------------------------- 1 | ***** START 2000 2 | ***** LDA FIVE 3 | ***** STA ALPHA 4 | ***** LDCH CHARZ 5 | ***** STCH C1 6 | ALPHA RESW 2 7 | FIVE WORD 5 8 | CHARZ BYTE C'Z' 9 | C1 RESB 1 10 | ***** END ***** 11 | -------------------------------------------------------------------------------- /Pass 2 of 2 pass Assembler/Input.dat: -------------------------------------------------------------------------------- 1 | - COPY START 1000 2 | 1000 - LDA ALPHA 3 | 1003 - ADD ONE 4 | 1006 - SUB TWO 5 | 1009 - STA BETA 6 | 1012 ALPHA BYTE C'KLNCE 7 | 1017 ONE RESB 2 8 | 1019 TWO WORD 5 9 | 1022 BETA RESW 1 10 | 1025 - END - 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CPU Scheduling Algorithm In C 2 | 3 | This repository contains various CPU Scheduling programs in C language as a part of our System Software Lab. 4 | The programs that have been completed so far are : 5 | 6 | 1. First Come First Serve(FCFS) Non Pre-emptive 7 | 2. Shortest Job First(SJF) Non Pre-emptive 8 | 3. Priority Scheduling Non Pre-emptive 9 | 4. Round Robin Scheduling Pre-emptive 10 | 5. Bankers Algorithm 11 | 6. Simulation of Disk Scheduling Algorithm -> FCFS, SSTF, SCAN 12 | 7. Single Level Directory File Organization 13 | 8. Pass One of a Two Pass Assembler 14 | 9. Pass Two of a Two pass Assembler 15 | 10. Absolute Loader 16 | -------------------------------------------------------------------------------- /Absolute Loader/AbsoluteLoader.c: -------------------------------------------------------------------------------- 1 | // CPU-Scheduling-Algorithm-In-C 2 | // Absolute Loader 3 | 4 | # include 5 | # include 6 | 7 | void main() 8 | { 9 | char input[10]; 10 | int start, length, address; 11 | FILE *fp1, *fp2; 12 | 13 | fp1=fopen("Input.dat","r"); 14 | fp2=fopen("Output.dat","w"); 15 | 16 | fscanf(fp1,"%s",input); 17 | while(strcmp(input,"E")!=0) 18 | { 19 | if(strcmp(input,"H") == 0) 20 | { 21 | fscanf(fp1,"%d",&start); 22 | fscanf(fp1,"%d",&length); 23 | } 24 | else if(strcmp(input,"T") == 0) 25 | { 26 | fscanf(fp1,"%d",&address); 27 | fscanf(fp1,"%s",input); 28 | fprintf(fp2,"%d\t%c%c\n",address,input[0],input[1]); 29 | fprintf(fp2,"%d\t%c%c\n",(address+1),input[2],input[3]); 30 | fprintf(fp2,"%d\t%c%c\n",(address+2),input[4],input[5]); 31 | address+=3; 32 | } 33 | else 34 | { 35 | fprintf(fp2,"%d\t%c%c\n",address,input[0],input[1]); 36 | fprintf(fp2,"%d\t%c%c\n",(address+1),input[2],input[3]); 37 | fprintf(fp2,"%d\t%c%c\n",(address+2),input[4],input[5]); 38 | address+=3; 39 | } 40 | fscanf(fp1,"%s",input); 41 | } 42 | fclose(fp1); 43 | fclose(fp2); 44 | } 45 | -------------------------------------------------------------------------------- /FCFS.c: -------------------------------------------------------------------------------- 1 | 2 | // CPU-Scheduling-Algorithm-In-C 3 | // First Come First Served(FCFS) Scheduling Algorithm(Non Pre-emptive) 4 | 5 | #include 6 | #include 7 | 8 | void main() 9 | { 10 | int i, n, *bt, *wt, *tat; 11 | float avgtat, avgwt; 12 | printf("\n Enter the number of processes : "); 13 | scanf("%d", &n); 14 | 15 | bt = (int*)malloc(n*sizeof(int)); 16 | wt = (int*)malloc(n*sizeof(int)); 17 | tat = (int*)malloc(n*sizeof(int)); 18 | 19 | printf("\n Enter the burst time for each process \n"); 20 | for(i=0; i 5 | #include 6 | #include 7 | 8 | void main() 9 | { 10 | char label[20], opcode[20], operand[20], code[20], length[20]; 11 | int locctr, start, size; 12 | FILE *f1, *f2, *f3, *f4; 13 | 14 | f1 = fopen("Input.dat", "r"); 15 | f2 = fopen("Optab.dat", "r"); 16 | f3 = fopen("Output.dat", "w"); 17 | f4 = fopen("Symtab.dat", "w"); 18 | 19 | fscanf(f1, "%s\t%s\t%s", label, opcode, operand); 20 | if(strcmp(opcode, "START") == 0) 21 | { 22 | locctr = atoi(operand); 23 | start = locctr; 24 | fprintf(f3, "\t\t%s\t%s\t%s\n", label, opcode, operand); 25 | fscanf(f1, "%s\t%s\t%s", label, opcode, operand); 26 | } 27 | else 28 | { 29 | locctr = 0; 30 | } 31 | 32 | while(strcmp(opcode, "END") != 0) 33 | { 34 | fprintf(f3, "%d\t", locctr); 35 | 36 | if(strcmp(label, "*****") != 0) 37 | { 38 | fprintf(f4, "%d\t%s\n", locctr, label); 39 | } 40 | fscanf(f2, "%s\t%s", code, length); 41 | 42 | while(strcmp(code, "END") != 0) 43 | { 44 | if(strcmp(opcode, code) == 0) 45 | { 46 | locctr += 3; 47 | rewind(f2); 48 | break; 49 | } 50 | fscanf(f2, "%s\t%s", code, length); 51 | } 52 | 53 | if(strcmp(opcode, "RESW") == 0) 54 | { 55 | locctr += 3*(atoi(operand)); 56 | } 57 | else if(strcmp(opcode, "WORD") == 0) 58 | { 59 | locctr += 3; 60 | } 61 | else if(strcmp(opcode, "BYTE") == 0) 62 | { 63 | locctr += 1; 64 | } 65 | else if(strcmp(opcode, "RESB") == 0) 66 | { 67 | locctr += atoi(operand); 68 | } 69 | fprintf(f3, "%s\t%s\t%s\n", label, opcode, operand); 70 | fscanf(f1, "%s\t%s\t%s", label, opcode, operand); 71 | } 72 | fprintf(f3, "%d\t%s\t%s\t%s\n", locctr, label, opcode, operand); 73 | 74 | size = locctr - start; 75 | printf("\n\n Length of code = %d \n\n", size); 76 | 77 | fclose(f1); 78 | fclose(f2); 79 | fclose(f3); 80 | fclose(f4); 81 | } 82 | -------------------------------------------------------------------------------- /SJF.c: -------------------------------------------------------------------------------- 1 | 2 | // CPU-Scheduling-Algorithm-In-C 3 | // Shortest Job First(SJF) Scheduling Algorithm(Non Pre-emptive) 4 | 5 | #include 6 | #include 7 | 8 | void main() 9 | { 10 | int n, i, j, pos, temp, *bt, *wt, *tat, *p; 11 | float avgwt = 0, avgtat = 0; 12 | printf("\n Enter the number of processes : "); 13 | scanf("%d", &n); 14 | 15 | p = (int*)malloc(n*sizeof(int)); 16 | bt = (int*)malloc(n*sizeof(int)); 17 | wt = (int*)malloc(n*sizeof(int)); 18 | tat = (int*)malloc(n*sizeof(int)); 19 | 20 | printf("\n Enter the burst time for each process \n"); 21 | for(i=0; i 6 | #include 7 | 8 | void main() 9 | { 10 | int n, i, j, pos, temp, *bt, *wt, *tat, *p, *pt; 11 | float avgwt = 0, avgtat = 0; 12 | printf("\n Enter the number of processes : "); 13 | scanf("%d", &n); 14 | 15 | p = (int*)malloc(n*sizeof(int)); 16 | bt = (int*)malloc(n*sizeof(int)); 17 | wt = (int*)malloc(n*sizeof(int)); 18 | tat = (int*)malloc(n*sizeof(int)); 19 | 20 | printf("\n Enter the burst time and priority for each process "); 21 | for(i=0; i 6 | #include 7 | 8 | void main() 9 | { 10 | int n, i, tempn, count, terminaltime=0, initialtime, qt, flag=0, *bt, *wt, *tat, *tempbt; 11 | float avgwt = 0, avgtat = 0; 12 | printf("\n Enter the number of processes : "); 13 | scanf("%d", &n); 14 | tempn = n; 15 | 16 | tempbt = (int*)malloc(n*sizeof(int)); 17 | bt = (int*)malloc(n*sizeof(int)); 18 | wt = (int*)malloc(n*sizeof(int)); 19 | tat = (int*)malloc(n*sizeof(int)); 20 | 21 | printf("\n Enter the Quantum Time : "); 22 | scanf("%d", &qt); 23 | printf("\n Enter the burst time for each process \n\n"); 24 | for(i=0; i 0) { 39 | terminaltime += tempbt[count]; 40 | tempbt[count] = 0; 41 | wt[count] = terminaltime - bt[count]; 42 | tat[count] = wt[count] + bt[count]; 43 | flag = 1; 44 | } 45 | else if(tempbt[count] > qt) { 46 | tempbt[count] -= qt; 47 | terminaltime += qt; 48 | } 49 | if(tempbt[count] == 0 && flag == 1) { 50 | tempn--; 51 | flag=0; 52 | } 53 | if(initialtime != terminaltime) { 54 | printf(" %d\t|| P%d ||\t%d\n", initialtime, count, terminaltime); 55 | } 56 | if(count == n-1) 57 | count = 0; 58 | else 59 | ++count; 60 | } 61 | 62 | printf("\n PROCESS \t BURST TIME \t WAITING TIME \t TURNAROUND TIME \n"); 63 | printf("--------------------------------------------------------------\n"); 64 | for(i=0; i 5 | #include 6 | #include 7 | void main() 8 | { 9 | FILE *f1, *f3, *f2, *f4, *f5; 10 | int op1[10], txtlen, txtlen1, i, j=0, len; 11 | char addr[5], symaddr[5], op[5], start[10], temp[30], line[20], label[20], mne[10], operand[10], symtab[10], opmne[10]; 12 | 13 | f1=fopen("Input.dat","r"); 14 | f2=fopen("Length.dat","r"); 15 | f3=fopen("Optab.dat","r"); 16 | f4=fopen("Symbol.dat","r"); 17 | f5=fopen("Output.dat","w"); 18 | 19 | fscanf(f1,"%s%s%s%s", addr, label, mne, operand); 20 | if(strcmp(mne,"START") == 0) 21 | { 22 | strcpy(start, operand); 23 | fscanf(f2,"%d", &len); 24 | } 25 | fprintf(f5,"H^%s^%s^%d\nT^00%s^", label, start, len, start); 26 | fscanf(f1,"%s%s%s%s", addr, label, mne, operand); 27 | while(strcmp(mne,"END") != 0) 28 | { 29 | fscanf(f3,"%s%s", opmne, op); 30 | while(!feof(f3)) 31 | { 32 | if(strcmp(mne,opmne) == 0) 33 | { 34 | fclose(f3); 35 | fscanf(f4,"%s%s", symaddr, symtab); 36 | while(!feof(f4)) 37 | { 38 | if(strcmp(operand, symtab) == 0) 39 | { 40 | fprintf(f5,"%s%s^",op, symaddr); 41 | break; 42 | } 43 | else 44 | fscanf(f4,"%s%s", symaddr, symtab); 45 | } 46 | break; 47 | } 48 | else 49 | fscanf(f3,"%s%s", opmne, op); 50 | } 51 | if((strcmp(mne,"BYTE") == 0)||(strcmp(mne,"WORD") == 0)) 52 | { 53 | if(strcmp(mne,"WORD") == 0){ 54 | fprintf(f5,"0000%s^", operand); 55 | } 56 | else 57 | { 58 | len=strlen(operand); 59 | for(i=2;i 5 | #include 6 | #include 7 | 8 | struct { 9 | char fileName[50][50]; 10 | int fileCount; 11 | }dir; 12 | 13 | void main() 14 | { 15 | char file[50]; 16 | int choice, i; 17 | dir.fileCount = 0; 18 | 19 | do { 20 | printf("\n 1. CREATE FILE \n 2. DELETE FILE \n 3. SEARCH FILE \n 4. DISPLAY FILES \n 5. EXIT \n"); 21 | printf("\n Enter your choice : "); 22 | scanf("%d", &choice); 23 | 24 | switch(choice) 25 | { 26 | case 1: printf("\n Enter the name of file : "); 27 | scanf("%s", file); 28 | 29 | for(i=0; i 5 | 6 | void main() 7 | { 8 | int process, resource, i, j, instance, k=0, count=0, temp=0; 9 | printf("\n Enter No. of Process : "); 10 | scanf("%d", &process); 11 | printf(" Enter No. of Resources : "); 12 | scanf("%d",&resource); 13 | int available[resource], max[process][resource], allocated[process][resource], need[process][resource], completed[process]; 14 | 15 | for(i=0; i P[%d] ", i); 70 | completed[i] = 1; 71 | for(j=0; j FCFS, SSTF, SCAN 3 | 4 | #include 5 | #include 6 | int seektime = 0; 7 | 8 | void fcfs(int sequence[], int head, int n) 9 | { 10 | int temp = head; 11 | seektime = 0; 12 | printf("\n The Disk sequence is : \n"); 13 | for(int i=0; i %d", sequence[i]); 16 | seektime += abs(sequence[i]-temp); 17 | temp = sequence[i]; 18 | } 19 | 20 | printf("\n\n Seek Time = %d", seektime); 21 | } 22 | 23 | 24 | void sstf(int sequence[], int head, int n) 25 | { 26 | seektime = 0; 27 | int arr[n], min, temp, i, j, pos; 28 | 29 | for(i=0; i abs(head - sequence[i])) 42 | { 43 | min = abs(head - sequence[i]); 44 | pos = i; 45 | } 46 | } 47 | } 48 | if(min == 999) 49 | break; 50 | arr[pos] = 1; 51 | seektime += min; 52 | head = sequence[pos]; 53 | printf(" > %d", sequence[pos]); 54 | } 55 | printf("\n\n Seek Time = %d", seektime); 56 | 57 | } 58 | 59 | void scan(int sequence[], int head, int n, int t) 60 | { 61 | int temp, i, j; 62 | seektime = 0; 63 | 64 | printf("\n The Disk sequence is : \n"); 65 | for(i=0;i sequence[j]) 70 | { 71 | temp = sequence[i]; 72 | sequence[i] = sequence[j]; 73 | sequence[j] = temp; 74 | } 75 | } 76 | 77 | } 78 | temp = head; 79 | for(i=0; i head) 82 | { 83 | printf(" > %d", sequence[i]); 84 | seektime += abs(temp - sequence[i]); 85 | temp = sequence[i]; 86 | } 87 | } 88 | 89 | 90 | seektime += abs(t-1 - sequence[i-1]); 91 | temp = t-1; 92 | for(i=n-1; i>=0; i--) 93 | { 94 | if(head > sequence[i]) 95 | { 96 | printf(" > %d", sequence[i]); 97 | seektime += abs(temp - sequence[i]); 98 | temp = sequence[i]; 99 | } 100 | } 101 | printf("\n\n Seek Time = %d", seektime); 102 | } 103 | 104 | 105 | void main() 106 | { 107 | int n, t, i, head, temp, choice; 108 | printf("\n Enter number of disk request in queue : "); 109 | scanf("%d", &n); 110 | printf(" Enter total number of tracks : "); 111 | scanf("%d", &t); 112 | int sequence[n]; 113 | printf("\n Enter the disk request sequence for a disk with %d tracks : ", t); 114 | for(i=0; i