Untitled

 avatar
unknown
plain_text
9 months ago
1.3 kB
3
Indexable
#include <stdio.h>
#include <limits.h>

struct process {
	int at;  
	int bt;  
	int rt;  
	int wt;  
	int ct;  
	int tat; 
};

void main() {
	int i, n, q,curt = 0,r = 0,avgtat=0,avgwt=0;
	printf("Enter the number of processes: ");
	scanf("%d", &n);
	struct process p[n];
	for (i = 0; i < n; i++)
	{
		printf("Enter the arrival time and burst time of process %d:\n", i + 1);
		scanf("%d%d", &p[i].at, &p[i].bt);
		p[i].rt = p[i].bt;
		p[i].wt = 0;       
		p[i].ct = 0;       
		p[i].tat = 0;      
	}
	printf("Enter the time quantum: ");
	scanf("%d", &q);
	r = n;
	while (r > 0) {
		int cc = 0; 
		for (i=0;i<n;i++)
		{
			if (p[i].rt > 0 && p[i].at <= curt)
			{
				if (p[i].rt <= q)
				{
					curt += p[i].rt;
					p[i].ct = curt;
					p[i].tat = p[i].ct - p[i].at;
					p[i].wt = p[i].tat - p[i].bt;
					avgtat+=p[i].tat;
					avgwt+=p[i].wt;
					p[i].rt = 0;
					r--;
					cc = 1;
				} 
				else 
				{
					curt += q;
					p[i].rt -= q;
					cc = 1;
				}
			}
		}
		if (!cc)
		{
			curt++;
		}
	}
	printf("\nProcess\tAT\tBT\tCT\tTAT\tWT\n");
	for (i = 0; i < n; i++)
	{
		printf("%d\t%d\t%d\t%d\t%d\t%d\n", i + 1, p[i].at, p[i].bt, p[i].ct, p[i].tat, p[i].wt);
	}
	printf("Average Turn Around Time:%.2f\n",(float)avgtat/n);
	printf("Average Waiting Time:%.2f\n",(float)avgwt/n);
}
Editor is loading...
Leave a Comment