First come first service scheduling process algorithm FIFO

Source: Internet
Author: User

A few months ago on the operating system experiment class, wrote a bunch of code is nothing to share with you.

The following is the first to first service scheduling process algorithm FIFO code implementation:

/* FIFODemo.cpp author:xxx */#include <stdio.h> #include <conio.h> #define N 20//Process Control block structure typedef struct PCB {char pname[n];//process name int parrivetime;//arrival time int pruntime;//Estimated run time char pState;//process state struct pcb* pnext;//Link pointer} PCB; Chain Head PCB head_input; Custom system time int sysTime; Total number of processes int num=0; Defines the build process function void Createprocesslist () {pcb* pcs1; pcb* PCS2; Number of input processes printf ("Please enter the number of processes:/n"); scanf ("%d", &num); GetChar (); pcs1=&head_input; PCS2=PCS1; Accept the process information in turn for (int i=0;i<num;i++) {pcs1->pnext=new PCB; pcs2=pcs1; pcs1=pcs1->pnext; printf ("Enter the process number no.%3d Cheng name pName: ", i+1); scanf ("%s", pcs1->pname); GetChar (); printf ("Process run time Pruntime:"); scanf ("%d", &pcs1->pruntime); GetChar (); printf ("Process arrival time Parrivetime:"); scanf ("%d", &pcs1->parrivetime); GetChar (); Establish process queue Pcs1->pruntime= (pcs1->pruntime) *1000; Pcs1->parrivetime= (pcs1->parrivetime) *1000; Set the process status to Ready pcs1->pstate= ' R '; }//Last empty node pcs1->pnext=null; printf ("/n"); }//, because the process is ordered to arrive, soSequentially executes the output operation void Execprocesslist () {pcb* pcs1; pcb* PCS2; Scheduling time for each process int timeused=0; There is no idle time in town to execute int freetime=0; Total dispatch time int totaltimeused=0; Pcs1=head_input.pnext; while (Pcs1!=null) {//PCS1 points to the next process Pcs1=head_input.pnext to the head;//At this time no process waits in the process queue for if (pcs1->parrivetime/1000> SysTime) {freetime=pcs1->parrivetime/1000-systime; for (int i=0;i<freetime;i++) {//per output, system time increased by one second printf ("System time systime:%3d./n ", sysTime); systime++; printf ("There is no waiting process in the process queue./n/n"); }///At this time there are already processes arriving at else if (pcs1->parrivetime/1000<=systime) {//output per run statistics for (int j=0;j< (pcs1->pruntime)/ 1000;j++) {//output system time as reference printf ("System Time systime:%3d./n", sysTime); if (0==j) {//arrival process begins execution of printf ("process%s starts running./n/n",pcs1-> PName); } printf ("process%s is running./n", pcs1->pname); printf ("Elapsed time:%3d./n", j+1); printf ("Time Remaining:%3d./n", (pcs1->pruntime/1000)-(j+1)); Determine what other processes are in the queue at this time pcs2=pcs1->pnext; while (pcs2!=null) {//At this time there is a process waiting, indicating there is a process inside the queue if (pcs2->parrivetime/1000<=systime) {printf ("process%s waits in the process queue to run./n", Pcs2-&gT;pname); } pcs2=pcs2->pnext; } systime++; printf ("/n"); }//Scheduling time for a single process timeused=systime-pcs1->parrivetime/1000; Set the state of the city to run out of state pcs1->pstate= ' C '; printf ("process%s terminated./n", pcs1->pname); printf ("Process Scheduling time timeused:%3d./n/n", timeused); Total time totaltimeused=totaltimeused+timeused of process scheduling; Timeused=0; PCS1 refers to the pcs2=pcs1 of the next element; pcs1=pcs1->pnext; HEAD_INPUT.PNEXT=PCS1; Delete the delete pcs2 from the process queue; }//Output the turnaround time for each task and the average turnaround time printf ("Total scheduling time for all Processes totaltimeused:%3d./n", totaltimeused); printf ("Average scheduling time timeused:%lf./n for All Processes", (double) totaltimeused/(double) num);

Implementation results:

Oh, in fact, just a simulation to achieve, no real use process those.

Because the younger brother's level is limited, the code is only for the reference, the bug is many, welcome to shoot the brick.

Oh.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.