Dear Sir,
We are from BNF Technology Inc, Taejon, South Korea.
We are the QNX 4.0 license holder and using QNX OS as a server in our
project. This project is fully realtime based. We have extensively used many
features of QNX. But, we stuck at “Thread programming”.
The exact situation is :
(Please, refer the following sample code)
In our program there are two functions which should be executed
continuously, so we implemented these functins in threads. When we compiled
our program, it was compiled successufuly but when executed, it was giving
error for thread 1 “Stack overflow”. Then we reduced the size of stack,
again compiled and executed. then it was giving diferent error “Segmentation
error”. We thought this may be because to less Stack size. So we tried with
all the possibilities of the stack size (increasing and decreasing), but
still we are getting the same errors altenatively. We are not able to decide
the stack size. The used functions are very big. We tried to optimize these
functions also.
So our query is whether, there is any procedure or function which can decide
the stack size automatically for each function in the runtime. In our
program the stack size is constant which is needed to pass in the
“_beginthread” function. Or is there any alternative method to implement
threads? Please, suggest us.
// Sample program
// Reference : From the Help of QNX OS
// Search Topic : _beginthread
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stddef.h>
#include <malloc.h>
#include <process.h>
#define FAR
#define STACK_SIZE 1024 // Stack size for (_386) 32 bit compiler
char *stack1, *stack2;
int ThreadId1;
int ThreadId2;
static volatile int WaitForThread;
// Functions
void Function1()
{
// This is just a sample and we are not passing anything to this
function.
.
.
.
.
.
.
}
void Functin2()
{
// This is just a sample and we are not passing anything to this
function.
.
.
.
.
.
}
// Thread 1
void FAR Thread1( void FAR *parm )
{
printf( “Thread1 ID = %x\n”, *_threadid );
while(1)
{
Function1();
printf( “Inside Thread1 ---------------\n”);
sleep(1);
}
WaitForThread = 0;
_endthread();
}
//Thread 2
void FAR Thread2(void FAR *parm)
{
printf( “Thread2 ID = %x\n”, *_threadid );
while(1)
{
Function2();
printf( “--------------- Inside Thread2\n”);
sleep(1);
}
WaitForThread = 0;
_endthread();
}
int main( void )
{
char *Temp = NULL;
// Allocate the memory
stack1 = (char *) malloc( STACK_SIZE );
stack2 = (char *) malloc( STACK_SIZE );
WaitForThread = 1;
printf(“Before Initialization of Threads\n”);
// Begin both threads
ThreadId1 = _beginthread( Thread1, stack1, STACK_SIZE, Temp);
ThreadId2 = _beginthread( Thread2, stack2, STACK_SIZE, Temp);
printf(“After Initialization of Threads\n”);
// Continue executing
while( WaitForThread ) sleep( 1 );
return( EXIT_SUCCESS );
}
Thank you.
Sunil J. Warke
BNF Technology, Inc.,
150, Duckjin-Dong, Yusong-Ku,
Taejon, South Korea.
Zip: 305 353
Tel: (82)(42) 868 2817
Fax: (82)(42) 862 8828
Mob: 011 9696 2288
Website: http://www.bnftech.com
Email: sunil@bnftech.com