0% found this document useful (0 votes)
29 views19 pages

Notes 241115 173837

Uploaded by

aryansunil01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views19 pages

Notes 241115 173837

Uploaded by

aryansunil01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

#include <stdio.

h>

int i, n;
float tatAvg, wtAvg;

void read(int b[]) {


for (i = 0; i < n; ++i) {
printf("Enter the burst time of process %d: ", i);
scanf("%d", &b[i]);
}
}

void findWaitingtime(int b[], int wt[]) {


wt[0] = 0;
int wtSum = 0;
for (i = 1; i < n; ++i) {
wt[i] = wt[i - 1] + b[i - 1];
wtSum += wt[i];
}
wtAvg = (float)wtSum / n;
}

void findTurnAroundtime(int tat[], int b[], int wt[]) {


int tatSum = 0;
for (i = 0; i < n; ++i) {
tat[i] = b[i] + wt[i];
tatSum += tat[i];
}
tatAvg = (float)tatSum / n;
}

void display(int b[], int wt[], int tat[]) {


printf("Process\tBurstTime WaitingTime TurnAroundTime\n");
for (i = 0; i < n; ++i) {
printf("%d\t%d\t\t%d\t%d\n", i, b[i], wt[i], tat[i]);
}
printf("average waiting time: %f", wtAvg);
printf("\naverage turnaround time: %f", tatAvg);
printf("\n");
}

void calcTime(int b[]) {


int wt[20], tat[20];
findWaitingtime(b, wt);
findTurnAroundtime(tat, b, wt);
display(b, wt, tat);
}

int main() {

int b[20];
printf("Number of Processes:");
scanf("%d", &n);
read(b);
calcTime(b);
return 0;
}
#include <stdio.h>

int i, j, n, pId[20];
float tatAvg, wtAvg;

void swap(int *a, int *b) {


int temp = *a;
*a = *b;
*b = temp;
}

void sort(int b[]) {

for (i = 0; i < n - 1; ++i) {


for (j = 0; j < n - 1 - i; ++j) {
if (b[j] > b[j + 1]) {
swap(&b[j], &b[j + 1]);
swap(&pId[j], &pId[j + 1]);
}
}
}
}

void read(int b[]) {


for (i = 0; i < n; ++i) {
printf("Enter the burst time of process %d: ", i);
scanf("%d", &b[i]);
pId[i] = i + 1; /* process 1, process 2,... */
}
sort(b);
}

void findWaitingtime(int b[], int wt[]) {


wt[0] = 0;
int wtSum = 0;
for (i = 1; i < n; ++i) {
wt[i] = wt[i - 1] + b[i - 1];
wtSum += wt[i];
}
wtAvg = (float)wtSum / n;
}

void findTurnAroundtime(int tat[], int b[], int wt[]) {


int tatSum = 0;
for (i = 0; i < n; ++i) {
tat[i] = b[i] + wt[i];
tatSum += tat[i];
}
tatAvg = (float)tatSum / n;
}

void display(int b[], int wt[], int tat[]) {


printf("Process\tBurstTime WaitingTime TurnAroundTime\n");
for (i = 0; i < n; ++i) {
printf("%d\t%d\t\t%d\t%d\n", pId[i], b[i], wt[i], tat[i]);
}
printf("average waiting time: %f", wtAvg);
printf("\naverage turnaround time: %f", tatAvg);
printf("\n");
}

void calcTime(int b[]) {


int wt[20], tat[20];
findWaitingtime(b, wt);
findTurnAroundtime(tat, b, wt);
display(b, wt, tat);
}

void main() {
int b[20];
printf("Number of Processes:");
scanf("%d", &n);
read(b);
calcTime(b);
#include <stdio.h>

int i, n, quantum; /* give a time quantum */

void read(int b[]) {


for (i = 0; i < n; ++i) {
printf("Enter the burst time of process %d: ", i);
scanf("%d", &b[i]);
}
}

void findWaitingtime(int b[], int wt[]) {


int b_rem[20];
for (i = 0; i < n; ++i) {
b_rem[i] = b[i]; /* Create a copy of the burst time array */
}
int time = 0; /* initialize time as 0 */

while (1)
{
/* Traverse */

int flag = 0;

for (i = 0; i < n; ++i) {

if (b_rem[i] > 0) { /* continue only if burst time is greater than 0 */


flag = 1; /* there is a pending process */
if (b_rem[i] > quantum) {
time += quantum; /* shows how much time a process has been processed */
b_rem[i] -= quantum; /* Decrease the burst_time of current process by quantum
*/
}
else { /* If burst time is smaller than or equal to quantum. Last cycle for
this process */
time += b_rem[i]; /* shows how much time a process has been processed */
wt[i] = time - b[i]; /* Waiting time is current time minus time used by this process
*/
b_rem[i] = 0; /* fully executed, so remaining burst time=0 */
}
}
}
if (flag == 0) { /* there is no pending process */
break;
}
}
}

void findTurnAroundtime(int tat[], int b[], int wt[]) {


for (i = 0; i < n; ++i) {
tat[i] = b[i] + wt[i];
}
}

void display(int b[], int wt[], int tat[]) {


int wtSum = 0, tatSum = 0;
printf("Process\tBurstTime WaitingTime TurnAroundTime\n");
for (i = 0; i < n; ++i) {
wtSum += wt[i];
tatSum += tat[i];
printf("%d\t%d\t\t%d\t%d\n", i, b[i], wt[i], tat[i]);
}
printf("average waiting time: %f", (float)wtSum / n);
printf("\naverage turnaround time: %f", (float)tatSum / n);
printf("\n");
}

void calcTime(int b[], int wt[], int tat[]) {


findWaitingtime(b, wt);
findTurnAroundtime(tat, b, wt);
display(b, wt, tat);
}

void main()
{
int b[20], tat[20], wt[20];
printf("Number of Processes:");
scanf("%d", &n);
read(b);
printf("Enter time quantum:");
scanf("%d", &quantum);
calcTime(b, wt, tat);
}
#include <stdio.h>

int i, j, n;
float tatAvg, wtAvg;

struct Process {
int pId;
int bt;
int priority;
};

void read(struct Process p[]) {


printf("\n");
for (i = 0; i < n; ++i) {
p[i].pId = i + 1;
printf("Enter the burst time for process %d: ", p[i].pId);
scanf("%d", &p[i].bt);
printf("Enter the priority: ");
scanf("%d", &p[i].priority);
}
}

void display(struct Process p[], int wt[], int tat[]) {


printf("\nProcessID | BurstTime | Priority | Waiting time | Turn Around Time\n");
for (i = 0; i < n; ++i) {
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]);
}
printf("average waiting time: %f", wtAvg);
printf("\naverage turnaround time: %f", tatAvg);
printf("\n");
}

void sort(struct Process p[]) {


struct Process temp;
for (i = 0; i < n - 1; ++i) {
for (j = 0; j < n - 1 - i; ++j) {
if (p[j].priority > p[j + 1].priority) {
temp = p[j];
p[j] = p[j + 1];
p[j + 1] = temp;
}
}
}
}

void findWaitingTime(struct Process p[], int wt[]) {


wt[0] = 0;
int wtSum = 0;
for (i = 1; i < n; ++i) {
wt[i] = wt[i - 1] + p[i - 1].bt;
wtSum += wt[i];
}
wtAvg = (float)wtSum / n;
}
void findTurnAroundtime(struct Process p[], int tat[], int wt[]) {
int tatSum = 0;
for (i = 0; i < n; ++i) {
tat[i] = p[i].bt + wt[i];
tatSum += tat[i];
}
tatAvg = (float)tatSum / n;
}

int main()
{
struct Process p[20]; /* Array object for the structure */
int wt[20], tat[20];
printf("Number of Processes:");
scanf("%d", &n);
read(p);
sort(p);
findWaitingTime(p, wt);
findTurnAroundtime(p, tat, wt);
display(p, wt, tat);
return 0;
#include <stdio.h>

int n, m;
void read(int alloc[][10], int max[][10], int avail[10], int need[][10], int n, int m);
void display(int temp[][10], int n, int m);
int safety(int alloc[][10], int avail[10], int need[][10], int n, int m);

int main()
{
int alloc[10][10], max[10][10], avail[10], need[10][10];

printf("DEADLOCK AVOIDANCE USING BANKER'S ALGORITHM\n");


read(alloc, max, avail, need, n, m);
return 0;
}
//Banker's Algorithm
void read(int alloc[][10], int max[][10], int avail[10], int need[][10], int n, int m) {

int i, j;
printf("Enter total number of processes: ");
scanf("%d", &n);
printf("Enter total number of resources: ");
scanf("%d", &m);

for (i = 0; i < n; i++) {


printf("\nProcess %d\n", i + 1);
printf("Allocation Resource: ");
for (j = 0; j < m; ++j) {
scanf("%d", &alloc[i][j]);
}
printf("Maximum Resource: ");
for (j = 0; j < m; ++j) {
scanf("%d", &max[i][j]);
}
}

printf("\nAvailable System Resources: ");


for (i = 0; i < m; ++i) {
scanf("%d", &avail[i]);
}
// Calculating Need matrix
for (i = 0; i < n; ++i) {
for (j = 0; j < m; ++j) {
need[i][j] = max[i][j] - alloc[i][j]; /* need = max - allocated */
}
}

printf("\nProcess\tCurrently-Allocated-Resources\n");
display(alloc, n, m);
printf("\nProcess\tMaximum-Resources\n");
display(max, n, m);
printf("\nProcess\tPossibly-Needed-Resources\n");
display(need, n, m);

safety(alloc, avail, need, n, m);


}

//Safety algorithm

/* available >= need, then available += allocated */

int safety(int alloc[][10], int avail[10], int need[][10], int n, int m) {

int i, j, k, index = 0;
int isDone[10], sequence[10], count = 0; /* array to store safety sequence */

for (i = 0; i < n; ++i) {


isDone[i] = 0; /* array-> |0|0|0|0|0| */
}
for (k = 0; k < n; ++k) { /* Iterating for possible deadlock to prevent infinite loop
*/

// main process starts


for (i = 0; i < n; ++i)
{
if (isDone[i] == 0) { /* pending */
for (j = 0; j < m; ++j)
{
if (avail[j] >= need[i][j]) {
continue;
}
else {
break;
}
}
if (j == m) { /* True: avail >= need */
for (j = 0; j < m; ++j) {
avail[j] += alloc[i][j];
}
count++; /* done process's count */
isDone[i] = 1;
sequence[count - 1] = i + 1; /* safe state sequence */
}
}
}

if (count == n) { /* safe state */


printf("\nIT'S A SAFE STATE\n");
printf("The safe sequence is\n");
int i;
for (i = 0; i < n; ++i) {
printf("P%d ", sequence[i]);
}
printf("\n");
break;
}
// main process ends

} /* deadlock check loop ends */

if (count != n) {
printf("\nDeadlock has occured.\n"); /* deadlock */
}
}

void display(int temp[][10], int n, int m)


{
int i, j;
for (i = 0; i < n; ++i) {
printf("P%d", i + 1);
for (j = 0; j < m; ++j) {
printf("\t%d", temp[i][j]);
}
printf("\n");
}
printf("\n");
}
#include<stdio.h>
#include<stdlib.h>

int main() {
int diskQueue[20], n, i, seekTime=0, diff;
printf("Enter the size of Queue: ");
scanf("%d", &n);
printf("Enter the Queue: ");
for(i=1;i<=n;i++) { /* head element to be read */
scanf("%d",&diskQueue[i]);
}
printf("Enter the initial head position: ");
scanf("%d", &diskQueue[0]); /* head element */
printf("\nMovement of Cylinders\n");
for(i=0;i<n;i++) {
diff= abs(diskQueue[i+1] - diskQueue[i]); /* abs( ) function in C returns the absolute
value of an integer, which is always positive. */
seekTime+= diff;
printf("Move from %d to %d with seek time %d\n", diskQueue[i], diskQueue[i+1], diff);
}
printf("\nTotal Seek Time: %d", seekTime);
printf("\nAverage Seek Time = %f",(float) seekTime/n);
printf("\n");
return 0;
}
#include <stdio.h>

void fifo(int[], int[], int, int);

int main()
{
int i, pCount, fCount, pages[30], frames[20];
printf("Number of Frames : ");
scanf("%d", &fCount);
// create frames array will null value
for (i = 0; i < fCount; ++i) {
frames[i] = -1;
}
printf("Number of Pages : ");
scanf("%d", &pCount);
printf("Enter the reference string\n");
for (i = 0; i < pCount; ++i) {
scanf("%d", &pages[i]);
}
// call the function
fifo(pages, frames, pCount, fCount);

return 0;
}

void fifo(int pages[], int frames[], int pCount, int fCount) {


printf("\nRef.String |\tFrames\n");
printf("-------------------------------\n");
int i, j, k, flag, faultCount = 0, queue = 0;
for (i = 0; i < pCount; ++i) {
printf(" %d\t|\t", pages[i]);
flag = 0;
for (j = 0; j < fCount; ++j) {
if (frames[j] == pages[i]) { // compare with string in str[]
flag = 1;
printf(" Hit");
break;
}
}
if (flag == 0) { // not present in frames
frames[queue] = pages[i];
faultCount++;
queue = (queue + 1) % fCount; // Queue position in circular way
// display
for (k = 0; k < fCount; ++k) {
printf("%d ", frames[k]);
}
}
printf("\n\n");
}
printf("Total Page Faults = %d\n", faultCount);
}
#include <stdio.h>

void LRU(int[], int[], int[], int, int);


int findLRU(int[], int);

int main() {
int i, pCount, fCount, pages[30], frames[20], time[20];
printf("Number of Frames : ");
scanf("%d", &fCount);
// create frames array will null values
for (i = 0; i < fCount; ++i) {
frames[i] = -1;
}
printf("Number of Pages : ");
scanf("%d", &pCount);
printf("Enter the reference string\n");
for (i = 0; i < pCount; ++i) {
scanf("%d", &pages[i]);
}
LRU(pages, frames, time, fCount, pCount);

return 0;
}

void LRU(int pages[], int frames[], int time[], int fCount, int pCount) {
printf("\nRef.String |\tFrames\n");
printf("-------------------------------\n");

int i, j, k, pos, flag, faultCount, counter, queue;


counter = 0, queue = 0, faultCount = 0;

for (i = 0; i < pCount; ++i) {


flag = 0;
printf(" %d\t|\t", pages[i]);
// check if already present
for (j = 0; j < fCount; ++j) {
if (frames[j] == pages[i]) {
flag = 1; // this means that the character is already there in one of
the frames.
counter++;
time[j] = counter; // update occurance
printf(" Hit\n\n");
break;
}
}
// push if there is free space
if ((flag == 0) && (queue < fCount)) { // empty frames[] spaces
faultCount++;
counter++;
frames[queue] = pages[i];
time[queue] = counter;
queue++; // queue is to keep the count of number of frames
which have been filled.
}
// replacement as frames[] is full
else if ((flag == 0) && (queue == fCount)) { // the frames are full now
faultCount++;
counter++;
pos = findLRU(time, fCount);
frames[pos] = pages[i];
time[pos] = counter;
}
// printing
if (flag == 0) {
for (k = 0; k < fCount; ++k) {
printf("%d ", frames[k]);
}
printf("\n\n");
}
}
printf("Total Page Faults = %d\n\n", faultCount);
}

int findLRU(int time[], int fCount) {


int k, min, pos;
pos = 0;
min = time[0];
for (k = 1; k < fCount; ++k) {
if (time[k] < min) {
min = time[k];
pos = k;
}
}
return pos;
}

You might also like