Page 1 of 1

Exec

Posted: Sat Jan 24, 2009 11:32 pm
by System123
I have finally gotten my multitasking to work, and I have now implemented an Exec function that allows me to execute bin and com files. This will later be upgraded to run ELF files. At the moment I just need some advice.

When the exec function is called, I load the file to memory, then I create a new user task for the program. I now need to implement a way to kill the task and free the memory used by it when the program is finished running. My problem is how would I implement this. Would adding a system call in the program at the end be the easiest way? ie:

Code: Select all


...Program Code...

mov eax, 0x00 //System call to kill task
int 0x80

Or is there a better way to do this?

Re: Exec

Posted: Sat Jan 24, 2009 11:37 pm
by Hangin10
AFAIK, it's usually (always?) done like that, with that code probably ending the routine that called main.

Re: Exec

Posted: Sun Jan 25, 2009 8:13 am
by JamesM
Almost - under UNIX that call is executed when main returns, or when exit() is called. It's a function called _exit() - exit() calls all functions registered with atexit(), then falls through to _exit() which calls your OS cleanup code.

so in your crt0.c you'd normally have:

Code: Select all

_exit(main(argc, argv, env));