⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 assign1.c

📁 This is a CPU Scheduling Algorithm implemented in C in order to Simulate CPU Scheduling
💻 C
字号:
#include <stdio.h>#include <stdlib.h>#include <string.h>struct Node;typedef struct Node *PtrtoNode;typedef PtrtoNode List;typedef PtrtoNode Position;int ReadFile(List l );int CloseFile(FILE *);int PrintList(List l);int SortList(List l);int Simulate(List l);int DeleteNode(List l);struct Node{        char process[100];        int time;        Position next;};int Timer=0;int main(){		        List l;        		Position p;		l = malloc(sizeof(struct Node));        strcpy(l->process , "");		l->time=-1;		l->next = NULL;	        p = l;				ReadFile(l);		SortList(l);		while(l->next!=NULL)		{			PrintList(l);			Simulate(l);			/*DeleteNode(l);*/				}			printf("\n\nLeaving Main()\n\n");        return 0;}int ReadFile(List t){        FILE *fin;        Position temp , temp1;        char res[75] , str[70] ,sec[5]  ,*sec2;		int tim;			temp1 = t;        fin = fopen("input.txt" , "r");				if(!fin)		{			printf("\nWelcome to the scheduler\n");			return 1;		}        while(!feof(fin) )        {				temp = malloc(sizeof(struct Node));							                fgets(res,75,fin );				if(strlen(res)==0)					break;				if(feof(fin))					break;				sec2= strtok(res,",");	                strcpy(str,sec2);				sec2 = strtok(NULL,",");                strcpy(sec , sec2);				tim = atoi(sec2);				strcpy(temp->process , str);				temp->time = tim;				temp->next = temp1->next;				temp1->next = temp;				temp = temp1;				        }		fclose(fin);		CloseFile(fin);		return 0;}int CloseFile(FILE *fin){	fin = fopen("input.txt" , "w");	return 0;}int PrintList(List l){	Position temp;	temp = l;	if(temp->next==NULL)	{		printf("\nThe list is empty\n");		return 0;	}	printf("\nThe Ready Queue for execution.\n");	while(temp->next!= NULL)	{		temp = temp->next;		printf("\nThe value is = %s,%d \n" , temp->process , temp->time);	}	return 0;}int SortList(List l){	Position first , second , temp;	first = l->next;	if(!first)	{		printf("\nThe list is empty\n");		return 0;	}	while(first)	{		second = first->next;		while(second)		{			if((first->time) > (second->time))			{				temp = malloc(sizeof(struct Node));				temp->time = second->time;				strcpy(temp->process , second->process);				second->time= first->time;				strcpy(second->process , first->process);				first->time = temp->time;				strcpy(first->process , temp->process);			}			second= second->next;		}		first= first->next;	}	return 0;}int DeleteNode(List l ){	Position p , temp;	p = l;	if(p->next==NULL)	{		printf("\nEmpty List.\n");		return 0;			}	temp = p->next;	p->next= temp->next;	temp->next=NULL;	free(temp);	return 0;}int Simulate(List l){	Position p,q;	int i;	long j;	int flag=1;	q = l;	p=q->next;	printf("\nStarting Simulation\n.");	while(p!=NULL)	{		/*p=q->next;*/		flag=1;		for(i =0; i < p->time; i++, Timer++)			if(Timer ==99)			{				p->time=p->time - i;				ReadFile(l);				SortList(l);				p = l->next;				Timer=0;				flag=0;				break;			}			for(j = 0 ; j < 3000 ; j++)				;			if(flag)	{		printf("\nDeleting the node %s,%d\n", p->process,p->time);		/*printf("next process %s %d\n",p->next->process,p->next->time);*/		DeleteNode(l);		p=q->next;	}	}	printf("\nAll Processes finished.Bye.\n");	return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -