라운드로빈 스케쥴러..
권애교
질문 제목 : 라운드로빈 스케쥴러 제작 라운드 로빈질문 내용 :
#include stdio.h
#include stdlib.h#define max_queue_length 10000#define quantum 5
struct process_control_block { int state; // 0: not running, 1: running int time_serviced;};
struct process_log { int entry_time; int turnaround_time; int waiting_time;};
struct process { int pid; int arrival_time; int required_service_time; int first_serviced; struct process_control_block pcb; struct process_log log;};
struct process create_process(int pid, int arrival_time, int required_service_time){ struct process p;
p.pid = pid; p.arrival_time = arrival_time; p.required_service_time = required_service_time; p.first_serviced = 1; p.pcb.state = 0; // not running p.pcb.time_serviced = 0; p.log.turnaround_time = 0; p.log.waiting_time = 0;
return(p);}
struct process *process_list;int num_processes;int process_queue[max_queue_length];int f=0, r=0;int queue_size=0;
void push(int p){ if (queue_size max_queue_length) { process_queue[r] = p; r = (r+1)%max_queue_length; queue_size ++; } else { printf(error: queue is full\n); }}
int pop(){ int fprev = f; f = (f+1)%max_queue_length; queue_size --; return(process_queue[fprev]);}
void report_log(int option, int cpu_used, int total_time){ int turnaround_sum = 0; int waiting_sum = 0; int i;
printf(\nlog of process scheduling\n); for(i=0;inum_processes;i++) { printf(pid(%d), process_list[i].pid); printf( turnaround time: %d, process_list[i].log.turnaround_time); printf( waiting time : %d\n, process_list[i].log.waiting_time); turnaround_sum += process_list[i].log.turnaround_time; waiting_sum += process_list[i].log.waiting_time; }
printf(\n-----------------------------------------------------\n); switch (option) { case 1: printf(short-term scheduling method: first-come-first-served\n); break; case 2: printf(short-term scheduling method: round robin\n); break; case 3: printf(short-term scheduling method: shorted process next\n); break; case 4: printf(short-term scheduling method: shorted remaining time\n); break; default: break; } printf(-----------------------------------------------------\n);
printf(\naverage turnaround time: %.2f\n, (float)turnaround_sum / num_processes); printf(average waiting time : %.2f\n, (float)waiting_sum / num_processes); printf(cpu utilization : %.2f%%\n, ((float)cpu_used / total_time)*100);}
struct process *make_process_list(char *filename){ struct process *plist, p; file *process_file; int pid, arrival_time, required_service_time; int i;
process_file=fopen(filename, r);
if (process_file==null) { printf(file open error: %s\n, filename); exit(-1); }
fscanf(process_file, %d, &num_processes); plist = (struct process *) malloc(sizeof(struct process) * num_processes);
// construct process list for(i=0;inum_processes;i++) { fscanf(process_file, %d %d %d, &pid, &arrival_time, &required_service_time); p = create_process(pid, arrival_time, required_service_time);
// add p to the end of process list plist[i] = p; } fclose(process_file); return(plist);}
void admit_to_queue(int t){ static int i = 0;
while (t == process_list[i].arrival_time) { // to handle processes that have same arrival time push(i); process_list[i].log.entry_time = t; printf([%d] pid(%d): entered into queue\n, t, process_list[i].pid); i++; }}
int short_term_schedule(option, pindex, q){ int next_pindex;
next_pindex = pindex;
switch (option) { case 1: // fcfs // if cpu is idle and queue is not empty, pop a process from ready queue if (pindex 0 && queue_size 0) next_pindex = pop(); break;//여기를 수정해야합니다.. case 2: // round robin
// hw4: you should fill here...
break;
case 3: // spn break;
case 4: // sjt break;
default: break; } return next_pindex;}
void preempt_process(int t, int process_index){ printf([%d] pid(%d): preempted\n, t, process_list[process_index].pid);
// hw4: you should fill here...}
int main(int argc, char *argv[]){ int option, total_time; int cpu_used = 0; int num_processed = 0; int t = 0; int q = 0;
int process_index = -1; // cpu is idle with no processes int prev_process_index = -1; int time_serviced, required_service_time;
if (argc != 3) { printf(usage: hw4 option process_file\n); printf( option: 1 (fcfs: first-come-first-served)\n); printf( option: 2 (rr : round robin)\n); printf( option: 3 (spn : shorted process next)\n); printf( option: 4 (srt : shorted remaining time)\n); exit(-1); } else { option = atoi(argv[1]); }
process_list = make_process_list(argv[2]);
while(num_processed num_processes) { // until all processes are processed
// admit process to ready queue admit_to_queue(t);
// short-term scheduling process_index = short_term_schedule(option, prev_process_index, q);
// preempt prev_process_index if (prev_process_index =0 && process_index != prev_process_index) { preempt_process(t, prev_process_index); }
// if cpu is not idle if (process_index =0) { // service one cpu tick to the process if (process_list[process_index].first_serviced) { process_list[process_index].log.waiting_time = t - process_list[process_index].log.entry_time; process_list[process_index].first_serviced = 0; } process_list[process_index].pcb.state = 1; // running if (process_index != prev_process_index) printf([%d] pid(%d): running\n, t, process_list[process_index].pid);
// cpu services one time tick for the process of process_index process_list[process_index].pcb.time_serviced ++; cpu_used ++;
// hw4: modify variable q here... }
t++; // increase a time tick
if (process_index =0) { // check if the current process can be flushed out from cpu time_serviced = process_list[process_index].pcb.time_serviced; required_service_time = process_list[process_index].required_service_time;
if (required_service_time == time_serviced) { printf([%d] pid(%d): finished\n, t, process_list[process_index].pid); num_processed++; process_list[process_index].log.turnaround_time = t - process_list[process_index].log.entry_time; process_index = -1; // indicates cpu is idle now
// hw4: modify variable q here... } } prev_process_index = process_index; }
total_time = t;
report_log(option, cpu_used, total_time);
return(0);}
코드는 이렇게 되있는데 round robin 스케쥴러 짜는건데 빨간색부분을 수정해야하는데 어떻게해야할지모르겟네요 ㅠㅠ1. process list를 구성 할 것2. 새로운 process가 발생하면 process를 creation하여 list에추가3. creation된 process는 process 정보 (process id, cpuexecution time)와 process control block으로 구성됨• process control block에는 process 상태 (running, not-running등) 등의 정보를 담고 있음실제 과제 평가에 사용될 process list는 약 수 만개 정도의process 들을 가지는 list를 사용할 예정임이것이 요구조건인데..힌트들을 보자면,
• 내부적으로 가상의 현재 시간 t를 두고 loop을 한바뀌 돌때마다 1씩 증가시킨다.1. process list, queue 등 자료구조를 만든다.2. process file list를 읽어 들여 process list를 만든다.• array, vector, linked-list 형태이든 상관없음3. queue를 초기화 한다.4. 현재 시간 t를 0으로 초기화 한다.5. while loop (모든 process가 처리되기 전까지)a. process admissionprocess 중 arrival time이 현재시간인 process를 ready queue에 집어 넣는다.b. short-term scheduler를 이용해서 다음번에 수행할 process를 고른다c. 이전에 수행중인 process와 다음 번 수행할 process가 다르면,해당 process를 시간 1 만큼 서비스 한다.d. 시간 t를 1만큼 증가 시킨다.e. 현재 cpu가 처리중인 process의 required_service_time이 전부 처리 되었는지확인하여 처리 완료 시킨다.6. turnaround time, waiting time, cpu utilization 등 로그 결과를 리포트 한다
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2692144 | C언어와 리눅스에 대한 질문입니다. | 싴흐한세여니 | 2025-04-20 |
2692114 | 컨텍스트 스위칭하는데 걸리는 시간 측정.. | YourWay | 2025-04-19 |
2692086 | 간접참조 연산자, 증감연산자 질문이용! (2) | 블랙캣 | 2025-04-19 |
2692056 | 주석좀 달아주세요. 몇개적엇는데 몇개만달아주세요. (2) | DevilsTears | 2025-04-19 |
2691978 | 진수 쉽게 이해하는법... (3) | 지지않는 | 2025-04-18 |
2691949 | getchar() 한 문자를 입력받는 함수 질문 | 채꽃 | 2025-04-18 |
2691919 | 배열 정렬 및 합치기 질문입니다. | 사과 | 2025-04-18 |
2691845 | c언어왕초보 질문이 있습니다........ | 루나 | 2025-04-17 |
2691815 | void add(int num); 함수... (4) | 살랑살랑 | 2025-04-17 |
2691756 | 명령 프롬프트 스크롤바가 없어요 | 두메꽃 | 2025-04-16 |
2691725 | 자료구조에 관련해서 질문이 있어 글을 올립니다. | 누리알찬 | 2025-04-16 |
2691697 | if 문에서 구조체 배열에 저장되있던 문자열 검사하는 법 ? (2) | 민트맛사탕 | 2025-04-16 |
2691678 | C언어 함수 질문이요~!!! | 연보라 | 2025-04-15 |
2691650 | 반복문 | 돋가이 | 2025-04-15 |
2691618 | 링크드리스트 개념 질문이예요 (3) | 맨마루 | 2025-04-15 |
2691592 | 동적할당 이용 배열선언 질문입니다.ㅠㅠ (3) | 허리달 | 2025-04-15 |
2691542 | /=의 용도를 알려주세요 ㅠㅠ! (2) | 아라 | 2025-04-14 |
2691510 | sizeof 연산자 질문입니다 (2) | 종달 | 2025-04-14 |
2691483 | 파일 오픈시 에러 질문드립니다. (2) | 호습다 | 2025-04-14 |
2691450 | [visual c++ 툴]기초 질문 (3) | 해긴 | 2025-04-13 |