The shell now forks, uses exit, and wait.

This commit is contained in:
Jonas 'Sortie' Termansen 2011-11-06 23:51:02 +01:00
parent 024f1581ea
commit b27fa68c81
3 changed files with 22 additions and 10 deletions

View File

@ -75,12 +75,7 @@ namespace Maxsi
DUAL_FUNCTION(void, exit, Exit, (int status))
{
// TODO: Once wait() works and is used in the shell, call _exit!
//_exit(status);
const char* sh = "sh";
const char* argv[] = { sh };
Execute("sh", 1, argv);
while(true);
_exit(status);
}
DUAL_FUNCTION(pid_t, fork, Fork, ())

View File

@ -1,4 +1,5 @@
#include <stdio.h>
#include <sys/wait.h>
#include <libmaxsi/platform.h>
#include <libmaxsi/process.h>
#include <libmaxsi/thread.cpp>
@ -7,8 +8,9 @@ using namespace Maxsi;
int parent(pid_t childid)
{
// TODO: wait for child to finish.
while ( true ) { Thread::Sleep(100000); }
int status;
waitpid(childid, &status, 0);
return status;
}
int child()
@ -18,7 +20,7 @@ int child()
Process::Execute(programname, 1, newargv);
return 1;
return 2;
}
int main(int argc, char* argv[])
@ -31,7 +33,7 @@ int main(int argc, char* argv[])
if ( childpid < 0 )
{
printf("init: unable to fork a child\n");
return 1;
return 2;
}
return ( childpid == 0 ) ? child() : parent(childpid);

View File

@ -1,4 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <libmaxsi/platform.h>
#include <libmaxsi/process.h>
#include <libmaxsi/sortix-keyboard.h>
@ -43,12 +46,24 @@ void command()
if ( String::Compare(command, "$$") == 0 ) { printf("%u\n", Process::GetPID()); return; }
if ( String::Compare(command, "$PPID") == 0 ) { printf("%u\n", Process::GetParentPID()); return; }
if ( String::Compare(command, "exit") == 0 ) { exit(0); return; }
pid_t child = fork();
if ( child < 0 ) { printf("fork failed\n"); return; }
if ( child != 0 )
{
int status;
pid_t childpid = wait(&status);
return;
}
// Replace the current process with another process image.
Process::Execute(command, 0, NULL);
// This is clever. This only happens if the program didn't change.
printf("%s: command not found\n", command);
exit(127);
}
int main(int argc, char* argv[])