Mail Archives: djgpp/1996/03/01/21:06:31
>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 <stdio.h>
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 <setjmp.h>
#include <stdio.h>
#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/
- Raw text -