The shell now forks, uses exit, and wait.
This commit is contained in:
parent
024f1581ea
commit
b27fa68c81
|
@ -75,12 +75,7 @@ namespace Maxsi
|
||||||
|
|
||||||
DUAL_FUNCTION(void, exit, Exit, (int status))
|
DUAL_FUNCTION(void, exit, Exit, (int status))
|
||||||
{
|
{
|
||||||
// TODO: Once wait() works and is used in the shell, call _exit!
|
_exit(status);
|
||||||
//_exit(status);
|
|
||||||
const char* sh = "sh";
|
|
||||||
const char* argv[] = { sh };
|
|
||||||
Execute("sh", 1, argv);
|
|
||||||
while(true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
DUAL_FUNCTION(pid_t, fork, Fork, ())
|
DUAL_FUNCTION(pid_t, fork, Fork, ())
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
#include <libmaxsi/platform.h>
|
#include <libmaxsi/platform.h>
|
||||||
#include <libmaxsi/process.h>
|
#include <libmaxsi/process.h>
|
||||||
#include <libmaxsi/thread.cpp>
|
#include <libmaxsi/thread.cpp>
|
||||||
|
@ -7,8 +8,9 @@ using namespace Maxsi;
|
||||||
|
|
||||||
int parent(pid_t childid)
|
int parent(pid_t childid)
|
||||||
{
|
{
|
||||||
// TODO: wait for child to finish.
|
int status;
|
||||||
while ( true ) { Thread::Sleep(100000); }
|
waitpid(childid, &status, 0);
|
||||||
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
int child()
|
int child()
|
||||||
|
@ -18,7 +20,7 @@ int child()
|
||||||
|
|
||||||
Process::Execute(programname, 1, newargv);
|
Process::Execute(programname, 1, newargv);
|
||||||
|
|
||||||
return 1;
|
return 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char* argv[])
|
int main(int argc, char* argv[])
|
||||||
|
@ -31,7 +33,7 @@ int main(int argc, char* argv[])
|
||||||
if ( childpid < 0 )
|
if ( childpid < 0 )
|
||||||
{
|
{
|
||||||
printf("init: unable to fork a child\n");
|
printf("init: unable to fork a child\n");
|
||||||
return 1;
|
return 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
return ( childpid == 0 ) ? child() : parent(childpid);
|
return ( childpid == 0 ) ? child() : parent(childpid);
|
||||||
|
|
|
@ -1,4 +1,7 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <sys/wait.h>
|
||||||
#include <libmaxsi/platform.h>
|
#include <libmaxsi/platform.h>
|
||||||
#include <libmaxsi/process.h>
|
#include <libmaxsi/process.h>
|
||||||
#include <libmaxsi/sortix-keyboard.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, "$$") == 0 ) { printf("%u\n", Process::GetPID()); return; }
|
||||||
if ( String::Compare(command, "$PPID") == 0 ) { printf("%u\n", Process::GetParentPID()); 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.
|
// Replace the current process with another process image.
|
||||||
Process::Execute(command, 0, NULL);
|
Process::Execute(command, 0, NULL);
|
||||||
|
|
||||||
// This is clever. This only happens if the program didn't change.
|
// This is clever. This only happens if the program didn't change.
|
||||||
printf("%s: command not found\n", command);
|
printf("%s: command not found\n", command);
|
||||||
|
|
||||||
|
exit(127);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char* argv[])
|
int main(int argc, char* argv[])
|
||||||
|
|
Loading…
Reference in New Issue