Hi Seshi,

 I  suppose  race is generated with  LOOPCONSTANT = 100000,
 Actually for race  on the variables the threads need to be scheduled in the
middle of updating the variables.
 So you should do some minimum  work  in the thread function so other
threads get chance to run.

Thanks,
Mukti


On Fri, May 8, 2009 at 10:00 AM, seshikanth varma <[email protected]
> wrote:

> Hi Ramesh,
> My program above has uselock=0 for not using pthread_mutex_lock and 1 for
> using pthread_mutex_lock.  I tried with files too. Theoritically speaking,
> this should generate race condition but in practice no :(
> One more observation here it is implicitly writing to a file called
> "stdout"
>
> Thanks
> Seshikanth
>
> On Fri, May 8, 2009 at 5:19 AM, Ramesh <[email protected]> wrote:
>
>> Hi Seshi,
>>
>> This is my suggestion, you can increase the # of threads to may be 100
>> or 200 & run the program without locks & dump the results to a file.
>>
>> Run the same program with pthread_lock & pthread_unlock enabled, dump
>> the results to another file. You could compare the values.
>>
>> Cheers
>> /R
>> On Thu, May 7, 2009 at 12:38 PM, seshikanth varma
>> <[email protected]> wrote:
>> > Hi,
>> > I am learning linux kernel. I have written a simple program to
>> understand
>> > the usage of mutex variable.
>> > Ideally the following program should generate race condition and should
>> > produce the different values on i,j,k and l rather than i = j = k = l =
>> > 100(LOOPCONSTANT * Number of threads) on usage of mutex. Here shared
>> > variables between threads are i,j,k and l.
>> > My kernel is not SMP.  Can u please tell me where am i going wrong?
>> >
>> ======================================================================================================
>> >
>> > #include <pthread.h>
>> > #include <stdio.h>
>> > #define checkResults(string, val) {             \
>> >     if (val) {                                     \
>> >         printf("Failed with %d at %s", val, string); \
>> >         exit(1);                                     \
>> >     }                                              \
>> > }
>> > //#define            LOOPCONSTANT     100000
>> > #define            LOOPCONSTANT     10
>> > #define            THREADS          10
>> >
>> > pthread_mutex_t    mutex = PTHREAD_MUTEX_INITIALIZER;
>> > volatile int                i asm("eax"); /* For using eax ebx ecx & edx
>> for
>> > i, j , k  and l respectively.
>> >
>> >                                            *  All the threads shud use
>> eax
>> > for i, ebx for j ...
>> >
>> >                                            */
>> > volatile int                j asm("ebx");
>> > volatile int                k asm("ecx");
>> > volatile int                l asm("edx");
>> > int                uselock=0;
>> >
>> > void *threadfunc(void *parm)
>> > {
>> >     int   loop = 0;
>> >     int   rc;
>> >
>> >     for (loop=0; loop<LOOPCONSTANT; ++loop) {
>> >         if (uselock) {
>> >             rc = pthread_mutex_lock(&mutex);
>> >             checkResults("pthread_mutex_lock()\n", rc);
>> >         }
>> >         ++i; ++j; ++k; ++l;
>> >         if (uselock) {
>> >             rc = pthread_mutex_unlock(&mutex);
>> >             checkResults("pthread_mutex_unlock()\n", rc);
>> >         }
>> >     }
>> >     return NULL;
>> > }
>> >
>> > int main(int argc, char **argv)
>> > {
>> >     pthread_t             threadid[THREADS];
>> >     int                   rc=0;
>> >
>> >     int                   loop=0;
>> >     pthread_attr_t        pta;
>> >
>> >     printf("Entering testcase\n");
>> >     printf("Give any number of parameters to show data corruption\n");
>> >     if (argc != 1) {
>> >         printf("A parameter was specified, no serialization is being
>> > done!\n");
>> >         uselock = 0;
>> >     }
>> >
>> >     pthread_attr_init(&pta);
>> >     pthread_attr_setdetachstate(&pta, PTHREAD_CREATE_JOINABLE);
>> >
>> >     printf("Creating %d threads\n", THREADS);
>> >     for (loop=0; loop<THREADS; ++loop) {
>> >         rc = pthread_create(&threadid[loop], &pta, threadfunc, NULL);
>> >         checkResults("pthread_create()\n", rc);
>> >     }
>> >
>> >     printf("Wait for results\n");
>> >     for (loop=0; loop<THREADS; ++loop) {
>> >         rc = pthread_join(threadid[loop], NULL);
>> >         checkResults("pthread_join()\n", rc);
>> >     }
>> >
>> >     printf("Cleanup and show results\n");
>> >     pthread_attr_destroy(&pta);
>> >     pthread_mutex_destroy(&mutex);
>> >
>> >     printf("\nUsing %d threads and LOOPCONSTANT = %d\n",
>> >            THREADS, LOOPCONSTANT);
>> >     printf("Values are: (should be %d)\n", THREADS * LOOPCONSTANT);
>> >     printf("  ==> seshikanth | %d, %d, %d, %d\n", i, j, k, l);
>> >
>> >     printf("Main completed\n");
>> >     return 0;
>> >
>> > }
>> >
>> ======================================================================================================
>> >
>> > O/p of the above program:
>> > ---------------------------------------
>> > with userlock = 0 (i.e., shud hit race condition)
>> > --------------------------------------------------------------------
>> > gcc check.c -pthread
>> > bash-2.05b$ ./a.out
>> > Creating 10 threads
>> > Wait for results
>> > Cleanup and show results
>> > Using 10 threads and LOOPCONSTANT = 10
>> > Values are: (should be 100)
>> >   ==> | 100, 100, 100, 100 <<<<<<<<<<<==========*******
>> > Main completed
>> > bash-2.05b$
>> > =====================================
>> > with userlock = 1;
>> > -------------------------
>> > I am getting the same o/p which is correct....
>> >
>> > Any help regarding this is greatly appreciated.
>> > Thanks,
>> > Seshikanth
>> >
>> >
>>
>
>
>
> --
> Regards,
> Seshikanth
>

Reply via email to