Sjf Scheduling

Shortest job First

Page 1 of 1

3 Replies - 25920 Views - Last Post: 14 December 2010 - 11:49 AM Rate Topic: -----

#1 sanjog  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 23-August 08

Sjf Scheduling

Post icon  Posted 24 August 2008 - 06:51 AM

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<alloc.h>

struct node
	{
		int process;
		int bt;
		int wt;
		struct node *next;
	};

void printll(struct node* start,float avg_wt)
	{
		   printf("ProcessNumber    Burst time    Waiting time\n");
		   while(start!=NULL)
			{
				printf("%d \t\t%d \t\t\t%d\n",start->process,start->bt,start->wt);
				start=start->next;
			}

		   printf("\n Average wt is %f",avg_wt);
	}

void sjf(struct node * head)
	{
		struct node *ptr1,*ptr2;
		int x;
		ptr1=head;
		ptr2=head->next;
		while(ptr1!=NULL)
		{
			while(ptr2!=NULL)
			{
				if(ptr1->bt>ptr2->bt)
				{
					x=ptr1->bt;
					ptr1->bt=ptr2->bt;
					ptr2->bt=x;

					x=ptr1->process;
					ptr1->process=ptr2->process;
					ptr2->process=x;
				}

				ptr2=ptr2->next;
			}
			ptr1=ptr1->next;
			ptr2=ptr1->next;
		}
	}
float waiting(struct node *head,int n)
	{
		struct node *ptr,*ptr1;
		int wt=0;
		float avg_wt;
		head->wt=wt;
		ptr1=head;
		ptr=head->next;
		while(ptr!=NULL)
			{
				wt=wt+ptr1->bt;
				ptr->wt=wt;
				ptr1=ptr1->next;
				ptr=ptr->next;
			}
		ptr=head;
		wt=0;
		while(ptr!=NULL)
			{
				wt=ptr->wt+wt;
				ptr=ptr->next;
			}
		avg_wt=float(wt/n);

		return(avg_wt);
	}

void main()
	{
		struct node *head;
		struct node *node,*ptr;

		int i,n;
		float avg_wt;
		clrscr();
		printf("\t\t***** Program For SJF Scheduling ******");
		puts("\nEnter the no. of Processes:");
		scanf("%d",&n);
		printf("\nEnter the process Number:");

		head=(struct node*)malloc(sizeof(struct node));

		scanf("%d",&head->process);
		printf("Enter the Burst time");
		scanf("%d",&head->bt);
		head->next=NULL;
		ptr=head;
		for(i=1;i<n;i++)
			{
				node=(struct node*)malloc(sizeof(struct node));
				printf("Enter the Process Number: ");
				scanf("%d",&node->process);
				printf("Enter the Burst time:");
				scanf("%d",&node->bt);
				node->next=NULL;
				ptr->next=node;
				ptr=node;
			}

		sjf(head);

		avg_wt=waiting(head,n);

		printll(head,avg_wt);

		getch();

	}


When I run this code then the avg. waiting time doesnot give fraction value.
Although i've taken wt as float help me out...

Mod edit: Please :code:
Thanks, gabehabe :)

Is This A Good Question/Topic? 0
  • +

Replies To: Sjf Scheduling

#2 Public_account  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 16-February 07

Re: Sjf Scheduling

Posted 24 August 2008 - 09:04 AM

In the waiting function replace:
avg_wt=float(wt/n);

with:
avg_wt=(float)wt/float(n);

Was This Post Helpful? 0
  • +
  • -

#3 Guest_zephyre*


Reputation:

Re: Sjf Scheduling

Posted 10 February 2010 - 05:16 AM

is this a non-preemptive sjf code?
Was This Post Helpful? 0

#4 thanhuit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 14-December 10

Re: Sjf Scheduling

Posted 14 December 2010 - 11:49 AM

it's not true.
no arrival time???
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1