PDA

View Full Version : Testing race conditions in c program



myquidproquo
December 7th, 2008, 01:42 PM
Hi everyone.

I've written a server-client program with threads and processes and I would like to test it for race conditions...

Anyone know how can I make a script or any other way to test it?

I've tried writing some commands to a text file and initialize the program like this "./program < file.txt". It didn't work.

I'm still learning unix :)

Thank you.

Delever
December 7th, 2008, 03:40 PM
I used to avoid race conditions, not to test them :D

Seriously, you need to have good understanding of threads when you are writing code in which threads are involved. Usually race conditions are the hell to find and remove, and often requires basically reading source code to find them.

That said, you can modify your client to send some random data to test, then run lots of clients. If you can, test using real network, not loopback interface (127.0.0.1).

myquidproquo
December 7th, 2008, 05:04 PM
Ok, thank you :)

Another thing that I can't find the answer...
I'm using a fork execl().
How can I make it work if the program is compiled in another computer with different folder names? Is there a path variable I can use?

Even if I make the execl() path argument as "./program" it will look "program" in the current working directory, not the directory where the first program is installed...

odyniec
December 8th, 2008, 12:43 AM
How can I make it work if the program is compiled in another computer with different folder names? Is there a path variable I can use?

Even if I make the execl() path argument as "./program" it will look "program" in the current working directory, not the directory where the first program is installed...

If you declare your main() function like this:

int main(int argc, char *argv[])

then argv[0] will be the actual path used to execute your program, either absolute or relative. You can then use the dirname() function to extract the directory part. Here's a simple example:

#include <stdio.h>
#include <libgen.h>

int main(int argc, char *argv[]) {
printf("My directory is %s\n", dirname(argv[0]));
return 0;
}

myquidproquo
December 8th, 2008, 02:17 PM
Thank you. That helped me a lot.

I love this forum :)