constchar*CLEAR_SCREEN_ANSI=" \e[1;1H\e[2J";//These two lines clear the console
write(STDOUT_FILENO,CLEAR_SCREEN_ANSI,12);//These two lines clear the console
}
voidloop(void){
char*line;
char**args;
intstatus;
while(1){
pid_tpid,wpid;
printshell();
line=read_line();
intsize=strlen(line);
boolbackground=true;
if(line[size-2]=='&'){
background=false;
line[size-2]='\0';
}
args=split_line(line);
if(strcmp(line,"logout")!=0){
pid=fork();
}
else{
printf("press y to quit\n");
charc=getchar();
if('y'==c){
break;
}
continue;
}
if(pid==0){
// Child process
if(execvp(args[0],args)==-1){// excute command
perror("something bad happen");
}
exit(EXIT_FAILURE);
}elseif(pid<0){
// Error forking
perror("something bad happen");
}else{
// Parent process
if(background){//parent process because pid bigger than 0
do{
wpid=waitpid(pid,&status,WUNTRACED);
}while(!WIFEXITED(status)&&!WIFSIGNALED(status));
}
else{
do{
wpid=waitpid(pid,&status,WNOHANG);
printf("%ld Started\n",pid);
}while(!WIFEXITED(status)&&!WIFSIGNALED(status));
}
}
}
}
intmain()
{
initshell();
loop();
return0;
}
/**
Was bedeutet Ausführung im Hintergrund? Ans: es wird sorfort weiter arbeiten ohne diese Process zu warten
Funktionsweise : wir haben einen Loop, am Anfang wird eine Line von terminal bekommen , dannch wird es zum viele token (um space oder andere zeichen zu vermeiden) aufgeteilt.
dann erstellen wir einen neuen Process mit fork().
Zombie: ist ein Prozess, der schon abgeschlossen ausgefuehrt ist (completed execution) ,hat aber immer noch einen Eintrag in der Prozesstabelle.
Vordergrundprozesse beziehen sich auf Anwendungen, die wir gerade ausführen und mit denen wir gerade interagieren. Dies gilt sowohl für grafische UI als auch für die Befehlszeile aka command line.