Ticksize problem

Hi!

I am newbie on QNX
I ahve program which has threads that starts more threads and scheduled On round robin
it seems to be working fine but

the threads are preempting with a time slice for the Roundrobin is
very long i.e at the range of about 4-5 minutes. and i didn’t wait for
that long to findout.

may be somebody else changed the ClockPeriod() so i did a fresh
installation and repeated the same still i am getting the same result.

Please somebody pont me in a direction so that i can debug this and
see the roundrobin working as it suppose to with millisecond
timeslices instead of 4-5 minites

here is my actual code

Regards

Sridhar

#include <stdio.h>
#include <sys/neutrino.h>
#include <pthread.h>
#include <sched.h>

#define NumThreads 16

volatile int var1;
volatile int var2;

void *update_thread (void *);

char *progname = “thread2”;

main ()

{
pthread_t threadID [NumThreads];
pthread_attr_t attrib;
struct sched_param param;
int i;

var1 = var2 = 0;       

printf ("%s:  starting; creating threads\n", progname);

pthread_attr_init (&attrib);
pthread_attr_setinheritsched (&attrib, PTHREAD_EXPLICIT_SCHED);
pthread_attr_setschedpolicy (&attrib, SCHED_RR);
param.sched_priority = getprio (0);         
pthread_attr_setschedparam (&attrib, &param);
attrib.flags |= PTHREAD_CANCEL_ASYNCHRONOUS;

 for (i = 0; i < NumThreads; i++) {
    pthread_create (&threadID [i], &attrib, &update_thread, (void

*) i);
}

sleep (20);

/*  and then kill them all */

printf ("%s:  stopping; cancelling threads\n", progname);

for (i = 0; i < NumThreads; i++) {
    pthread_cancel (threadID [i]);
}

printf ("%s:  all done, var1 is %d, var2 is %d\n", progname, var1,

var2);

fflush (stdout);

exit (0);

}

/*

  • the actual thread.
  • The thread ensures that var1 == var2. If this is not the
  • case, the thread sets var1 = var2, and prints a message.
  • Var1 and Var2 are incremented.

*/

void do_work()

{
static int var3;
var3++;

if ( !(var3 % 100000) )
    printf ("%s: thread %d did some work\n", progname,

pthread_self());
}

void *
update_thread (void *i)

{
while (1) {
if (var1 != var2) {
int lvar1, lvar2;
lvar1 = var1; lvar2 = var2;
var1 = var2;
printf ("%s: thread %d, var1 (%d) is not equal to var2
(%d)!\n",
progname, (int) i, lvar1, lvar2);
}

    /* do some work here */

    do_work();
    var1++;
    var2++;
}

return (NULL);

}

Time slice and tick size are two things, the time slice cannot be set and is i beleive 50ms.

The main thread, the one that start all the other thread, isn’t set to RR. Hence as soon as the first update thread starts the main thread never gets the CPU, since it isn’t set to RR by default. It probably never get a change to start the other update thread, nor get to run sleep() and the rest of its code.

Also beware that thinks such as

var3++;
if ( ! (var3 % 10000) ) …

May not behave the way you want it (or maybe it does). Thread can be swithec between the var3++ and the evaluation. It is theoreticaly possible the expression never evaluation to true. Because each time var3++ becomes a multiple of 10000 it could be incremented by another thread just before the evalution. This is VERY VERY VERY unlikely to happen, but will at leas happen one during the execution of the program if it runs long enough. It’s a basics rules that if things can go wrong they will ;-)

Time Slice = 4 * Clock Period

Hi again

after executing this program i did a pidin

and found out that all the thread that generated are in round robin and thread one is sleeping and all the thread that are created atre at priority 10

then atleat i expected the thread that are created by the program should be round robin with the time slice

but it is not the case
even these threads the therad which is running doesn’t renounce the cpu for atleast 4 minutes the gives the control to next thread???

why i donot know
Sridhar