C/ C++ Stats:

How to support the site


Site Wide Message: (current site time 7/29/2010 9:07:01 PM EDT)
  • We want your input! One of our sponsors wants to know your opinion about development related issues. Click here to tell us what you think.
  • Are you an emerging/young developer (aged 18-30)? If so, would you like the chance to affect future developer tools and products?
    If so, then click here to give your feedback.
 

Priority Scheduling Algorithm

Print
Email
VB icon
Submitted on: 3/11/2010 8:01:46 PM
By: Mahmud_Hasan 
Level: Beginner
User Rating: By 1 Users
Compatibility:C

Users have accessed this code  2349 times.
 
(About the author)
 
     This code solves the problem of priority scheduling algorithm of operating systems. I guessed that no two processes may have same priority. A gantt chart has been shown for easy understanding of the outcomes. Any improvement on this code is highly appreciable.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: Priority Scheduling Algorithm
// Description:This code solves the problem of priority scheduling algorithm of operating systems. I guessed that no two processes may have same priority. A gantt chart has been shown for easy understanding of the outcomes. Any improvement on this code is highly appreciable.
// By: Mahmud_Hasan
//
// Inputs:It asks for no of processes and their burst times along with their priorities.
//
// Returns:Nothing
//
// Side Effects:Nothing
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=13081&lngWId=3//for details.//**************************************

#include<iostream.h>
void main()
{
	int priority[20],btime[20],wtime[20],process[20],priority_2[20], sort_p[20],btime2[20];
	int n,i,j;
	cout<<"Please enter the number of processes:";
	cin>>n;
	cout<<endl;
	for(i=0;i<n;i++)
	{
		cout<<"Enter Busrt Time for Process P"<<i+1<<":";
		cin>>btime[i];
		cout<<"Enter Priority for Process P"<<i+1<<":";
		cin>>priority[i];
		cout<<endl;
		priority_2[i]=priority[i];
		process[i]=i+1;
	}
	//sorting priority
	for(i=0;i<n-1;i++)
	{
		for(j=n-1;i<j;j--)
		{
			if(priority[j]<priority[j-1])
			{
				double t;
				t=priority[j];
				priority[j]=priority[j-1];
				priority[j-1]=t;
			}
		}
	}
	//sorting processes according to priority
	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
		{
			if(priority[i]==priority_2[j])
			{
				sort_p[i]=j+1;
				btime2[i]=btime[j];
			}
		}
	}
	cout<<endl<<"Process ---> Burst Time"<<endl<<endl;
	for(i=0;i<n;i++)
	{
		cout<<""<<sort_p[i]<<"\t--->"<<"\t "<<btime2[i]<<endl;
	}
	//finding waiting time
	cout<<"\n\n\n\t\t\t::Time Calculation::\n\n\n";
	int sum=0;
	for(i=0;i<n;i++)
	{
		if(i==0)
			wtime[i]=0;
		else
		{
			sum+=btime2[i-1];
			wtime[i]=sum;
		}
		cout<<"waiting time for process p"<<sort_p[i]<<": "<<wtime[i]<<endl;
	}
	wtime[i]=sum+btime2[i-1];
	sum=0;
	for(i=0;i<n;i++)
	{
		sum+=wtime[i];
	}
	double avg=(double)sum/(double)n;
	cout<<endl<<endl<<"Average Waiting Time: "<<avg<<" milliseconds.";
	//drawing gantt chart....
	cout<<"\n\n\n\t\t\t::GANTT CHART::\n\n";
	cout<<endl<<"---------------------------------------------------------------------------"<<endl;
	for(i=0;i<n;i++)
		cout<<"|\tP"<<sort_p[i]<<"\t";
	//cout<<"|";
	cout<<endl<<"---------------------------------------------------------------------------"<<endl;
	cout<<endl;
	for(i=0;i<=n;i++)
	{
		if(i==n-1)
			cout<<wtime[i]<<"\t";
		else
			cout<<wtime[i]<<"\t\t";
	}
	cout<<endl<<"---------------------------------------------------------------------------"<<endl;
	cout<<endl;
}
/*
sample input:
5
10
3
1
1
2
4
1
5
5
2
*/

 
 Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:
 
Your Vote!

What do you think of this code(in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments

 There are no comments on this submission.
 
Add Your Feedback!

Note:Not only will your feedback be posted, but an email will be sent to the code's author from the email account you registered on the site, so you can correspond directly.

NOTICE: The author of this code has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular code, please click here.
 
To post feedback, first please login.