Skip to content

Commit 86fe2bf

Browse files
committed
实现ps命令
1 parent ef66599 commit 86fe2bf

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

src/sysapp/shell.c

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,21 +144,40 @@ static inline void foreach(list_t list){
144144
}
145145
}
146146

147+
// 实现ps命令
148+
static void ps(){
149+
extern pcb_t *running_proc_head;
150+
// 找出最长进程名
151+
pcb_t *longest_name = running_proc_head;
152+
int longest_name_len = 0;
153+
while(longest_name != NULL){
154+
if(strlen(longest_name->name) > longest_name_len)
155+
longest_name_len = strlen(longest_name->name);
156+
longest_name = longest_name->next;
157+
}
158+
pcb_t *pcb = running_proc_head;
159+
printk("PID %-*s RAM Level Priority Time\n",longest_name_len,"NAME");
160+
while(pcb != NULL){
161+
printk("%-5d%-*s%10d %-8s%-10d%-d\n",pcb->pid,longest_name_len,pcb->name,pcb->program_break_end - pcb->program_break,pcb->task_level == TASK_KERNEL_LEVEL ? "Kernel" : pcb->task_level == TASK_SYSTEM_SERVICE_LEVEL ? "System" : "User",pcb->task_level,pcb->cpu_clock);
162+
pcb = pcb->next;
163+
}
164+
}
165+
147166
extern uint32_t phy_mem_size;
148167

149168
static void sys_info(){
150169
cpu_t *cpu = get_cpuid();
151170

152171
extern void* program_break;
153172
extern void* program_break_end;
154-
uint32_t bytes = get_kernel_memory_usage() + (uint32_t)program_break + (uint32_t)program_break_end;
173+
uint32_t bytes = get_kernel_memory_usage() + (uint32_t)program_break_end - (uint32_t)program_break;
155174

156175
extern pcb_t *running_proc_head;
157176
pcb_t *pcb = running_proc_head;
158177
while(pcb != NULL){
159178
pcb = pcb->next;
160179
if(pcb->task_level != TASK_KERNEL_LEVEL){
161-
bytes += (uint32_t)pcb->program_break + (uint32_t)pcb->program_break_end;
180+
bytes += (uint32_t)pcb->program_break_end - (uint32_t)pcb->program_break;
162181
}
163182
}
164183
int memory = (bytes > 10485760) ? bytes/1048576 : bytes/1024;
@@ -234,6 +253,8 @@ void setup_shell(){
234253
shutdown_os();
235254
else if(!strcmp("sysinfo",argv[0]))
236255
sys_info();
256+
else if(!strcmp("ps",argv[0]))
257+
ps();
237258
else{
238259
int pid;
239260
if((pid = create_user_process(argv[0],com_copy,"User",TASK_APPLICATION_LEVEL)) == -1)

0 commit comments

Comments
 (0)