osslip17
/*
Write the simulation program for demand paging and show the page
scheduling and total number of page faults according the Optimal page
replacement algorithm. Assume the memory of n frames.
Reference String : 7, 5, 4, 8, 5, 7, 2, 3, 1, 3, 5, 9, 4, 6,
*/
#include<stdio.h>
int main()
{
int no_of_frames, no_of_pages, frames[10], pages[30], temp[10], flag1, flag2, flag3, i, j, k, pos, max, faults = 0;
printf("Enter number of frames: ");
scanf("%d", &no_of_frames);
printf("Enter number of pages: ");
scanf("%d", &no_of_pages);
printf("Enter page reference string: ");
for(i = 0; i < no_of_pages; ++i){
scanf("%d", &pages[i]);
}
for(i = 0; i < no_of_frames; ++i){
frames[i] = -1;
}
for(i = 0; i < no_of_pages; ++i){
flag1 = flag2 = 0;
for(j = 0; j < no_of_frames; ++j){
if(frames[j] == pages[i]){
flag1 = flag2 = 1;
break;
}
}
if(flag1 == 0){
for(j = 0; j < no_of_frames; ++j){
if(frames[j] == -1){
faults++;
frames[j] = pages[i];
flag2 = 1;
break;
}
}
}
if(flag2 == 0){
flag3 =0;
for(j = 0; j < no_of_frames; ++j){
temp[j] = -1;
for(k = i + 1; k < no_of_pages; ++k){
if(frames[j] == pages[k]){
temp[j] = k;
break;
}
}
}
for(j = 0; j < no_of_frames; ++j){
if(temp[j] == -1){
pos = j;
flag3 = 1;
break;
}
}
if(flag3 ==0){
max = temp[0];
pos = 0;
for(j = 1; j < no_of_frames; ++j){
if(temp[j] > max){
max = temp[j];
pos = j;
}
}
}
frames[pos] = pages[i];
faults++;
}
printf("\n");
for(j = 0; j < no_of_frames; ++j){
printf("%d\t", frames[j]);
}
}
printf("\n\nTotal Page Faults = %d", faults);
return 0;
}
/*
Write the program to simulate FCFS CPU-scheduling. The arrival time and
first CPU-burst of different jobs should be input to the system. Accept no. of
Processes, arrival time and burst time. The output should give Gantt chart,
turnaround time and waiting time for each process. Also find the average
waiting time and turnaround time.
*/
#include<stdio.h>
#include <stdlib.h>
#define MAX 100
typedef struct
{
int pid;
int burst_time;
int waiting_time;
int turnaround_time;
} Process;
void print_table(Process p[], int n);
void print_gantt_chart(Process p[], int n);
int main()
{
Process p[MAX];
int i, j, n;
int sum_waiting_time = 0, sum_turnaround_time;
printf("Enter total number of process: ");
scanf("%d", &n);
printf("Enter burst time for each process:\n");
for(i=0; i<n; i++) {
p[i].pid = i+1;
printf("P[%d] : ", i+1);
scanf("%d", &p[i].burst_time);
p[i].waiting_time = p[i].turnaround_time = 0;
}
// calculate waiting time and turnaround time
p[0].turnaround_time = p[0].burst_time;
for(i=1; i<n; i++) {
p[i].waiting_time = p[i-1].waiting_time + p[i-1].burst_time;
p[i].turnaround_time = p[i].waiting_time + p[i].burst_time;
}
// calculate sum of waiting time and sum of turnaround time
for(i=0; i<n; i++) {
sum_waiting_time += p[i].waiting_time;
sum_turnaround_time += p[i].turnaround_time;
}
// print table
puts(""); // Empty line
print_table(p, n);
puts(""); // Empty Line
printf("Total Waiting Time : %-2d\n", sum_waiting_time);
printf("Average Waiting Time : %-2.2lf\n", (double)sum_waiting_time / (double) n);
printf("Total Turnaround Time : %-2d\n", sum_turnaround_time);
printf("Average Turnaround Time : %-2.2lf\n", (double)sum_turnaround_time / (double) n);
// print Gantt chart
puts(""); // Empty line
puts(" GANTT CHART ");
puts(" *********** ");
print_gantt_chart(p, n);
return 0;
}
void print_table(Process p[], int n)
{
int i;
puts("+-----+------------+--------------+-----------------+");
puts("| PID | Burst Time | Waiting Time | Turnaround Time |");
puts("+-----+------------+--------------+-----------------+");
for(i=0; i<n; i++) {
printf("| %2d | %2d | %2d | %2d |\n"
, p[i].pid, p[i].burst_time, p[i].waiting_time, p[i].turnaround_time );
puts("+-----+------------+--------------+-----------------+");
}
}
void print_gantt_chart(Process p[], int n)
{
int i, j;
// print top bar
printf(" ");
for(i=0; i<n; i++) {
for(j=0; j<p[i].burst_time; j++) printf("--");
printf(" ");
}
printf("\n|");
// printing process id in the middle
for(i=0; i<n; i++) {
for(j=0; j<p[i].burst_time - 1; j++) printf(" ");
printf("P%d", p[i].pid);
for(j=0; j<p[i].burst_time - 1; j++) printf(" ");
printf("|");
}
printf("\n ");
// printing bottom bar
for(i=0; i<n; i++) {
for(j=0; j<p[i].burst_time; j++) printf("--");
printf(" ");
}
printf("\n");
// printing the time line
printf("0");
for(i=0; i<n; i++) {
for(j=0; j<p[i].burst_time; j++) printf(" ");
if(p[i].turnaround_time > 9) printf("\b"); // backspace : remove 1 space
printf("%d", p[i].turnaround_time);
}
printf("\n");
}