0% found this document useful (0 votes)
50 views

First Come First Serve Scheduling

The document contains code for implementing different CPU scheduling algorithms: 1. First Come First Serve scheduling which maintains a queue of processes and schedules them in the order of their arrival time. 2. Shortest Job First scheduling which prioritizes processes with the smallest burst time. 3. Round Robin scheduling which allows each process to run for a time quantum before switching to another process. 4. Priority scheduling which schedules processes based on their priority, with higher priority processes going first.

Uploaded by

UREZNOM
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

First Come First Serve Scheduling

The document contains code for implementing different CPU scheduling algorithms: 1. First Come First Serve scheduling which maintains a queue of processes and schedules them in the order of their arrival time. 2. Shortest Job First scheduling which prioritizes processes with the smallest burst time. 3. Round Robin scheduling which allows each process to run for a time quantum before switching to another process. 4. Priority scheduling which schedules processes based on their priority, with higher priority processes going first.

Uploaded by

UREZNOM
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 9

First Come First Serve Scheduling

#include<stdio.h> struct data { char process[3]; int atime; int btime; int start; int end; int finished; }list[100]; int time; int front=-1,rear=-1; PQ(); schedule(int); calc(int); main() { int n,i; printf("\nEnter the no of process:"); scanf("%d",&n); front=0; rear=n-1; for(i=0;i<n;i++) { printf("Enter the process name:"); scanf(" %s",&list[i].process); printf("Enter the arrival time:"); scanf(" %d",&list[i].atime); printf("Enter the burst time:"); scanf(" %d",&list[i].btime); } PQ(); schedule(n); calc(n); } PQ() { struct data t; int i,j; for(i=front;i<rear;i++) { for(j=i+1;j<=rear;j++) {

if(list[i].atime>list[j].atime) { t=list[i]; list[i]=list[j]; list[j]=t; } } } } schedule(int n) { int i; time=list[front].atime; while(front<n) { if(list[front].atime>time) time=list[front].atime; list[front].start=time; list[front].end=time+list[front].btime; time=list[front].end; list[front].finished=1; front++; } for(i=0; i<n;i++) printf("\n process : %s start: %d end: %d\n",list[i].process,list[i].start,list[i].end); } calc(int n) { int z=0,wt,tn; int i; for(i=0;i<n;i++) z=z+(list[i].start-list[i].atime); wt=z/n; printf("\nThe waiting time is %d",wt); z=0; for(i=0;i<n;i++) z=z+(list[i].end-list[i].atime); tn=z/n; printf("\nThe turnover time is %d",tn); }

Shortest Job First Scheduling


#include<stdio.h> #include<conio.h> struct data { char process[3]; int atime; int btime; int start; int end; int finished; }list[100],list1[100]; int time; int front=-1,rear=-1; void PQ(); void schedule(int); void calc(int); void main() { int n,i; clrscr(); printf("\nEnter the no of process\n"); scanf("%d",&n); front=0; rear=n-1; for(i=0;i<n;i++) { printf("\nEnter the process name\n"); scanf("%s",&list[i].process); printf("\nEnter the arrival time\n"); scanf("%d",&list[i].atime); printf("\nEnter the burst time\n"); scanf("%d",&list[i].btime); } for(i=0;i<n;i++) list1[i]=list[i]; PQ(); schedule(n); calc(n); getch(); } void PQ() { struct data t;

int i,j; for(i=front;i<rear;i++) { for(j=i+1;j<=rear;j++) { if(list[i].btime>list[j].btime && list[i].atime > list[j].atime) { t=list[i]; list[i]=list[j]; list[j]=t; } } } } void schedule(int n) { int i; time=list[front].atime; while(front<n) { list[front].start=time; list[front].end=time+list[front].btime; time=list[front].end; list[front].finished=1; front++; } for(i=0; i<n;i++) printf("process : %s start: %d end: %d\n", list[i].process,list[i].start,list[i].end); } void calc(int n) { int z=0,wt,tn; int i; for(i=0;i<n;i++) z=z+(list[i].start-list[i].atime); wt=z/n; printf("\nThe waiting time is %d",wt); z=0; for(i=0;i<n;i++) z=z+(list[i].end-list[i].atime); tn=z/n; printf("\nThe turnover time is %d",tn); }

Producer consumer problem:


#include<stdio.h> #include<conio.h> #define MAX 5 int buffer[MAX]; int front=-1,rear=-1; void main() { int choice,data,x; void produce(int); int consume(); clrscr(); do { puts("1-Producer"); puts("2-Consumer"); scanf("%d",&choice); switch(choice) { case 1: printf("\nEnter the data\n"); scanf("%d",&data); produce(data); break; case 2: x=consume(); if(x==-1) printf("\nNothing to remove\n"); else printf("\nThe data consumed is %d\n",x); break; default: printf("\nEnter either 1 or 2\n"); } printf("\nEnter ~ to exit\n"); }while(getch()!='~'); getch(); } void produce(int data) { if(rear+1==front || (front==0 && rear==MAX-1)) { printf("\nThe buffer is full"); return;

} if(front==-1) front=0; if(rear==MAX-1 && front!=0) rear=0; else rear++; buffer[rear]=data; } int consume() { int data; if(front==-1 && rear==-1) { printf("\nNo jobs in the buffer\n"); return -1; } else if(front==rear) { data=buffer[front]; front=rear=-1; } else if(front==MAX-1 ) { data=buffer[front]; front=0; } else { data=buffer[front]; front++; } return data; }

PRIORITY SCHEDULING
#include<stdio.h> #include<string.h> main() { struct process

{ char p[2]; int bt,wt,ft,pri; }s[10].t; int i,j,n,totw=0,totf=0; printf(enter no of process); scanf(%d,&n); printf(enter the process, burst time and priority); for(i=0;i<n;i++) scanf(\n%s\t%d\t%d,s[i].p,&s[i].bt,&s[i].pri); for(i=0;i<n-1;i++) for(j=i+1;j<n;j++) if(s[i].pri>s[j].pri) { t=s[i]; s[i]=s[j]; s[j]=t; } printf(\nThe process, bt, priority, wt and ft are); s[0].wt=0; for(i=0;i<n;i++) { s[i+1].wt=s[i].bt+s[i].wt; s[i].ft=s[i].wt+s[i].bt; totw+=s[i].wt; totf+=s[i].ft; printf(\n\n\t%s\t%d\t%d\t%d\t%d,s[i].p,s[i].bt,s[i].pri,s[i].wt,s[i].ft); } printf(avg wait time is %d,totw/n); printf(avg finish time is %d,totf/n); }

MEMORY MANAGEMENT
#include<stdio.h> main() { int a[10],p,p2,c,i,j,n,e; printf("Enter the number of items::"); scanf("%d",&n); printf("Enter the items:::::"); for(i=0;i<n;i++) scanf("%d",&a[i]);

printf("Enter the choice:"); printf("\n 1. Insertion \n 2. Deletion\n"); scanf("%d",&c); switch(c) { case 1: printf("Enter the position to be inserted:"); scanf("%d",&p); printf("Enter the item to be inserted"); scanf("%d",&e); for(i=n;i>=p;i--) a[i]=a[i-1]; a[i]=e; printf("The final list is:::"); for(i=0;i<=n;i++) printf("\t%d",a[i]); printf("\n"); break; case 2: printf("Enter the position to be deleted"); scanf("%d",&p2); for(i=p2;i<n;i++) a[i-1]=a[i]; n--; printf("the final list is::"); for(i=0;i<n;i++) printf("\t%d",a[i]); break; default: printf("Enter the choice correctly"); break; } }

ROUND ROBIN

#include<stdio.h> #include<conio.h> main() {

int t[10],a[10],b,s,f,k,q,i,tot,j,n; tot=0,s=0,k=0,b=0; clrscr(); printf("enter the no of process"); scanf("%d",&n); printf("enter the time quantum"); scanf("%d",&q); printf("enter time taken for each process"); for(i=0;i<n;i++) scanf("%d",&t[i]); for(i=0;i<n;i++) s=s+t[i]; while(tot<s) { if(i>=n) { i=a[k]; k++; } j=j+1; if(t[i]==q) { printf("process %d entered at %d \n"); tot=tot+q; printf("it ended at %d \n",tot); } if(t[i]<q) { printf("process %d "); tot=tot+q; t[i]=t[i]-q; printf("ended at %d ms \n",tot); a[b]=i; b++; } i++; } getch(); }

You might also like