├── .github └── out_img │ ├── img_11.png │ ├── p_01_out.jpg │ ├── p_02_out.jpg │ ├── p_03_out.jpg │ ├── p_04_out.jpg │ ├── p_05_out-1.jpg │ ├── p_05_out-2.jpg │ ├── p_06_out-1.png │ ├── p_06_out-2.png │ ├── p_06_out-3.png │ ├── p_07_out.png │ ├── p_08_out.png │ ├── p_09_out.png │ ├── p_10_out.jpg │ ├── p_11_out.png │ ├── p_12_out.png │ ├── p_16_out.png │ ├── p_17_out.png │ └── p_18_out.png ├── .gitignore ├── CPU Scheduling ├── FCFS │ ├── 1.fcfs.c │ └── README.md ├── Priority │ ├── 3.priority.c │ └── README.md ├── README.md ├── RoundRobin │ ├── 4.roundrobin.c │ └── README.md └── SJF │ ├── 2.sjf.c │ └── README.md ├── Deadlocks ├── BankersAlgorithm │ ├── 7.bankers.c │ └── README.md └── README.md ├── Disk Management ├── CSCAN │ ├── 10.csan.c │ └── README.md ├── FCFS │ ├── 8.fcfsDisk.c │ └── README.md ├── README.md └── SCAN │ ├── 9.scan.c │ └── README.md ├── LICENSE ├── Memory Management ├── README.md ├── SingleLevelDir │ ├── 5.singleLevelDir.c │ └── README.md └── TwoLevelDir │ ├── 6.twoLevelDir.c │ └── README.md ├── Process Synchronization ├── Absolute Loader │ ├── 19.absolute.c │ ├── README.md │ ├── input.txt │ └── output.txt ├── Assemblers │ ├── README.md │ ├── Single Pass Assembler │ │ ├── 18.singlepass.c │ │ ├── README.md │ │ ├── input.txt │ │ ├── intermediate.txt │ │ ├── objcode.txt │ │ ├── optab.txt │ │ ├── output.txt │ │ └── symtab.txt │ └── Two Pass Assembler │ │ ├── Pass 1 of a Two Pass Assembler │ │ ├── 16.pass1.c │ │ ├── README.md │ │ ├── input.txt │ │ ├── intermediate.txt │ │ ├── length.txt │ │ ├── optab.txt │ │ └── symtab.txt │ │ └── Pass 2 of a Two Pass Assembler │ │ ├── 17.pass2.c │ │ ├── README.md │ │ ├── intermediate.txt │ │ ├── objcode.txt │ │ ├── output.txt │ │ └── symtab.txt ├── Dining Philosophers │ ├── 15.dining.c │ └── README.md ├── Producer Consumer │ ├── 14.producer.c │ └── README.md └── README.md ├── README.md ├── Virtual Memory ├── FIFO │ ├── 11.fifoPage.c │ └── README.md ├── LFU │ ├── 13.lfu.c │ └── README.md ├── LRU │ ├── 12.lru.c │ └── README.md └── README.md └── _config.yml /.github/out_img/img_11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ceadoor/System-Software-lab/d6a95057c72ad3eacd932ad0214251a262b54211/.github/out_img/img_11.png -------------------------------------------------------------------------------- /.github/out_img/p_01_out.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ceadoor/System-Software-lab/d6a95057c72ad3eacd932ad0214251a262b54211/.github/out_img/p_01_out.jpg -------------------------------------------------------------------------------- /.github/out_img/p_02_out.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ceadoor/System-Software-lab/d6a95057c72ad3eacd932ad0214251a262b54211/.github/out_img/p_02_out.jpg -------------------------------------------------------------------------------- /.github/out_img/p_03_out.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ceadoor/System-Software-lab/d6a95057c72ad3eacd932ad0214251a262b54211/.github/out_img/p_03_out.jpg -------------------------------------------------------------------------------- /.github/out_img/p_04_out.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ceadoor/System-Software-lab/d6a95057c72ad3eacd932ad0214251a262b54211/.github/out_img/p_04_out.jpg -------------------------------------------------------------------------------- /.github/out_img/p_05_out-1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ceadoor/System-Software-lab/d6a95057c72ad3eacd932ad0214251a262b54211/.github/out_img/p_05_out-1.jpg -------------------------------------------------------------------------------- /.github/out_img/p_05_out-2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ceadoor/System-Software-lab/d6a95057c72ad3eacd932ad0214251a262b54211/.github/out_img/p_05_out-2.jpg -------------------------------------------------------------------------------- /.github/out_img/p_06_out-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ceadoor/System-Software-lab/d6a95057c72ad3eacd932ad0214251a262b54211/.github/out_img/p_06_out-1.png -------------------------------------------------------------------------------- /.github/out_img/p_06_out-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ceadoor/System-Software-lab/d6a95057c72ad3eacd932ad0214251a262b54211/.github/out_img/p_06_out-2.png -------------------------------------------------------------------------------- /.github/out_img/p_06_out-3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ceadoor/System-Software-lab/d6a95057c72ad3eacd932ad0214251a262b54211/.github/out_img/p_06_out-3.png -------------------------------------------------------------------------------- /.github/out_img/p_07_out.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ceadoor/System-Software-lab/d6a95057c72ad3eacd932ad0214251a262b54211/.github/out_img/p_07_out.png -------------------------------------------------------------------------------- /.github/out_img/p_08_out.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ceadoor/System-Software-lab/d6a95057c72ad3eacd932ad0214251a262b54211/.github/out_img/p_08_out.png -------------------------------------------------------------------------------- /.github/out_img/p_09_out.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ceadoor/System-Software-lab/d6a95057c72ad3eacd932ad0214251a262b54211/.github/out_img/p_09_out.png -------------------------------------------------------------------------------- /.github/out_img/p_10_out.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ceadoor/System-Software-lab/d6a95057c72ad3eacd932ad0214251a262b54211/.github/out_img/p_10_out.jpg -------------------------------------------------------------------------------- /.github/out_img/p_11_out.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ceadoor/System-Software-lab/d6a95057c72ad3eacd932ad0214251a262b54211/.github/out_img/p_11_out.png -------------------------------------------------------------------------------- /.github/out_img/p_12_out.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ceadoor/System-Software-lab/d6a95057c72ad3eacd932ad0214251a262b54211/.github/out_img/p_12_out.png -------------------------------------------------------------------------------- /.github/out_img/p_16_out.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ceadoor/System-Software-lab/d6a95057c72ad3eacd932ad0214251a262b54211/.github/out_img/p_16_out.png -------------------------------------------------------------------------------- /.github/out_img/p_17_out.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ceadoor/System-Software-lab/d6a95057c72ad3eacd932ad0214251a262b54211/.github/out_img/p_17_out.png -------------------------------------------------------------------------------- /.github/out_img/p_18_out.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ceadoor/System-Software-lab/d6a95057c72ad3eacd932ad0214251a262b54211/.github/out_img/p_18_out.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Linker output 11 | *.ilk 12 | *.map 13 | *.exp 14 | 15 | # Precompiled Headers 16 | *.gch 17 | *.pch 18 | 19 | # Libraries 20 | *.lib 21 | *.a 22 | *.la 23 | *.lo 24 | 25 | # Shared objects (inc. Windows DLLs) 26 | *.dll 27 | *.so 28 | *.so.* 29 | *.dylib 30 | 31 | # Executables 32 | *.exe 33 | *.out 34 | *.app 35 | *.i*86 36 | *.x86_64 37 | *.hex 38 | 39 | # Debug files 40 | *.dSYM/ 41 | *.su 42 | *.idb 43 | *.pdb 44 | 45 | # Kernel Module Compile Results 46 | *.mod* 47 | *.cmd 48 | .tmp_versions/ 49 | modules.order 50 | Module.symvers 51 | Mkfile.old 52 | dkms.conf 53 | 54 | # Text Editor Cache 55 | .vscode/ 56 | 57 | # working directory to store unwanted files 58 | .working -------------------------------------------------------------------------------- /CPU Scheduling/FCFS/1.fcfs.c: -------------------------------------------------------------------------------- 1 | /* 2 | Author : abhijithvijayan 3 | Created on : 23 Sep,18 21:45 4 | Title : FCFS Scheduling 5 | */ 6 | 7 | #include 8 | 9 | int i, n; 10 | float tatAvg, wtAvg; 11 | 12 | void read(int b[]) { 13 | for (i = 0; i < n; ++i) { 14 | printf("Enter the burst time of process %d: ", i); 15 | scanf("%d", &b[i]); 16 | } 17 | } 18 | 19 | void findWaitingtime(int b[], int wt[]) { 20 | wt[0] = 0; 21 | int wtSum = 0; 22 | for (i = 1; i < n; ++i) { 23 | wt[i] = wt[i - 1] + b[i - 1]; 24 | wtSum += wt[i]; 25 | } 26 | wtAvg = (float)wtSum / n; 27 | } 28 | 29 | void findTurnAroundtime(int tat[], int b[], int wt[]) { 30 | int tatSum = 0; 31 | for (i = 0; i < n; ++i) { 32 | tat[i] = b[i] + wt[i]; 33 | tatSum += tat[i]; 34 | } 35 | tatAvg = (float)tatSum / n; 36 | } 37 | 38 | void display(int b[], int wt[], int tat[]) { 39 | printf("Process\tBurstTime WaitingTime TurnAroundTime\n"); 40 | for (i = 0; i < n; ++i) { 41 | printf("%d\t%d\t\t%d\t%d\n", i, b[i], wt[i], tat[i]); 42 | } 43 | printf("average waiting time: %f", wtAvg); 44 | printf("\naverage turnaround time: %f", tatAvg); 45 | printf("\n"); 46 | } 47 | 48 | void calcTime(int b[]) { 49 | int wt[20], tat[20]; 50 | findWaitingtime(b, wt); 51 | findTurnAroundtime(tat, b, wt); 52 | display(b, wt, tat); 53 | } 54 | 55 | int main() { 56 | 57 | int b[20]; 58 | printf("Number of Processes:"); 59 | scanf("%d", &n); 60 | read(b); 61 | calcTime(b); 62 | 63 | return 0; 64 | } 65 | 66 | /* OUTPUT 67 | 68 | Number of Processes:3 69 | Enter the burst time of process 0: 23 70 | Enter the burst time of process 1: 33 71 | Enter the burst time of process 2: 11 72 | Process BurstTime WaitingTime TurnAroundTime 73 | 0 23 0 23 74 | 1 33 23 56 75 | 2 11 56 67 76 | average waiting time: 26.333334 77 | average turnaround time: 48.666668 78 | 79 | */ -------------------------------------------------------------------------------- /CPU Scheduling/FCFS/README.md: -------------------------------------------------------------------------------- 1 | ## Program for FCFS Scheduling 2 | 3 | [![solution](https://img.shields.io/badge/View-Solution-blue.svg?logo=appveyor&longCache=true&style=for-the-badge)](https://github.com/KTU-CSE/System-Software-lab/blob/master/CPU%20Scheduling/FCFS/1.fcfs.c) 4 | [![raw](https://img.shields.io/badge/-raw-green.svg?logo=appveyor&longCache=true&style=for-the-badge )](https://github.com/KTU-CSE/System-Software-lab/raw/master/CPU%20Scheduling/FCFS/1.fcfs.c) 5 | [![output](https://img.shields.io/badge/-output-ff69b4.svg?logo=appveyor&longCache=true&style=for-the-badge)](https://github.com/KTU-CSE/System-Software-lab/blob/master/CPU%20Scheduling/FCFS/README.md#output) 6 | 7 | Given n processes with their burst times, the task is to find average waiting time and average turn around time using FCFS scheduling algorithm. 8 | 9 | First in, first out (FIFO), also known as first come, first served (FCFS), is the simplest scheduling algorithm. FIFO simply queues processes in the order that they arrive in the ready queue. 10 | In this, the process that comes first will be executed first and next process starts only after the previous gets fully executed. 11 | 12 | Here we are considering that arrival time for all processes is 0. 13 | 14 | - Completion Time: Time at which process completes its execution. 15 | 16 | - Turn Around Time: Time Difference between completion time and arrival time. Turn Around Time = Completion Time – Arrival Time 17 | 18 | - Waiting Time(W.T): Time Difference between turn around time and burst time. 19 | 20 | - Waiting Time = Turn Around Time – Burst Time 21 | 22 | Implementation: 23 | 24 | 1- Input the processes along with their burst time (bt). 25 | 26 | 2- Find waiting time (wt) for all processes. 27 | 28 | 3- As first process that comes need not to wait so 29 | waiting time for process 1 will be 0 i.e. wt[0] = 0. 30 | 31 | 4- Find waiting time for all other processes i.e. for 32 | process i -> 33 | wt[i] = bt[i-1] + wt[i-1] . 34 | 35 | 5- Find turnaround time = waiting_time + burst_time 36 | for all processes. 37 | 38 | 6- Find average waiting time = 39 | total_waiting_time / no_of_processes. 40 | 41 | 7- Similarly, find average turnaround time = 42 | total_turn_around_time / no_of_processes. 43 | 44 | ## Output 45 | 46 | ![output_image](/.github/out_img/p_01_out.jpg) 47 | 48 | [Reference](http://web.cse.ohio-state.edu/~agrawal/660/Slides/jan18.pdf) 49 | -------------------------------------------------------------------------------- /CPU Scheduling/Priority/3.priority.c: -------------------------------------------------------------------------------- 1 | /* 2 | Author : abhijithvijayan 3 | Created on : 29 Sep,18 11:03 4 | Title : Priority Scheduling 5 | */ 6 | 7 | #include 8 | 9 | int i, j, n; 10 | float tatAvg, wtAvg; 11 | 12 | struct Process { 13 | int pId; 14 | int bt; 15 | int priority; 16 | }; 17 | 18 | void read(struct Process p[]) { 19 | printf("\n"); 20 | for (i = 0; i < n; ++i) { 21 | p[i].pId = i + 1; 22 | printf("Enter the burst time for process %d: ", p[i].pId); 23 | scanf("%d", &p[i].bt); 24 | printf("Enter the priority: "); 25 | scanf("%d", &p[i].priority); 26 | } 27 | } 28 | 29 | void display(struct Process p[], int wt[], int tat[]) { 30 | printf("\nProcessID | BurstTime | Priority | Waiting time | Turn Around Time\n"); 31 | for (i = 0; i < n; ++i) { 32 | printf("%d\t\t%d\t%d\t\t%d\t\t%d\n", p[i].pId, p[i].bt, p[i].priority, wt[i], tat[i]); 33 | } 34 | printf("average waiting time: %f", wtAvg); 35 | printf("\naverage turnaround time: %f", tatAvg); 36 | printf("\n"); 37 | } 38 | 39 | void sort(struct Process p[]) { 40 | struct Process temp; 41 | for (i = 0; i < n - 1; ++i) { 42 | for (j = 0; j < n - 1 - i; ++j) { 43 | if (p[j].priority > p[j + 1].priority) { 44 | temp = p[j]; 45 | p[j] = p[j + 1]; 46 | p[j + 1] = temp; 47 | } 48 | } 49 | } 50 | } 51 | 52 | void findWaitingTime(struct Process p[], int wt[]) { 53 | wt[0] = 0; 54 | int wtSum = 0; 55 | for (i = 1; i < n; ++i) { 56 | wt[i] = wt[i - 1] + p[i - 1].bt; 57 | wtSum += wt[i]; 58 | } 59 | wtAvg = (float)wtSum / n; 60 | } 61 | 62 | void findTurnAroundtime(struct Process p[], int tat[], int wt[]) { 63 | int tatSum = 0; 64 | for (i = 0; i < n; ++i) { 65 | tat[i] = p[i].bt + wt[i]; 66 | tatSum += tat[i]; 67 | } 68 | tatAvg = (float)tatSum / n; 69 | } 70 | 71 | int main() 72 | { 73 | struct Process p[20]; /* Array object for the structure */ 74 | int wt[20], tat[20]; 75 | printf("Number of Processes:"); 76 | scanf("%d", &n); 77 | read(p); 78 | sort(p); 79 | findWaitingTime(p, wt); 80 | findTurnAroundtime(p, tat, wt); 81 | display(p, wt, tat); 82 | 83 | return 0; 84 | } 85 | 86 | /* OUTPUT 87 | 88 | Number of Processes:4 89 | 90 | Enter the burst time for process 1: 22 91 | Enter the priority: 4 92 | Enter the burst time for process 2: 3 93 | Enter the priority: 0 94 | Enter the burst time for process 3: 22 95 | Enter the priority: 6 96 | Enter the burst time for process 4: 7 97 | Enter the priority: 2 98 | 99 | ProcessID | BurstTime | Priority | Waiting time | Turn Around Time 100 | 2 3 0 0 3 101 | 4 7 2 3 10 102 | 1 22 4 10 32 103 | 3 22 6 32 54 104 | average waiting time: 11.250000 105 | average turnaround time: 24.750000 106 | 107 | */ -------------------------------------------------------------------------------- /CPU Scheduling/Priority/README.md: -------------------------------------------------------------------------------- 1 | ## Program for Priority Scheduling 2 | 3 | [![solution](https://img.shields.io/badge/View-Solution-blue.svg?logo=appveyor&longCache=true&style=for-the-badge)](https://github.com/KTU-CSE/System-Software-lab/blob/master/CPU%20Scheduling/Priority/3.priority.c) 4 | [![raw](https://img.shields.io/badge/-raw-green.svg?logo=appveyor&longCache=true&style=for-the-badge )](https://github.com/KTU-CSE/System-Software-lab/raw/master/CPU%20Scheduling/Priority/3.priority.c) 5 | [![output](https://img.shields.io/badge/-output-ff69b4.svg?logo=appveyor&longCache=true&style=for-the-badge)](https://github.com/KTU-CSE/System-Software-lab/blob/master/CPU%20Scheduling/Priority/README.md#output) 6 | 7 | Priority scheduling is a non-preemptive algorithm and one of the most common scheduling algorithms in batch systems. 8 | - Each process is assigned a priority. Process with the highest priority is to be executed first and so on. 9 | - Processes with the same priority are executed on first come first served basis. 10 | - Priority can be decided based on memory requirements, time requirements or any other resource requirement. 11 | 12 | Implementation : 13 | 14 | 1- First input the processes with their burst time 15 | and priority. 16 | 17 | 2- Sort the processes, burst time and priority 18 | according to the priority. 19 | 20 | 3- Now simply apply FCFS algorithm. 21 | 22 | ## Output 23 | 24 | ![output_img](/.github/out_img/p_03_out.jpg) 25 | -------------------------------------------------------------------------------- /CPU Scheduling/README.md: -------------------------------------------------------------------------------- 1 | ### CPU Scheduling Algorithms 2 | 3 | - [FCFS Scheduling](https://github.com/KTU-CSE/System-Software-lab/tree/master/CPU%20Scheduling/FCFS/README.md) 4 | - [SJF Scheduling](https://github.com/KTU-CSE/System-Software-lab/tree/master/CPU%20Scheduling/SJF/README.md) 5 | - [Priority Scheduling](https://github.com/KTU-CSE/System-Software-lab/tree/master/CPU%20Scheduling/Priority/README.md) 6 | - [Round Robin scheduling](https://github.com/KTU-CSE/System-Software-lab/tree/master/CPU%20Scheduling/RoundRobin/README.md) -------------------------------------------------------------------------------- /CPU Scheduling/RoundRobin/4.roundrobin.c: -------------------------------------------------------------------------------- 1 | /* 2 | Author : abhijithvijayan 3 | Created on : 24 Sep,18 10:07 4 | Title : Round Robin Scheduling 5 | */ 6 | 7 | #include 8 | 9 | int i, n, quantum; /* give a time quantum */ 10 | 11 | void read(int b[]) { 12 | for (i = 0; i < n; ++i) { 13 | printf("Enter the burst time of process %d: ", i); 14 | scanf("%d", &b[i]); 15 | } 16 | } 17 | 18 | void findWaitingtime(int b[], int wt[]) { 19 | int b_rem[20]; 20 | for (i = 0; i < n; ++i) { 21 | b_rem[i] = b[i]; /* Create a copy of the burst time array */ 22 | } 23 | int time = 0; /* initialize time as 0 */ 24 | 25 | while (1) 26 | { 27 | /* Traverse */ 28 | 29 | int flag = 0; 30 | 31 | for (i = 0; i < n; ++i) { 32 | 33 | if (b_rem[i] > 0) { /* continue only if burst time is greater than 0 */ 34 | flag = 1; /* there is a pending process */ 35 | if (b_rem[i] > quantum) { 36 | time += quantum; /* shows how much time a process has been processed */ 37 | b_rem[i] -= quantum; /* Decrease the burst_time of current process by quantum */ 38 | } 39 | else { /* If burst time is smaller than or equal to quantum. Last cycle for this process */ 40 | time += b_rem[i]; /* shows how much time a process has been processed */ 41 | wt[i] = time - b[i]; /* Waiting time is current time minus time used by this process */ 42 | b_rem[i] = 0; /* fully executed, so remaining burst time=0 */ 43 | } 44 | } 45 | } 46 | if (flag == 0) { /* there is no pending process */ 47 | break; 48 | } 49 | } 50 | } 51 | 52 | void findTurnAroundtime(int tat[], int b[], int wt[]) { 53 | for (i = 0; i < n; ++i) { 54 | tat[i] = b[i] + wt[i]; 55 | } 56 | } 57 | 58 | void display(int b[], int wt[], int tat[]) { 59 | int wtSum = 0, tatSum = 0; 60 | printf("Process\tBurstTime WaitingTime TurnAroundTime\n"); 61 | for (i = 0; i < n; ++i) { 62 | wtSum += wt[i]; 63 | tatSum += tat[i]; 64 | printf("%d\t%d\t\t%d\t%d\n", i, b[i], wt[i], tat[i]); 65 | } 66 | printf("average waiting time: %f", (float)wtSum / n); 67 | printf("\naverage turnaround time: %f", (float)tatSum / n); 68 | printf("\n"); 69 | } 70 | 71 | void calcTime(int b[], int wt[], int tat[]) { 72 | findWaitingtime(b, wt); 73 | findTurnAroundtime(tat, b, wt); 74 | display(b, wt, tat); 75 | } 76 | 77 | void main() 78 | { 79 | int b[20], tat[20], wt[20]; 80 | printf("Number of Processes:"); 81 | scanf("%d", &n); 82 | read(b); 83 | printf("Enter time quantum:"); 84 | scanf("%d", &quantum); 85 | calcTime(b, wt, tat); 86 | } 87 | 88 | /* OUTPUT 89 | 90 | Number of Processes:3 91 | Enter the burst time of process 0: 54 92 | Enter the burst time of process 1: 22 93 | Enter the burst time of process 2: 6 94 | Enter time quantum:5 95 | Process BurstTime WaitingTime TurnAroundTime 96 | 0 54 28 82 97 | 1 22 31 53 98 | 2 6 20 26 99 | average waiting time: 26.333334 100 | average turnaround time: 53.666668 101 | 102 | */ -------------------------------------------------------------------------------- /CPU Scheduling/RoundRobin/README.md: -------------------------------------------------------------------------------- 1 | ## Program for Round Robin scheduling 2 | 3 | [![solution](https://img.shields.io/badge/View-Solution-blue.svg?logo=appveyor&longCache=true&style=for-the-badge)](https://github.com/KTU-CSE/System-Software-lab/blob/master/CPU%20Scheduling/RoundRobin/4.roundrobin.c) 4 | [![raw](https://img.shields.io/badge/-raw-green.svg?logo=appveyor&longCache=true&style=for-the-badge )](https://github.com/KTU-CSE/System-Software-lab/raw/master/CPU%20Scheduling/RoundRobin/4.roundrobin.c) 5 | [![output](https://img.shields.io/badge/-output-ff69b4.svg?logo=appveyor&longCache=true&style=for-the-badge)](https://github.com/KTU-CSE/System-Software-lab/blob/master/CPU%20Scheduling/RoundRobin/README.md#output) 6 | 7 | Round Robin is a CPU scheduling algorithm where each process is assigned a fixed time slot in a cyclic way. 8 | 9 | - It is simple, easy to implement, and starvation-free as all processes get fair share of CPU. 10 | - One of the most commonly used technique in CPU scheduling as a core. 11 | - It is preemptive as processes are assigned CPU only for a fixed slice of time at most. 12 | - The disadvantage of it is more overhead of context switching. 13 | 14 | Here we are considering that arrival time for all processes is 0. 15 | 16 | ### Terms 17 | 18 | - Completion Time: Time at which process completes its execution. 19 | - Turn Around Time: Time Difference between completion time and arrival time. 20 | - Turn Around Time = Completion Time – Arrival Time 21 | - Waiting Time(W.T): Time Difference between turn around time and burst time. 22 | - Waiting Time = Turn Around Time – Burst Time 23 | 24 | The tricky part is to compute waiting times. Once waiting times are computed, turn around times can be quickly computed. 25 | 26 | ### Steps to find waiting times of all processes: 27 | 28 | 1- Create an array rem_bt[] to keep track of remaining 29 | burst time of processes. This array is initially a 30 | copy of bt[] (burst times array) 31 | 32 | 2- Create another array wt[] to store waiting times 33 | of processes. Initialize this array as 0. 34 | 35 | 3- Initialize time : t = 0 36 | 37 | 4- Keep traversing the all processes while all processes 38 | are not done. Do following for i'th process if it is 39 | not done yet. 40 | 41 | a- If rem_bt[i] > quantum 42 | (i) t = t + quantum 43 | (ii) bt_rem[i] -= quantum; 44 | c- Else // Last cycle for this process 45 | (i) t = t + bt_rem[i]; 46 | (ii) wt[i] = t - bt[i] 47 | (ii) bt_rem[i] = 0; // This process is over 48 | 49 | Once we have waiting times, we can compute turn around time tat[i] of a process as sum of waiting and burst times, i.e., wt[i] + bt[i] 50 | 51 | ## Output 52 | 53 | ![output_image](/.github/out_img/p_04_out.jpg) -------------------------------------------------------------------------------- /CPU Scheduling/SJF/2.sjf.c: -------------------------------------------------------------------------------- 1 | /* 2 | Author : abhijithvijayan 3 | Created on : 24 Sep,18 07:24 4 | Title : SJF Scheduling 5 | */ 6 | 7 | #include 8 | 9 | int i, j, n, pId[20]; 10 | float tatAvg, wtAvg; 11 | 12 | void swap(int *a, int *b) { 13 | int temp = *a; 14 | *a = *b; 15 | *b = temp; 16 | } 17 | 18 | void sort(int b[]) { 19 | 20 | for (i = 0; i < n - 1; ++i) { 21 | for (j = 0; j < n - 1 - i; ++j) { 22 | if (b[j] > b[j + 1]) { 23 | swap(&b[j], &b[j + 1]); 24 | swap(&pId[j], &pId[j + 1]); 25 | } 26 | } 27 | } 28 | } 29 | 30 | void read(int b[]) { 31 | for (i = 0; i < n; ++i) { 32 | printf("Enter the burst time of process %d: ", i); 33 | scanf("%d", &b[i]); 34 | pId[i] = i + 1; /* process 1, process 2,... */ 35 | } 36 | sort(b); 37 | } 38 | 39 | void findWaitingtime(int b[], int wt[]) { 40 | wt[0] = 0; 41 | int wtSum = 0; 42 | for (i = 1; i < n; ++i) { 43 | wt[i] = wt[i - 1] + b[i - 1]; 44 | wtSum += wt[i]; 45 | } 46 | wtAvg = (float)wtSum / n; 47 | } 48 | 49 | void findTurnAroundtime(int tat[], int b[], int wt[]) { 50 | int tatSum = 0; 51 | for (i = 0; i < n; ++i) { 52 | tat[i] = b[i] + wt[i]; 53 | tatSum += tat[i]; 54 | } 55 | tatAvg = (float)tatSum / n; 56 | } 57 | 58 | void display(int b[], int wt[], int tat[]) { 59 | printf("Process\tBurstTime WaitingTime TurnAroundTime\n"); 60 | for (i = 0; i < n; ++i) { 61 | printf("%d\t%d\t\t%d\t%d\n", pId[i], b[i], wt[i], tat[i]); 62 | } 63 | printf("average waiting time: %f", wtAvg); 64 | printf("\naverage turnaround time: %f", tatAvg); 65 | printf("\n"); 66 | } 67 | 68 | void calcTime(int b[]) { 69 | int wt[20], tat[20]; 70 | findWaitingtime(b, wt); 71 | findTurnAroundtime(tat, b, wt); 72 | display(b, wt, tat); 73 | } 74 | 75 | void main() { 76 | int b[20]; 77 | printf("Number of Processes:"); 78 | scanf("%d", &n); 79 | read(b); 80 | calcTime(b); 81 | } 82 | 83 | /* OUTPUT 84 | 85 | Number of Processes:4 86 | Enter the burst time of process 0: 6 87 | Enter the burst time of process 1: 8 88 | Enter the burst time of process 2: 7 89 | Enter the burst time of process 3: 3 90 | Process BurstTime WaitingTime TurnAroundTime 91 | 4 3 0 3 92 | 1 6 3 9 93 | 3 7 9 16 94 | 2 8 16 24 95 | average waiting time: 7.000000 96 | average turnaround time: 13.000000 97 | 98 | */ -------------------------------------------------------------------------------- /CPU Scheduling/SJF/README.md: -------------------------------------------------------------------------------- 1 | ## Program for Shortest Job First (or SJF) scheduling 2 | 3 | [![solution](https://img.shields.io/badge/View-Solution-blue.svg?logo=appveyor&longCache=true&style=for-the-badge)](https://github.com/KTU-CSE/System-Software-lab/blob/master/CPU%20Scheduling/SJF/2.sjf.c) 4 | [![raw](https://img.shields.io/badge/-raw-green.svg?logo=appveyor&longCache=true&style=for-the-badge )](https://github.com/KTU-CSE/System-Software-lab/raw/master/CPU%20Scheduling/SJF/2.sjf.c) 5 | [![output](https://img.shields.io/badge/-output-ff69b4.svg?logo=appveyor&longCache=true&style=for-the-badge)](https://github.com/KTU-CSE/System-Software-lab/blob/master/CPU%20Scheduling/SJF/README.md#output) 6 | 7 | Shortest job first (SJF) or shortest job next, is a scheduling policy that selects the waiting process with the smallest execution time to execute next. SJN is a non-preemptive algorithm. 8 | 9 | - Shortest Job first has the advantage of having minimum average waiting time among all scheduling algorithms. 10 | - It is a Greedy Algorithm. 11 | - It may cause starvation if shorter processes keep coming. This problem can be solved using the concept of aging. 12 | - It is practically infeasible as Operating System may not know burst time and therefore may not sort them. While it is not possible to predict execution time, several methods can be used to estimate the execution time for a job, such as a weighted average of previous execution times. SJF can be used in specialized environments where accurate estimates of running time are available. 13 | 14 | Algorithm: 15 | 16 | 1- Sort all the processes(process ID too) in increasing order according to burst time. 17 | 18 | 2- Then simply, apply FCFS. 19 | 20 | Terms 21 | 22 | 1. Completion Time: Time at which process completes its execution. 23 | 24 | 2. Turn Around Time: Time Difference between completion time and arrival time. 25 | - Turn Around Time = Completion Time – Arrival Time 26 | 27 | 3. Waiting Time(W.T): Time Difference between turn around time and burst time. 28 | - Waiting Time = Turn Around Time – Burst Time 29 | 30 | ## Output 31 | 32 | ![output_img](/.github/out_img/p_02_out.jpg) 33 | -------------------------------------------------------------------------------- /Deadlocks/BankersAlgorithm/7.bankers.c: -------------------------------------------------------------------------------- 1 | /* 2 | Author : abhijithvijayan 3 | Created on : 27 Oct 18, 15:50 4 | Title : Banker’s Algorithm 5 | */ 6 | 7 | #include 8 | 9 | int n, m; 10 | void read(int alloc[][10], int max[][10], int avail[10], int need[][10], int n, int m); 11 | void display(int temp[][10], int n, int m); 12 | int safety(int alloc[][10], int avail[10], int need[][10], int n, int m); 13 | 14 | int main() 15 | { 16 | int alloc[10][10], max[10][10], avail[10], need[10][10]; 17 | 18 | printf("DEADLOCK AVOIDANCE USING BANKER'S ALGORITHM\n"); 19 | read(alloc, max, avail, need, n, m); 20 | return 0; 21 | } 22 | 23 | //Banker's Algorithm 24 | void read(int alloc[][10], int max[][10], int avail[10], int need[][10], int n, int m) { 25 | 26 | int i, j; 27 | printf("Enter total number of processes: "); 28 | scanf("%d", &n); 29 | printf("Enter total number of resources: "); 30 | scanf("%d", &m); 31 | 32 | for (i = 0; i < n; i++) { 33 | printf("\nProcess %d\n", i + 1); 34 | printf("Allocation Resource: "); 35 | for (j = 0; j < m; ++j) { 36 | scanf("%d", &alloc[i][j]); 37 | } 38 | printf("Maximum Resource: "); 39 | for (j = 0; j < m; ++j) { 40 | scanf("%d", &max[i][j]); 41 | } 42 | } 43 | 44 | printf("\nAvailable System Resources: "); 45 | for (i = 0; i < m; ++i) { 46 | scanf("%d", &avail[i]); 47 | } 48 | // Calculating Need matrix 49 | for (i = 0; i < n; ++i) { 50 | for (j = 0; j < m; ++j) { 51 | need[i][j] = max[i][j] - alloc[i][j]; /* need = max - allocated */ 52 | } 53 | } 54 | 55 | printf("\nProcess\tCurrently-Allocated-Resources\n"); 56 | display(alloc, n, m); 57 | printf("\nProcess\tMaximum-Resources\n"); 58 | display(max, n, m); 59 | printf("\nProcess\tPossibly-Needed-Resources\n"); 60 | display(need, n, m); 61 | 62 | safety(alloc, avail, need, n, m); 63 | } 64 | 65 | //Safety algorithm 66 | 67 | /* available >= need, then available += allocated */ 68 | 69 | int safety(int alloc[][10], int avail[10], int need[][10], int n, int m) { 70 | 71 | int i, j, k, index = 0; 72 | int isDone[10], sequence[10], count = 0; /* array to store safety sequence */ 73 | 74 | for (i = 0; i < n; ++i) { 75 | isDone[i] = 0; /* array-> |0|0|0|0|0| */ 76 | } 77 | 78 | for (k = 0; k < n; ++k) { /* Iterating for possible deadlock to prevent infinite loop */ 79 | 80 | // main process starts 81 | for (i = 0; i < n; ++i) 82 | { 83 | if (isDone[i] == 0) { /* pending */ 84 | for (j = 0; j < m; ++j) 85 | { 86 | if (avail[j] >= need[i][j]) { 87 | continue; 88 | } 89 | else { 90 | break; 91 | } 92 | } 93 | if (j == m) { /* True: avail >= need */ 94 | for (j = 0; j < m; ++j) { 95 | avail[j] += alloc[i][j]; 96 | } 97 | count++; /* done process's count */ 98 | isDone[i] = 1; 99 | sequence[count - 1] = i + 1; /* safe state sequence */ 100 | } 101 | } 102 | } 103 | 104 | if (count == n) { /* safe state */ 105 | printf("\nIT'S A SAFE STATE\n"); 106 | printf("The safe sequence is\n"); 107 | int i; 108 | for (i = 0; i < n; ++i) { 109 | printf("P%d ", sequence[i]); 110 | } 111 | printf("\n"); 112 | break; 113 | } 114 | // main process ends 115 | 116 | } /* deadlock check loop ends */ 117 | 118 | if (count != n) { 119 | printf("\nDeadlock has occured.\n"); /* deadlock */ 120 | } 121 | } 122 | 123 | void display(int temp[][10], int n, int m) 124 | { 125 | int i, j; 126 | for (i = 0; i < n; ++i) { 127 | printf("P%d", i + 1); 128 | for (j = 0; j < m; ++j) { 129 | printf("\t%d", temp[i][j]); 130 | } 131 | printf("\n"); 132 | } 133 | printf("\n"); 134 | } 135 | 136 | /* OUTPUT 137 | 138 | DEADLOCK AVOIDANCE USING BANKER'S ALGORITHM 139 | Enter total number of processes: 3 140 | Enter total number of resources: 4 141 | 142 | Process 1 143 | Allocation Resource: 1 2 2 1 144 | Maximum Resource: 3 3 2 2 145 | 146 | Process 2 147 | Allocation Resource: 1 0 3 3 148 | Maximum Resource: 1 2 3 4 149 | 150 | Process 3 151 | Allocation Resource: 1 2 1 0 152 | Maximum Resource: 1 3 5 0 153 | 154 | Available System Resources: 3 1 1 2 155 | 156 | Process Currently-Allocated-Resources 157 | P1 1 2 2 1 158 | P2 1 0 3 3 159 | P3 1 2 1 0 160 | 161 | Process Maximum-Resources 162 | P1 3 3 2 2 163 | P2 1 2 3 4 164 | P3 1 3 5 0 165 | 166 | Process Possibly-Needed-Resources 167 | P1 2 1 0 1 168 | P2 0 2 0 1 169 | P3 0 1 4 0 170 | 171 | IT'S A SAFE STATE 172 | The safe sequence is 173 | P1 P2 P3 174 | 175 | */ -------------------------------------------------------------------------------- /Deadlocks/BankersAlgorithm/README.md: -------------------------------------------------------------------------------- 1 | ## Program for Banker’s Algorithm 2 | 3 | [![solution](https://img.shields.io/badge/View-Solution-blue.svg?logo=appveyor&longCache=true&style=for-the-badge)](https://github.com/KTU-CSE/System-Software-lab/blob/master/Deadlocks/BankersAlgorithm/7.bankers.c) 4 | [![raw](https://img.shields.io/badge/-raw-green.svg?logo=appveyor&longCache=true&style=for-the-badge )](https://github.com/KTU-CSE/System-Software-lab/raw/master/Deadlocks/BankersAlgorithm/7.bankers.c) 5 | [![output](https://img.shields.io/badge/-output-ff69b4.svg?logo=appveyor&longCache=true&style=for-the-badge)](https://github.com/KTU-CSE/System-Software-lab/blob/master/Deadlocks/BankersAlgorithm/README.md#output) 6 | 7 | The Banker algorithm, sometimes referred to as the detection algorithm, is a resource allocation and deadlock avoidance algorithm that tests for safety by simulating the allocation of predetermined maximum possible amounts of all resources, and then makes an "s-state" check to test for possible deadlock conditions for all other pending activities, before deciding whether allocation should be allowed to continue. 8 | 9 | Resources 10 | 11 | For the Banker's algorithm to work, it needs to know three things: 12 | 13 | - How much of each resource the system currently has available[AVAILABLE] 14 | - How much of each resource each process is currently holding[ALLOCATED] 15 | - How much of each resource each process could possibly request[MAX] 16 | 17 | Let ‘n’ be the number of processes in the system and ‘m’ be the number of resources types. 18 | 19 | Available : 20 | 21 | - It is a 1-d array of size ‘m’ indicating the number of available resources of each type. 22 | - Available[ j ] = k means there are ‘k’ instances of resource type Rj 23 | 24 | Max : 25 | 26 | - It is a 2-d array of size ‘n*m’ that defines the maximum demand of each process in a system. 27 | - Max[ i, j ] = k means process Pi may request at most ‘k’ instances of resource type Rj. 28 | 29 | Allocation : 30 | 31 | - It is a 2-d array of size ‘n*m’ that defines the number of resources of each type currently allocated to each process. 32 | - Allocation[ i, j ] = k means process Pi is currently allocated ‘k’ instances of resource type Rj 33 | 34 | Need : 35 | 36 | - It is a 2-d array of size ‘n*m’ that indicates the remaining resource need of each process. 37 | - Need [ i, j ] = k means process Pi currently allocated ‘k’ instances of resource type Rj 38 | - Need [ i, j ] = Max [ i, j ] – Allocation [ i, j ] 39 | 40 | Allocationi specifies the resources currently allocated to process Pi and Needi specifies the additional resources that process Pi may still request to complete its task. 41 | 42 | Banker’s algorithm consist of Safety algorithm and Resource request algorithm 43 | 44 | Safety Algorithm 45 | 46 | The algorithm for finding out whether or not a system is in a safe state can be described as follows: 47 | 48 | 1. Let Work and Finish be vectors of length ‘m’ and ‘n’ respectively. 49 | 50 | - Initialize: Work= Available 51 | - Finish [i]=false; for i=1,2,……,n 52 | 53 | 2. Find an i such that both 54 | - a) Finish [i]=false 55 | - b) Need_i<=work 56 | 57 | - if no such i exists goto step (4) 58 | 59 | 3. Work=Work + Allocation_i 60 | - Finish[i]= true 61 | - goto step(2) 62 | 63 | 4. If Finish[i]=true for all i, 64 | - then the system is in safe state. 65 | 66 | Safe sequence is the sequence in which the processes can be safely executed. 67 | 68 | ## Output 69 | 70 | ![output_img](/.github/out_img/p_07_out.png) 71 | 72 | References: 73 | 74 | - [Wikipedia](https://en.wikipedia.org/wiki/Banker%27s_algorithm) 75 | - [YouTube](https://www.youtube.com/watch?v=2V2FfP_olaA) 76 | - [stackoverflow](https://stackoverflow.com/questions/15501861/bankers-algorithm-for-deadlock-avoidance-in-c) 77 | - [GeeksForGeeks](https://www.geeksforgeeks.org/program-bankers-algorithm-set-1-safety-algorithm/) 78 | - [Gist](https://gist.github.com/pallabpain/68909d4ac6ff77c9aa40) 79 | -------------------------------------------------------------------------------- /Deadlocks/README.md: -------------------------------------------------------------------------------- 1 | - [Banker's Algorithm](https://github.com/KTU-CSE/System-Software-lab/tree/master/Deadlocks/BankersAlgorithm/README.md) -------------------------------------------------------------------------------- /Disk Management/CSCAN/10.csan.c: -------------------------------------------------------------------------------- 1 | /* 2 | Author : abhijithvijayan 3 | Created on : 05 Nov 18, 08:24 4 | Title : C-SCAN Disk Scheduling 5 | */ 6 | 7 | #include 8 | #include 9 | 10 | void cscan(int Ar[20], int n, int start); 11 | void sort(int Ar[20], int n); 12 | 13 | int main() { 14 | int diskQueue[20], n, start, i, max; 15 | printf("Enter the size of Queue: "); 16 | scanf("%d", &n); 17 | printf("Enter the Queue: "); 18 | for(i=1;i<=n;i++) { /* head element to be read */ 19 | scanf("%d",&diskQueue[i]); 20 | } 21 | printf("Enter the initial head position: "); 22 | scanf("%d", &start); 23 | diskQueue[0] = start; /* injecting to the first position */ 24 | ++n; 25 | sort(diskQueue, n); /* total of n+1 elements */ 26 | cscan(diskQueue, n, start); 27 | 28 | return 0; 29 | } 30 | 31 | void cscan(int Ar[20], int n, int start) { 32 | int i, pos, diff, seekTime=0, current; 33 | // position of the disk to start seeking 34 | for(i=0;iAr[j+1]) { 68 | tmp = Ar[j]; 69 | Ar[j] = Ar[j+1]; 70 | Ar[j+1] = tmp; 71 | } 72 | } 73 | } 74 | } 75 | 76 | /* OUTPUT 77 | 78 | Enter the size of Queue: 5 79 | Enter the Queue: 100 50 10 20 75 80 | Enter the initial head position: 35 81 | 82 | Movement of Cylinders 83 | Move from 35 to 50 with seek time 15 84 | Move from 50 to 75 with seek time 25 85 | Move from 75 to 100 with seek time 25 86 | Move from 100 to 0 with seek time 0 87 | Move from 0 to 10 with seek time 10 88 | Move from 10 to 20 with seek time 10 89 | 90 | Total Seek Time: 85 91 | Average Seek Time = 17.000000 92 | 93 | */ -------------------------------------------------------------------------------- /Disk Management/CSCAN/README.md: -------------------------------------------------------------------------------- 1 | ## Program for C-SCAN Disk Scheduling 2 | 3 | [![solution](https://img.shields.io/badge/View-Solution-blue.svg?logo=appveyor&longCache=true&style=for-the-badge)](https://github.com/KTU-CSE/System-Software-lab/blob/master/Disk%20Management/CSCAN/10.csan.c) 4 | [![raw](https://img.shields.io/badge/-raw-green.svg?logo=appveyor&longCache=true&style=for-the-badge )](https://github.com/KTU-CSE/System-Software-lab/raw/master/Disk%20Management/CSCAN/10.csan.c) 5 | [![output](https://img.shields.io/badge/-output-ff69b4.svg?logo=appveyor&longCache=true&style=for-the-badge)](https://github.com/KTU-CSE/System-Software-lab/blob/master/Disk%20Management/CSCAN/README.md#output) 6 | 7 | Definition of C-SCAN: C-SCAN moves the head from one end of the Disk to the other end, servicing requests along the way. The head on reaching the other end, however immediately returns to the beginning of the Disk without servicing any requests on the return. 8 | 9 | - The huge jump from one end to the other is not considered as a head movement as cylinders are treated as a circular list. 10 | 11 | ### Example 12 | 13 | - Example list of pending disk requests (listed by track number): 100, 50, 10, 20, 75. 14 | 15 | - The starting track number for the examples will be 35. 16 | 17 | - The list will need to be sorted in ascending order: 10, 20, 50, 75, 100. 18 | 19 | - CSCAN algorithm is currently going from a lower track number to a higher track number 20 | - Seek 1: 50 − 35 = 15 21 | - Seek 2: 75 − 50 = 25 22 | - Seek 3: 100 − 75 = 25 23 | 24 | - At this point both have reached the highest (end) track request. C-SCAN will go back to track 0 and start going to higher track requests. 25 | - **Seek 4 (C-SCAN): 0 − 100 = 0 head movement as cylinders are treated as a circular list (C-SCAN always goes back to the first track) 26 | - Seek 5 (C-SCAN): 10 − 0 = 10 27 | - Seek 6 (C-SCAN): 20 − 10 = 10 28 | - Total (C-SCAN): 85 29 | - Average (C-SCAN): 85 ÷ 5 = 17 30 | 31 | Note: Even though six seeks were performed using the C-SCAN algorithm, only five I/Os were actually done. 32 | 33 | ## Output 34 | 35 | ![output_img](/.github/out_img/p_10_out.jpg) 36 | 37 | Reference 38 | 39 | - [Wikipedia](https://en.wikipedia.org/wiki/Elevator_algorithm) 40 | - [GeeksforGeeks](https://www.geeksforgeeks.org/operating-system-page-replacement-algorithm/) 41 | -------------------------------------------------------------------------------- /Disk Management/FCFS/8.fcfsDisk.c: -------------------------------------------------------------------------------- 1 | /* 2 | Author : abhijithvijayan 3 | Created on : 28 Oct 18, 22:14 4 | Title : FCFS Disk Scheduling 5 | */ 6 | 7 | #include 8 | #include 9 | 10 | int main() { 11 | int diskQueue[20], n, i, seekTime=0, diff; 12 | printf("Enter the size of Queue: "); 13 | scanf("%d", &n); 14 | printf("Enter the Queue: "); 15 | for(i=1;i<=n;i++) { /* head element to be read */ 16 | scanf("%d",&diskQueue[i]); 17 | } 18 | printf("Enter the initial head position: "); 19 | scanf("%d", &diskQueue[0]); /* head element */ 20 | printf("\nMovement of Cylinders\n"); 21 | for(i=0;i 8 | 9 | ### Disk Scheduling 10 | 11 | Disk scheduling is is done by operating systems to schedule I/O requests arriving for disk. It is also known as I/O scheduling. 12 | 13 | Disk scheduling is important because: 14 | 15 | - Multiple I/O requests may arrive by different processes and only one I/O request can be served at a time by disk controller. Thus other I/O requests need to wait in waiting queue and need to be scheduled. 16 | - Two or more request may be far from each other so can result in greater disk arm movement. 17 | - Hard drives are one of the slowest parts of computer system and thus need to be accessed in an efficient manner. 18 | 19 | There are many Disk Scheduling Algorithms but before discussing them let’s have a quick look at some of the important terms: 20 | 21 | - Seek Time: Seek time is the time taken to locate the disk arm to a specified track where the data is to be read or write. 22 | - So the disk scheduling algorithm that gives minimum average seek time is better. 23 | - Rotational Latency: Rotational Latency is the time taken by the desired sector of disk to rotate into a position so that it can access the read/write heads. 24 | - So the disk scheduling algorithm that gives minimum rotational latency is better. 25 | - Transfer Time: Transfer time is the time to transfer the data. It depends on the rotating speed of the disk and number of bytes to be transferred. 26 | - Disk Access Time = Seek Time + Rotational Latency + Transfer Time 27 | - Disk Response Time: Response Time is the average of time spent by a request waiting to perform its I/O operation. 28 | - Average Response time is the response time of the all requests. 29 | - Variance Response Time is measure of how individual request are serviced with respect to average response time. 30 | - So the disk scheduling algorithm that gives minimum variance response time is better. -------------------------------------------------------------------------------- /Disk Management/SCAN/9.scan.c: -------------------------------------------------------------------------------- 1 | /* 2 | Author : abhijithvijayan 3 | Created on : 04 Nov 18, 20:15 4 | Title : SCAN Disk Scheduling 5 | */ 6 | 7 | #include 8 | #include 9 | 10 | void scan(int Ar[20], int n, int start); 11 | void sort(int Ar[20], int n); 12 | 13 | int main() { 14 | int diskQueue[20], n, start, i; 15 | printf("Enter the size of Queue: "); 16 | scanf("%d", &n); 17 | printf("Enter the Queue: "); 18 | for(i=1;i<=n;i++) { /* head element to be read */ 19 | scanf("%d",&diskQueue[i]); 20 | } 21 | printf("Enter the initial head position: "); 22 | scanf("%d", &start); 23 | diskQueue[0] = start; /* injecting to the first position */ 24 | ++n; 25 | sort(diskQueue, n); /* total of n+1 elements */ 26 | scan(diskQueue, n, start); 27 | 28 | return 0; 29 | } 30 | 31 | void scan(int Ar[20], int n, int start) { 32 | int i, pos, diff, seekTime=0, current; 33 | // position of the disk to start seeking 34 | for(i=0;i=0;i--) { 50 | diff = abs(Ar[current] - Ar[i]); 51 | seekTime += diff; 52 | printf("Move from %d to %d with seek time %d\n", Ar[current], Ar[i], diff); 53 | current=i; 54 | } 55 | printf("\nTotal Seek Time: %d", seekTime); 56 | // average of entered elements(n-1) excluding head 57 | printf("\nAverage Seek Time = %f",(float) seekTime/(n-1)); 58 | printf("\n"); 59 | } 60 | 61 | void sort(int Ar[20], int n) { 62 | int i, j, tmp; 63 | for(i=0;iAr[j+1]) { 66 | tmp = Ar[j]; 67 | Ar[j] = Ar[j+1]; 68 | Ar[j+1] = tmp; 69 | } 70 | } 71 | } 72 | } 73 | 74 | /* OUTPUT 75 | 76 | Enter the size of Queue: 5 77 | Enter the Queue: 100 50 10 20 75 78 | Enter the initial head position: 35 79 | 80 | Movement of Cylinders 81 | Move from 35 to 50 with seek time 15 82 | Move from 50 to 75 with seek time 25 83 | Move from 75 to 100 with seek time 25 84 | Move from 100 to 20 with seek time 80 85 | Move from 20 to 10 with seek time 10 86 | 87 | Total Seek Time: 155 88 | Average Seek Time = 31.000000 89 | 90 | */ -------------------------------------------------------------------------------- /Disk Management/SCAN/README.md: -------------------------------------------------------------------------------- 1 | ## Program for SCAN Disk Scheduling 2 | 3 | [![solution](https://img.shields.io/badge/View-Solution-blue.svg?logo=appveyor&longCache=true&style=for-the-badge)](https://github.com/KTU-CSE/System-Software-lab/blob/master/Disk%20Management/SCAN/9.scan.c) 4 | [![raw](https://img.shields.io/badge/-raw-green.svg?logo=appveyor&longCache=true&style=for-the-badge )](https://github.com/KTU-CSE/System-Software-lab/raw/master/Disk%20Management/SCAN/9.scan.c) 5 | [![output](https://img.shields.io/badge/-output-ff69b4.svg?logo=appveyor&longCache=true&style=for-the-badge)](https://github.com/KTU-CSE/System-Software-lab/blob/master/Disk%20Management/SCAN/README.md#output) 6 | 7 | The SCAN algorithm is a disk scheduling algorithm that helps in determining the motion of a disk’s arm and head in executing the read and write requests. 8 | 9 | In SCAN algorithm the disk arm moves into a particular direction and services the requests coming in its path and after reaching the end of disk, it reverses its direction and again services the request arriving in its path. 10 | 11 | So, this algorithm works like an elevator and hence also known as elevator algorithm. As a result, the requests at the midrange are serviced more and those arriving behind the disk arm will have to wait. 12 | 13 | Advantages: 14 | 15 | - High throughput 16 | - Low variance of response time 17 | - Average response time 18 | 19 | Disadvantages: 20 | 21 | - Long waiting time for requests for locations just visited by disk arm 22 | 23 | ### Example 24 | 25 | - Example list of pending disk requests (listed by track number): 100, 50, 10, 20, 75. 26 | 27 | - The starting track number for the examples will be 35. 28 | 29 | - The list will need to be sorted in ascending order: 10, 20, 50, 75, 100. 30 | 31 | - For the sake of this example let us assume that the SCAN algorithm is currently going from a lower track number to a higher track number 32 | - Seek 1: 50 − 35 = 15 33 | - Seek 2: 75 − 50 = 25 34 | - Seek 3: 100 − 75 = 25 35 | 36 | - At this point both have reached the highest (end) track request. SCAN will just reverse direction and service the next closest disk request (in this example, 20). 37 | - Seek 4: 20 − 100 = 80 38 | - Seek 5): 10 − 20 = 10 39 | - Total: 155 40 | - Average: 155 ÷ 5 = 31 41 | 42 | ## Output 43 | 44 | ![output_img](/.github/out_img/p_09_out.png) 45 | 46 | Reference 47 | 48 | - [Wikipedia](https://en.wikipedia.org/wiki/Elevator_algorithm) -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 KTU Computer Science & Engineering 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Memory Management/README.md: -------------------------------------------------------------------------------- 1 | - [Single-Level Directory-File Organisation Technique](https://github.com/KTU-CSE/System-Software-lab/tree/master/Memory%20Management/SingleLevelDir/README.md) 2 | - [Two-Level Directory-File Organisation Technique](https://github.com/KTU-CSE/System-Software-lab/tree/master/Memory%20Management/TwoLevelDir/README.md) -------------------------------------------------------------------------------- /Memory Management/SingleLevelDir/5.singleLevelDir.c: -------------------------------------------------------------------------------- 1 | /* 2 | Author : abhijithvijayan 3 | Created on : 20 Oct,18 23:38 4 | Title : Single Level Directory Organisation 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | struct directory { 12 | char dirName[10]; 13 | char fileName[10][10]; /* fileName[10][i] -> i Array of 10 char */ 14 | int fileCount; 15 | } dir; 16 | 17 | void main() 18 | { 19 | int i, ch; 20 | char tmp[10]; 21 | dir.fileCount = 0; 22 | printf("Enter a directory name:"); 23 | scanf("%s", dir.dirName); 24 | 25 | while (1) 26 | { 27 | printf("\nOperations\n\n1.Create File\n2.Delete File\n3.Search in Directory\n4.View Files\n5.Exit\n"); 28 | printf("Enter your choice:"); 29 | scanf("%d", &ch); 30 | switch (ch) { 31 | 32 | case 1: 33 | printf("Enter the name of the file:"); 34 | scanf("%s", dir.fileName[dir.fileCount++]); 35 | break; 36 | 37 | case 2: 38 | printf("Enter the name of the file:"); 39 | scanf("%s", tmp); 40 | for (i = 0; i < dir.fileCount; ++i) { 41 | if (strcmp(tmp, dir.fileName[i]) == 0) { /* The strcmp() function takes two strings and return an integer. 0->identical */ 42 | printf("The File %s is deleted!\n", tmp); /* Copy the last element to the place of deleted one */ 43 | strcpy(dir.fileName[i], dir.fileName[dir.fileCount - 1]); /* The strcpy() function copies the string to the another character array. strcpy(destination, source) */ 44 | dir.fileCount--; 45 | break; 46 | } 47 | } 48 | if (i == dir.fileCount) { 49 | printf("404 | File Not Found\n"); 50 | } 51 | break; 52 | 53 | case 3: 54 | printf("Enter the name of the file to be searched for:"); 55 | scanf("%s", tmp); 56 | for (i = 0; i < dir.fileCount; ++i) { 57 | if (strcmp(tmp, dir.fileName[i]) == 0) { 58 | printf("File Found!!"); 59 | break; 60 | } 61 | } 62 | if (i == dir.fileCount) { 63 | printf("404 | File Not Found"); 64 | } 65 | break; 66 | 67 | case 4: 68 | if (dir.fileCount == 0) { 69 | printf("Empty Directory!!"); 70 | } 71 | else { 72 | printf("Files:\n"); 73 | for (i = 0; i < dir.fileCount; ++i) 74 | { 75 | printf("%s\n", dir.fileName[i]); 76 | } 77 | printf("Total %d files in 1 directory", dir.fileCount); 78 | } 79 | break; 80 | 81 | default: 82 | exit(0); 83 | } 84 | } 85 | } -------------------------------------------------------------------------------- /Memory Management/SingleLevelDir/README.md: -------------------------------------------------------------------------------- 1 | ## Program for Single Level Directory Organisation Technique 2 | 3 | [![solution](https://img.shields.io/badge/View-Solution-blue.svg?logo=appveyor&longCache=true&style=for-the-badge)](https://github.com/KTU-CSE/System-Software-lab/blob/master/Memory%20Management/SingleLevelDir/5.singleLevelDir.c) 4 | [![raw](https://img.shields.io/badge/-raw-green.svg?logo=appveyor&longCache=true&style=for-the-badge )](https://github.com/KTU-CSE/System-Software-lab/raw/master/Memory%20Management/SingleLevelDir/5.singleLevelDir.c) 5 | [![output](https://img.shields.io/badge/-output-ff69b4.svg?logo=appveyor&longCache=true&style=for-the-badge)](https://github.com/KTU-CSE/System-Software-lab/blob/master/Memory%20Management/SingleLevelDir/README.md#output) 6 | 7 | The directory structure is the organization of files into a hierarchy of folders. 8 | 9 | In a single-level directory system, all the files are placed in one directory. 10 | There is a root directory which has all files. 11 | It has a simple architecture and there are no sub directories. 12 | 13 | Advantage of single level directory system is that it is easy to find a file in the directory. 14 | 15 | ## Output 16 | 17 | ![output_img](/.github/out_img/p_05_out-1.jpg) 18 | ![output_img](/.github/out_img/p_05_out-2.jpg) 19 | -------------------------------------------------------------------------------- /Memory Management/TwoLevelDir/6.twoLevelDir.c: -------------------------------------------------------------------------------- 1 | /* 2 | Author : abhijithvijayan 3 | Created on : 21 Oct,18 18:57 4 | Title : Two Level Directory Organisation (check line#95) 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | int i,j, dirCount=0; /* initialize directory count here */ 12 | char dName[10],fName[10]; /* temporary variables */ 13 | 14 | struct directory { 15 | char dirName[10]; 16 | char fileName[10][10]; /* fileName[10][i] -> i Array of 10 char */ 17 | int fileCount; 18 | } dir[10]; /* structure of Array */ 19 | 20 | void createDir(struct directory dir[]); 21 | void createFile(struct directory dir[]); 22 | void deleteFile(struct directory dir[]); 23 | void listDir(struct directory dir[]); 24 | int listFiles(struct directory dir[], char dName[10]); 25 | void search(struct directory dir[]); 26 | void display(struct directory dir[]); 27 | 28 | void main() { 29 | int ch; 30 | while(1) { 31 | printf("1.Create Directory\n2.Create File\n3.Delete File\n4.Search in Directory\n5.View Files\n6.Exit\n"); 32 | printf("Enter your choice:"); 33 | scanf("%d",&ch); 34 | printf("\n"); 35 | switch(ch) { 36 | case 1: createDir(dir); 37 | break; 38 | case 2: createFile(dir); 39 | break; 40 | case 3: deleteFile(dir); 41 | break; 42 | case 4: search(dir); 43 | break; 44 | case 5: display(dir); 45 | break; 46 | default: exit(0); 47 | } 48 | } 49 | } 50 | 51 | void createDir(struct directory dir[]) { 52 | printf("Enter directory name:"); 53 | scanf("%s", dir[dirCount++].dirName); 54 | dir[dirCount].fileCount=0; /* initialize count of files in the directory created */ 55 | printf("Directory '%s' created\n\n", dir[dirCount-1].dirName); 56 | } 57 | 58 | void createFile(struct directory dir[]) { 59 | listDir(dir); 60 | printf("\nChoose the directory:"); 61 | scanf("%s",dName); 62 | for(i=0;i Found */ 64 | printf("Enter a file name:"); 65 | scanf("%s", dir[i].fileName[dir[i].fileCount++]); 66 | printf("File '%s' is created\n\n", dir[i].fileName[dir[i].fileCount-1]); 67 | break; 68 | } 69 | } 70 | if(i==dirCount) { 71 | printf("404 | Directory Not Found!!\n\n"); /* 404 */ 72 | } 73 | } 74 | 75 | void deleteFile(struct directory dir[]) { 76 | if(dirCount!=0) { 77 | listDir(dir); 78 | printf("\nEnter directory name to select:"); 79 | scanf("%s",dName); 80 | i=listFiles(dir, dName); 81 | if(dir[i].fileCount==0) { 82 | printf("\nNo Files Found!!"); 83 | } 84 | else { 85 | printf("\nChoose file to be deleted:"); 86 | scanf("%s",fName); 87 | for(j=0;j 7 | #include 8 | char input[10], label[10], ch1, ch2; 9 | int addr, w = 0, start, ptaddr, l, length = 0, end, count = 0, k, taddr, address, i = 0; 10 | FILE *fp1, *fp2; 11 | void check(); 12 | void main() 13 | { 14 | fp1 = fopen("input.txt", "r"); 15 | fp2 = fopen("output.txt", "w"); 16 | fscanf(fp1, "%s", input); 17 | printf("\n\nABSOLUTE LOADER\n"); 18 | fprintf(fp2, "\n-------------------------------------------------------\n"); 19 | fprintf(fp2, "MEMORY ADDRESS\t\t\tCONTENTS"); 20 | fprintf(fp2, "\n-------------------------------------------------------\n"); 21 | while (strcmp(input, "E") != 0) 22 | { 23 | if (strcmp(input, "H") == 0) 24 | { 25 | fscanf(fp1, "%s %x %x %s", label, &start, &end, input); 26 | address = start; 27 | } 28 | else if (strcmp(input, "T") == 0) 29 | { 30 | l = length; 31 | ptaddr = addr; 32 | fscanf(fp1, "%x %x %s", &taddr, &length, input); 33 | addr = taddr; 34 | if (w == 0) 35 | { 36 | ptaddr = address; 37 | w = 1; 38 | } 39 | for (k = 0; k < (taddr - (ptaddr + l)); k++) 40 | { 41 | address = address + 1; 42 | fprintf(fp2, "xx"); 43 | count++; 44 | if (count == 4) 45 | { 46 | fprintf(fp2, " "); 47 | i++; 48 | if (i == 4) 49 | { 50 | fprintf(fp2, "\n\n%x\t\t", address); 51 | i = 0; 52 | } 53 | count = 0; 54 | } 55 | } 56 | if (taddr == start) 57 | fprintf(fp2, "\n\n%x\t\t", taddr); 58 | fprintf(fp2, "%c%c", input[0], input[1]); 59 | check(); 60 | fprintf(fp2, "%c%c", input[2], input[3]); 61 | check(); 62 | fprintf(fp2, "%c%c", input[4], input[5]); 63 | check(); 64 | fscanf(fp1, "%s", input); 65 | } 66 | else 67 | { 68 | fprintf(fp2, "%c%c", input[0], input[1]); 69 | check(); 70 | fprintf(fp2, "%c%c", input[2], input[3]); 71 | check(); 72 | fprintf(fp2, "%c%c", input[4], input[5]); 73 | check(); 74 | fscanf(fp1, "%s", input); 75 | } 76 | } 77 | fprintf(fp2, "\n-------------------------------------------------------\n"); 78 | fclose(fp1); 79 | fclose(fp2); 80 | printf("\n\n The contents of output file:\n"); 81 | fp2 = fopen("output.txt", "r"); 82 | ch2 = fgetc(fp2); 83 | while (ch2 != EOF) 84 | { 85 | printf("%c", ch2); 86 | ch2 = fgetc(fp2); 87 | } 88 | fclose(fp2); 89 | } 90 | void check() 91 | { 92 | count++; 93 | address++; 94 | taddr = taddr + 1; 95 | if (count == 4) 96 | { 97 | fprintf(fp2, " "); 98 | i++; 99 | if (i == 4) 100 | { 101 | fprintf(fp2, "\n\n%x\t\t", taddr); 102 | i = 0; 103 | } 104 | count = 0; 105 | } 106 | } 107 | 108 | /* 109 | 110 | input.txt 111 | --------- 112 | 113 | H COPY 001000 00107A 114 | T 001000 1E 141033 482039 001036 281030 301015 482061 3C1003 00102A 0C1039 00102D 115 | T 00101E 15 0C1036 482061 081033 4C0000 454F46 000003 000000 116 | T 001047 1E 041030 001030 E0205D 30203F D8205D 281030 302057 549039 2C205E 38203F 117 | T 001077 1C 101036 4C0000 000000 001000 041030 E02079 302064 509039 DC2079 2C1036 118 | E 001000 119 | 120 | OUTPUT 121 | ------ 122 | 123 | ABSOLUTE LOADER 124 | 125 | The contents of output file: 126 | 127 | ------------------------------------------------------- 128 | MEMORY ADDRESS CONTENTS 129 | ------------------------------------------------------- 130 | 131 | 132 | 1000 14103348 20390010 36281030 30101548 133 | 134 | 1010 20613C10 0300102A 0C103900 102D0C10 135 | 136 | 1020 36482061 0810334C 0000454F 46000003 137 | 138 | 1030 000000xx xxxxxxxx xxxxxxxx xxxxxxxx 139 | 140 | 1040 xxxxxxxx xxxxxx04 10300010 30E0205D 141 | 142 | 1050 30203FD8 205D2810 30302057 5490392C 143 | 144 | 1060 205E3820 3Fxxxxxx xxxxxxxx xxxxxxxx 145 | 146 | 1070 xxxxxxxx xxxxxx10 10364C00 00000000 147 | 148 | 1080 00100004 1030E020 79302064 509039DC 149 | 150 | 1090 20792C10 36 151 | ------------------------------------------------------- 152 | 153 | */ -------------------------------------------------------------------------------- /Process Synchronization/Absolute Loader/README.md: -------------------------------------------------------------------------------- 1 | ## Program for Absolute Loader 2 | 3 | [![solution](https://img.shields.io/badge/View-Solution-blue.svg?logo=appveyor&longCache=true&style=for-the-badge)](https://github.com/KTU-CSE/System-Software-lab/blob/master/Process%20Synchronization/Absolute%20Loader/19.absolute.c) 4 | [![raw](https://img.shields.io/badge/-raw-green.svg?logo=appveyor&longCache=true&style=for-the-badge)](https://github.com/KTU-CSE/System-Software-lab/raw/master/Process%20Synchronization/Absolute%20Loader/19.absolute.c) 5 | [![output](https://img.shields.io/badge/-output-ff69b4.svg?logo=appveyor&longCache=true&style=for-the-badge)](https://github.com/KTU-CSE/System-Software-lab/blob/master/Process%20Synchronization/Absolute%20Loader/README.md#output) -------------------------------------------------------------------------------- /Process Synchronization/Absolute Loader/input.txt: -------------------------------------------------------------------------------- 1 | H COPY 001000 00107A 2 | T 001000 1E 141033 482039 001036 281030 301015 482061 3C1003 00102A 0C1039 00102D 3 | T 00101E 15 0C1036 482061 081033 4C0000 454F46 000003 000000 4 | T 001047 1E 041030 001030 E0205D 30203F D8205D 281030 302057 549039 2C205E 38203F 5 | T 001077 1C 101036 4C0000 000000 001000 041030 E02079 302064 509039 DC2079 2C1036 6 | E 001000 -------------------------------------------------------------------------------- /Process Synchronization/Absolute Loader/output.txt: -------------------------------------------------------------------------------- 1 | 2 | ------------------------------------------------------- 3 | MEMORY ADDRESS CONTENTS 4 | ------------------------------------------------------- 5 | 6 | 7 | 1000 14103348 20390010 36281030 30101548 8 | 9 | 1010 20613C10 0300102A 0C103900 102D0C10 10 | 11 | 1020 36482061 0810334C 0000454F 46000003 12 | 13 | 1030 000000xx xxxxxxxx xxxxxxxx xxxxxxxx 14 | 15 | 1040 xxxxxxxx xxxxxx04 10300010 30E0205D 16 | 17 | 1050 30203FD8 205D2810 30302057 5490392C 18 | 19 | 1060 205E3820 3Fxxxxxx xxxxxxxx xxxxxxxx 20 | 21 | 1070 xxxxxxxx xxxxxx10 10364C00 00000000 22 | 23 | 1080 00100004 1030E020 79302064 509039DC 24 | 25 | 1090 20792C10 36 26 | ------------------------------------------------------- 27 | -------------------------------------------------------------------------------- /Process Synchronization/Assemblers/README.md: -------------------------------------------------------------------------------- 1 | - [Single Pass Assembler](https://github.com/KTU-CSE/System-Software-lab/tree/master/Process%20Synchronization/Assemblers/Single%20Pass%20Assembler/README.md) 2 | - [Pass 1 of a Two Pass Assembler](https://github.com/KTU-CSE/System-Software-lab/tree/master/Process%20Synchronization/Assemblers/Two%20Pass%20Assembler/Pass%201%20of%20a%20Two%20Pass%20Assembler/README.md) 3 | - [Pass 2 of a Two Pass Assembler](https://github.com/KTU-CSE/System-Software-lab/tree/master/Process%20Synchronization/Assemblers/Two%20Pass%20Assembler/Pass%202%20of%20a%20Two%20Pass%20Assembler/README.md) 4 | 5 | # Theory 6 | 7 | ## Assembly Language 8 | An assembly language is a low-level programming language for a computer, or other programmable device, in which there is a very strong (generally one-to-one) correspondence between the language and the architecture's machine code instructions. Each assembly language is specific to a particular computer architecture, in contrast to most high-level programming languages, which are generally portable across multiple architectures, but require interpreting or compiling. 9 | Assembly language uses a mnemonic to represent each low-level machine operation or opcode. Some op-codes require one or more operands as part of the instruction. 10 | 11 | ## Assembler 12 | Assembly language is converted into executable machine code by a utility program referred to as an assembler; the conversion process is referred to as assembly, or assembling the code. 13 | 14 | ![1](http://i.imgur.com/HPjwJOQ.jpg) 15 | 16 | An assembler is a translator that translates an assembler program into a conventional machine language program. Basically, the assembler goes through the program one line at a time, and generates machine code for that instruction. Then the assembler proceeds to the next instruction. In this way, the entire machine code program is created. 17 | 18 | ![2](http://i.imgur.com/Mqntp81.jpg) 19 | 20 | ## Assembler Directives 21 | - Assembler directives are pseudo instructions 22 | - They will not be translated into machine instructions. 23 | - They only provide instruction/direction/information to the assembler. 24 | - Basic assembler directives : 25 | - **START :** Specify name and starting address for the program 26 | - **END :** Indicate the end of the source program. 27 | - **EQU :** The EQU directive is used to replace a number by a symbol. For example: MAXIMUM EQU 99. 28 | After using this directive, every appearance of the label “MAXIMUM” in the program will be interpreted by the assembler as the number 99 (MAXIMUM = 99). Symbols may be defined this way only once in the program. The EQU directive is mostly used at the beginning of the program. 29 | 30 | ## Three Main Data Structures 31 | - Operation Code Table (OPTAB) 32 | 33 | - Location Counter (LOCCTR) 34 | 35 | - Symbol Table (SYMTAB) 36 | 37 | ## One-pass assemblers 38 | A one pass assembler passes over the source file exactly once, in the same pass collecting the labels, resolving future references and doing the actual assembly. The difficult part is to resolve future label references (the problem of forward referencing) and assemble code in one pass 39 | 40 | ![4](http://i.imgur.com/oyn0JCi.png) 41 | 42 | ### Forward reference in one pass assembler 43 | - Omits the operand address if the symbol has not yet been defined 44 | - Enters this undefined symbol into SYMTAB and indicates that it is undefined 45 | - Adds the address of this operand address to a list of forward references associated with the SYMTAB entry 46 | - When the definition for the symbol is encountered, scans the reference list and inserts the address. 47 | - At the end of the program, reports the error if there are still SYMTAB entries indicated undefined symbols. 48 | 49 | ## Two-pass assemblers 50 | The two pass assembler performs two passes over the source program. 51 | 52 | In the first pass, it reads the entire source program, looking only for label definitions. All the labels are collected, assigned address, and placed in the symbol table in this pass, no instructions as assembled and at the end the symbol table should contain all the labels defined in the program. To assign address to labels, the assembles maintains a Location Counter (LC). 53 | 54 | 55 | In the second pass the instructions are again read and are assembled using the symbol table. 56 | Basically, the assembler goes through the program one line at a time, and generates machine code for that instruction. Then the assembler proceeds to the next instruction. In this way, the entire machine code program is created. For most instructions this process works fine, for example for instructions that only reference registers, the assembler can compute the machine code easily, since the assembler knows where the registers are. 57 | 58 | ![5](http://i.imgur.com/XIway7g.jpg) 59 | 60 | ### Difference between One Pass and Two Pass Assemblers 61 | The difference between one pass and two pass assemblers are:- 62 | 63 | A one pass assembler passes over the source file exactly once, in the same pass collecting the labels, resolving future references and doing the actual assembly. The difficult part is to resolve future label references (the problem of forward referencing) and assemble code in one pass. The one pass assembler prepares an intermediate file, which is used as input by the two pass assembler. 64 | 65 | 66 | A two pass assembler does two passes over the source file (the second pass can be over an intermediate file generated in the first pass of the assembler). In the first pass all it does is looks for label definitions and introduces them in the symbol table (a dynamic table which includes the label name and address for each label in the source program). In the second pass, after the symbol table is complete, it does the actual assembly by translating the operations into machine codes and so on. 67 | -------------------------------------------------------------------------------- /Process Synchronization/Assemblers/Single Pass Assembler/18.singlepass.c: -------------------------------------------------------------------------------- 1 | /* 2 | title: Single Pass Assembler 3 | Created on : 30 Nov 18, 10:49 4 | */ 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | void main() 11 | { 12 | char opcode[10], operand[10], label[10], a[10], ad[10], symbol[10], ch; 13 | char code[10][10], code1[10][10] = {"33", "44", "53", "57"}; 14 | char mnemonic[10][10] = {"START", "LDA", "STA", "LDCH", "STCH", "END"}; 15 | char mnemonic1[10][10] = {"LDA", "STA", "LDCH", "STCH"}; 16 | int locctr, start, length, i = 0, j = 0, k, l = 0; 17 | int st, diff, address, add, len, actual_len, finaddr, prevaddr; 18 | FILE *fp1, *fp2, *fp3, *fp4, *fp5, *fp6, *fp7; 19 | 20 | fp1 = fopen("input.txt", "r"); 21 | fp2 = fopen("symtab.txt", "w"); 22 | fp3 = fopen("intermediate.txt", "w"); 23 | fscanf(fp1, "%s%s%s", label, opcode, operand); 24 | if (strcmp(opcode, "START") == 0) 25 | { 26 | start = atoi(operand); 27 | locctr = start; 28 | fprintf(fp3, "%s\t%s\t%s\n", label, opcode, operand); 29 | fscanf(fp1, "%s%s%s", label, opcode, operand); 30 | } 31 | else 32 | locctr = 0; 33 | while (strcmp(opcode, "END") != 0) 34 | { 35 | fprintf(fp3, "%d", locctr); 36 | if (strcmp(label, "**") != 0) 37 | fprintf(fp2, "%s\t%d\n", label, locctr); 38 | strcpy(code[i], mnemonic[j]); 39 | while (strcmp(mnemonic[j], "END") != 0) 40 | { 41 | if (strcmp(opcode, mnemonic[j]) == 0) 42 | { 43 | locctr += 3; 44 | break; 45 | } 46 | strcpy(code[i], mnemonic[j]); 47 | j++; 48 | } 49 | if (strcmp(opcode, "WORD") == 0) 50 | locctr += 3; 51 | else if (strcmp(opcode, "RESW") == 0) 52 | locctr += (3 * (atoi(operand))); 53 | else if (strcmp(opcode, "RESB") == 0) 54 | locctr += (atoi(operand)); 55 | else if (strcmp(opcode, "BYTE") == 0) 56 | ++locctr; 57 | fprintf(fp3, "\t%s\t%s\t%s\n", label, opcode, operand); 58 | fscanf(fp1, "%s%s%s", label, opcode, operand); 59 | } 60 | fprintf(fp3, "%d\t%s\t%s\t%s\n", locctr, label, opcode, operand); 61 | length = locctr - start; 62 | 63 | fclose(fp3); 64 | fclose(fp2); 65 | fclose(fp1); 66 | 67 | printf("\n\nThe contents of Input file:\n\n"); 68 | fp1 = fopen("input.txt", "r"); 69 | ch = fgetc(fp1); 70 | while (ch != EOF) 71 | { 72 | printf("%c", ch); 73 | ch = fgetc(fp1); 74 | } 75 | printf("\n\nLength of the input program is %d.", length); 76 | printf("\n\nThe contents of Symbol Table:\n\n"); 77 | fp2 = fopen("symtab.txt", "r"); 78 | ch = fgetc(fp2); 79 | while (ch != EOF) 80 | { 81 | printf("%c", ch); 82 | ch = fgetc(fp2); 83 | } 84 | 85 | fclose(fp2); 86 | fclose(fp1); 87 | 88 | fp4 = fopen("output.txt", "w"); 89 | fp5 = fopen("symtab.txt", "r"); 90 | fp6 = fopen("intermediate.txt", "r"); 91 | fp7 = fopen("objcode.txt", "w"); 92 | fscanf(fp6, "%s%s%s", label, opcode, operand); 93 | while (strcmp(opcode, "END") != 0) 94 | { 95 | prevaddr = address; 96 | fscanf(fp6, "%d%s%s%s", &address, label, opcode, operand); 97 | } 98 | finaddr = address; 99 | fclose(fp6); 100 | fp6 = fopen("intermediate.txt", "r"); 101 | fscanf(fp6, "%s%s%s", label, opcode, operand); 102 | if (strcmp(opcode, "START") == 0) 103 | { 104 | fprintf(fp4, "\t%s\t%s\t%s\n", label, opcode, operand); 105 | fprintf(fp7, "H^%s^00%s^00%d\n", label, operand, finaddr); 106 | fscanf(fp6, "%d%s%s%s", &address, label, opcode, operand); 107 | st = address; 108 | diff = prevaddr - st; 109 | fprintf(fp7, "T^00%d^%d", address, diff); 110 | } 111 | while (strcmp(opcode, "END") != 0) 112 | { 113 | if (strcmp(opcode, "BYTE") == 0) 114 | { 115 | fprintf(fp4, "%d\t%s\t%s\t%s\t", address, label, opcode, operand); 116 | len = strlen(operand); 117 | actual_len = len - 3; 118 | fprintf(fp7, "^"); 119 | for (k = 2; k < (actual_len + 2); k++) 120 | { 121 | itoa(operand[k], ad, 16); 122 | fprintf(fp4, "%s", ad); 123 | fprintf(fp7, "%s", ad); 124 | } 125 | fprintf(fp4, "\n"); 126 | } 127 | else if (strcmp(opcode, "WORD") == 0) 128 | { 129 | len = strlen(operand); 130 | itoa(atoi(operand), a, 10); 131 | fprintf(fp4, "%d\t%s\t%s\t%s\t00000%s\n", address, label, opcode, operand, a); 132 | fprintf(fp7, "^00000%s", a); 133 | } 134 | else if ((strcmp(opcode, "RESB") == 0) || (strcmp(opcode, "RESW") == 0)) 135 | fprintf(fp4, "%d\t%s\t%s\t%s\n", address, label, opcode, operand); 136 | else 137 | { 138 | while (strcmp(opcode, mnemonic1[l]) != 0) 139 | l++; 140 | if (strcmp(operand, "COPY") == 0) 141 | fprintf(fp4, "%d\t%s\t%s\t%s\t%s0000\n", address, label, opcode, operand, code1[l]); 142 | else 143 | { 144 | rewind(fp5); 145 | fscanf(fp5, "%s%d", symbol, &add); 146 | while (strcmp(operand, symbol) != 0) 147 | fscanf(fp5, "%s%d", symbol, &add); 148 | fprintf(fp4, "%d\t%s\t%s\t%s\t%s%d\n", address, label, opcode, operand, code1[l], add); 149 | fprintf(fp7, "^%s%d", code1[l], add); 150 | } 151 | } 152 | fscanf(fp6, "%d%s%s%s", &address, label, opcode, operand); 153 | } 154 | fprintf(fp4, "%d\t%s\t%s\t%s\n", address, label, opcode, operand); 155 | fprintf(fp7, "\nE^00%d", st); 156 | printf("\nObject Program has been generated."); 157 | 158 | fclose(fp7); 159 | fclose(fp6); 160 | fclose(fp5); 161 | fclose(fp4); 162 | 163 | printf("\n\nObject Program:\n\n"); 164 | fp7 = fopen("objcode.txt", "r"); 165 | ch = fgetc(fp7); 166 | while (ch != EOF) 167 | { 168 | printf("%c", ch); 169 | ch = fgetc(fp7); 170 | } 171 | fclose(fp7); 172 | } 173 | -------------------------------------------------------------------------------- /Process Synchronization/Assemblers/Single Pass Assembler/README.md: -------------------------------------------------------------------------------- 1 | ## Single Pass Assembler 2 | 3 | [![solution](https://img.shields.io/badge/View-Solution-blue.svg?logo=appveyor&longCache=true&style=for-the-badge)](https://github.com/KTU-CSE/System-Software-lab/blob/master/Process%20Synchronization/Assemblers/Single%20Pass%20Assembler/18.singlepass.c) 4 | [![raw](https://img.shields.io/badge/-raw-green.svg?logo=appveyor&longCache=true&style=for-the-badge )](https://github.com/KTU-CSE/System-Software-lab/raw/master/Process%20Synchronization/Assemblers/Single%20Pass%20Assembler/18.singlepass.c) 5 | [![output](https://img.shields.io/badge/-output-ff69b4.svg?logo=appveyor&longCache=true&style=for-the-badge)](https://github.com/KTU-CSE/System-Software-lab/blob/master/Process%20Synchronization/Assemblers/Single%20Pass%20Assembler/README.md#output) 6 | 7 | 8 | 9 | ## Output 10 | 11 | ![output_img](/.github/out_img/p_18_out.png) -------------------------------------------------------------------------------- /Process Synchronization/Assemblers/Single Pass Assembler/input.txt: -------------------------------------------------------------------------------- 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 ** -------------------------------------------------------------------------------- /Process Synchronization/Assemblers/Single Pass Assembler/intermediate.txt: -------------------------------------------------------------------------------- 1 | ** START 2000 2 | 2000 ** LDA FIVE 3 | 2003 ** STA ALPHA 4 | 2006 ** LDCH CHARZ 5 | 2009 ** STCH C1 6 | 2012 ALPHA RESW 2 7 | 2018 FIVE WORD 5 8 | 2021 CHARZ BYTE C'Z' 9 | 2022 C1 RESB 1 10 | 2023 ** END ** 11 | -------------------------------------------------------------------------------- /Process Synchronization/Assemblers/Single Pass Assembler/objcode.txt: -------------------------------------------------------------------------------- 1 | H^**^002000^002023 2 | T^002000^22^332018^442012^532021^572022^000005^5a 3 | E^002000 -------------------------------------------------------------------------------- /Process Synchronization/Assemblers/Single Pass Assembler/optab.txt: -------------------------------------------------------------------------------- 1 | LDA 03 2 | STA 0f 3 | LDCH 53 4 | STCH 57 5 | END * -------------------------------------------------------------------------------- /Process Synchronization/Assemblers/Single Pass Assembler/output.txt: -------------------------------------------------------------------------------- 1 | ** START 2000 2 | 2000 ** LDA FIVE 332018 3 | 2003 ** STA ALPHA 442012 4 | 2006 ** LDCH CHARZ 532021 5 | 2009 ** STCH C1 572022 6 | 2012 ALPHA RESW 2 7 | 2018 FIVE WORD 5 000005 8 | 2021 CHARZ BYTE C'Z' 5a 9 | 2022 C1 RESB 1 10 | 2023 ** END ** 11 | -------------------------------------------------------------------------------- /Process Synchronization/Assemblers/Single Pass Assembler/symtab.txt: -------------------------------------------------------------------------------- 1 | ALPHA 2012 2 | FIVE 2018 3 | CHARZ 2021 4 | C1 2022 5 | -------------------------------------------------------------------------------- /Process Synchronization/Assemblers/Two Pass Assembler/Pass 1 of a Two Pass Assembler/16.pass1.c: -------------------------------------------------------------------------------- 1 | /* 2 | Author : abhijithvijayan 3 | Created on : 25 Oct,18 09:24 4 | Title : Pass 1 of a Two Pass Assembler 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | void passOne(char label[10], char opcode[10], char operand[10], char code[10], char mnemonic[3]); 12 | void display(); 13 | 14 | int main() 15 | { 16 | // for reading from input 17 | char label[10], opcode[10], operand[10]; 18 | // for reading from optab 19 | char code[10], mnemonic[3]; 20 | // call the function 21 | passOne(label, opcode, operand, code, mnemonic); 22 | 23 | return 0; 24 | } 25 | 26 | void passOne(char label[10], char opcode[10], char operand[10], char code[10], char mnemonic[3]) 27 | { 28 | int locctr, start, length; 29 | 30 | FILE *fp1, *fp2, *fp3, *fp4, *fp5; // file pointers 31 | 32 | // read mode 33 | fp1 = fopen("input.txt", "r"); 34 | fp2 = fopen("optab.txt", "r"); 35 | // write mode 36 | fp3 = fopen("symtab.txt", "w"); 37 | fp4 = fopen("intermediate.txt", "w"); 38 | fp5 = fopen("length.txt", "w"); 39 | 40 | fscanf(fp1, "%s\t%s\t%s", label, opcode, operand); // read first line 41 | 42 | if (strcmp(opcode, "START") == 0) { 43 | // atoi() requires stdlib.h header file 44 | start = atoi(operand); // convert operand value from string to integer and assign to start 45 | locctr = start; 46 | fprintf(fp4, "\t%s\t%s\t%s\n", label, opcode, operand); // write to output file (additional tab space as start will not have any locctr) 47 | fscanf(fp1, "%s\t%s\t%s", label, opcode, operand); // read next line 48 | } 49 | else { 50 | locctr = 0; 51 | } 52 | 53 | // iterate till end 54 | while (strcmp(opcode, "END") != 0) { 55 | 56 | // 1. transfer address and read line to output file 57 | fprintf(fp4, "%d\t%s\t%s\t%s\n", locctr, label, opcode, operand); 58 | 59 | // 2. make symtab file with values not starting with ** 60 | if (strcmp(label, "**") != 0) { 61 | fprintf(fp3, "%s\t%d\n", label, locctr); 62 | } 63 | 64 | // 3. read from optab (code and mnemonic value) 65 | fscanf(fp2, "%s\t%s", code, mnemonic); 66 | 67 | // 4. traverse till the end of optab file 68 | while (strcmp(code, "END") != 0) { 69 | if (strcmp(opcode, code) == 0) { // if opcode in input matches the one in optab, increment locctr by 3 70 | locctr += 3; 71 | break; 72 | } 73 | // read next line 74 | fscanf(fp2, "%s\t%s", code, mnemonic); 75 | } 76 | 77 | // 5. Searching opcode for WORD, RESW, BYTE, RESB keywords and updating locctr 78 | 79 | // WORD -> add 3 to locctr 80 | if (strcmp(opcode, "WORD") == 0) { 81 | locctr += 3; 82 | } 83 | // RESW -> add 3*operand to locctr 84 | else if (strcmp(opcode, "RESW") == 0) { 85 | locctr += (3 * (atoi(operand))); // convert operand to integer and multiply with 3 86 | } 87 | // BYTE -> add 1 to locctr 88 | else if (strcmp(opcode, "BYTE") == 0) { 89 | ++locctr; 90 | } 91 | // RESB -> add operand to locctr 92 | else if (strcmp(opcode, "RESB") == 0) { 93 | locctr += atoi(operand); 94 | } 95 | 96 | // read next line 97 | fscanf(fp1, "%s\t%s\t%s", label, opcode, operand); 98 | } 99 | // 6. transfer last line to file 100 | fprintf(fp4, "%d\t%s\t%s\t%s\n", locctr, label, opcode, operand); 101 | 102 | // 7. Close all files 103 | fclose(fp4); 104 | fclose(fp3); 105 | fclose(fp2); 106 | fclose(fp1); 107 | 108 | // 8. display outputs 109 | display(); 110 | 111 | // 9. calculate length of program 112 | length = locctr - start; 113 | fprintf(fp5, "%d", length); 114 | fclose(fp5); 115 | printf("\nThe length of the code : %d\n", length); 116 | } 117 | 118 | void display() { 119 | 120 | char str; 121 | FILE *fp1, *fp2, *fp3; 122 | 123 | // 1. Input Table 124 | printf("\nThe contents of Input Table :\n\n"); 125 | fp1 = fopen("input.txt", "r"); 126 | str = fgetc(fp1); 127 | while (str != EOF) { 128 | printf("%c", str); 129 | str = fgetc(fp1); 130 | } 131 | fclose(fp1); 132 | 133 | //2. Output Table 134 | printf("\n\nThe contents of Output Table :\n\n"); 135 | fp2 = fopen("intermediate.txt", "r"); 136 | str = fgetc(fp2); 137 | while (str != EOF) { 138 | printf("%c", str); 139 | str = fgetc(fp2); 140 | } 141 | fclose(fp2); 142 | 143 | // 3. Symtable 144 | printf("\n\nThe contents of Symbol Table :\n\n"); 145 | fp3 = fopen("symtab.txt", "r"); 146 | str = fgetc(fp3); 147 | while (str != EOF) { 148 | printf("%c", str); 149 | str = fgetc(fp3); 150 | } 151 | fclose(fp3); 152 | } 153 | 154 | /* 155 | 156 | input.txt 157 | --------- 158 | 159 | ** START 2000 160 | ** LDA FIVE 161 | ** STA ALPHA 162 | ** LDCH CHARZ 163 | ** STCH C1 164 | ALPHA RESW 2 165 | FIVE WORD 5 166 | CHARZ BYTE C'Z' 167 | C1 RESB 1 168 | ** END ** 169 | 170 | 171 | optab.txt 172 | --------- 173 | 174 | LDA 03 175 | STA 0f 176 | LDCH 53 177 | STCH 57 178 | END * 179 | 180 | ----------------------------- 181 | 182 | symtab.txt 183 | ---------- 184 | 185 | ALPHA 2012 186 | FIVE 2018 187 | CHARZ 2021 188 | C1 2022 189 | 190 | intermediate.txt 191 | ---------------- 192 | 193 | ** START 2000 194 | 2000 ** LDA FIVE 195 | 2003 ** STA ALPHA 196 | 2006 ** LDCH CHARZ 197 | 2009 ** STCH C1 198 | 2012 ALPHA RESW 2 199 | 2018 FIVE WORD 5 200 | 2021 CHARZ BYTE C'Z' 201 | 2022 C1 RESB 1 202 | 2023 ** END ** 203 | 204 | length.txt 205 | ---------- 206 | 23 207 | 208 | */ -------------------------------------------------------------------------------- /Process Synchronization/Assemblers/Two Pass Assembler/Pass 1 of a Two Pass Assembler/README.md: -------------------------------------------------------------------------------- 1 | ## Pass 1 of a Two Pass Assembler 2 | 3 | [![solution](https://img.shields.io/badge/View-Solution-blue.svg?logo=appveyor&longCache=true&style=for-the-badge)](https://github.com/KTU-CSE/System-Software-lab/blob/master/Process%20Synchronization/Assemblers/Two%20Pass%20Assembler/Pass%201%20of%20a%20Two%20Pass%20Assembler/16.pass1.c) 4 | [![raw](https://img.shields.io/badge/-raw-green.svg?logo=appveyor&longCache=true&style=for-the-badge )](https://github.com/KTU-CSE/System-Software-lab/raw/master/Process%20Synchronization/Assemblers/Two%20Pass%20Assembler/Pass%201%20of%20a%20Two%20Pass%20Assembler/16.pass1.c) 5 | [![output](https://img.shields.io/badge/-output-ff69b4.svg?logo=appveyor&longCache=true&style=for-the-badge)](https://github.com/KTU-CSE/System-Software-lab/blob/master/Process%20Synchronization/Assemblers/Two%20Pass%20Assembler/Pass%201%20of%20a%20Two%20Pass%20Assembler/README.md#output) 6 | 7 | 8 | 9 | ## Output 10 | 11 | ![output_img](/.github/out_img/p_16_out.png) -------------------------------------------------------------------------------- /Process Synchronization/Assemblers/Two Pass Assembler/Pass 1 of a Two Pass Assembler/input.txt: -------------------------------------------------------------------------------- 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 ** -------------------------------------------------------------------------------- /Process Synchronization/Assemblers/Two Pass Assembler/Pass 1 of a Two Pass Assembler/intermediate.txt: -------------------------------------------------------------------------------- 1 | ** START 2000 2 | 2000 ** LDA FIVE 3 | 2003 ** STA ALPHA 4 | 2006 ** LDCH CHARZ 5 | 2009 ** STCH C1 6 | 2012 ALPHA RESW 2 7 | 2018 FIVE WORD 5 8 | 2021 CHARZ BYTE C'Z' 9 | 2022 C1 RESB 1 10 | 2023 ** END ** 11 | -------------------------------------------------------------------------------- /Process Synchronization/Assemblers/Two Pass Assembler/Pass 1 of a Two Pass Assembler/length.txt: -------------------------------------------------------------------------------- 1 | 23 -------------------------------------------------------------------------------- /Process Synchronization/Assemblers/Two Pass Assembler/Pass 1 of a Two Pass Assembler/optab.txt: -------------------------------------------------------------------------------- 1 | LDA 03 2 | STA 0f 3 | LDCH 53 4 | STCH 57 5 | END * -------------------------------------------------------------------------------- /Process Synchronization/Assemblers/Two Pass Assembler/Pass 1 of a Two Pass Assembler/symtab.txt: -------------------------------------------------------------------------------- 1 | ALPHA 2012 2 | FIVE 2018 3 | CHARZ 2021 4 | C1 2022 5 | -------------------------------------------------------------------------------- /Process Synchronization/Assemblers/Two Pass Assembler/Pass 2 of a Two Pass Assembler/17.pass2.c: -------------------------------------------------------------------------------- 1 | /* 2 | Author : abhijithvijayan 3 | Created on : 27 Oct,18 23:50 4 | Title : Pass 2 of a Two Pass Assembler 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | void display(); 12 | 13 | int main() 14 | { 15 | char a[10], ad[10], label[10], opcode[10], operand[10], symbol[10]; 16 | int start, diff, i, address, add, len, actual_len, finaddr, prevaddr, j = 0; 17 | char mnemonic[15][15] = {"LDA", "STA", "LDCH", "STCH"}; 18 | char code[15][15] = {"33", "44", "53", "57"}; 19 | 20 | FILE *fp1, *fp2, *fp3, *fp4; 21 | fp1 = fopen("output.txt", "w"); 22 | fp2 = fopen("symtab.txt", "r"); 23 | fp3 = fopen("intermediate.txt", "r"); 24 | fp4 = fopen("objcode.txt", "w"); 25 | 26 | fscanf(fp3, "%s\t%s\t%s", label, opcode, operand); 27 | 28 | while (strcmp(opcode, "END") != 0) 29 | { 30 | prevaddr = address; 31 | fscanf(fp3, "%d%s%s%s", &address, label, opcode, operand); 32 | } 33 | finaddr = address; 34 | 35 | fclose(fp3); 36 | fp3 = fopen("intermediate.txt", "r"); 37 | 38 | fscanf(fp3, "\t%s\t%s\t%s", label, opcode, operand); 39 | if (strcmp(opcode, "START") == 0) 40 | { 41 | fprintf(fp1, "\t%s\t%s\t%s\n", label, opcode, operand); 42 | fprintf(fp4, "H^%s^00%s^00%d\n", label, operand, finaddr); 43 | fscanf(fp3, "%d%s%s%s", &address, label, opcode, operand); 44 | start = address; 45 | diff = prevaddr - start; 46 | fprintf(fp4, "T^00%d^%d", address, diff); 47 | } 48 | 49 | while (strcmp(opcode, "END") != 0) 50 | { 51 | if (strcmp(opcode, "BYTE") == 0) 52 | { 53 | fprintf(fp1, "%d\t%s\t%s\t%s\t", address, label, opcode, operand); 54 | len = strlen(operand); 55 | actual_len = len - 3; 56 | fprintf(fp4, "^"); 57 | for (i = 2; i < (actual_len + 2); i++) 58 | { 59 | itoa(operand[i], ad, 16); 60 | fprintf(fp1, "%s", ad); 61 | fprintf(fp4, "%s", ad); 62 | } 63 | fprintf(fp1, "\n"); 64 | } 65 | 66 | else if (strcmp(opcode, "WORD") == 0) 67 | { 68 | len = strlen(operand); 69 | itoa(atoi(operand), a, 10); 70 | fprintf(fp1, "%d\t%s\t%s\t%s\t00000%s\n", address, label, opcode, operand, a); 71 | fprintf(fp4, "^00000%s", a); 72 | } 73 | 74 | else if ((strcmp(opcode, "RESB") == 0) || (strcmp(opcode, "RESW") == 0)) { 75 | fprintf(fp1, "%d\t%s\t%s\t%s\n", address, label, opcode, operand); 76 | } 77 | 78 | else 79 | { 80 | while (strcmp(opcode, mnemonic[j]) != 0) 81 | j++; 82 | if (strcmp(operand, "COPY") == 0) 83 | fprintf(fp1, "%d\t%s\t%s\t%s\t%s0000\n", address, label, opcode, operand, code[j]); 84 | else 85 | { 86 | rewind(fp2); 87 | fscanf(fp2, "%s%d", symbol, &add); 88 | while (strcmp(operand, symbol) != 0) 89 | fscanf(fp2, "%s%d", symbol, &add); 90 | fprintf(fp1, "%d\t%s\t%s\t%s\t%s%d\n", address, label, opcode, operand, code[j], add); 91 | fprintf(fp4, "^%s%d", code[j], add); 92 | } 93 | } 94 | 95 | fscanf(fp3, "%d%s%s%s", &address, label, opcode, operand); 96 | } 97 | 98 | fprintf(fp1, "%d\t%s\t%s\t%s\n", address, label, opcode, operand); 99 | fprintf(fp4, "\nE^00%d", start); 100 | 101 | fclose(fp4); 102 | fclose(fp3); 103 | fclose(fp2); 104 | fclose(fp1); 105 | 106 | display(); 107 | 108 | return 0; 109 | } 110 | 111 | void display() { 112 | char ch; 113 | FILE *fp1, *fp2, *fp3, *fp4; 114 | 115 | printf("\nIntermediate file is converted into object code"); 116 | 117 | printf("\n\nThe contents of Intermediate file:\n\n"); 118 | fp3 = fopen("intermediate.txt", "r"); 119 | ch = fgetc(fp3); 120 | while (ch != EOF) 121 | { 122 | printf("%c", ch); 123 | ch = fgetc(fp3); 124 | } 125 | fclose(fp3); 126 | 127 | printf("\n\nThe contents of Symbol Table :\n\n"); 128 | fp2 = fopen("symtab.txt", "r"); 129 | ch = fgetc(fp2); 130 | while (ch != EOF) 131 | { 132 | printf("%c", ch); 133 | ch = fgetc(fp2); 134 | } 135 | fclose(fp2); 136 | 137 | printf("\n\nThe contents of Output file :\n\n"); 138 | fp1 = fopen("output.txt", "r"); 139 | ch = fgetc(fp1); 140 | while (ch != EOF) 141 | { 142 | printf("%c", ch); 143 | ch = fgetc(fp1); 144 | } 145 | fclose(fp1); 146 | 147 | printf("\n\nThe contents of Object code file :\n\n"); 148 | fp4 = fopen("objcode.txt", "r"); 149 | ch = fgetc(fp4); 150 | while (ch != EOF) 151 | { 152 | printf("%c", ch); 153 | ch = fgetc(fp4); 154 | } 155 | fclose(fp4); 156 | 157 | } 158 | 159 | /* 160 | 161 | intermediate.txt 162 | ---------------- 163 | 164 | ** START 2000 165 | 2000 ** LDA FIVE 166 | 2003 ** STA ALPHA 167 | 2006 ** LDCH CHARZ 168 | 2009 ** STCH C1 169 | 2012 ALPHA RESW 2 170 | 2018 FIVE WORD 5 171 | 2021 CHARZ BYTE C'Z' 172 | 2022 C1 RESB 1 173 | 2023 ** END ** 174 | 175 | symtab.txt 176 | ---------- 177 | 178 | ALPHA 2012 179 | FIVE 2018 180 | CHARZ 2021 181 | C1 2022 182 | 183 | ----------------------------- 184 | 185 | output.txt 186 | ---------- 187 | 188 | ** START 2000 189 | 2000 ** LDA FIVE 332018 190 | 2003 ** STA ALPHA 442012 191 | 2006 ** LDCH CHARZ 532021 192 | 2009 ** STCH C1 572022 193 | 2012 ALPHA RESW 2 194 | 2018 FIVE WORD 5 000005 195 | 2021 CHARZ BYTE C'Z' 5a 196 | 2022 C1 RESB 1 197 | 2023 ** END ** 198 | 199 | objcode.txt 200 | ----------- 201 | 202 | H^**^002000^002023 203 | T^002000^22^332018^442012^532021^572022^000005^5a 204 | E^002000 205 | 206 | */ -------------------------------------------------------------------------------- /Process Synchronization/Assemblers/Two Pass Assembler/Pass 2 of a Two Pass Assembler/README.md: -------------------------------------------------------------------------------- 1 | ## Pass 2 of a Two Pass Assembler 2 | 3 | [![solution](https://img.shields.io/badge/View-Solution-blue.svg?logo=appveyor&longCache=true&style=for-the-badge)](https://github.com/KTU-CSE/System-Software-lab/blob/master/Process%20Synchronization/Assemblers/Two%20Pass%20Assembler/Pass%202%20of%20a%20Two%20Pass%20Assembler/17.pass2.c) 4 | [![raw](https://img.shields.io/badge/-raw-green.svg?logo=appveyor&longCache=true&style=for-the-badge )](https://github.com/KTU-CSE/System-Software-lab/raw/master/Process%20Synchronization/Assemblers/Two%20Pass%20Assembler/Pass%202%20of%20a%20Two%20Pass%20Assembler/17.pass2.c) 5 | [![output](https://img.shields.io/badge/-output-ff69b4.svg?logo=appveyor&longCache=true&style=for-the-badge)](https://github.com/KTU-CSE/System-Software-lab/blob/master/Process%20Synchronization/Assemblers/Two%20Pass%20Assembler/Pass%202%20of%20a%20Two%20Pass%20Assembler/README.md#output) 6 | 7 | 8 | 9 | ## Output 10 | 11 | ![output_img](/.github/out_img/p_17_out.png) -------------------------------------------------------------------------------- /Process Synchronization/Assemblers/Two Pass Assembler/Pass 2 of a Two Pass Assembler/intermediate.txt: -------------------------------------------------------------------------------- 1 | ** START 2000 2 | 2000 ** LDA FIVE 3 | 2003 ** STA ALPHA 4 | 2006 ** LDCH CHARZ 5 | 2009 ** STCH C1 6 | 2012 ALPHA RESW 2 7 | 2018 FIVE WORD 5 8 | 2021 CHARZ BYTE C'Z' 9 | 2022 C1 RESB 1 10 | 2023 ** END ** 11 | -------------------------------------------------------------------------------- /Process Synchronization/Assemblers/Two Pass Assembler/Pass 2 of a Two Pass Assembler/objcode.txt: -------------------------------------------------------------------------------- 1 | H^**^002000^002023 2 | T^002000^22^332018^442012^532021^572022^000005^5a 3 | E^002000 -------------------------------------------------------------------------------- /Process Synchronization/Assemblers/Two Pass Assembler/Pass 2 of a Two Pass Assembler/output.txt: -------------------------------------------------------------------------------- 1 | ** START 2000 2 | 2000 ** LDA FIVE 332018 3 | 2003 ** STA ALPHA 442012 4 | 2006 ** LDCH CHARZ 532021 5 | 2009 ** STCH C1 572022 6 | 2012 ALPHA RESW 2 7 | 2018 FIVE WORD 5 000005 8 | 2021 CHARZ BYTE C'Z' 5a 9 | 2022 C1 RESB 1 10 | 2023 ** END ** 11 | -------------------------------------------------------------------------------- /Process Synchronization/Assemblers/Two Pass Assembler/Pass 2 of a Two Pass Assembler/symtab.txt: -------------------------------------------------------------------------------- 1 | ALPHA 2012 2 | FIVE 2018 3 | CHARZ 2021 4 | C1 2022 5 | -------------------------------------------------------------------------------- /Process Synchronization/Dining Philosophers/15.dining.c: -------------------------------------------------------------------------------- 1 | /* 2 | title: Dining Philosophers Problem 3 | Created on : 08 Dec 18, 10:49 4 | Note – The below program may compile only with C compilers with semaphore and pthread library. 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #define N 5 12 | #define THINKING 0 13 | #define HUNGRY 1 14 | #define EATING 2 15 | #define LEFT (ph_num + 4) % N 16 | #define RIGHT (ph_num + 1) % N 17 | 18 | sem_t mutex; 19 | sem_t S[N]; 20 | 21 | void *philospher(void *num); 22 | void take_fork(int); 23 | void put_fork(int); 24 | void test(int); 25 | 26 | int state[N]; 27 | int phil_num[N] = {0, 1, 2, 3, 4}; 28 | 29 | int main() 30 | { 31 | int i; 32 | pthread_t thread_id[N]; 33 | sem_init(&mutex, 0, 1); 34 | for (i = 0; i < N; i++) 35 | sem_init(&S[i], 0, 0); 36 | for (i = 0; i < N; i++) 37 | { 38 | pthread_create(&thread_id[i], NULL, philospher, &phil_num[i]); 39 | printf("Philosopher %d is thinking\n", i + 1); 40 | } 41 | for (i = 0; i < N; i++) 42 | pthread_join(thread_id[i], NULL); 43 | } 44 | 45 | void *philospher(void *num) 46 | { 47 | while (1) 48 | { 49 | int *i = num; 50 | sleep(1); 51 | take_fork(*i); 52 | sleep(0); 53 | put_fork(*i); 54 | } 55 | } 56 | 57 | void take_fork(int ph_num) 58 | { 59 | sem_wait(&mutex); 60 | state[ph_num] = HUNGRY; 61 | printf("Philosopher %d is Hungry\n", ph_num + 1); 62 | test(ph_num); 63 | sem_post(&mutex); 64 | sem_wait(&S[ph_num]); 65 | sleep(1); 66 | } 67 | 68 | void test(int ph_num) 69 | { 70 | if (state[ph_num] == HUNGRY && state[LEFT] != EATING && state[RIGHT] != EATING) 71 | { 72 | state[ph_num] = EATING; 73 | sleep(2); 74 | printf("Philosopher %d takes fork %d and %d\n", ph_num + 1, LEFT + 1, ph_num + 1); 75 | printf("Philosopher %d is Eating\n", ph_num + 1); 76 | sem_post(&S[ph_num]); 77 | } 78 | } 79 | 80 | void put_fork(int ph_num) 81 | { 82 | sem_wait(&mutex); 83 | state[ph_num] = THINKING; 84 | printf("Philosopher %d putting fork %d and %d down\n", ph_num + 1, LEFT + 1, ph_num + 1); 85 | printf("Philosopher %d is thinking\n", ph_num + 1); 86 | test(LEFT); 87 | test(RIGHT); 88 | sem_post(&mutex); 89 | } 90 | 91 | /* OUPUT 92 | 93 | Philosopher 1 is thinking 94 | Philosopher 2 is thinking 95 | Philosopher 3 is thinking 96 | Philosopher 4 is thinking 97 | Philosopher 5 is thinking 98 | Philosopher 1 is Hungry 99 | Philosopher 1 takes fork 5 and 1 100 | Philosopher 1 is Eating 101 | Philosopher 5 is Hungry 102 | Philosopher 3 is Hungry 103 | Philosopher 3 takes fork 2 and 3 104 | Philosopher 3 is Eating 105 | Philosopher 4 is Hungry 106 | Philosopher 2 is Hungry 107 | Philosopher 1 putting fork 5 and 1 down 108 | Philosopher 1 is thinking 109 | Philosopher 5 takes fork 4 and 5 110 | Philosopher 5 is Eating 111 | Philosopher 3 putting fork 2 and 3 down 112 | Philosopher 3 is thinking 113 | Philosopher 2 takes fork 1 and 2 114 | Philosopher 2 is Eating 115 | Philosopher 5 putting fork 4 and 5 down 116 | Philosopher 5 is thinking 117 | Philosopher 4 takes fork 3 and 4 118 | Philosopher 4 is Eating 119 | Philosopher 1 is Hungry 120 | Philosopher 3 is Hungry 121 | Philosopher 2 putting fork 1 and 2 down 122 | Philosopher 2 is thinking 123 | Philosopher 1 takes fork 5 and 1 124 | Philosopher 1 is Eating 125 | Philosopher 5 is Hungry 126 | Philosopher 4 putting fork 3 and 4 down 127 | Philosopher 4 is thinking 128 | Philosopher 3 takes fork 2 and 3 129 | Philosopher 3 is Eating 130 | Philosopher 2 is Hungry 131 | Philosopher 1 putting fork 5 and 1 down 132 | Philosopher 1 is thinking 133 | . 134 | . 135 | . 136 | . 137 | . 138 | . 139 | . 140 | . 141 | 142 | */ -------------------------------------------------------------------------------- /Process Synchronization/Dining Philosophers/README.md: -------------------------------------------------------------------------------- 1 | ## Program for Dining Philosophers Problem 2 | 3 | [![solution](https://img.shields.io/badge/View-Solution-blue.svg?logo=appveyor&longCache=true&style=for-the-badge)](https://github.com/KTU-CSE/System-Software-lab/blob/master/Process%20Synchronization/Dining%20Philosophers/15.dining.c) 4 | [![raw](https://img.shields.io/badge/-raw-green.svg?logo=appveyor&longCache=true&style=for-the-badge)](https://github.com/KTU-CSE/System-Software-lab/raw/master/Process%20Synchronization/Dining%20Philosophers/15.dining.c) 5 | [![output](https://img.shields.io/badge/-output-ff69b4.svg?logo=appveyor&longCache=true&style=for-the-badge)](https://github.com/KTU-CSE/System-Software-lab/blob/master/Process%20Synchronization/Dining%20Philosophers/README.md#output) -------------------------------------------------------------------------------- /Process Synchronization/Producer Consumer/14.producer.c: -------------------------------------------------------------------------------- 1 | /* 2 | title: Producer Consumer Problem 3 | Created on : 08 Dec 18, 10:42 4 | */ 5 | 6 | #include 7 | #include 8 | 9 | int mutex = 1, full = 0, empty = 3, x = 0; 10 | 11 | int main() 12 | { 13 | int n; 14 | void producer(); 15 | void consumer(); 16 | int wait(int); 17 | int signal(int); 18 | printf("\n1.Producer\n2.Consumer\n3.Exit"); 19 | while (1) 20 | { 21 | printf("\nEnter your choice: "); 22 | scanf("%d", &n); 23 | switch (n) 24 | { 25 | case 1: 26 | if ((mutex == 1) && (empty != 0)) 27 | producer(); 28 | else 29 | printf("Buffer is full!!"); 30 | break; 31 | case 2: 32 | if ((mutex == 1) && (full != 0)) 33 | consumer(); 34 | else 35 | printf("Buffer is empty!!"); 36 | break; 37 | case 3: 38 | exit(0); 39 | break; 40 | } 41 | } 42 | 43 | return 0; 44 | } 45 | 46 | int wait(int s) 47 | { 48 | return (--s); 49 | } 50 | 51 | int signal(int s) 52 | { 53 | return (++s); 54 | } 55 | 56 | void producer() 57 | { 58 | mutex = wait(mutex); 59 | full = signal(full); 60 | empty = wait(empty); 61 | x++; 62 | printf("\nProducer produces the item %d", x); 63 | mutex = signal(mutex); 64 | } 65 | 66 | void consumer() 67 | { 68 | mutex = wait(mutex); 69 | full = wait(full); 70 | empty = signal(empty); 71 | printf("\nConsumer consumes item %d", x); 72 | x--; 73 | mutex = signal(mutex); 74 | } 75 | 76 | /* OUTPUT 77 | 78 | 1.Producer 79 | 2.Consumer 80 | 3.Exit 81 | Enter your choice: 1 82 | 83 | Producer produces the item 1 84 | Enter your choice: 1 85 | 86 | Producer produces the item 2 87 | Enter your choice: 2 88 | 89 | Consumer consumes item 2 90 | Enter your choice: 2 91 | 92 | Consumer consumes item 1 93 | Enter your choice: 2 94 | Buffer is empty!! 95 | Enter your choice: 3 96 | 97 | */ -------------------------------------------------------------------------------- /Process Synchronization/Producer Consumer/README.md: -------------------------------------------------------------------------------- 1 | ## Program for Producer Consumer Problem 2 | 3 | [![solution](https://img.shields.io/badge/View-Solution-blue.svg?logo=appveyor&longCache=true&style=for-the-badge)](https://github.com/KTU-CSE/System-Software-lab/blob/master/Process%20Synchronization/Producer%20Consumer/14.producer.c) 4 | [![raw](https://img.shields.io/badge/-raw-green.svg?logo=appveyor&longCache=true&style=for-the-badge)](https://github.com/KTU-CSE/System-Software-lab/raw/master/Process%20Synchronization/Producer%20Consumer/14.producer.c) 5 | [![output](https://img.shields.io/badge/-output-ff69b4.svg?logo=appveyor&longCache=true&style=for-the-badge)](https://github.com/KTU-CSE/System-Software-lab/blob/master/Process%20Synchronization/Producer%20Consumer/README.md#output) -------------------------------------------------------------------------------- /Process Synchronization/README.md: -------------------------------------------------------------------------------- 1 | - [Producer Consumer Problem](https://github.com/KTU-CSE/System-Software-lab/tree/master/Process%20Synchronization/Producer%20Consumer/README.md) 2 | - [Dining Philosophers Problem](https://github.com/KTU-CSE/System-Software-lab/tree/master/Process%20Synchronization/Dining%20Philosophers/README.md) 3 | - [Pass 1 of a Two Pass Assembler](https://github.com/KTU-CSE/System-Software-lab/tree/master/Process%20Synchronization/Assemblers/Two%20Pass%20Assembler/Pass%201%20of%20a%20Two%20Pass%20Assembler/README.md) 4 | - [Pass 2 of a Two Pass Assembler](https://github.com/KTU-CSE/System-Software-lab/tree/master/Process%20Synchronization/Assemblers/Two%20Pass%20Assembler/Pass%202%20of%20a%20Two%20Pass%20Assembler/README.md) 5 | - [Single Pass Assembler](https://github.com/KTU-CSE/System-Software-lab/tree/master/Process%20Synchronization/Assemblers/Single%20Pass%20Assembler/README.md) 6 | - [Absolute Loader](https://github.com/KTU-CSE/System-Software-lab/tree/master/Process%20Synchronization/Absolute%20Loader/README.md) 7 | - [Symbol Table With Hashing](#) // toBeDone -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SYSTEM-SOFTWARE-LAB 2 | 3 | [![download](https://img.shields.io/badge/Direct_Download-zip-blue.svg?logo=appveyor&longCache=true&style=for-the-badge)](https://github.com/KTU-CSE/System-Software-lab/releases/latest) 4 | [![website](https://img.shields.io/badge/Live-Site-green.svg?logo=appveyor&longCache=true&style=for-the-badge)](https://ktu-cse.github.io/System-Software-lab/) 5 | 6 | B.Tech CSE S5 System Software Lab 7 | 8 | Batch: CSE 2k16-20, College of Engg. Adoor 9 | 10 | University: APJ Abdul Kalam Technological University 11 | 12 | ## Content 13 | 14 | - **[CPU Scheduling](CPU%20Scheduling/README.md)** 15 | 16 | - [FCFS Scheduling](CPU%20Scheduling/FCFS/README.md) 17 | - [SJF Scheduling](CPU%20Scheduling/SJF/README.md) 18 | - [Priority Scheduling](CPU%20Scheduling/Priority/README.md) 19 | - [Round Robin scheduling](CPU%20Scheduling/RoundRobin/README.md) 20 | 21 | - **[Memory Management](Memory%20Management/README.md)** 22 | 23 | - [Single-Level Directory-File Organisation Technique](Memory%20Management/SingleLevelDir/README.md) 24 | - [Two-Level Directory-File Organisation Technique](Memory%20Management/TwoLevelDir/README.md) 25 | 26 | - **[Deadlocks](Deadlocks/README.md)** 27 | 28 | - [Banker's Algorithm](Deadlocks/BankersAlgorithm/README.md) 29 | 30 | - **[Disk Management](Disk%20Management/README.md)** 31 | 32 | - [FCFS Disk Scheduling](Disk%20Management/FCFS/README.md) 33 | - [SCAN Disk Scheduling](Disk%20Management/SCAN/README.md) 34 | - [C-SCAN Disk Scheduling](Disk%20Management/CSCAN/README.md) 35 | 36 | - **[Virtual Memory](Virtual%20Memory/README.md)** 37 | 38 | - [FIFO Page Replacement Algorithm](Virtual%20Memory/FIFO/README.md) 39 | - [LRU Page Replacement Algorithm](Virtual%20Memory/LRU/README.md) 40 | - [LFU Page Replacement Algorithm](Virtual%20Memory/LFU/README.md) 41 | 42 | - **[Process Synchronization](Process%20Synchronization/README.md)** 43 | - [Producer Consumer Problem](Process%20Synchronization/Producer%20Consumer/README.md) 44 | - [Dining Philosophers Problem](Process%20Synchronization/Dining%20Philosophers/README.md) 45 | - [Pass 1 of a Two Pass Assembler](Process%20Synchronization/Assemblers/Two%20Pass%20Assembler/Pass%201%20of%20a%20Two%20Pass%20Assembler/README.md) 46 | - [Pass 2 of a Two Pass Assembler](Process%20Synchronization/Assemblers/Two%20Pass%20Assembler/Pass%202%20of%20a%20Two%20Pass%20Assembler/README.md) 47 | - [Single Pass Assembler](Process%20Synchronization/Assemblers/Single%20Pass%20Assembler/README.md) 48 | - [Absolute Loader](Process%20Synchronization/Absolute%20Loader/README.md) 49 | - [Symbol Table With Hashing](#) // toBeDone 50 | 51 |
52 | 53 | ## Requirements 54 | 55 | 1. GCC Compiler --> [TDM-GCC MinGW Compiler](https://sourceforge.net/projects/tdm-gcc/) 56 | 57 | 2. Any Text Editor of your preference 58 | - [VS Code](https://code.visualstudio.com/) 59 | - [Sublime Text](https://www.sublimetext.com/) 60 | - [Atom](https://atom.io/) 61 | - [Brackets](http://brackets.io/) 62 | 63 | ## What each directory in this repo contains 64 | 65 | ``` 66 | 1) CPU Scheduling 67 | └──── FCFS 68 | └──── 1.fcfs.c 69 | └──── README.md 70 | └──── SJF 71 | └──── 2.sjf.c 72 | └──── README.md 73 | └──── Priority 74 | └──── 3.priority.c 75 | └──── README.md 76 | └──── RoundRobin 77 | └──── 4.roundrobin.c 78 | └──── README.md 79 | └──── README.md 80 | 81 | 2) Memory Management 82 | └──── SingleLevelDir 83 | └──── 5.singleLevelDir.c 84 | └──── README.md 85 | └──── TwoLevelDir 86 | └──── 6.twoLevelDir.c 87 | └──── README.md 88 | └──── README.md 89 | 90 | 3) Deadlocks 91 | └──── BankersAlgorithm 92 | └──── 7.bankers.c 93 | └──── README.md 94 | └──── README.md 95 | 96 | 4) Disk Management 97 | └──── FCFS 98 | └──── 8.fcfsDisk 99 | └──── README.md 100 | └──── SCAN 101 | └──── 9.scan.c 102 | └──── README.md 103 | └──── CSCAN 104 | └──── 10.csan.c 105 | └──── README.md 106 | └──── README.md 107 | 108 | 5) Virtual Memory 109 | └──── FIFO 110 | └──── 11.fifoPage.c 111 | └──── README.md 112 | └──── LRU 113 | └──── 12.lru.c 114 | └──── README.md 115 | └──── LFU 116 | └──── 13.lfu.c 117 | └──── README.md 118 | └──── README.md 119 | 120 | 6) Process Synchronization 121 | └──── Producer Consumer Problem 122 | └──── 14.producer.c 123 | └──── README.md 124 | └──── Dining Philosophers Problem 125 | └──── 15.dining.c 126 | └──── README.md 127 | └──── Assemblers 128 | └──── Two Pass Assembler 129 | └──── Pass 1 of a Two Pass Assembler 130 | | └──── 16.pass1.c 131 | | └──── README.md 132 | └──── Pass 2 of a Two Pass Assembler 133 | | └──── 17.pass2.c 134 | | └──── README.md 135 | └──── Single Pass Assembler 136 | └──── 18.singlepass.c 137 | └──── README.md 138 | └──── Absolute Loader 139 | └──── 19.absolute.c 140 | └──── README.md 141 | └──── Symbol Table With Hashing 142 | ``` 143 | 144 | ## Getting Started 145 | 146 | Clone this repo 147 | ``` 148 | $ git clone https://github.com/KTU-CSE/System-Software-lab.git 149 | ``` 150 | Switch to the program folder 151 | ``` 152 | $ cd the/dir/where/program/file/is 153 | ``` 154 | Compile the source code 155 | ``` 156 | $ gcc yourProgramFile.c 157 | ``` 158 | To Execute 159 | ``` 160 | $ ./a.out 161 | ``` 162 | 163 | ## License 164 | This project is licensed under the MIT License - see the [LICENSE.md](LICENSE) file for details 165 | 166 | ## Spread the word 167 | Liked the project? Just give it a star :star: and spread the word! -------------------------------------------------------------------------------- /Virtual Memory/FIFO/11.fifoPage.c: -------------------------------------------------------------------------------- 1 | /* 2 | Author : abhijithvijayan 3 | Created on : 18 Nov 18, 14:32 4 | Title : FIFO page-replacement algorithm 5 | */ 6 | 7 | #include 8 | 9 | void fifo(int[], int[], int, int); 10 | 11 | int main() 12 | { 13 | int i, pCount, fCount, pages[30], frames[20]; 14 | printf("Number of Frames : "); 15 | scanf("%d", &fCount); 16 | // create frames array will null value 17 | for (i = 0; i < fCount; ++i) { 18 | frames[i] = -1; 19 | } 20 | printf("Number of Pages : "); 21 | scanf("%d", &pCount); 22 | printf("Enter the reference string\n"); 23 | for (i = 0; i < pCount; ++i) { 24 | scanf("%d", &pages[i]); 25 | } 26 | // call the function 27 | fifo(pages, frames, pCount, fCount); 28 | 29 | return 0; 30 | } 31 | 32 | void fifo(int pages[], int frames[], int pCount, int fCount) { 33 | printf("\nRef.String |\tFrames\n"); 34 | printf("-------------------------------\n"); 35 | int i, j, k, flag, faultCount = 0, queue = 0; 36 | for (i = 0; i < pCount; ++i) { 37 | printf(" %d\t|\t", pages[i]); 38 | flag = 0; 39 | for (j = 0; j < fCount; ++j) { 40 | if (frames[j] == pages[i]) { // compare with string in str[] 41 | flag = 1; 42 | printf(" Hit"); 43 | break; 44 | } 45 | } 46 | if (flag == 0) { // not present in frames 47 | frames[queue] = pages[i]; 48 | faultCount++; 49 | queue = (queue + 1) % fCount; // Queue position in circular way 50 | // display 51 | for (k = 0; k < fCount; ++k) { 52 | printf("%d ", frames[k]); 53 | } 54 | } 55 | printf("\n\n"); 56 | } 57 | printf("Total Page Faults = %d\n", faultCount); 58 | } 59 | 60 | /* OUTPUT 61 | 62 | Number of Frames : 3 63 | Number of Pages : 20 64 | Enter the reference string 65 | 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1 66 | 67 | Ref.String | Frames 68 | ------------------------------- 69 | 7 | 7 -1 -1 70 | 71 | 0 | 7 0 -1 72 | 73 | 1 | 7 0 1 74 | 75 | 2 | 2 0 1 76 | 77 | 0 | Hit 78 | 79 | 3 | 2 3 1 80 | 81 | 0 | 2 3 0 82 | 83 | 4 | 4 3 0 84 | 85 | 2 | 4 2 0 86 | 87 | 3 | 4 2 3 88 | 89 | 0 | 0 2 3 90 | 91 | 3 | Hit 92 | 93 | 2 | Hit 94 | 95 | 1 | 0 1 3 96 | 97 | 2 | 0 1 2 98 | 99 | 0 | Hit 100 | 101 | 1 | Hit 102 | 103 | 7 | 7 1 2 104 | 105 | 0 | 7 0 2 106 | 107 | 1 | 7 0 1 108 | 109 | Total Page Faults = 15 110 | 111 | */ -------------------------------------------------------------------------------- /Virtual Memory/FIFO/README.md: -------------------------------------------------------------------------------- 1 | ## Program for FIFO page-replacement algorithm 2 | 3 | [![solution](https://img.shields.io/badge/View-Solution-blue.svg?logo=appveyor&longCache=true&style=for-the-badge)](https://github.com/KTU-CSE/System-Software-lab/blob/master/Virtual%20Memory/FIFO/11.fifoPage.c) 4 | [![raw](https://img.shields.io/badge/-raw-green.svg?logo=appveyor&longCache=true&style=for-the-badge)](https://github.com/KTU-CSE/System-Software-lab/raw/master/Virtual%20Memory/FIFO/11.fifoPage.c) 5 | [![output](https://img.shields.io/badge/-output-ff69b4.svg?logo=appveyor&longCache=true&style=for-the-badge)](https://github.com/KTU-CSE/System-Software-lab/blob/master/Virtual%20Memory/FIFO/README.md#output) 6 | 7 | The first-in, first-out (FIFO) page replacement algorithm is a low-overhead algorithm that requires little book keeping on the part of the operating system. 8 | 9 | The idea is obvious from the name – the operating system keeps track of all the pages in memory in a queue, with the most recent arrival at the back, and the oldest arrival in front. 10 | 11 | When a page needs to be replaced, the page at the front of the queue (the oldest page) is selected. 12 | 13 |
14 | 15 | **Implementation** – Let capacity be the number of pages that memory can hold. Let set be the current set of pages in memory. 16 | 17 | - Start traversing the pages : 18 | 19 | - If set holds less pages than capacity. 20 | - Check if current page is present in set 21 | - If not : 22 | - Remove the first page from the queue 23 | as it was the first to be entered in 24 | the memory 25 | - Replace the first page in the queue with 26 | the current page in the string. 27 | - Simultaneously maintain the pages in the 28 | queue to perform FIFO. 29 | 30 | - Return page faults. 31 | 32 | ![](/.github/out_img/img_11.png) 33 | 34 |
35 | 36 | While FIFO is cheap and intuitive, it performs poorly in practical application. Thus, it is rarely used in its unmodified form. 37 | This algorithm experiences Bélády's anomaly. 38 | 39 | In simple words, on a page fault, the frame that has been in memory the longest is replaced. 40 | 41 | FIFO is a conservative algorithm, so it is -competitive. 42 | 43 | ## Output 44 | 45 | ![output_img](/.github/out_img/p_11_out.png) 46 | 47 | Reference 48 | 49 | - [Wikipedia](https://en.wikipedia.org/wiki/Page_replacement_algorithm) 50 | - [GeeksforGeeks](https://www.geeksforgeeks.org/program-page-replacement-algorithms-set-2-fifo/) 51 | - [CampusCoke](https://campuscoke.blogspot.com/2015/01/fifo-page-replacement-algorithm-in-c.html) 52 | -------------------------------------------------------------------------------- /Virtual Memory/LFU/13.lfu.c: -------------------------------------------------------------------------------- 1 | /* 2 | Title : LFU page-replacement algorithm 3 | Created on : 08 Dec 18, 10:33 4 | // Not sure this is the right way 5 | */ 6 | 7 | #include 8 | void main() 9 | { 10 | int q[20], p[50], c = 0, c1, d, f, i, j, k = 0, n, r, t, b[20], c2[20]; 11 | printf("Enter no of pages: "); 12 | scanf("%d", &n); 13 | printf("Enter the reference string: "); 14 | for (i = 0; i < n; i++) 15 | scanf("%d", &p[i]); 16 | printf("Enter no of frames: "); 17 | scanf("%d", &f); 18 | q[k] = p[k]; 19 | printf("\n\t%d\n", q[k]); 20 | c++; 21 | k++; 22 | for (i = 1; i < n; i++) 23 | { 24 | c1 = 0; 25 | for (j = 0; j < f; j++) 26 | { 27 | if (p[i] != q[j]) 28 | c1++; 29 | } 30 | if (c1 == f) 31 | { 32 | c++; 33 | if (k < f) 34 | { 35 | q[k] = p[i]; 36 | k++; 37 | for (j = 0; j < k; j++) 38 | printf("\t%d", q[j]); 39 | printf("\n"); 40 | } 41 | else 42 | { 43 | for (r = 0; r < f; r++) 44 | { 45 | c2[r] = 0; 46 | for (j = i - 1; j < n; j--) 47 | { 48 | if (q[r] != p[j]) 49 | c2[r]++; 50 | else 51 | break; 52 | } 53 | } 54 | for (r = 0; r < f; r++) 55 | b[r] = c2[r]; 56 | for (r = 0; r < f; r++) 57 | { 58 | for (j = r; j < f; j++) 59 | { 60 | if (b[r] < b[j]) 61 | { 62 | t = b[r]; 63 | b[r] = b[j]; 64 | b[j] = t; 65 | } 66 | } 67 | } 68 | for (r = 0; r < f; r++) 69 | { 70 | if (c2[r] == b[0]) 71 | q[r] = p[i]; 72 | printf("\t%d", q[r]); 73 | } 74 | printf("\n"); 75 | } 76 | } 77 | } 78 | printf("\nThe no of page faults is %d", c); 79 | } 80 | 81 | /* 82 | 83 | OUTPUT: 84 | 85 | Enter no of pages: 15 86 | Enter the reference string: 7 0 1 2 0 3 0 4 2 3 0 3 7 7 4 87 | Enter no of frames: 4 88 | 89 | 7 90 | 7 1 91 | 7 1 2 92 | 7 1 2 0 93 | 3 1 2 0 94 | 3 4 2 0 95 | 3 7 2 0 96 | 3 7 4 0 97 | 98 | The no of page faults is 8 99 | 100 | */ -------------------------------------------------------------------------------- /Virtual Memory/LFU/README.md: -------------------------------------------------------------------------------- 1 | ## Program for LFU page-replacement algorithm 2 | 3 | [![solution](https://img.shields.io/badge/View-Solution-blue.svg?logo=appveyor&longCache=true&style=for-the-badge)](https://github.com/KTU-CSE/System-Software-lab/blob/master/Virtual%20Memory/LFU/13.lfu.c) 4 | [![raw](https://img.shields.io/badge/-raw-green.svg?logo=appveyor&longCache=true&style=for-the-badge)](https://github.com/KTU-CSE/System-Software-lab/raw/master/Virtual%20Memory/LFU/13.lfu.c) 5 | [![output](https://img.shields.io/badge/-output-ff69b4.svg?logo=appveyor&longCache=true&style=for-the-badge)](https://github.com/KTU-CSE/System-Software-lab/blob/master/Virtual%20Memory/LFU/README.md#output) -------------------------------------------------------------------------------- /Virtual Memory/LRU/12.lru.c: -------------------------------------------------------------------------------- 1 | /* 2 | Author : abhijithvijayan 3 | Created on : 20 Nov 18, 14:42 4 | Title : LRU page-replacement algorithm 5 | */ 6 | 7 | #include 8 | 9 | void LRU(int[], int[], int[], int, int); 10 | int findLRU(int[], int); 11 | 12 | int main() { 13 | int i, pCount, fCount, pages[30], frames[20], time[20]; 14 | printf("Number of Frames : "); 15 | scanf("%d", &fCount); 16 | // create frames array will null values 17 | for (i = 0; i < fCount; ++i) { 18 | frames[i] = -1; 19 | } 20 | printf("Number of Pages : "); 21 | scanf("%d", &pCount); 22 | printf("Enter the reference string\n"); 23 | for (i = 0; i < pCount; ++i) { 24 | scanf("%d", &pages[i]); 25 | } 26 | LRU(pages, frames, time, fCount, pCount); 27 | 28 | return 0; 29 | } 30 | 31 | void LRU(int pages[], int frames[], int time[], int fCount, int pCount) { 32 | printf("\nRef.String |\tFrames\n"); 33 | printf("-------------------------------\n"); 34 | 35 | int i, j, k, pos, flag, faultCount, counter, queue; 36 | counter = 0, queue = 0, faultCount = 0; 37 | 38 | for (i = 0; i < pCount; ++i) { 39 | flag = 0; 40 | printf(" %d\t|\t", pages[i]); 41 | // check if already present 42 | for (j = 0; j < fCount; ++j) { 43 | if (frames[j] == pages[i]) { 44 | flag = 1; // this means that the character is already there in one of the frames. 45 | counter++; 46 | time[j] = counter; // update occurance 47 | printf(" Hit\n\n"); 48 | break; 49 | } 50 | } 51 | // push if there is free space 52 | if ((flag == 0) && (queue < fCount)) { // empty frames[] spaces 53 | faultCount++; 54 | counter++; 55 | frames[queue] = pages[i]; 56 | time[queue] = counter; 57 | queue++; // queue is to keep the count of number of frames which have been filled. 58 | } 59 | // replacement as frames[] is full 60 | else if ((flag == 0) && (queue == fCount)) { // the frames are full now 61 | faultCount++; 62 | counter++; 63 | pos = findLRU(time, fCount); 64 | frames[pos] = pages[i]; 65 | time[pos] = counter; 66 | } 67 | // printing 68 | if (flag == 0) { 69 | for (k = 0; k < fCount; ++k) { 70 | printf("%d ", frames[k]); 71 | } 72 | printf("\n\n"); 73 | } 74 | } 75 | printf("Total Page Faults = %d\n\n", faultCount); 76 | } 77 | 78 | int findLRU(int time[], int fCount) { 79 | int k, min, pos; 80 | pos = 0; 81 | min = time[0]; 82 | for (k = 1; k < fCount; ++k) { 83 | if (time[k] < min) { 84 | min = time[k]; 85 | pos = k; 86 | } 87 | } 88 | return pos; 89 | } 90 | 91 | /* OUTPUT 92 | 93 | Number of Frames : 3 94 | Number of Pages : 20 95 | Enter the reference string 96 | 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1 97 | 98 | Ref.String | Frames 99 | ------------------------------- 100 | 7 | 7 -1 -1 101 | 102 | 0 | 7 0 -1 103 | 104 | 1 | 7 0 1 105 | 106 | 2 | 2 0 1 107 | 108 | 0 | Hit 109 | 110 | 3 | 2 0 3 111 | 112 | 0 | Hit 113 | 114 | 4 | 4 0 3 115 | 116 | 2 | 4 0 2 117 | 118 | 3 | 4 3 2 119 | 120 | 0 | 0 3 2 121 | 122 | 3 | Hit 123 | 124 | 2 | Hit 125 | 126 | 1 | 1 3 2 127 | 128 | 2 | Hit 129 | 130 | 0 | 1 0 2 131 | 132 | 1 | Hit 133 | 134 | 7 | 1 0 7 135 | 136 | 0 | Hit 137 | 138 | 1 | Hit 139 | 140 | Total Page Faults = 12 141 | 142 | */ -------------------------------------------------------------------------------- /Virtual Memory/LRU/README.md: -------------------------------------------------------------------------------- 1 | ## Program for LRU page-replacement algorithm 2 | 3 | [![solution](https://img.shields.io/badge/View-Solution-blue.svg?logo=appveyor&longCache=true&style=for-the-badge)](https://github.com/KTU-CSE/System-Software-lab/blob/master/Virtual%20Memory/LRU/12.lru.c) 4 | [![raw](https://img.shields.io/badge/-raw-green.svg?logo=appveyor&longCache=true&style=for-the-badge)](https://github.com/KTU-CSE/System-Software-lab/raw/master/Virtual%20Memory/LRU/12.lru.c) 5 | [![output](https://img.shields.io/badge/-output-ff69b4.svg?logo=appveyor&longCache=true&style=for-the-badge)](https://github.com/KTU-CSE/System-Software-lab/blob/master/Virtual%20Memory/LRU/README.md#output) 6 | 7 | In Least Recently Used (LRU) algorithm is a Greedy algorithm where the page to be replaced is least recently used. 8 | 9 | Least Recently Used (LRU) page replacement algorithm works on the concept that the pages that are heavily used in previous instructions are likely to be used heavily in next instructions. And the page that are used very less are likely to be used less in future. Whenever a page fault occurs, the page that is least recently used is removed from the memory frames. Page fault occurs when a referenced page in not found in the memory frames. 10 | 11 | In simple words, on a page fault, the frame that has been in memory the longest is replaced. 12 | 13 |
14 | 15 | **Implementation :** 16 | 17 | Let capacity be the number of pages that 18 | memory can hold. Let set be the current 19 | set of pages in memory. 20 | 21 | - Start traversing the pages. 22 | - If set holds less pages than capacity. 23 | - Insert page into the set one by one until 24 | the size of set reaches capacity or all 25 | page requests are processed. 26 | - Simultaneously maintain the recent occurred 27 | index of each page in a map called indexes. 28 | - Increment page fault 29 | - Else 30 | - If current page is present in set, do nothing. 31 | - Else 32 | - Find the page in the set that was least 33 | recently used. We find it using index array. 34 | We basically need to replace the page with 35 | minimum index. 36 | - Replace the found page with current page. 37 | - Increment page faults. 38 | - Update index of current page. 39 | 40 | - Return page faults. 41 | 42 |
43 | 44 | LRU is a marking algorithm, so it is -competitive. 45 | 46 | ## Output 47 | 48 | ![output_img](/.github/out_img/p_12_out.png) 49 | 50 | Reference 51 | 52 | - [Wikipedia](https://en.wikipedia.org/wiki/Page_replacement_algorithm) 53 | - [TheCrazyProgrammer](https://www.thecrazyprogrammer.com/2016/11/lru-page-replacement-algorithm-c.html) -------------------------------------------------------------------------------- /Virtual Memory/README.md: -------------------------------------------------------------------------------- 1 | ### Page Replacement Algorithms 2 | 3 | - [FIFO Algorithm](https://github.com/KTU-CSE/System-Software-lab/tree/master/Virtual%20Memory/FIFO/README.md) 4 | - [LRU Algorithm](https://github.com/KTU-CSE/System-Software-lab/tree/master/Virtual%20Memory/LRU/README.md) 5 | - [LFU Algorithm](#) 6 | 7 |
8 | 9 | ### Page Replacement 10 | 11 | In operating systems that use paging for memory management, page replacement algorithm are needed to decide which page needed to be replaced when new page comes in. Whenever a new page is referred and not present in memory, page fault occurs and Operating System replaces one of the existing pages with newly needed page. 12 | 13 | When the page that was selected for replacement and paged out is referenced again it has to be paged in (read in from disk), and this involves waiting for I/O completion. 14 | 15 | This determines the quality of the page replacement algorithm: **the less time waiting for page-ins, the better the algorithm.** 16 | 17 | A page replacement algorithm looks at the limited information about accesses to the pages provided by hardware, and tries to guess which pages should be replaced to minimize the total number of page misses, while balancing this with the costs (primary storage and processor time) of the algorithm itself. -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | title: System-Software-lab 2 | description: B.Tech CSE S5 System Software Lab, College of Engineering. Adoor 3 | show_downloads: true 4 | theme: jekyll-theme-cayman 5 | --------------------------------------------------------------------------------