fix: mise en place d'une commande PS, correction de bogues

This commit is contained in:
Nicolas Hordé 2018-12-22 00:19:34 +01:00
parent 945bca37b5
commit 6776594ece
6 changed files with 90 additions and 40 deletions

View File

@ -10,13 +10,15 @@
#define MAXNUMPROCESS 256 #define MAXNUMPROCESS 256
#define PROCESS_STATUS_FREE 0x0 #define PROCESS_STATUS_FREE 0x0
#define PROCESS_STATUS_READY 0xF0 #define PROCESS_STATUS_READY 0x1
#define PROCESS_STATUS_RUN 0x1 #define PROCESS_STATUS_RUN 0x2
#define PROCESS_STATUS_SLEEP 0x2 #define PROCESS_STATUS_SLEEP 0x3
#define PROCESS_STATUS_ALL 0xFF
#define TASK_STATUS_READY 0x0 #define TASK_STATUS_READY 0x0
#define TASK_STATUS_RUN 0x1 #define TASK_STATUS_RUN 0x1
#define TASK_STATUS_STOP 0xFF #define TASK_STATUS_STOP 0x2
#define TASK_STATUS_ALL 0xFF
/* ELF type */ /* ELF type */
#define ET_NONE 0 //No file 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); pid_t exec(u8* entry, u8* args, bool kerneltask);
void switchtask(tid_t tid); void switchtask(tid_t tid);
process* getnextprocess(pid_t pid); process* getnextprocess(process *aprocess, u32 status);
task* getnexttask(void); task* getnexttask(task* atask, u32 status);
task* getschedule(void);
task* findtask(tid_t tid); task* findtask(tid_t tid);
task* findcurrenttask(void); task* findcurrenttask(void);
process* findprocess(pid_t pid); process* findprocess(pid_t pid);
process* findcurrentprocess(void); process* findcurrentprocess(void);
task* findfirsttask(process* aprocess);
tid_t createtask(pid_t pid,u8 *entry, bool kerneltask); tid_t createtask(pid_t pid,u8 *entry, bool kerneltask);
void deletetask(tid_t tid); void deletetask(tid_t tid);

View File

@ -30,3 +30,4 @@ int detectpci();
int showmem(); int showmem();
int testmem(); int testmem();
int testtask(); int testtask();
int ps();

View File

@ -152,10 +152,10 @@ void initprocesses(void)
processes = (process *) vmalloc(sizeof(process) * MAXNUMPROCESS); processes = (process *) vmalloc(sizeof(process) * MAXNUMPROCESS);
while (i < MAXNUMPROCESS) while (i < MAXNUMPROCESS)
{ {
processes[i].pid = NULL; processes[i].pid = i+1;
processes[i++].status = PROCESS_STATUS_FREE; processes[i++].status = PROCESS_STATUS_FREE;
} }
pid_t pid=getfreepid(); pid_t pid=(pid_t)1;
process *aprocess=findprocess(pid); process *aprocess=findprocess(pid);
if (aprocess==NULL) return NULL; if (aprocess==NULL) return NULL;
aprocess->pid = pid; aprocess->pid = pid;
@ -173,23 +173,17 @@ void initprocesses(void)
pid_t getfreepid(void) pid_t getfreepid(void)
{ {
if (lastpid==0 || lastpid>MAXNUMPROCESS) if ((u32)lastpid==0 || lastpid>MAXNUMPROCESS)
lastpid==1; lastpid=(pid_t)1;
u32 i = lastpid; process* aprocess=findprocess(lastpid);
u32 parsed = 0; aprocess=getnextprocess(aprocess, PROCESS_STATUS_FREE);
while (processes[i++].status != PROCESS_STATUS_FREE if (aprocess == NULL)
&& parsed++ < MAXNUMPROCESS)
{
if (i >= MAXNUMPROCESS)
i = 0;
}
if (parsed > MAXNUMPROCESS)
{ {
printf("PANIC: plus d'emplacement disponible pour un novueau processus\n"); printf("PANIC: plus d'emplacement disponible pour un novueau processus\n");
return NULL; return NULL;
} }
lastpid=i; lastpid=aprocess->pid;
return (pid_t)i; return lastpid;
} }
/*******************************************************************************/ /*******************************************************************************/
@ -285,40 +279,74 @@ void switchtask(tid_t tid)
iret(); iret();
} }
/*******************************************************************************/ /*******************************************************************************/
/* Cherche le prochain processus */ /* Cherche le prochain processus */
process* getnextprocess(pid_t pid)
process* getnextprocess(process* aprocess, u32 status)
{ {
u32 i = (u32) pid; u32 i = (u32) aprocess->pid;
while (processes[i++].status != PROCESS_STATUS_RUN) 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) if (i >= MAXNUMPROCESS)
i = 0; 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 */ /* Cherche la prochaine tâche */
task* getnexttask(void)
task* getschedule(void)
{ {
process *aprocess=findcurrentprocess(); process *aprocess=findcurrentprocess();
task *next=findcurrenttask();
u32 flag=0; u32 flag=0;
task *next;
while(flag<2) { while(flag<2) {
if (aprocess==NULL) return NULL; 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 (next!=NULL)
if (current.pid != next->tid.pid || next->tid.number>current.number)
return next;
if (current.pid==next->tid.pid && current.number==next->tid.number) if (current.pid==next->tid.pid && current.number==next->tid.number)
flag++; flag++;
} else
aprocess=getnextprocess(aprocess->pid); 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) if (atask->status == TASK_STATUS_READY)
{ {
atask->status = TASK_STATUS_RUN; atask->status = TASK_STATUS_RUN;
switchtask(tid); //switchtask(tid);
} }
sti(); sti();
} }
@ -463,7 +491,7 @@ pid_t createprocess(u8 *src, bool kerneltask)
current.number = 0; current.number = 0;
process* new=findcurrentprocess(); process* new=findcurrentprocess();
if (new==NULL) return NULL; if (new==NULL) return NULL;
new->pid = current.pid; //new->pid = current.pid;
new->pdd = virtual_pd_create(); new->pdd = virtual_pd_create();
TAILQ_INIT(&new->page_head); TAILQ_INIT(&new->page_head);
TAILQ_INIT(&new->task_head); TAILQ_INIT(&new->task_head);
@ -514,7 +542,7 @@ void runprocess(pid_t pid)
task *atask=findtask(tid); task *atask=findtask(tid);
if (atask==NULL) return; if (atask==NULL) return;
atask->status=TASK_STATUS_RUN; atask->status=TASK_STATUS_RUN;
switchtask(tid); //switchtask(tid);
} }
sti(); sti();
} }

View File

@ -81,11 +81,11 @@ __attribute__ ((noreturn)) void timer_handler(regs *dump)
showchar(0, 0, curs[curspos], 7); showchar(0, 0, curs[curspos], 7);
curspos = (curspos + 1) & 0x3; curspos = (curspos + 1) & 0x3;
time++; time++;
task *new=getnexttask(); task *new=getschedule();
if (new!=NULL) if (new!=NULL)
{ {
task *old=findcurrenttask(); task *old=findcurrenttask();
memcpy(&dump, &old->dump, sizeof(dump), 0); memcpy(dump, &old->dump, sizeof(regs), 0);
switchtask(new->tid); switchtask(new->tid);
} }
if (dump->cs==SEL_KERNEL_CODE) if (dump->cs==SEL_KERNEL_CODE)

View File

@ -45,6 +45,7 @@ static command commands[] = {
{"mem", "", &showmem}, {"mem", "", &showmem},
{"testmem", "", &testmem}, {"testmem", "", &testmem},
{"testtask", "", &testtask}, {"testtask", "", &testtask},
{"ps", "", &ps},
}; };
/*******************************************************************************/ /*******************************************************************************/
@ -88,6 +89,21 @@ int test(void)
return; 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 */ /* Test l'usage de création de tâche */

View File

@ -8,6 +8,7 @@
void main(void) void main(void)
{ {
print("demarrage...\r\n");
while (true) while (true)
{ {
if (getticks()%100000==0) if (getticks()%100000==0)