Page 1 of 1

multi tasking: run executables

Posted: Fri Oct 03, 2008 2:38 pm
by i586coder
Have a nice day guy's:

today i have very interested question,most of us will faced :idea:

HOW to run 2,3,...,n executables @ the same time
the code bellow is a part of my UNEXT/os kernel

Code: Select all

int EXE_returnedValue;//var. to save returned offset

byte exec(char* fileName,void* _segment,char* parameter){
     byte fileRetValue;
     fileRetValue=_fopen(fileName,r); //open file
     switch(fileRetValue){
      case 0: return DIR_NOT_FOUND;
      case 1: return FILE_NOT_FOUND;
     }
      _fcopy2segment(_segment); //copy content of file to buffer
     _fclose(); //close file

     _saveTCRegs(); //save turbo C registres and flags (AX,..)

     EXE_returnedValue=(int)return_point;//get returned offset

     _decompressMZ();//get CS,DS,SS from EXE
     _setupMZ(); //setup new Regs,Flags for EXE
     asm{ push MZ_segment //point to jump
          push MZ_offset  //CS:IP
          retf            //jump to our EXE 
     }

asm return_point label byte //when EXE finish will go here throu interrupt
 return EXE_DONE;
}

void interrupt killEXE(){//this method must call from EXE to KILL process
    _loadTCRegs();//return to previous TC registers
    memset(_segment,NULL,_fSize());//clear buffer
     asm{mov ax,cs
         push ax
         push EXE_returnedValue
         retf
     }
}

main(){
  THREAD _thread;     _thread.add(exec("system/Fileviw.exe",0x20000000L,"doc/test.txt"));
_thread.add(exec("system/bscript.exe",0x30000000L,"script/intro.bs"));
_thread.schedule();
}
the problem is:
when i tried this code in my multi thread, each EXE will start from begining #-o

need your opinions :!: :!: :!:

Re: multi tasking: run executables

Posted: Fri Oct 03, 2008 3:48 pm
by Kieran
Well, do you mean that each time your sceduler switches to the process containing the loaded program the program starts again from the beginning?

If so, check that you are saving EIP correctly.

Re: multi tasking: run executables

Posted: Sat Oct 04, 2008 12:55 am
by i586coder
ok,
the function:
_saveTCRegs();
save all registers even DS,CS,IP(code offset),...

i think
exec(char*,void*,char*);
need some fix,

anyway, if you have another method to run .EXE better than my method,please let my know [-o<


ThankX

Re: multi tasking: run executables

Posted: Sat Oct 04, 2008 3:02 am
by ru2aqare
AhmadTayseerDajani wrote:ok,
the function:
_saveTCRegs();
save all registers even DS,CS,IP(code offset),...

i think
exec(char*,void*,char*);
need some fix,

anyway, if you have another method to run .EXE better than my method,please let my know [-o<


ThankX
Saves the registers where? If it uses globals, it probably has reentrancy issues. And execution of image files (be it EXE or another format) is supposed to start at the entry point designated by its header, isn't it? Sorry, but I don't understand what the problem is, really.

Re: multi tasking: run executables

Posted: Sat Oct 04, 2008 3:20 am
by i586coder
Saves the registers where? If it uses globals, it probably has reentrancy issues. And execution of image files (be it EXE or another format) is supposed to start at the entry point designated by its header, isn't it? Sorry, but I don't understand what the problem is, really.
yes, regitsters saved in globals

Code: Select all

 void saveTCRegs(){
  _ax=_AX;
  :
  : 
  _cflags=_FLAGS;
 }
execution image is 16 bit MZ executable :shock:

Re: multi tasking: run executables

Posted: Sat Oct 04, 2008 5:11 am
by ru2aqare
AhmadTayseerDajani wrote:yes, regitsters saved in globals

Code: Select all

 void saveTCRegs(){
  _ax=_AX;
  :
  : 
  _cflags=_FLAGS;
 }
There is two problem with storing the processor context in global variables. First, if two threads concurrently try to save the context, either thead A overwrites thread B's context, or they write at the exact same time, resulting in a context X that is neither A nor B. I assume you use threads from this code:

Code: Select all

  THREAD _thread;     _thread.add(exec("system/Fileviw.exe",0x20000000L,"doc/test.txt"));
_thread.add(exec("system/bscript.exe",0x30000000L,"script/intro.bs"));
_thread.schedule();
Second, if you load an executable A that in turn loads another executable B, then the context that was saved before A was executed is overwritten by the context saved before B is executed.
AhmadTayseerDajani wrote:execution image is 16 bit MZ executable :shock:
Yeah, I got that from the Turbo C part.

Re: multi tasking: run executables

Posted: Sat Oct 04, 2008 6:51 am
by i586coder
ru2aqare wrote: There is two problem with storing the processor context in global variables. First, if two threads concurrently try to save the context, either thead A overwrites thread B's context, or they write at the exact same time, resulting in a context X that is neither A nor B.

Second, if you load an executable A that in turn loads another executable B, then the context that was saved before A was executed is overwritten by the context saved before B is executed.
Oh my guess, i forgot that,...,thank you ru2aqare for your help,& I am gona fix up my code by adding struct to save each EXE registers individually

ThAnKX AgAiN =D>