Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Download as pdf or txt
Download as pdf or txt
You are on page 1of 38

1.

// C program for implementation of FCFS scheduling

#include<stdio.h>

// Function to find the waiting time for all processes

void findWaitingTime(int processes[], int n, int bt[], int wt[])

// waiting time for first process is 0

wt[0] = 0;

// calculating waiting time

for (int i = 1; i < n ; i++ )

wt[i] = bt[i-1] + wt[i-1] ;

// Function to calculate turn around time

void findTurnAroundTime(int processes[], int n, int bt[], int wt[], int tat[])

// calculating turnaround time by adding bt[i] + wt[i]

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

tat[i] = bt[i] + wt[i];

//Function to calculate average time

void findavgTime( int processes[], int n, int bt[])

int wt[n], tat[n], total_wt = 0, total_tat = 0;

//Function to find waiting time of all processes

findWaitingTime(processes, n, bt, wt);


//Function to find turn around time for all processes

findTurnAroundTime(processes, n, bt, wt, tat);

//Display processes along with all details

printf("Processes Burst time Waiting time Turn around time\n");

// Calculate total waiting time and total turn around time

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

total_wt = total_wt + wt[i];

total_tat = total_tat + tat[i];

printf(" %d ",(i+1));

printf(" %d ", bt[i]);

printf(" %d",wt[i]);

printf(" %d\n",tat[i]);

float s=(float)total_wt / (float)n;

float t=(float)total_tat / (float)n;

printf("Average waiting time = %f",s);

printf("\n");

printf("Average turn around time = %f ",t);

// Driver code

int main()

//process id's

int processes[] = {1, 2, 3};

int n = sizeof processes / sizeof processes[0];

//Burst time of all processes


int burst_time[] = {30, 6, 8};

findavgTime(processes, n, burst_time);

return 0;

Output:-

Processes Burst time Waiting time Turn around time

1 30 0 30

2 6 30 36

3 8 36 44

Average waiting time = 22.000000

Average turn around time = 36.666668

=== Code Execution Successful ===

2.

//Program for shortest job first in C

#include <stdio.h>

int main()

// Matrix for storing Process Id, Burst Time, Average Waiting Time & Average Turn Around
Time.

int A[100][4];

int i, j, n, total = 0, index, temp;

float avg_wt, avg_tat;

printf("Enter number of process: ");

scanf("%d", &n);

printf("Enter Burst Time:\n");

// User Input Burst Time and alloting Process Id.

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

printf("P%d: ", i + 1);

scanf("%d", &A[i][1]);
A[i][0] = i + 1;

// Sorting process according to their Burst Time.

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

index = i;

for (j = i + 1; j < n; j++)

if (A[j][1] < A[index][1])

index = j;

temp = A[i][1];

A[i][1] = A[index][1];

A[index][1] = temp;

temp = A[i][0];

A[i][0] = A[index][0];

A[index][0] = temp;

A[0][2] = 0;

// Calculation of Waiting Times

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

A[i][2] = 0;

for (j = 0; j < i; j++)

A[i][2] += A[j][1];

total += A[i][2];

avg_wt = (float)total / n;

total = 0;

printf("P BT WT TAT\n");

// Calculation of Turn Around Time and printing the data.

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

A[i][3] = A[i][1] + A[i][2];

total += A[i][3];
printf("P%d %d %d %d\n", A[i][0],

A[i][1], A[i][2], A[i][3]);

avg_tat = (float)total / n;

printf("Average Waiting Time= %f", avg_wt);

printf("\nAverage Turnaround Time= %f", avg_tat);

Output

Enter number of process: 5

Enter Burst Time:

P1: 2

P2: 3

P3: 7

P4: 9

P5: 2

P BT WT TAT

P1 2 0 2

P5 2 2 4

P2 3 4 7

P3 7 7 14

P4 9 14 23

Average Waiting Time= 5.400000

Average Turnaround Time= 10.000000

3.

//Program for shortest job first in C

#include <stdio.h>

int main()

// Matrix for storing Process Id, Burst Time, Average Waiting Time & Average Turn Around
Time.

int A[100][4];
int i, j, n, total = 0, index, temp;

float avg_wt, avg_tat;

printf("Enter number of process: ");

scanf("%d", &n);

printf("Enter Burst Time:\n");

// User Input Burst Time and alloting Process Id.

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

printf("P%d: ", i + 1);

scanf("%d", &A[i][1]);

A[i][0] = i + 1;

// Sorting process according to their Burst Time.

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

index = i;

for (j = i + 1; j < n; j++)

if (A[j][1] < A[index][1])

index = j;

temp = A[i][1];

A[i][1] = A[index][1];

A[index][1] = temp;

temp = A[i][0];

A[i][0] = A[index][0];

A[index][0] = temp;

A[0][2] = 0;

// Calculation of Waiting Times

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

A[i][2] = 0;

for (j = 0; j < i; j++)

A[i][2] += A[j][1];
total += A[i][2];

avg_wt = (float)total / n;

total = 0;

printf("P BT WT TAT\n");

// Calculation of Turn Around Time and printing the data.

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

A[i][3] = A[i][1] + A[i][2];

total += A[i][3];

printf("P%d %d %d %d\n", A[i][0],

A[i][1], A[i][2], A[i][3]);

avg_tat = (float)total / n;

printf("Average Waiting Time= %f", avg_wt);

printf("\nAverage Turnaround Time= %f", avg_tat);

Output

Enter number of process: 3

Enter Burst Time:

P1: 2

P2: 464

P3:

P BT WT TAT

P1 2 0 2

P2 4 2 6

P3 46 6 52

Average Waiting Time= 2.666667

Average Turnaround Time= 20.000000

4.

/* Round Robin Scheduling Program in C */


#include<stdio.h>

int main()

//Input no of processed

int n;

printf("Enter Total Number of Processes:");

scanf("%d", &n);

int wait_time = 0, ta_time = 0, arr_time[n], burst_time[n], temp_burst_time[n];

int x = n;

//Input details of processes

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

printf("Enter Details of Process %d \n", i + 1);

printf("Arrival Time: ");

scanf("%d", &arr_time[i]);
printf("Burst Time: ");

scanf("%d", &burst_time[i]);

temp_burst_time[i] = burst_time[i];

//Input time slot

int time_slot;

printf("Enter Time Slot:");

scanf("%d", &time_slot);

//Total indicates total time

//counter indicates which process is executed

int total = 0, counter = 0,i;

printf("Process ID Burst Time Turnaround Time Waiting Time\n");

for(total=0, i = 0; x!=0; )

// define the conditions


if(temp_burst_time[i] <= time_slot && temp_burst_time[i] > 0)

total = total + temp_burst_time[i];

temp_burst_time[i] = 0;

counter=1;

else if(temp_burst_time[i] > 0)

temp_burst_time[i] = temp_burst_time[i] - time_slot;

total += time_slot;

if(temp_burst_time[i]==0 && counter==1)

x--; //decrement the process no.

printf("\nProcess No %d \t\t %d\t\t\t\t %d\t\t\t %d", i+1, burst_time[i],


total-arr_time[i], total-arr_time[i]-burst_time[i]);

wait_time = wait_time+total-arr_time[i]-burst_time[i];

ta_time += total -arr_time[i];

counter =0;

if(i==n-1)

i=0;

else if(arr_time[i+1]<=total)

i++;

else

i=0;
}

float average_wait_time = wait_time * 1.0 / n;

float average_turnaround_time = ta_time * 1.0 / n;

printf("\nAverage Waiting Time:%f", average_wait_time);

printf("\nAvg Turnaround Time:%f", average_turnaround_time);

return 0;

5.

//Program for priority based algorithm in C

#include<stdio.h>

// structure representing a structure

struct priority_scheduling {

// name of the process

char process_name;

// time required for execution

int burst_time;

// waiting time of a process

int waiting_time;

// total time of execution

int turn_around_time;
// priority of the process

int priority;

};

int main() {

// total number of processes

int number_of_process;

// total waiting and turnaround time

int total = 0;

// temporary structure for swapping

struct priority_scheduling temp_process;

// ASCII numbers are used to represent the name of the process

int ASCII_number = 65;

// swapping position

int position;

// average waiting time of the process

float average_waiting_time;

// average turnaround time of the process

float average_turnaround_time;

printf("Enter the total number of Processes: ");

// get the total number of the process as input

scanf("%d", & number_of_process);


// initializing the structure array

struct priority_scheduling process[number_of_process];

printf("\nPlease Enter the Burst Time and Priority of each process:\n");

// get burst time and priority of all process

for (int i = 0; i < number_of_process; i++) {

// assign names consecutively using ASCII number

process[i].process_name = (char) ASCII_number;

printf("\nEnter the details of the process %c \n", process[i].process_name);

printf("Enter the burst time: ");

scanf("%d", & process[i].burst_time);

printf("Enter the priority: ");

scanf("%d", & process[i].priority);

// increment the ASCII number to get the next alphabet

ASCII_number++;

// swap process according to high priority

for (int i = 0; i < number_of_process; i++) {

position = i;

for (int j = i + 1; j < number_of_process; j++) {


// check if priority is higher for swapping

if (process[j].priority > process[position].priority)

position = j;

// swapping of lower priority process with the higher priority process

temp_process = process[i];

process[i] = process[position];

process[position] = temp_process;

// First process will not have to wait and hence has a waiting time of 0

process[0].waiting_time = 0;

for (int i = 1; i < number_of_process; i++) {

process[i].waiting_time = 0;

for (int j = 0; j < i; j++) {

// calculate waiting time

process[i].waiting_time += process[j].burst_time;

// calculate total waiting time

total += process[i].waiting_time;

// calculate average waiting time

average_waiting_time = (float) total / (float) number_of_process;

// assigning total as 0 for next calculations

total = 0;

printf("\n\nProcess_name \t Burst Time \t Waiting Time \t Turnaround Time\n");

printf("------------------------------------------------------------\n");
for (int i = 0; i < number_of_process; i++) {

// calculating the turnaround time of the processes

process[i].turn_around_time = process[i].burst_time + process[i].waiting_time;

// calculating the total turnaround time.

total += process[i].turn_around_time;

// printing all the values

printf("\t %c \t\t %d \t\t %d \t\t %d", process[i].process_name, process[i].burst_time,


process[i].waiting_time, process[i].turn_around_time);

printf("\n-----------------------------------------------------------\n");

// calculating the average turn_around time

average_turnaround_time = (float) total / (float) number_of_process;

// average waiting time

printf("\n\n Average Waiting Time : %f", average_waiting_time);

// average turnaround time

printf("\n Average Turnaround Time: %f\n", average_turnaround_time);

return 0;

7.

// C program for the producer and consumer approach

#include <stdio.h>

#include <stdlib.h>
// Initialize a mutex to 1

int mutex = 1;

// Number of full slots as 0

int full = 0;

// Number of empty slots as size of buffer

int empty = 10, x = 0;

// Function to produce an item and add it to the buffer

void producer()

// Decrease mutex value by 1

--mutex;

// Increase the number of full slots by 1

++full;

// Decrease the number of empty slots by 1

--empty;

// Item produced

x++;

printf("\nProducer produces" "item %d", x);

// Increase mutex value by 1

++mutex;

// Function to consume an item and remove it from buffer

void consumer()
{

// Decrease mutex value by 1

--mutex;

// Decrease the number of full slots by 1

--full;

// Increase the number of empty slots by 1

++empty;

printf("\nConsumer consumes " "item %d", x);

x--;

// Increase mutex value by 1

++mutex;

// Driver Code

int main()

int n, i;

printf("\n1. Press 1 for Producer" "\n2. Press 2 for Consumer" "\n3. Press 3 for Exit");

// Using '#pragma omp parallel for' can give wrong value due to synchronization issues.

// 'critical' specifies that code is executed by only one thread at a time i.e., only one thread enters
the critical section at a given time

#pragma omp critical

for (i = 1; i > 0; i++)

{
printf("\nEnter your choice:");

scanf("%d", &n);

// Switch Cases

switch (n)

case 1:

// If mutex is 1 and empty is non-zero, then it is possible to produce

if ((mutex == 1) && (empty != 0))

producer();

// Otherwise, print buffer is full

else

printf("Buffer is full!");

break;

case 2:

// If mutex is 1 and full is non-zero, then it is possible to consume

if ((mutex == 1) && (full != 0))

consumer();

// Otherwise, print Buffer is empty


else

printf("Buffer is empty!");

break;

// Exit Condition

case 3:

exit(0);

break;

8.

#include <pthread.h>

#include <semaphore.h>

#include <stdio.h>

#define N 5

#define THINKING 2

#define HUNGRY 1

#define EATING 0

#define LEFT (phnum + 4) % N

#define RIGHT (phnum + 1) % N

int state[N];

int phil[N] = { 0, 1, 2, 3, 4 };

sem_t mutex;

sem_t S[N];
void test(int phnum)

if (state[phnum] == HUNGRY

&& state[LEFT] != EATING

&& state[RIGHT] != EATING) {

// state that eating

state[phnum] = EATING;

sleep(2);

printf("Philosopher %d takes fork %d and %d\n",

phnum + 1, LEFT + 1, phnum + 1);

printf("Philosopher %d is Eating\n", phnum + 1);

// sem_post(&S[phnum]) has no effect

// during takefork

// used to wake up hungry philosophers

// during putfork

sem_post(&S[phnum]);

// take up chopsticks

void take_fork(int phnum)

sem_wait(&mutex);

// state that hungry

state[phnum] = HUNGRY;
printf("Philosopher %d is Hungry\n", phnum + 1);

// eat if neighbours are not eating

test(phnum);

sem_post(&mutex);

// if unable to eat wait to be signalled

sem_wait(&S[phnum]);

sleep(1);

// put down chopsticks

void put_fork(int phnum)

sem_wait(&mutex);

// state that thinking

state[phnum] = THINKING;

printf("Philosopher %d putting fork %d and %d down\n",

phnum + 1, LEFT + 1, phnum + 1);

printf("Philosopher %d is thinking\n", phnum + 1);

test(LEFT);

test(RIGHT);

sem_post(&mutex);
}

void* philosopher(void* num)

while (1) {

int* i = num;

sleep(1);

take_fork(*i);

sleep(0);

put_fork(*i);

int main()

int i;

pthread_t thread_id[N];

// initialize the semaphores

sem_init(&mutex, 0, 1);

for (i = 0; i < N; i++)

sem_init(&S[i], 0, 0);
for (i = 0; i < N; i++) {

// create philosopher processes

pthread_create(&thread_id[i], NULL,

philosopher, &phil[i]);

printf("Philosopher %d is thinking\n", i + 1);

for (i = 0; i < N; i++)

pthread_join(thread_id[i], NULL);

9.

/* Deadlock Avoidance*/

#include<stdio.h>

int main()

int n,r,i,j,k,p,u=0,s=0,m;

int block[10],run[10],active[10],newreq[10];

int max[10][10],resalloc[10][10],resreq[10][10];

int totalloc[10],totext[10],simalloc[10];

printf("Enter the no of processes:");

scanf("%d",&n);

printf("Enter the no ofresource classes:");

scanf("%d",&r);

printf("Enter the total existed resource in each class:");

for(k=1; k<=r; k++)

scanf("%d",&totext[k]);

printf("Enter the allocated resources:");


for(i=1; i<=n; i++)

for(k=1; k<=r; k++)

scanf("%d",&resalloc[i][k]);

printf("Enter the process making the new request:");

scanf("%d",&p);

printf("Enter the requested resource:");

for(k=1; k<=r; k++)

scanf("%d",&newreq[k]);

printf("Enter the process which are n blocked or running:");

for(i=1; i<=n; i++)

if(i!=p)

printf("process %d:\n",i+1);

scanf("%d%d",&block[i],&run[i]);

block[p]=0;

run[p]=0;

for(k=1; k<=r; k++)

j=0;

for(i=1; i<=n; i++)

totalloc[k]=j+resalloc[i][k];

j=totalloc[k];

for(i=1; i<=n; i++)

{
if(block[i]==1||run[i]==1)

active[i]=1;

else

active[i]=0;

for(k=1; k<=r; k++)

resalloc[p][k]+=newreq[k];

totalloc[k]+=newreq[k];

for(k=1; k<=r; k++)

if(totext[k]-totalloc[k]<0)

u=1;

break;

if(u==0)

for(k=1; k<=r; k++)

simalloc[k]=totalloc[k];

for(s=1; s<=n; s++)

for(i=1; i<=n; i++)

if(active[i]==1)

j=0;

for(k=1; k<=r; k++)

if((totext[k]-simalloc[k])<(max[i][k]-resalloc[i][k]))
{

j=1;

break;

if(j==0)

active[i]=0;

for(k=1; k<=r; k++)

simalloc[k]=resalloc[i][k];

m=0;

for(k=1; k<=r; k++)

resreq[p][k]=newreq[k];

printf("Deadlock willn't occur");

else

for(k=1; k<=r; k++)

resalloc[p][k]=newreq[k];

totalloc[k]=newreq[k];

printf("Deadlock will occur");

return 0;

10.
/* deadlock Prevention*/

#include<stdio.h>

#include<conio.h>

void main()

int allocated[15][15],max[15][15],need[15][15],avail[15],tres[15],work[15],flag[15];

int pno,rno,i,j,prc,count,t,total;

count=0;

//clrscr();

printf("\n Enter number of process:");

scanf("%d",&pno);

printf("\n Enter number of resources:");

scanf("%d",&rno);

for(i=1;i<=pno;i++)

flag[i]=0;

printf("\n Enter total numbers of each resources:");

for(i=1;i<= rno;i++)

scanf("%d",&tres[i]);

printf("\n Enter Max resources for each process:");

for(i=1;i<= pno;i++)

printf("\n for process %d:",i);

for(j=1;j<= rno;j++)

scanf("%d",&max[i][j]);

}
printf("\n Enter allocated resources for each process:");

for(i=1;i<= pno;i++)

printf("\n for process %d:",i);

for(j=1;j<= rno;j++)

scanf("%d",&allocated[i][j]);

printf("\n available resources:\n");

for(j=1;j<= rno;j++)

avail[j]=0;

total=0;

for(i=1;i<= pno;i++)

total+=allocated[i][j];

avail[j]=tres[j]-total;

work[j]=avail[j];

printf(" %d \t",work[j]);

do

for(i=1;i<= pno;i++)

for(j=1;j<= rno;j++)

need[i][j]=max[i][j]-allocated[i][j];

}
printf("\n Allocated matrix Max need");

for(i=1;i<= pno;i++)

printf("\n");

for(j=1;j<= rno;j++)

printf("%4d",allocated[i][j]);

printf("|");

for(j=1;j<= rno;j++)

printf("%4d",max[i][j]);

printf("|");

for(j=1;j<= rno;j++)

printf("%4d",need[i][j]);

prc=0;

for(i=1;i<= pno;i++)

if(flag[i]==0)

prc=i;

for(j=1;j<= rno;j++)

if(work[j]< need[i][j])

{
prc=0;

break;

if(prc!=0)

break;

if(prc!=0)

printf("\n Process %d completed",i);

count++;

printf("\n Available matrix:");

for(j=1;j<= rno;j++)

work[j]+=allocated[prc][j];

allocated[prc][j]=0;

max[prc][j]=0;

flag[prc]=1;

printf(" %d",work[j]);

while(count!=pno&&prc!=0);

if(count==pno)

printf("\nThe system is in a safe state!!");

else

printf("\nThe system is in an unsafe state!!");


getch();

11.

/FIFO/

#include<stdio.h>

int main()

int i,j,n,a[50],frame[10],no,k,avail,count=0;

printf("\n ENTER THE NUMBER OF PAGES:\n");

scanf("%d",&n);

printf("\n ENTER THE PAGE NUMBER :\n");

for(i=1;i<=n;i++)

scanf("%d",&a[i]);

printf("\n ENTER THE NUMBER OF FRAMES :");

scanf("%d",&no);

for(i=0;i<no;i++)

frame[i]= -1;

j=0;

printf("\tref string\t page frames\n");

for(i=1;i<=n;i++)

printf("%d\t\t",a[i]);

avail=0;

for(k=0;k<no;k++)

if(frame[k]==a[i])

avail=1;

if (avail==0)

frame[j]=a[i];

j=(j+1)%no;
count++;

for(k=0;k<no;k++)

printf("%d\t",frame[k]);

printf("\n");

printf("Page Fault Is %d",count);

return 0;

12.

/FIFO/

#include<stdio.h>

int main()

int i,j,n,a[50],frame[10],no,k,avail,count=0;

printf("\n ENTER THE NUMBER OF PAGES:\n");

scanf("%d",&n);

printf("\n ENTER THE PAGE NUMBER :\n");

for(i=1;i<=n;i++)

scanf("%d",&a[i]);

printf("\n ENTER THE NUMBER OF FRAMES :");

scanf("%d",&no);

for(i=0;i<no;i++)

frame[i]= -1;

j=0;

printf("\tref string\t page frames\n");

for(i=1;i<=n;i++)

printf("%d\t\t",a[i]);

avail=0;

for(k=0;k<no;k++)
if(frame[k]==a[i])

avail=1;

if (avail==0)

frame[j]=a[i];

j=(j+1)%no;

count++;

for(k=0;k<no;k++)

printf("%d\t",frame[k]);

printf("\n");

printf("Page Fault Is %d",count);

return 0;

13.

#include<stdio.h>

void optimal(int string[20],int n,int size)

//Creating array for block storage

int frames[n];

//Initializing each block with -1

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

frames[i]=-1;

//Index to insert element

int index=-1;

//Counters

int page_miss=0;
int page_hits=0;

//Pointer to indicate initially frames filled or not

int full=0;

//Traversing each symbol in fifo

for (int i=0;i<size;i++)

int symbol=string[i];

int flag=0;

for(int j=0;j<n;j++)

if (symbol==frames[j])

flag=1;

break;

if (flag==1)

printf("\nSymbol: %d Frame: ",symbol);

for (int j=0;j<n;j++)

printf("%d ",frames[j]);

page_hits+=1;

else

//Frames are still empty

if (full==0)
{

index=(index+1)%n;

frames[index]=symbol;

page_miss+=1;

printf("\nSymbol: %d Frame: ",symbol);

for (int j=0;j<n;j++)

printf("%d ",frames[j]);

//Frames filled or not

if (i==n-1)

full=1;

//Frames are full, now we can apply optimal page replacement

else

//First find the index to replace with

int pos=-1;

int index=-1;

//Traversing each symbol and checking their optimal possibility

for(int j=0;j<n;j++)

//Whether symbol in frame found or not in future cached frame

int found=0;

for (int k=i+1;k<size;k++)

//If symbol exists in cached string

if (frames[j]==string[k])

found=1;
if (pos<k)

pos=k;

index=j;

break;

//Symbol does not exist in cached frame

if (found==0)

pos=size;

index=j;

//Now assign symbol in lru position

frames[index]=symbol;

printf("\nSymbol: %d Frame: ",symbol);

for (int j=0;j<n;j++)

printf("%d ",frames[j]);

printf("\nPage hits: %d",page_hits);

printf("\nPage misses: %d",page_miss);

//Main function

int main(void)
{

int string[]={2, 3, 4, 2, 1, 3, 7, 5, 4, 3};

int no_frames=3;

int size=sizeof(string)/sizeof(int);

optimal(string,no_frames,size);

return 0;

You might also like