반응형





001:
#include <fstream> 002: #include <iostream> 003: #include <windows.h> 004: #include <conio.h> 005: #include <stdlib.h> 006: using namespace std; 007: 008: #define CPU_COUNT 60 009: 010: //프로세스 데이타 구조체 생성. 011: typedef struct Node { 012: char *name; //프로세스 이름 013: int base_priority; //base_priority 014: int priority; //priority 015: int time; //실행 시간 016: int K_U; //커널 or 사용자 프로세스 017: int CPU_C; 018: struct Node *Next; 019: char state; 020: }Node; 021: 022: typedef Node* Nptr; 023: 024: class L_List { 025: public: 026: L_List(); 027: L_List(const L_List &y); 028: ~L_List(); 029: void AddTail(Nptr *header, char* _name, int _base_priority, int _time, int _K_U); 030: void AddHead(Nptr *header, char* _name, int _base_priority, int _time, int _K_U); 031: Nptr Ker_Usr(Nptr header); 032: 033: Nptr Check_priority(Nptr &header); 034: void Scheduling(Nptr _header, int _V=0); 035: 036: void display(Nptr header); 037: void Total_Print(); 038: void ReleaseList(); 039: Nptr FirstHeader; 040: static int END_Process_Count; 041: static int Process_Count; 042: }; 043: int L_List::END_Process_Count=0; 044: int L_List::Process_Count=0; 045: //생성자 046: L_List::L_List() { 047: FirstHeader = NULL; 048: } 049: //소멸자 050: L_List::~L_List() {} 051: //꼬리에 추가 052: void L_List::AddTail(Nptr *header, char* _name, int _base_priority, int _time, int _K_U){ 053: Nptr tail = *header; 054: Nptr NewNode = NULL; //새 노드 선언 055: 056: if(*header == NULL) 057: AddHead(header, _name, _base_priority, _time, _K_U); 058: else{ 059: // 꼬리를 찾는다. 060: while(1){ 061: if(tail->Next != NULL){ 062: tail = tail->Next; 063: continue; 064: } 065: break; 066: } 067: Nptr NewNode = new Node; //새 노드 동적 할당 068: tail->Next = NewNode; //꼬리에 새 노드를 연결 069: NewNode->Next = NULL; //새 노드를 꼬리로 선언 070: NewNode->name = _name; //프로세스 이름 입력 071: NewNode->base_priority = _base_priority; //Base priority 입력 072: NewNode->time = _time; //실행 시간 입력 073: NewNode->CPU_C=0; 074: NewNode->priority=_base_priority; 075: NewNode->K_U = _K_U; //커널 또는 사용자 프로세스 입력 076: Process_Count++; 077: NewNode->state=' '; 078: } 079: } 080: //앞에다 넣기 081: void L_List::AddHead(Nptr *header, char* _name, int _base_priority, int _time, int _K_U){ 082: Nptr NewNode = new Node; //새 노드 동적 할당 083: NewNode->name = _name; //프로세스 이름 입력 084: NewNode->base_priority = _base_priority; //Base priority 입력 085: NewNode->time = _time; //실행 시간 입력 086: NewNode->K_U = _K_U; //커널 또는 사용자 프로세스 입력 087: NewNode->Next = *header; //다음 포인터 설정 088: NewNode->CPU_C=0; 089: NewNode->priority=_base_priority; 090: *header = NewNode; //헤더를 교체 091: FirstHeader=NewNode; 092: Process_Count++; 093: NewNode->state=' '; 094: } 095: 096: 097: //출력용! 098: void L_List::display(Nptr header) { 099: Nptr temp = header; 100: cout<<"*:수행중인 프로세스 #:종료된 프로세스"<<endl; 101: cout<<"-----------------------------------------------------------------------------"<<endl; 102: while(temp != NULL) { 103: printf("%5s|%-9s",temp->name, temp->K_U ? "User_P" : "Kern_P"); 104: temp = temp->Next; 105: } 106: temp = header; 107: cout<<endl; 108: while(temp != NULL) { 109: printf("%-14s","Pri CC rem "); 110: temp = temp->Next; 111: } 112: cout<<"-----------------------------------------------------------------------------"<<endl; 113: } 114: 115: //종료된 프로세스를 삭제 하는 메소드 116: void L_List::ReleaseList() { 117: Nptr temp = FirstHeader; 118: 119: while(temp != NULL){ 120: if(temp->time==0) 121: END_Process_Count++; 122: temp=temp->Next; 123: } 124: if (Process_Count==END_Process_Count){ 125: cout<<"종료되었습니다."<<endl; 126: cout<<"2002160000 전호철"<<endl; 127: getch(); 128: cout<<"종료하시려면 아무키나 누르십시요"<<endl; 129: getch(); 130: exit(0); 131: } 132: else 133: END_Process_Count=0; 134: } 135: 136: Nptr L_List::Ker_Usr(Nptr header){ 137: Nptr Kernel_Min=NULL; 138: Nptr now = header; 139: bool firstflag=true; 140: bool Ufirstflag=true; 141: 142: int K_count=0; 143: 144: Nptr whiletemp = header; 145: while(whiletemp != NULL){ //커널의 갯수를 검사하여 저장 146: if (whiletemp->K_U == 0 && whiletemp->time!=0) 147: K_count++; 148: whiletemp = whiletemp->Next; 149: } 150: 151: while(now != NULL){ 152: if(K_count) { 153: if(now->K_U == 0 && now->time != 0){ 154: if(firstflag){ 155: firstflag=false; 156: Kernel_Min=now; 157: } 158: else{ 159: if(Kernel_Min->priority > now->priority) 160: Kernel_Min = now; 161: } 162: } 163: } 164: if(K_count==0 && now->time != 0) { 165: if(now->K_U==1) { 166: if(Ufirstflag) { 167: Ufirstflag = false; 168: Kernel_Min=now; 169: } else { 170: if(Kernel_Min->priority > now->priority) 171: Kernel_Min = now; 172: } 173: } 174: } 175: now=now->Next; 176: } 177: return Kernel_Min; 178: } 179: 180: void L_List::Scheduling(Nptr Top_priority, int _V){ //스케줄링 181: Nptr temp=FirstHeader; 182: 183: while(temp != NULL){ //전체 검색하여서 184: if(!strcmp(Top_priority->name, temp->name)){ //수행될 프로세스라면 185: temp->CPU_C=(temp->CPU_C+CPU_COUNT)/2; //CPU COUNT를 반으로 나눈다. 186: temp->priority=temp->base_priority+temp->CPU_C/2+_V; //우선순위 조작 187: temp->time-=1; //남은 실행시간을 계산하여 저장 188: temp->state='*'; 189: }else if(temp->time!=0){ //수행될 차례가 아니면 190: temp->CPU_C=temp->CPU_C/2; //CPU COUNT를 반으로 나눈다. 191: temp->priority=temp->base_priority+temp->CPU_C/2; //우선순위 조작 192: temp->state=' '; 193: }else if(temp->time == 0) 194: temp->state='#'; 195: temp = temp->Next; 196: } 197: Total_Print(); 198: } 199: 200: void L_List::Total_Print(){ 201: Nptr temp=FirstHeader; 202: while(temp != NULL){ 203: printf("%c%4d%4d%4d ",temp->state,temp->priority, temp->CPU_C, temp->time); 204: temp=temp->Next; 205: Sleep(50); 206: } 207: cout<<endl; 208: } 209: 210: 211: void main() 212: { 213: Nptr N=NULL; 214: L_List process; 215: char name[5][5]={0,}; //프로세스 이름 216: int base_priority[5]={0,}; 217: int time[5]={0,}; 218: int K_U[5]={0,}; 219: 220: int part=0; 221: int i=0; 222: int k=0; 223: int l=0; 224: int niceValue=0; 225: 226: bool space=false;//입력을 구분하기 위한 공백 검출 227: int temp_count=0,data_count=0; //카운트를 세기 위한 변수 228: char temp[5]={0,}, ch; //data.txt에서 추출한 수가 임시로 들어가있을 장소 229: 230: ifstream fin("data.txt"); //data.txt 파일을 읽는다. 231: if (fin.is_open()) //파일이 열렸다면 232: cout<<"File open success.\n\n"; 233: else 234: cout<<"File open fail.\n\n"; 235: 236: 237: char sizetemp[2]; //data.txt파일의 첫번째 줄을 읽기 위한 임시 저장소 238: fin.getline(sizetemp,2,'\n'); //data.txt 파일의 첫 라인(프로세스 갯수) 추출한다. 239: int p_count=atoi(sizetemp); //프로세스 갯수 240: cout<<"프로세스 갯수 : "<<p_count<<endl; 241: 242: // int* data=(int *)malloc(sizeof(int)*p_cpunt*4); //프로세세들의 정보를 입력받을 공간을 동적할당 243: 244: //---1 : 배열 크기 할당 ---------------------// 245: 246: while(!fin.eof()){ 247: fin.get(ch); // 한글자씩 읽어들인다. 248: if (!isspace(ch)) //만일 공백이 아니라면 249: temp[temp_count++]=ch; //임시 char에 넣어 둔다. 250: else{ //공백이 발생하면 251: switch(part){ 252: case 0: 253: for(i=0; i<5; i++) 254: name[l][i]=temp[i]; 255: l++; 256: for (i=0;i<5;i++) //temp를 초기화 한다. 257: temp[i]=0; 258: part=1; 259: break; 260: case 1: 261: base_priority[data_count]=atoi(temp); 262: for (i=0;i<5;i++) //temp를 초기화 한다. 263: temp[i]=0; 264: part=2; 265: break; 266: case 2: 267: time[data_count]=atoi(temp); 268: for (i=0;i<5;i++) //temp를 초기화 한다. 269: temp[i]=0; 270: part=3; 271: break; 272: case 3: 273: K_U[data_count]=atoi(temp); 274: for (i=0;i<5;i++) //temp를 초기화 한다. 275: temp[i]=0; 276: part=0; 277: data_count++; 278: break; 279: } 280: temp_count=0; //temp에 새로운 데이터를 넣어야 하니까 카운트를 0으로 잡고 281: } 282: } 283: fin.close(); //파일 닫기 284: 285: for (int j=0; j<5; j++) 286: process.AddTail(&process.FirstHeader, name[j], base_priority[j], time[j], K_U[j]); 287: process.display(process.FirstHeader); 288: 289: process.Total_Print(); 290: while(1){ 291: N=process.Ker_Usr(process.FirstHeader); 292: // cout<<"수행될 차례의 프로세스는 : "<<N->name<<endl; 293: if(N->K_U==1){ 294: cout<<"Nice Value를 입력하세요 : "; 295: cin >> niceValue; 296: cout<<endl<<endl; 297: process.display(process.FirstHeader); 298: } 299: process.Scheduling(N,niceValue); 300: N=process.Ker_Usr(process.FirstHeader); 301: process.ReleaseList(); 302: } 303: } 304: 305: /*-----------data.txt----------------------// 306: 5 307: p1 60 5 0 308: p2 50 8 1 309: p3 60 10 0 310: p4 30 4 1 311: p5 50 3 1 312: /*-----------------------------------------*/
반응형

'C & C++ 관련' 카테고리의 다른 글

c에서 프로그램 수행시간재는법?  (0) 2007.05.21
클래스를 이용한 계좌 관리 프로그램 연습  (0) 2007.05.09
realloc 연습  (0) 2007.05.09
Posted by Real_G