Xref: news2.mv.net comp.os.msdos.djgpp:1584 From: vogelsan AT i50s20 DOT ira DOT uka DOT de (Holger Vogelsang) Newsgroups: comp.os.msdos.djgpp Subject: Re: Multi-threads? Date: 1 Mar 1996 08:50:20 GMT Organization: IMA Karlsruhe Lines: 111 Sender: vogelsan AT i50s20 (Holger Vogelsang) Message-ID: <4h6doc$n69@nz12.rz.uni-karlsruhe.de> References: NNTP-Posting-Host: i50s20.ira.uka.de Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit To: Tom Sgouros DJ-Gateway: from newsgroup comp.os.msdos.djgpp >Has anyone who reads this come up with any kind of hack to do rudimentary >multi-threading on a DOS-box? Or can anyone point me to a possible >solution to the porting of a multi-threading application? Here is very simple solution for two processes, using longjmp, setjmp for task switching (without preemption).#include This can be expanded for many processes by modifying the "scheduler" lwp_switch. This examples works under DJGPP 1.x and DJGPP 2.0. The stack size for each process is set to 2000 bytes in this example. Hope this helps. #include #include #define DEFAULT_STACK_SIZE 2000 jmp_buf context[ 2 ]; short running_proc = 0; void lwp_switch() { if( !setjmp( context[ running_proc ] )) { running_proc++; running_proc %= 2; longjmp( context[ running_proc ], 1 ); } } void Proc1() { long i = 0; printf( "Proc 1 started\n" ); while( 1 ) { printf( "count1 = %d\n", i++ ); lwp_switch(); } } void Proc2() { long i = 0x800000; printf( "Proc 2 started\n" ); while( 1 ) { printf( "count2 = %d\n", i-- ); lwp_switch(); } } void InitProcs() { setjmp( context[ 0 ] ); setjmp( context[ 1 ] ); /* define entry point and stack */ /* first: DJGPP 2.0 */ #if defined(DJGPP) && (DJGPP >= 2) context[ 0 ][ 0 ].__eip = (unsigned long)Proc1; context[ 0 ][ 0 ].__esp = (unsigned long)malloc( DEFAULT_STACK_SIZE ) + DEFAULT_STACK_SIZE - sizeof( double ); context[ 1 ][ 0 ].__eip = (unsigned long)Proc2; context[ 1 ][ 0 ].__esp = (unsigned long)malloc( DEFAULT_STACK_SIZE ) + DEFAULT_STACK_SIZE - sizeof( double ); #else /* DJGPP 1.x */ context[ 0 ][ 0 ].eip = (unsigned long)Proc1; context[ 0 ][ 0 ].esp = (unsigned long)malloc( DEFAULT_STACK_SIZE ) + DEFAULT_STACK_SIZE - sizeof( double ); context[ 1 ][ 0 ].eip = (unsigned long)Proc2; context[ 1 ][ 0 ].esp = (unsigned long)malloc( DEFAULT_STACK_SIZE ) + DEFAULT_STACK_SIZE - sizeof( double ); #endif longjmp( context[ 0 ], 1 ); } void main() { InitProcs(): /* should never return */ } ----------------------------------------------------------------- Holger Vogelsang Institut fuer Mikrorechner und Automation, Universitaet Karlsruhe Haid-und-Neu-Strasse 7, D-76131 Karlsruhe Tel.: +49+721 6083170, Fax : +49+721 661732 WWW: http://www-ima.ira.uka.de/mitarbeiter/vogelsan/