How can I use rand(), srand() function in CCS v4.2?
require? libraries?
I compiled program, there are no errors.
I included <stdlib.h>
But Program doesn't response in rand() function, when I run program.
This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
How can I use rand(), srand() function in CCS v4.2?
require? libraries?
I compiled program, there are no errors.
I included <stdlib.h>
But Program doesn't response in rand() function, when I run program.
Thanks for your reply.
I tested _c_int00 function. It is always returned the same address.
I found another.
I made a function, it is called by DSP/BIOS every 0.002 sec.
rand() function is in above function.
When rand() function is called, memories are broken.
So I changed rand() to printf().
When printf() fuction is called, memories are broken, too.
I think I know what's going on. rand is a non-reentrant function; you shouldn't call it from an interrupt function because you might be interrupting another call to rand. When used with DSP/BIOS, DSP/BIOS installs mutex handling for program state, such as the state of the random number generator (RNG). When execution enters rand, DSP/BIOS locks the RNG state so that no other thread can modify the state until rand has finished execution. However, you are interrupting that execution and calling rand again. The second call cannot proceed because the mutex it needs is already locked, so the interrupt cannot finish, so the first call to rand cannot release the mutex. This is called deadlock. rand and printf, as well as many other functions, are non-reentrant. You should not call any of them from any kind of interrupt.
I had used rand() function in DSP/BIOS at CCSv2.1 before using CCSv4.2
Do you say rand() function changed above CCSv4 version ?
If changed, Which function replace rand() function ?
Thank you for your reply.
You said "There is no replacement for rand() function TI provides". Non-reentrant function !!
If so, how do I solve this problem?
I have to use rand() function or equivalent function in DSP/BIOS Task.
Should I make other function ?
Maybe, If I will make, int Random (void)
static _DATA_ACCESS unsigned long next = 1;
_CODE_ACCESS int Random(void)
{
int r;
// _lock();
next = next * 1103515245 + 12345;
r = (int)((next/65536) % ((unsigned long)RAND_MAX + 1));
// _unlock();
return r;
}
Like this way, I will make equivalent function? This is provided from TI.
I want to know better way than this way
If you absolutely must have a RNG in two or more threads, you basically have two options
If you simply remove the locks, you will have a race condition between threads. You may have two threads getting the same value from rand at the same time. You may or may not care about this; I don't know.