00001
00002
00003
00004
00005
00006 #include "interpreteur.h"
00007
00008
00009
00010
00011
00012
00013
00014
00015 int pid_libre ( ) {
00016 mot_t mot,mot2;
00017 int pid_libre;
00018 int i;
00019
00020
00021 if (lire_mot_mem(0, NUM_PAGE_META, mot) == -1)
00022 return -1;
00023
00024 if ((pid_libre=(int)mot[3]) == NO_MORE_PID) {
00025 fprintf(stderr,"Il n'y a plus de pid libre\n");
00026 return -2;
00027 }
00028
00029 for (i=pid_libre+1 ; i<N_MAX_PROC+1 ; i++) {
00030 if (lire_mot_mem(i, NUM_PAGE_META, mot2) == -1)
00031 return -1;
00032 if ((int)mot2[2] == PP_NULL) {
00033
00034 mot[3] = (octet_t)i;
00035 if (ecrire_mot_mem(0, NUM_PAGE_META, mot) == -1)
00036 return -1;
00037
00038 return pid_libre;
00039 }
00040 }
00041
00042
00043 mot[3] = (octet_t)NO_MORE_PID;
00044 if (ecrire_mot_mem(0, NUM_PAGE_META, mot) == -1)
00045 return -1;
00046
00047 return pid_libre;
00048 }
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059 int liberer_pid (int pid) {
00060 mot_t mot,mot2;
00061
00062
00063 mot2[2] = PP_NULL;
00064 if (ecrire_mot_mem(pid,NUM_PAGE_META, mot2) == -1)
00065 return -1;
00066
00067 if (lire_mot_mem(0, NUM_PAGE_META, mot) == -1)
00068 return -2;
00069 if (pid < (int)mot[3]) {
00070 mot[3] = (octet_t)pid;
00071 if (ecrire_mot_mem(0,NUM_PAGE_META, mot) == -1)
00072 return -2;
00073 }
00074 return 0;
00075 }
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086 int page_libre (int pid) {
00087 int page_libre;
00088 int i,j;
00089 int num_mot, num_octet;
00090 mot_t mot, mot2;
00091
00092
00093 if (lire_mot_mem(DEBUT_T_PAGES, NUM_PAGE_META, mot) == -1)
00094 return -1;
00095
00096 if ((page_libre=(int)mot[0]) == NO_MORE_PAGE) {
00097 fprintf(stdout,"Il n'y a plus de page libre en memoire\n");
00098 return -2;
00099 }
00100
00101 num_mot = (page_libre + 1) / T_MOT + DEBUT_T_PAGES;
00102 num_octet = (page_libre + 1) % T_MOT;
00103 for (i=num_mot ; i<NMPPP ; i++) {
00104 if (lire_mot_mem(i, NUM_PAGE_META, mot2) == -1)
00105 return -1;
00106 for (j=num_octet ; j<T_MOT ; j++) {
00107 if (mot2[j] == PAGE_VIDE) {
00108
00109 mot[0] = (octet_t)(i - DEBUT_T_PAGES) * T_MOT + j;
00110 if (ecrire_mot_mem(DEBUT_T_PAGES, NUM_PAGE_META, mot) == -1)
00111 return -1;
00112
00113 liste_add_page(liste_pages,page_libre);
00114 return page_libre;
00115 }
00116 }
00117 num_octet = 0;
00118 }
00119
00120
00121 mot[0] = (octet_t)NO_MORE_PAGE;
00122 if (ecrire_mot_mem(DEBUT_T_PAGES, NUM_PAGE_META, mot) == -1)
00123 return -1;
00124
00125 liste_add_page(liste_pages,page_libre);
00126
00127 num_mot = page_libre / T_MOT + DEBUT_T_PAGES;
00128 num_octet = page_libre % T_MOT;
00129 if (lire_mot_mem(num_mot, NUM_PAGE_META, mot2) == -1) {
00130 liberer_page(page_libre);
00131 return -1;
00132 }
00133 mot2[num_octet] = (octet_t)pid;
00134 if (ecrire_mot_mem(num_mot, NUM_PAGE_META, mot2) == -1) {
00135 liberer_page(page_libre);
00136 return -1;
00137 }
00138 return page_libre;
00139 }
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150 int liberer_page(int page) {
00151 mot_t mot;
00152 int num_mot,num_octet;
00153
00154
00155 num_mot = page/T_MOT + DEBUT_T_PAGES;
00156 num_octet = page % T_MOT;
00157 if (lire_mot_mem(num_mot, NUM_PAGE_META, mot) == -1)
00158 return -1;
00159 mot[num_octet] = PAGE_VIDE;
00160 if (ecrire_mot_mem(num_mot,NUM_PAGE_META, mot) == -1)
00161 return -1;
00162
00163 if (lire_mot_mem(DEBUT_T_PAGES, NUM_PAGE_META, mot) == -1)
00164 return -2;
00165 if (page < (int)mot[0]) {
00166 mot[0] = (octet_t)page;
00167 if (ecrire_mot_mem(DEBUT_T_PAGES,NUM_PAGE_META, mot) == -1)
00168 return -2;
00169 }
00170
00171
00172 if (liste_supp_page(liste_pages,page) != 0) {
00173 fprintf(stderr,"La table des pages memoire utilisees n'etait pas coherente.\n");
00174
00175 }
00176 return 0;
00177 }
00178
00179
00180
00181
00182
00183
00184
00185
00186 int page_swap_libre () {
00187 mot_t mot,mot2;
00188 int pc,num_mot,num_page_libre;
00189
00190
00191 if (lire_mot_mem(DEBUT_T_PAGES, NUM_PAGE_META, mot) == -1)
00192 return -1;
00193 pc = (int)mot[1];
00194
00195 num_mot = pc / T_MOT;
00196 if (lire_mot_mem(num_mot, NUM_PAGE_SWAP, mot2) == -1)
00197 return -1;
00198 num_page_libre = (int)mot2[pc%T_MOT];
00199 if (num_page_libre == NO_MORE_SWAP) {
00200 fprintf(stderr,"Il n'y a plus de page libre dans le swap\n");
00201 return -2;
00202 }
00203
00204 pc ++;
00205
00206 if (pc >= NMPPP*T_MOT) {
00207 if (page_swap2mem(num_page_libre, NUM_PAGE_SWAP) == -1)
00208 return -1;
00209 mot[1] = (octet_t)0;
00210 if (ecrire_mot_mem(DEBUT_T_PAGES, NUM_PAGE_META, mot) == -1) {
00211 fprintf(stderr,"Position courante de la table des pages swap libres incoherente\n");
00212 return -1;
00213 }
00214 }
00215 return num_page_libre;
00216 }
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226 int liberer_page_swap (int page) {
00227 mot_t mot,mot2;
00228 int pc,num_mot;
00229
00230
00231 if (lire_mot_mem(DEBUT_T_PAGES, NUM_PAGE_META, mot) == -1)
00232 return -1;
00233 pc = (int)mot[1];
00234 if (pc > 0) {
00235 pc --;
00236 num_mot = pc / T_MOT;
00237 if (lire_mot_mem(num_mot, NUM_PAGE_SWAP, mot2) == -1)
00238 return -1;
00239 mot2[pc%T_MOT] = (octet_t)page;
00240
00241 if (ecrire_mot_mem(num_mot,NUM_PAGE_SWAP, mot2) == -1)
00242 return -1;
00243
00244 mot[1] = (octet_t)pc;
00245 if (ecrire_mot_mem(DEBUT_T_PAGES,NUM_PAGE_META, mot) == -1)
00246 return -2;
00247 }
00248 else {
00249
00250 if (page_mem2swap(NUM_PAGE_SWAP,page) == -1)
00251 return -1;
00252
00253 mot2[3] = (octet_t)page;
00254 if (ecrire_mot_mem(NMPPP-1,NUM_PAGE_SWAP,mot2) == -1)
00255 return -1;
00256 pc = NMPPP*T_MOT-1;
00257 mot[1] = (octet_t)pc;
00258 if (ecrire_mot_mem(DEBUT_T_PAGES,NUM_PAGE_META, mot) == -1)
00259 return -2;
00260 }
00261 return 0;
00262 }
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272 int rapatrier_page_swap (int pid, int page) {
00273 int num_page_mem,num_page_swap,num_table_pages;
00274 int old_pid;
00275 int find=0;
00276 mot_t mot_tab_page,mot_proc;
00277 int *tab,taille;
00278 int i,j;
00279
00280
00281 num_page_mem = page_libre(pid);
00282 if (num_page_mem > 0) {
00283
00284 if (page_swap2mem(page, num_page_mem) == -1)
00285 return -1;
00286 }
00287 else if (num_page_mem == -1) {
00288 return -1;
00289 }
00290 else {
00291
00292 if ((num_page_mem=liste_old_page(liste_pages)) == -1)
00293 return -1;
00294
00295 if ((num_page_swap=page_swap_libre()) == -1)
00296 return -1;
00297 if (page_mem2swap(num_page_mem,num_page_swap) == -1)
00298 return -1;
00299
00300 if (lire_mot_mem(DEBUT_T_PAGES+num_page_mem/T_MOT,NUM_PAGE_META,mot_tab_page) == -1)
00301 return -1;
00302 old_pid =(int) mot_tab_page[num_page_mem%T_MOT];
00303
00304 if (lire_mot_mem(old_pid,NUM_PAGE_META,mot_proc) == -1)
00305 return -1;
00306 num_table_pages = (int)mot_proc[3];
00307 if (num_table_pages == num_page_mem) {
00308
00309 mot_proc[3] = (octet_t)num_page_swap;
00310 mot_proc[2] = (octet_t)PP_SWAP;
00311 if (ecrire_mot_mem(old_pid,NUM_PAGE_META,mot_proc) == -1)
00312 return -1;
00313
00314 mot_tab_page[num_page_mem%T_MOT] = (octet_t)pid;
00315 if (ecrire_mot_mem(DEBUT_T_PAGES+num_page_mem/T_MOT,NUM_PAGE_META,mot_tab_page) == -1) {
00316
00317 mot_proc[3] = (octet_t)num_page_mem;
00318 mot_proc[2] = (octet_t)PP_RAM;
00319 ecrire_mot_mem(old_pid,NUM_PAGE_META,mot_proc);
00320 return -1;
00321 }
00322 }
00323 else {
00324
00325 for (i=0 ; (i<3) && (find==0) ; i++) {
00326 for (j=0 ; j<8 ; j++) {
00327 if (lire_mot_mem(i*8+j,num_table_pages,mot_proc) == -1)
00328 return -1;
00329 if ((int)mot_proc[2] == PP_NULL)
00330 break;
00331 if (mot_proc[3] == num_page_mem) {
00332
00333
00334 mot_proc[2] = (octet_t)PP_SWAP;
00335 mot_proc[3] = (octet_t)num_page_swap;
00336 if (ecrire_mot_mem(i*8+j,num_table_pages,mot_proc) == -1)
00337 return -1;
00338 find = 1;
00339 break;
00340 }
00341 }
00342 }
00343 if (find == 0) {
00344
00345 fprintf(stderr,"La table des pages du processus %d n'est pas coherente avec la ",old_pid);
00346 fprintf(stderr,"table des pages de la memoire principale.\n");
00347 return -1;
00348 }
00349 }
00350 }
00351
00352
00353 return num_page_mem;
00354 }
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365 int lire_mot_mem(int num_mot, int num_pp, mot_t mot) {
00366 unsigned char *p_mot;
00367
00368
00369 if (num_pp < 0 || num_pp > NPP-1) {
00370 fprintf(stderr,"%d n'est pas un numero de page physique en memoire correct\n",num_pp);
00371 return -1;
00372 }
00373 if (num_mot < 0 || num_mot > NMPPP-1) {
00374 fprintf(stderr,"%d n'est pas un numero de mot correct\n",num_mot);
00375 return -1;
00376 }
00377
00378 p_mot = mem[num_pp][num_mot];
00379
00380 memcpy(mot,p_mot,sizeof(mot_t));
00381
00382 if (liste_acces_page(liste_pages,num_pp) != 0) {
00383 fprintf(stderr,"Tentative de lecture ds une page non reservee (%d)\n",num_pp);
00384 return -1;
00385 }
00386 return 0;
00387 }
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398 int ecrire_mot_mem(int num_mot, int num_pp, mot_t mot) {
00399 unsigned char *p_mot;
00400
00401
00402 if (num_pp < 0 || num_pp > NPP-1) {
00403 fprintf(stderr,"%d n'est pas un numero de page physique en memoire correct\n",num_pp);
00404 return -1;
00405 }
00406 if (num_mot < 0 || num_mot > NMPPP-1) {
00407 fprintf(stderr,"%d n'est pas un numero de mot correct\n",num_mot);
00408 return -1;
00409 }
00410
00411 p_mot = mem[num_pp][num_mot];
00412 memcpy(p_mot,mot,sizeof(mot_t));
00413
00414 if (liste_acces_page(liste_pages,num_pp) != 0) {
00415 fprintf(stderr,"Tentative d'ecriture ds une page non reservee (%d)\n",num_pp);
00416 return -1;
00417 }
00418 return 0;
00419 }
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433 int adresse_phys (int pid, vad_t ad_v, info_page_t info) {
00434 int num_page, num_mot;
00435 mot_t mot;
00436
00437
00438 if (lire_mot_mem(pid,NUM_PAGE_META,mot) == -1)
00439 return -1;
00440 switch ((int)mot[2]) {
00441 case PP_NULL:
00442
00443 fprintf(stderr,"Le num de processus %d ne correspond a aucune page physique\n",pid);
00444 return -1;
00445 case PP_SWAP:
00446
00447
00448 if ((num_page=rapatrier_page_swap(pid,(int)mot[3])) == -1)
00449 return -1;
00450 break;
00451 case PP_RAM:
00452 num_page = (int)mot[3];
00453 break;
00454 default:
00455
00456 fprintf(stderr,"La table des processus est incoherente\n");
00457 }
00458 num_mot = ad_v.zone*8+ad_v.num/NMPPP;
00459 if (lire_mot_mem(num_mot, num_page, mot) == -1)
00460 return -1;
00461
00462 info.type = (int)mot[2];
00463 info.droits = (int)mot[1];
00464 info.num = (int)mot[3];
00465 return 0;
00466 }
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476 int page_swap2mem (int num_page_swap, int num_page_mem) {
00477 if (lseek(swap_d,num_page_swap*T_PAGE,SEEK_SET) == (off_t)-1)
00478 return -1;
00479 if (read(swap_d,mem[num_page_mem],T_PAGE) != T_PAGE)
00480 return -1;
00481 return 0;
00482 }
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492 int page_mem2swap (int num_page_mem, int num_page_swap) {
00493 if (lseek(swap_d,num_page_swap*T_PAGE,SEEK_SET) == -1)
00494 return -1;
00495 if (write(swap_d,mem[num_page_mem],T_PAGE) != T_PAGE)
00496 return -1;
00497 return 0;
00498 }
00499
00500
00501
00502
00503
00504
00505 void next_inst (int pid) {
00506 mot_t mot;
00507 vad_t ad_v;
00508
00509
00510 if (pid < 0 || pid > N_MAX_PROC) {
00511 fprintf(stderr, "Le proc %d n'existe pas\n", pid);
00512 return;
00513 }
00514 else {
00515 if (lire_mot_mem(pid,NUM_PAGE_META,mot) == -1)
00516 return;
00517 if ((int)mot[2] == PP_NULL) {
00518 fprintf(stderr, "Tentative d'execution d'un proc termine (%d est vide)\n", pid);
00519 return;
00520 }
00521 }
00522
00523 ad_v.zone = Z_CODE;
00524 ad_v.num = (int)mot[1];
00525 if (lire_mot(pid, ad_v, mot) == -1)
00526 return;
00527 switch ((int)mot[0]) {
00528 case I_AFFECT:
00529 break;
00530 case I_AFFECTP:
00531 break;
00532 case I_SI:
00533 break;
00534 case I_SAUT:
00535 break;
00536 case I_CPILE:
00537 break;
00538 case I_DPILE:
00539 break;
00540 case I_APPEL:
00541 break;
00542 case I_RETOUR:
00543 break;
00544 case I_TEST:
00545 break;
00546 case I_TRAPPE:
00547 switch ((int)mot[2]) {
00548 case T_CLONE:
00549 break;
00550 case T_RECOUVRE:
00551 break;
00552 case T_ATTENDS:
00553 break;
00554 case T_FIN:
00555 break;
00556 case T_CAPTURE:
00557 break;
00558 case T_EMETS:
00559 break;
00560 case T_ID:
00561 break;
00562 case T_IDP:
00563 break;
00564 case T_LIT:
00565 break;
00566 case T_ECRIT:
00567 break;
00568 default:
00569
00570 return;
00571 }
00572 break;
00573 case I_AFFECTSP:
00574 break;
00575 case I_DONNEES:
00576 break;
00577 default:
00578
00579 return;
00580 }
00581 }