From 6776594ece7d71f919aa99d074a0e5bcfedbfade Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20Hord=C3=A9?= Date: Sat, 22 Dec 2018 00:19:34 +0100 Subject: [PATCH] fix: mise en place d'une commande PS, correction de bogues --- include/process.h | 16 +++++---- include/shell.h | 1 + lib/process.c | 92 ++++++++++++++++++++++++++++++----------------- lib/scheduler.c | 4 +-- lib/shell.c | 16 +++++++++ programs/test2.c | 1 + 6 files changed, 90 insertions(+), 40 deletions(-) diff --git a/include/process.h b/include/process.h index e0d478d..57ada0f 100644 --- a/include/process.h +++ b/include/process.h @@ -10,13 +10,15 @@ #define MAXNUMPROCESS 256 #define PROCESS_STATUS_FREE 0x0 -#define PROCESS_STATUS_READY 0xF0 -#define PROCESS_STATUS_RUN 0x1 -#define PROCESS_STATUS_SLEEP 0x2 +#define PROCESS_STATUS_READY 0x1 +#define PROCESS_STATUS_RUN 0x2 +#define PROCESS_STATUS_SLEEP 0x3 +#define PROCESS_STATUS_ALL 0xFF #define TASK_STATUS_READY 0x0 #define TASK_STATUS_RUN 0x1 -#define TASK_STATUS_STOP 0xFF +#define TASK_STATUS_STOP 0x2 +#define TASK_STATUS_ALL 0xFF /* ELF type */ #define ET_NONE 0 //No file type @@ -196,12 +198,14 @@ pid_t clone(void); pid_t exec(u8* entry, u8* args, bool kerneltask); void switchtask(tid_t tid); -process* getnextprocess(pid_t pid); -task* getnexttask(void); +process* getnextprocess(process *aprocess, u32 status); +task* getnexttask(task* atask, u32 status); +task* getschedule(void); task* findtask(tid_t tid); task* findcurrenttask(void); process* findprocess(pid_t pid); process* findcurrentprocess(void); +task* findfirsttask(process* aprocess); tid_t createtask(pid_t pid,u8 *entry, bool kerneltask); void deletetask(tid_t tid); diff --git a/include/shell.h b/include/shell.h index 76b43cb..4a46ba8 100644 --- a/include/shell.h +++ b/include/shell.h @@ -30,3 +30,4 @@ int detectpci(); int showmem(); int testmem(); int testtask(); +int ps(); diff --git a/lib/process.c b/lib/process.c index b33ce75..4cc4ede 100644 --- a/lib/process.c +++ b/lib/process.c @@ -152,10 +152,10 @@ void initprocesses(void) processes = (process *) vmalloc(sizeof(process) * MAXNUMPROCESS); while (i < MAXNUMPROCESS) { - processes[i].pid = NULL; + processes[i].pid = i+1; processes[i++].status = PROCESS_STATUS_FREE; } - pid_t pid=getfreepid(); + pid_t pid=(pid_t)1; process *aprocess=findprocess(pid); if (aprocess==NULL) return NULL; aprocess->pid = pid; @@ -173,23 +173,17 @@ void initprocesses(void) pid_t getfreepid(void) { - if (lastpid==0 || lastpid>MAXNUMPROCESS) - lastpid==1; - u32 i = lastpid; - u32 parsed = 0; - while (processes[i++].status != PROCESS_STATUS_FREE - && parsed++ < MAXNUMPROCESS) - { - if (i >= MAXNUMPROCESS) - i = 0; - } - if (parsed > MAXNUMPROCESS) + if ((u32)lastpid==0 || lastpid>MAXNUMPROCESS) + lastpid=(pid_t)1; + process* aprocess=findprocess(lastpid); + aprocess=getnextprocess(aprocess, PROCESS_STATUS_FREE); + if (aprocess == NULL) { printf("PANIC: plus d'emplacement disponible pour un novueau processus\n"); return NULL; } - lastpid=i; - return (pid_t)i; + lastpid=aprocess->pid; + return lastpid; } /*******************************************************************************/ @@ -285,40 +279,74 @@ void switchtask(tid_t tid) iret(); } - /*******************************************************************************/ /* Cherche le prochain processus */ -process* getnextprocess(pid_t pid) + +process* getnextprocess(process* aprocess, u32 status) { - u32 i = (u32) pid; - while (processes[i++].status != PROCESS_STATUS_RUN) + u32 i = (u32) aprocess->pid; + u32 parsed = 0; + while (parsed++ < MAXNUMPROCESS && ((status != PROCESS_STATUS_ALL && processes[i++].status != status) || + (status == PROCESS_STATUS_ALL && processes[i++].status == PROCESS_STATUS_FREE))) { if (i >= MAXNUMPROCESS) i = 0; } - return &processes[i-1]; + if (parsed > MAXNUMPROCESS) + return NULL; + else + return &processes[i-1]; +} + +/*******************************************************************************/ +/* Cherche la prochaine tâche du processus */ + +task* getnexttask(task *atask, u32 status) +{ + task *next=atask; + while (next!=NULL) + { + next=TAILQ_NEXT(next, tailq); + if (next!=NULL && (status == TASK_STATUS_ALL || next->status == status)) + return next; + } + return NULL; } /*******************************************************************************/ /* Cherche la prochaine tâche */ -task* getnexttask(void) + +task* getschedule(void) { process *aprocess=findcurrentprocess(); + task *next=findcurrenttask(); u32 flag=0; - task *next; while(flag<2) { if (aprocess==NULL) return NULL; - TAILQ_FOREACH(next, &aprocess->task_head, tailq) + if (next==NULL) + next=findfirsttask(aprocess); + while(true) { - if (next->status == TASK_STATUS_RUN) - if (current.pid != next->tid.pid || next->tid.number>current.number) - return next; + if (next!=NULL) if (current.pid==next->tid.pid && current.number==next->tid.number) flag++; - } - aprocess=getnextprocess(aprocess->pid); + else + return next; + else + break; + next=getnexttask(next,TASK_STATUS_RUN); + } + aprocess=getnextprocess(aprocess,PROCESS_STATUS_RUN); } - return NULL; +} + +/*******************************************************************************/ +/* Cherche la première tâche du processeur */ + +task* findfirsttask(process* aprocess) +{ + task *first; + return TAILQ_FIRST(&aprocess->task_head); } /*******************************************************************************/ @@ -371,7 +399,7 @@ void runtask(tid_t tid) if (atask->status == TASK_STATUS_READY) { atask->status = TASK_STATUS_RUN; - switchtask(tid); + //switchtask(tid); } sti(); } @@ -463,7 +491,7 @@ pid_t createprocess(u8 *src, bool kerneltask) current.number = 0; process* new=findcurrentprocess(); if (new==NULL) return NULL; - new->pid = current.pid; + //new->pid = current.pid; new->pdd = virtual_pd_create(); TAILQ_INIT(&new->page_head); TAILQ_INIT(&new->task_head); @@ -514,7 +542,7 @@ void runprocess(pid_t pid) task *atask=findtask(tid); if (atask==NULL) return; atask->status=TASK_STATUS_RUN; - switchtask(tid); + //switchtask(tid); } sti(); } diff --git a/lib/scheduler.c b/lib/scheduler.c index 1dda468..e76ae3f 100644 --- a/lib/scheduler.c +++ b/lib/scheduler.c @@ -81,11 +81,11 @@ __attribute__ ((noreturn)) void timer_handler(regs *dump) showchar(0, 0, curs[curspos], 7); curspos = (curspos + 1) & 0x3; time++; - task *new=getnexttask(); + task *new=getschedule(); if (new!=NULL) { task *old=findcurrenttask(); - memcpy(&dump, &old->dump, sizeof(dump), 0); + memcpy(dump, &old->dump, sizeof(regs), 0); switchtask(new->tid); } if (dump->cs==SEL_KERNEL_CODE) diff --git a/lib/shell.c b/lib/shell.c index 91bfafc..a909024 100644 --- a/lib/shell.c +++ b/lib/shell.c @@ -45,6 +45,7 @@ static command commands[] = { {"mem", "", &showmem}, {"testmem", "", &testmem}, {"testtask", "", &testtask}, + {"ps", "", &ps}, }; /*******************************************************************************/ @@ -88,6 +89,21 @@ int test(void) return; } +/*******************************************************************************/ +/* Afiche les processus */ + +int ps() +{ + print("Processus en memoire\r\n[ PID | Status |\r\n"); + process* aprocess=findprocess((pid_t)1); + while(true) + { + printf("|%Y|%Y|\r\n",(u32)aprocess->pid,aprocess->status); + aprocess=getnextprocess(aprocess,PROCESS_STATUS_ALL); + if (aprocess==NULL || aprocess->pid==(pid_t)1) break; + } +} + /*******************************************************************************/ /* Test l'usage de création de tâche */ diff --git a/programs/test2.c b/programs/test2.c index a974baf..28b39c6 100644 --- a/programs/test2.c +++ b/programs/test2.c @@ -8,6 +8,7 @@ void main(void) { + print("demarrage...\r\n"); while (true) { if (getticks()%100000==0)