Docsity

First Come First Search (FCFS) Algorithm, Assignments of Operating Systems

Partial preview of the text

Download First Come First Search (FCFS) Algorithm and more Assignments Operating Systems in PDF only on Docsity!

Department of Computer Science and Engineering

Lab Report

Course Title: Operating System Lab

Course Code: CSE- 3632

Submitted by:

Name: Mahiah Kulsum

ID: C

Sec: 6BF

Submitted to:

Shefayatuj Johara Chowdhury

Lecturer

Date : 26/06/

1. First Come First Search(FCFS)

include

include

using namespace std;

define s 100

struct Process { int id; int burstTime; int arrivalTime; }; bool compareArrivalTime ( const Process& a, const Process& b) { return a.arrivalTime < b.arrivalTime; } int main () { int n, wt[s], tat[s], avwt = 0 , avtat = 0 ; cout << "Total number of processes" << endl; cin >> n; Process processes[s]; cout << "Enter Process Burst Time and Arrival Time" << endl; for ( int i = 0 ; i < n; i++) { cout << "P[" << i + 1 << "]:"; processes[i].id = i + 1 ; cin >> processes[i].burstTime >> processes[i].arrivalTime; } // Sort processes based on arrival time sort(processes, processes + n, compareArrivalTime); wt[ 0 ] = 0 ; for ( int i = 1 ; i < n; i++) { wt[i] = processes[i - 1 ].burstTime + wt[i - 1 ]; } cout << "\nProcess\t\tBurst Time\tArrival Time\tWaiting Time\tTurnaround Time"; for ( int i = 0 ; i < n; i++) { tat[i] = processes[i].burstTime + wt[i]; avwt += wt[i]; avtat += tat[i]; cout << "\nP[" << processes[i].id << "]\t\t" << processes[i].burstTime << "\t\t" << processes[i].arrivalTime << "\t\t" << wt[i] << "\t\t" << tat[i]; }

2. Shortest Job First(SJF)

include

using namespace std; int main () { int p, i, j, sum = 0 , min, index; float awt = 0 , atat = 0 ; cout << "\nEnter number of process: "; cin >> p; int proc[p]; int *bt = new int [p]; int *wt = new int [p]; int *gc = new int [p]; int *tat = new int [p]; int *tmp = new int [p]; cout << "\nEnter Burst time:\n"; for (i = 0 ; i < p; i++) { cin >> bt[i]; tmp[i] = bt[i]; } sort(bt, bt + p); cout << "\n========================================================\n"; cout << "\t\tGantt Chart"; cout << "\n========================================================\n"; for (j = 0 ; j <= p; j++) { min = 100 ; for (i = 0 ; i < p; i++) { if (min > tmp[i] && tmp[i] != - 1 ) { min = tmp[i]; index = i;

gc[j] = sum; wt[j] = sum; sum += tmp[index]; tat[j] = sum; tmp[index] = - 1 ; //indicate process executed if (j == p) break ; cout << 'P' << index + 1 << " | "; proc[j] = index + 1 ; } cout << "\n------------------------------------------------------ --\n"; sum = 0 ; for (j = 0 ; j <= p; j++) { if (gc[j] < 10 ) cout << 0 ; cout << gc[j] << " "; sum += gc[j]; } cout << endl; atat = (sum *1.0) / p; cout << "\n------------------------------------------------------ --"; cout << "\nProcess\t\tCBT\tWaiting Time\tTurn Around Time"; cout << "\n------------------------------------------------------ --\n"; for (i = 0 ; i < p; i++) { cout << "P[" << proc[i] << "]\t\t" << bt[i] << "\t" << wt[i] << "\t\t" << tat[i] << endl; awt = awt + wt[i]; } awt = (awt *1.0) / p; cout << "\n\nTotal Waiting Time: " << awt;

cout << "Enter the time quantum: "; cin >> t; for (i = 0 ; i < n; i++) { b[i] = bur[i] / t; if ((bur[i] % t) != 0 ) b[i] += 1 ; m += b[i]; } cout << "\n\t\tRound Robin Scheduling\n"; cout << "\nGantt Chart\n"; for (i = 0 ; i < m; i++) cout << "--------"; cout << "\n"; a[ 0 ] = 0 ; while (j < m) { if (x == n - 1 ) x = 0 ; else x++; if (bur[x] >= t) { bur[x] - = t; a[j + 1 ] = a[j] + t; if (b[x] == 1 ) { p[s] = x; k[s] = a[j + 1 ]; s++; } j++; b[x] - = 1 ; cout << " P" << (x + 1 ) << " |"; } else if (bur[x] != 0 ) { a[j + 1 ] = a[j] + bur[x]; bur[x] = 0 ; if (b[x] == 1 ) { p[s] = x; k[s] = a[j + 1 ]; s++; }

j++; b[x] - = 1 ; cout << " P" << (x + 1 ) << " |"; } } cout << "\n"; for (i = 0 ; i < m; i++) cout << "--------"; cout << "\n"; for (j = 0 ; j <= m; j++) cout << a[j] << "\t"; for (i = 0 ; i < n; i++) { for (j = i + 1 ; j < n; j++) { //ascending order if (p[i] > p[j]) { temp = p[i]; p[i] = p[j]; p[j] = temp; temp = k[i]; k[i] = k[j]; k[j] = temp; } } } for (i = 0 ; i < n; i++) { wat[i] = k[i] - bur1[i]; tur[i] = k[i]; } for (i = 0 ; i < n; i++) { ttur += tur[i]; twat += wat[i]; } cout << "\n\n"; for (i = 0 ; i < 30 ; i++) cout << "-"; cout << "\nProcess\tBurst\tTrnd\tWait\n"; for (i = 0 ; i < 30 ; i++) cout << "-"; for (i = 0 ; i < n; i++)

4. Banker's Algorithm

include

using namespace std; int main () { int p,r,i,j,k; cout<<"Enter the number of processes: "; cin>>p; cout<<"Enter the number of resources: "; cin>>r; int alloc[p][r],max[p][r],avail[r]; cout<<"Enter the allocation matrix:"<>alloc[i][j]; } } cout<<"Enter the maximum matrix:"<>max[i][j]; } } cout<<"Enter the available resources:"<>avail[i]; } int finish[p],safe[p],a= 0 ; for (k= 0 ;k for (k= 0 ;k avail[j]) { f = 1 ; break ; } } if (f== 0 ) { safe[a++]=i; for (y= 0 ;y"; cout<< " P" < h++; pageFaults--; } } pageFaults++; //miss hole if((pageFaults <= frames) && (h == 0 )) { temp[m] = incomingStream[m]; } else if(h == 0 ) { temp[(pageFaults - 1 ) % frames] = incomingStream[m];// Replace the oldest page } cout << "\n"; cout << incomingStream[m] << "\t\t\t"; for(n = 0 ; n < frames; n++) { if(temp[n] != - 1 ) cout << temp[n] << "\t\t\t"; else cout << "- \t\t\t"; } } cout << "\n\nPage Faults:\t" << pageFaults; cout << "\nHits :\t" << pages - pageFaults; return 0 ; }

Output:

6. Least Recently Used (LRU) Page Replacement

include

using namespace std; int main () { int incomingStream[ 100 ]; int numPages, frames,h; cout<<"Enter the number of pages: "; cin>>numPages; cout<<"Enter the page references: "; for ( int i = 0 ; i < numPages; ++i) { cin>>incomingStream[i]; } cout<<"Enter the number of frames: "; cin>>frames; int pageFaults = 0 ; int hitCount = 0 ; int m, n, s, pages; pages = numPages; cout<<"Incoming"; for ( int i = 0 ; i < frames; ++i) { cout<<"Frame "<

Output:

7. Optimal Page Replacement(OPR)

include

using namespace std; int search(int key, int frame_items[], int frame_occupied) { for (int i = 0 ; i < frame_occupied; i++) if (frame_items[i] == key) return 1 ; return 0 ; } void printOuterStructure(int max_frames){ printf("Ref str "); for(int i = 0 ; i < max_frames; i++) printf("Frame%d ", i+ 1 ); }

void printCurrFrames(int item, int frame_items[], int frame_occupied, int max_frames){ cout << "\n" << item << "\t\t"; for(int i = 0 ; i < max_frames; i++){ if (i < frame_occupied) cout << frame_items[i] << "\t\t"; else cout << "- \t\t"; } } int predict(int ref_str[], int frame_items[], int refStrLen, int index, int frame_occupied) { int result = - 1 , farthest = index; for (int i = 0 ; i < frame_occupied; i++) { int j; for (j = index; j < refStrLen; j++) { if (frame_items[i] == ref_str[j]) { if (j > farthest) { farthest = j; result = i; } break; } } if (j == refStrLen) return i; } return (result == - 1 )? 0 : result; } void optimalPage(int ref_str[], int refStrLen, int frame_items[], int max_frames) { int frame_occupied = 0 ; printOuterStructure(max_frames);

Output: