Demo entry 6634327

CPU Scheduling

   

Submitted by anonymous on Aug 09, 2017 at 18:36
Language: C. Code size: 3.4 kB.

#include<stdio.h>
int n,i,j,wt,tq;
void swap(int *a, int *b)
{
	int temp;
	temp=*b;
	*b=*a;
	*a=temp;   
}
void fcfs()
{
	printf("Enter the number of processes: \n");
	scanf("%d",&n);
	int proc[n],bt[n],at[n];
	printf("Enter details of the processes:\n");
	for(i=0;i<n;i++){
		proc[i]=i;
		printf("\nProcess %d:\nArrival Time: ",i+1);
		scanf("%d",&at[i]);
		printf("Burst Time: ");
		scanf("%d",&bt[i]);
	}
	printf("\n\t\t**** FCFS ****\n\n");
	for(j=0;j<n;j++){
		for(i=0;i<n-1;i++){
			if(at[i+1]<at[i]){
				swap(&at[i],&at[i+1]);
				swap(&bt[i],&bt[i+1]);
				swap(&proc[i],&proc[i+1]);
			}
		}
	}
	printf("Process Name\tWait Time\tTurnaround Time\n");
	for(i=0,wt=0;i<n;i++){
		wt+=bt[i];
		printf("Process %d\t%dms\t\t%dms\n",proc[i]+1,wt-bt[i],wt);
	}
}
void sjf()
{
	printf("Enter the number of processes: \n");
	scanf("%d",&n);
	int proc[n],bt[n];
	printf("Enter details of the processes:\n");
	for(i=0;i<n;i++){
		proc[i]=i;
		printf("\nProcess %d:\n",i+1);
		printf("Burst Time: ");
		scanf("%d",&bt[i]);
	}
	printf("\n\t\t**** SJF ****\n\n");
	for(j=0;j<n;j++){
		for(i=0;i<n-1;i++){
			if(bt[i+1]<bt[i]){
				swap(&bt[i],&bt[i+1]);
				swap(&proc[i],&proc[i+1]);
			}
		}
	}
	printf("Process Name\tWait Time\tTurnaround Time\n");
	for(i=0,wt=0;i<n;i++){
		wt+=bt[i];
		printf("Process %d\t%dms\t\t%dms\n",proc[i]+1,wt-bt[i],wt);
	}
}
void rr()
{
	int clk=0,cmpl=0;
	printf("Enter the number of processes: \n");
	scanf("%d",&n);
	printf("Enter the time quantum: \n");
	scanf("%d",&tq);
	int at[n],bt[n],rt[n],rem=n;
	printf("Enter details of the processes:\n");
	for(i=0;i<n;i++){
		printf("\nProcess %d:\nArrival Time: ",i+1);
		scanf("%d",&at[i]);
		printf("Burst Time: ");
		scanf("%d",&bt[i]);
		rt[i]=bt[i];
	}
	printf("\n\t\t**** Round Robin ****\n\n");
	i=0;
	printf("Process Name\tTurnaround Time\tWaiting Time\n\n");
	while(rem!=0){
		if(rt[i]<=tq && rt[i]>0){
			clk=clk+rt[i];
			rt[i]=0;
			cmpl=1;
		}
		else if(rt[i]>0){
			rt[i]-=tq;
			clk+=tq;
		}

		if(rt[i]==0 && cmpl==1){
			rem--;
			printf("Process %d\t%dms\t\t%dms\n",i+1,clk-at[i],clk-at[i]-bt[i]); 
      		cmpl=0;
		}

		if(i==(n-1))
			i=0;
		else if(at[i+1]<=clk)
			i++;
		else
			i=0;
	}
}
void pb()
{
	printf("Enter the number of processes: \n");
	scanf("%d",&n);
	int proc[n],prt[n],bt[n];
	printf("Enter details of the processes:\n");
	for(i=0;i<n;i++){
		proc[i]=i;
		printf("\nProcess %d:\nPriority Level: ",i+1);
		scanf("%d",&prt[i]);
		printf("Burst Time: ");
		scanf("%d",&bt[i]);
	}
	printf("\n\t\t**** Priority Based ****\n\n");
	for(j=0;j<n;j++){
		for(i=0;i<n-1;i++){
			if(prt[i+1]<prt[i])
			{
				swap(&prt[i],&prt[i+1]);
				swap(&bt[i],&bt[i+1]);
				swap(&proc[i],&proc[i+1]);
			}
		}
	}
	printf("Process Name\tWait Time\tTurnaround Time\n");
	for(i=0,wt=0;i<n;i++){
		wt+=bt[i];
		printf("Process %d\t%dms\t\t%dms\n",proc[i]+1,wt-bt[i],wt);
	}
}
void main()
{
	int ch=0;
	while(ch!=5)
	{
		printf("\nEnter the CPU scheduling to simulate:\n\t1.FCFS\n\t2.SJF\n\t3.Round Robin");
		printf("\n\t4.Priority Based\n\t5.Exit\nEnter Choice: ");
		scanf("%d",&ch);
		switch(ch)
		{
			case 1: fcfs(); break;
			case 2: sjf(); break;
			case 3: rr(); break;
			case 4: pb(); break;
		}
	}	
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).