In unix how we can test or check race condition in c program by using multi threads

azaz's Avatar, Join Date: Feb 2009
Newbie Member
In unix how we can test or check race condition in any c program by using multi thread programming
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Could you give more details about what problem you're trying to solve? The question as stated doesn't make a lot of sense. A race condition is where the behaviour of the application critically depends on things happening in a specific order but where the order hasn't been enforced with critical sections, mutexes or other suitable lockout mechanisms, and typically you would know about them by the application intermittently not working properly, the actual behaviour depending on what exactly doesn't go in what order and what the implications of the reordering are. Consequently testing for them can be extremely difficult as the behaviour depends on many things including the overall system performance.

While race conditions can show up in multithreaded programs they can also show up in single threaded event-driven programs.
0
azaz's Avatar, Join Date: Feb 2009
Newbie Member
Hi,
Thanks 4 ur concern. I want to know any way is there to know race condition happening while executing the code.
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
If the order of events is not sequenced then you will get race conditions. For example if you have thread 1 that does tasks A,B,C, and thread 2 that does D,E,F, the only thing you can be certain of is that A will be done before B which will be done before C and likewise for D,E,F, you can't know whether any of A,B,C will occur before or after any of D,E,F. So if, for example, F is dependent on A and B having completed, then you must enforce a sequence point before F is allowed to start, for example:

thread 1:
perform A
perform B
set flag "A and B complete"
perform C

thread 2:
perform D
perform E
if flag "A and B complete" is not set wait until it is
perform F.

So in fact race conditions happen all the time, and without the AB flag we have no way of knowing if the completed event sequence in this example will be AD BE CF, or AB DE CF, or ABC DEF, or D ABC EF, or A DEF BC etc. A race condition only matters when something in one thread is dependent on something in another; if that is not the case in your application then you don't need to worry about race conditions.
0
azaz's Avatar, Join Date: Feb 2009
Newbie Member
HI,
Thanks 4 ur concern .
And i need to know after compilation or while compilattion how we can say any programme is victim of Race Condition or not
0
azaz's Avatar, Join Date: Feb 2009
Newbie Member
Hi,

My actual problem is below one. And iam mnot getting any idea please help me in this
Developing a script to detect race conditions in a given library or code using multi-thread programminging.
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
As far as I know this is not actually possible, and you have to design race conditions out rather than try to detect them after the event. If your code is modular with clean interfaces and limited or non-existent interdependency then it will be relatively easy for a human programmer to review the code and determine what might exist.

Essentially what you're asking for is an automated tool that detects bugs. No such thing I'm afraid. Good design and thorough testing is the only general solution for bugs.