Operating System Experiment 5 Process Scheduling

Source: Internet
Author: User

Purpose:

The implementation first serves FCFS, short job priority SJF, and time slice rotation scheduling algorithm.

Lab content:

① Implement the FCFS algorithm: schedule several processes in the order of arrival time.

② Implement the SJF algorithm: select the minimum time for the process to run according to the current time.

③ Implement the time slice Rotation Algorithm: first, determine the size of the time slice and add it to the queue based on the arrival time of the process. Each time a time slice size is allocated, if it is not completed to participate in the next competition, this process is completed and the queue is exited when a time slice earlier than or equal to the time slice is required.

④ Calculate the average turnaround time and average weighted turnaround time of the system after each algorithm is scheduled.

Algorithm Implementation:

void FCFS(){inti;floatsumT = 0.0;floatsumW = 0.0;floatlast ;process[0].start = process[0].arrive;process[0].finish = process[0].start + process[0].service;last = process[0].finish;i = 1;while(i != N){if(process[i].arrive > last)last = process[i].arrive;process[i].start = last;process[i].finish = last + process[i].service;last += process[i].service;i++;}for(i = 0 ; i < N; i++){process[i].T = process[i].finish - process[i].arrive;process[i].W = process[i].T / (float)process[i].service;sumT += process[i].T;sumW += process[i].W;}FCFS_T = sumT / N;FCFS_W = sumW / N;}int getmin(float t){inti;intaddr = -1;floatmin=10000.0;for(i = 0 ; i < N ; i++){if(process[i].state == 0 && process[i].service < min && process[i].arrive <= t){addr = i;min = process[i].service;}}process[addr].finish = t + process[addr].service;return addr;}void SJF(){inti;floatsumT = 0.0;floatsumW = 0.0;process[0].finish = process[0].arrive + process[0].service;process[0].state  = 1;intaddr = 0;intsign = 0; floatlast = process[0].finish;while(sign != N-1){addr = getmin(last);if(addr == -1){last = process[getmin(1000)].arrive;continue;}process[addr].start = last;process[addr].state = 1;last = process[addr].finish;sign++;}for(i = 0 ; i < N; i++){process[i].T = process[i].finish - process[i].arrive;process[i].W = process[i].T / (float)process[i].service;sumT += process[i].T;sumW += process[i].W;}SJF_T = sumT / N;SJF_W = sumW / N;}void RR(){inti = 0;intj;intt = process[0].arrive;floatsumT = 0.0;floatsumW = 0.0;for(j = 0 ; j < N ; j++){process[j].state = 0;process[j].start = -1;process[j].left = process[j].service;}process[0].start = t;while(1){for(j = 0 ; j < N ; j++)if(process[(i+j)% N].state == 0 && t >= process[(i+j)% N].arrive)break;i = (i+j)% N;if( process[i].state == 1){break;}if(process[i].start == -1)process[i].start = t;for(j = 0 ; j < min(process[i].left,q) ; j++)cout <<process[i].name;if(process[i].left > q){t += q;process[i].left -= q;}else{t += process[i].left;process[i].left = 0;process[i].finish = t;process[i].state = 1;}i = (++i) %N;}for(i = 0 ; i < N ; i++){process[i].T = process[i].finish - process[i].arrive;process[i].W = process[i].T / (float)process[i].service;sumT += process[i].T;sumW += process[i].W;}RR_T = sumT / N;RR_W = sumW / N; }

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.