fork(2) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include "lcgrand.h"
  5.  
  6. #define Q_LIMIT_i 100
  7. #define Q_LIMIT_r 100
  8. #define BUSY 1
  9. #define IDLE 0
  10. #define arrival_i1 0
  11. #define arrival_i2 1
  12. #define arrival_r 2
  13. #define departure_i1 3
  14. #define departure_r1 4
  15. #define departure_r2 5
  16. #define termination 6
  17.  
  18. typedef struct event{
  19. float time;
  20. int event_type;
  21. struct event *next;
  22. } *EVENT;
  23.  
  24. EVENT event;//事件
  25.  
  26. int server_status_i1, server_status_r1,server_status_r2;//server 狀態 busy或idle
  27. int num_in_qi, num_in_qr; //inspection & repair 已使用queue數目
  28. int event_type; //系統當前資料型態
  29. int count; //計數器,計算多少人次成功從inspection離開
  30. float time_arrival_i[Q_LIMIT_i], time_arrival_r[Q_LIMIT_r];//inspection & repair的queue
  31. float sim_time;//使用者想模擬多久
  32. float present_time;//系統當前時間
  33. float cumulative_using_i1, cumulative_using_r1, cumulative_using_r2;//累計使用inspection & repair 各個server的時間
  34. float last_BUSY_start_i1, last_BUSY_start_r1, last_BUSY_start_r2;// 最近一次 inspection & repair 各個server 是開始busy的時刻
  35. int Max_capacity_i;//inspection 最大服務數目;
  36.  
  37.  
  38. int initialize(float);//初始化(預計模擬的時間)
  39. int timing(EVENT); //更新現在時間,系統最新狀態(事件list)
  40. int arrive_i(float,int);//inspection到達事件發生時,系統inspection要進行的事,包含立即處理並產生離開事件,新到達事件or加入queue(事件發生時間,事件來源)
  41. int arrive_r(float);//repair到達事件發生時,系統inspection要進行的事,包含立即處理並產生離開事件or加入queue(事件發生時間,事件來源)
  42. int depart_i(float);//inspection離開事件發生時,系統inspection要進行的事,包含從queue讀取新資料並機率產生離開事件or改成idle(事件發生時間)
  43. int depart_r(float,int);//repair離開事件發生時,系統repair要進行的事,包含從queue讀取新資料並機率產生離開事件,or改成idle(事件發生時間,事件來源)
  44.  
  45. int report(FILE *);//資料統計
  46. int generate_event(int,float);//產生事件並加入事件串列中(事件資料型態,事件發生時間)
  47. int add_in_q(float[],float,int*); //將資料加入queue中(目標queue,要傳進去的資料,已使用queue的數目)
  48. float read_from_q(float[],int*);//從queue中讀取一筆資料(目標queue,已使用queue的數目)
  49.  
  50. float uniform(float,float);
  51. float expon(float);
  52.  
  53.  
  54.  
  55.  
  56.  
  57. int main(){
  58. FILE *outfile;
  59. outfile = fopen("d:\\example file\\bus_maintenance_depot.dat", "a");
  60.  
  61. printf("Input the desired simulation time:\n");
  62. scanf("%f",&sim_time);
  63.  
  64. initialize(sim_time);
  65. timing(event);
  66.  
  67. while(event_type != termination){
  68. switch(event_type){
  69. case 0:
  70. case 1:
  71. arrive_i(present_time,event_type);
  72. break;
  73. case 2:
  74. arrive_r(present_time);
  75. break;
  76. case 3:
  77. depart_i(present_time);
  78. break;
  79. case 4:
  80. case 5:
  81. depart_r(present_time,event_type);
  82. break;
  83. }
  84. timing(event);
  85. }
  86. report(outfile);
  87. fclose(outfile);
  88. system("PAUSE");
  89. return 0;
  90. }
  91.  
  92. int initialize(float simulation_time){
  93. present_time = 0;
  94.  
  95. event = NULL;
  96. server_status_i1 = IDLE; server_status_r1 = IDLE; server_status_r2 = IDLE;
  97. num_in_qi = 0; num_in_qr = 0;
  98. event_type = 7;
  99. generate_event(arrival_i1, present_time + expon(2));
  100. generate_event(termination, simulation_time);
  101. count = 0;
  102. cumulative_using_i1 = 0; cumulative_using_r1 = 0; cumulative_using_r2 = 0;
  103. last_BUSY_start_i1 = 0; last_BUSY_start_r1 = 0; last_BUSY_start_r2 = 0;
  104. Max_capacity_i = 1;
  105. return 0;
  106. }
  107.  
  108.  
  109.  
  110. int timing(EVENT event_recieved){
  111. present_time = event_recieved->time;
  112. event_type = event_recieved->event_type;
  113. EVENT temp = NULL;
  114. temp = event_recieved;
  115. event_recieved = event_recieved->next;
  116. free(temp);
  117. return 0;
  118. }
  119.  
  120.  
  121. int arrive_i(float time_arrival,int event_type_a){
  122. if(server_status_i1 == IDLE){
  123. server_status_i1 = BUSY;
  124. last_BUSY_start_i1 = present_time;
  125. generate_event(departure_i1, present_time + uniform(0.25,1.05));
  126. if(event_type_a == arrival_i1)
  127. generate_event(arrival_i1, present_time + expon(2));
  128. }
  129. else{
  130. if(num_in_qi < Q_LIMIT_i){
  131. add_in_q(time_arrival_i, time_arrival, &num_in_qi);
  132. if(event_type_a == arrival_i1)
  133. generate_event(arrival_i1, present_time + expon(2));
  134. Max_capacity_i = (num_in_qi + 1 > Max_capacity_i)? (num_in_qi + 1) : Max_capacity_i;
  135. }
  136. else{
  137. printf("Queue of inspection shop is empty, so stop simulation clock");
  138. generate_event(termination, present_time);
  139. }
  140. }
  141. return 0;
  142. }
  143.  
  144. int arrive_r(float time_arrival){
  145. if(server_status_r1 == IDLE){
  146. server_status_r1 = BUSY;
  147. last_BUSY_start_r1 = present_time;
  148. generate_event(departure_r1, present_time + uniform(2.1,4.5));
  149. }
  150. else if(server_status_r2 == IDLE){
  151. server_status_r2 = BUSY;
  152. last_BUSY_start_r2 = present_time;
  153. generate_event(departure_r2, present_time + uniform(2.1,4.5));
  154. }
  155. else{
  156. if(num_in_qr < Q_LIMIT_r)
  157. add_in_q(time_arrival_r, time_arrival, &num_in_qr);
  158. else{
  159. printf("Queue of repair shop is empty, so stop simulation clock");
  160. generate_event(termination, present_time);
  161. }
  162. }
  163. return 0;
  164. }
  165.  
  166.  
  167. int depart_i(float time_departure){
  168. if(uniform(0,1) <= 0.3)
  169. generate_event(arrival_r, present_time);
  170. else
  171. count++;
  172.  
  173. if(num_in_qi == 0){
  174. cumulative_using_i1 = present_time - last_BUSY_start_i1;
  175. server_status_i1 = IDLE;
  176. }
  177. else{
  178. read_from_q(time_arrival_i, &num_in_qi);
  179. generate_event(departure_i1, present_time + uniform(0.25,1.05));
  180. }
  181. return 0;
  182. }
  183.  
  184.  
  185. int depart_r(float time_departure,int event_type_d){
  186. if(uniform(0,1) <= 0.5)
  187. generate_event(arrival_i2, present_time);
  188. if(num_in_qr == 0){
  189. switch(event_type_d){
  190. case 4:
  191. cumulative_using_r1 = present_time - last_BUSY_start_r1;
  192. server_status_r1 = IDLE;
  193. break;
  194. case 5:
  195. cumulative_using_r2 = present_time - last_BUSY_start_r2;
  196. server_status_r2 = IDLE;
  197. break;
  198. }
  199. }
  200. else{
  201. read_from_q(time_arrival_r, &num_in_qr);
  202. switch(event_type_d){
  203. case 4:
  204. generate_event(departure_r1, present_time + uniform(2.1,4.5));
  205. break;
  206. case 5:
  207. generate_event(departure_r2, present_time + uniform(2.1,4.5));
  208. break;
  209. }
  210. }
  211. return 0;
  212. }
  213.  
  214.  
  215. int report(FILE *o_file){
  216. static float utilization_i1, utilization_r1, utilization_r2;
  217.  
  218. fprintf(o_file, "average no. successful passing inspection per hr = %f\n", count/present_time);
  219.  
  220. if(server_status_i1 == BUSY)
  221. utilization_i1 = (cumulative_using_i1 + present_time - last_BUSY_start_i1)/present_time;
  222. else
  223. utilization_i1 = cumulative_using_i1/present_time;
  224. if(server_status_r1 == BUSY)
  225. utilization_r1 = (cumulative_using_r1 + present_time - last_BUSY_start_r1)/present_time;
  226. else
  227. utilization_r1 = cumulative_using_r1/present_time;
  228. if(server_status_r2 == BUSY)
  229. utilization_r2 = (cumulative_using_r2 + present_time - last_BUSY_start_r2)/present_time;
  230. else
  231. utilization_r2 = cumulative_using_r2/present_time;
  232. fprintf(o_file, "utilzation\n");
  233. fprintf(o_file, "inspecton_server_1 repair_server_1 repair_server_2\n");
  234. fprintf(o_file, "%15.4f %15.4f %15.4f",utilization_i1,utilization_r1,utilization_r2);
  235.  
  236. fprintf(o_file, "maximum capcity of inspection depot with p = 0.5 is %d\n", Max_capacity_i);
  237. return 0;
  238. }
  239.  
  240.  
  241.  
  242. int generate_event(int next_event_type,float next_event_time){
  243.  
  244. EVENT ptr = (EVENT)malloc(sizeof(struct event));
  245. ptr->event_type = next_event_type;
  246. ptr->time = next_event_time;
  247. ptr->next = NULL;
  248.  
  249. if(event == NULL)
  250. event = ptr;
  251. else if(ptr->time <= event->time){
  252. ptr->next = event;
  253. event = ptr;
  254. }
  255. else{
  256. EVENT current = event;
  257. EVENT prev = NULL;
  258.  
  259. while(current != NULL && ptr->time > current->time){
  260. prev = current;
  261. current = current->next;
  262. }
  263. prev->next = ptr;
  264. ptr->next = current;
  265. }
  266. return 0;
  267. }
  268.  
  269. int add_in_q(float data[],float data_arrival,int *num_in_qu){
  270. data[*num_in_qu] = data_arrival;
  271. (*num_in_qu)++;
  272. return 0;
  273. }
  274.  
  275.  
  276. float read_from_q(float data[],int *num_in_qu){
  277. (*num_in_qu)--;
  278. static float temp;
  279. temp = data[0];
  280. int i;
  281. for(i=0; i< *num_in_qu - 1; i++)
  282. data[i] = data[i+1];
  283. return temp;
  284. }
  285.  
  286.  
  287. float uniform(float lower,float upper){
  288. return lower + lcgrand(1)*(upper - lower);
  289. }
  290.  
  291. float expon(float mean){
  292. return -mean*log(lcgrand(1));
  293. }
  294.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:4:21: error: lcgrand.h: No such file or directory
prog.cpp: In function ‘int main()’:
prog.cpp:62: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result
prog.cpp:88: warning: ignoring return value of ‘int system(const char*)’, declared with attribute warn_unused_result
prog.cpp: In function ‘float uniform(float, float)’:
prog.cpp:288: error: ‘lcgrand’ was not declared in this scope
prog.cpp: In function ‘float expon(float)’:
prog.cpp:292: error: ‘lcgrand’ was not declared in this scope
stdout
Standard output is empty