From 85321ea6cc45a36bfea167e046637c5f0b0c14b9 Mon Sep 17 00:00:00 2001 From: RayAndrew Date: Sun, 16 Oct 2022 16:45:55 +0800 Subject: [PATCH] Avoid executing unnecessary fork() Ignore blank string, e.g., 1. '\n' 2. ' \n' 3. '\t \n' Fork() needs to execute a lot of instructions, especially before copy-on-write is supported. --- user/sh.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/user/sh.c b/user/sh.c index 836ebcbc7e..c9e992e68f 100644 --- a/user/sh.c +++ b/user/sh.c @@ -142,6 +142,25 @@ getcmd(char *buf, int nbuf) return 0; } +int +isblank(char c) +{ + // \t = 9, \n = 10, \v = 11, \f = 12, \r = 13 + return (('\t' <= c && c <= '\r') || (c == ' ')); +} + +int +isblankstr(const char *s) +{ + char c = 0; + while((c = *s++)){ + if(!isblank(c)){ + return 0; + } + } + return 1; +} + int main(void) { @@ -165,6 +184,8 @@ main(void) fprintf(2, "cannot cd %s\n", buf+3); continue; } + if(isblankstr(buf)) + continue; if(fork1() == 0) runcmd(parsecmd(buf)); wait(0);