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

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

How are you creating the race condition?

You can easily prevent race conditions by creating a temporary file and using rename() to replace the file after checking the timestamp of the original file.

If only your program is changing the file, you can also use flock(fd, LOCK_EX) so that if another instance of your program also uses flock() it will block until the other instance finishes processing or calls flock(fd, LOCK_UN).

Keep in mind that flock() doesn't prevent file unlink()ing.

Thans 4 ur concern

My actual problem is below one. And iam not getting any idea please help me in this
" Developing a script to detect race conditions in a given library or code using multi-thread programming "

That is a very...broad...problem. If you don't have any details or context it will be difficult to help you.

Script to detect race conditions in a given library or code? So you want something that will load the library or disassemble the library and search for possible race conditions? You also want that script to analyze plain code for race conditions? This is FAR from being simple. I'll post the possible solutions:

1) use some program (example: objdump) to disassemble the whole library and simply use regex to search for unsafe calls/interrupts.

2) load/use the library (dlopen/dlsym/dlclose) in thread1 and use thread2 to analyze calls for open() with ptrace() to check if there is any possible race condition.

3) Why is multi-threading an issue? Is this homework?

I guess some code analyzer or compiler could create a dictionary of all variables and identifiable memory addresses that are accessible by more than one thread and are not guarded with some kind of mutex. OpenMP is an Intel-sponsored and industry standard to help write parallel programs in such a fashion; it relies on compiler "hints" to tell when it's safe to thread over a section of code, and which variables need to be mutexed.