LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-24-2005, 02:33 AM   #1
tzzdvd
LQ Newbie
 
Registered: May 2005
Posts: 1

Rep: Reputation: 0
Semaphore


Hi,
I try to use cygwin and linux expecting to have the same result but I discover this "problem".
I try to learn about thread and semaphore and I write this simple program

Code:
struct sembuf lock_it;
struct sembuf unlock_it;
int a = 0;
int mutex;

void *thRoutine1(void *arg)
{
    while(1)
    {
        semop(mutex, & lock_it, 1);
        printf ("\nSemaphore locked by 'Routine 1'\n");
        fflush(stdout);
        a ++;
        printf ("\n 'Routine 1' unlock Semaphore");
        fflush(stdout);
        semop(mutex, & unlock_it, 1);
    }
}

void *thRoutine2(void *arg)
{
    while(1)
    {
        semop(mutex, & lock_it, 1);
        printf ("\nSemaphore locked by 'Routine 2'\n");
        fflush(stdout);
        a += 10000;
        printf ("\n 'Routine 1' unlock Semaphore");
        fflush(stdout);
        semop(mutex, & unlock_it, 1);
    }
}

void *thRead(void *arg)
{
    while(1)
    {
        semop(mutex, & lock_it, 1); // faccio un lock sulla risorsa
        printf ("\nSemaphore locked by 'Routine Read'\n");
        fflush(stdout);
        printf("\n          a = %d; ", a);
        fflush(stdout);
        printf ("\n 'Routine Read' unlock Semaphore");
        fflush(stdout);
        semop(mutex, & unlock_it, 1); // faccio un lock sulla risorsa
    }
}

int main()
{
pthread_t       th1, th2, thR;
key_t           unique_key;
union semun     options;
    unique_key = ftok(".", 'r');
    printf("\n Unique_key = %d", unique_key);
    fflush(stdout);
    if ( (mutex = semget(unique_key, 1, IPC_CREAT | IPC_EXCL | 0x0666)) == -1)
    {
        printf ("\nError");
        fflush(stdout);
        abort();
    }
    printf ("\n\nSemaphore ID = %d\n", mutex);
    fflush(stdout);
    options.val = 1;
    semctl(mutex, 0, SETVAL, options);
    // for lock operation
    lock_it.sem_num = 0;
    lock_it.sem_op  = -1;
    lock_it.sem_flg = IPC_NOWAIT;
    // for unlock operation
    unlock_it.sem_num = 0;
    unlock_it.sem_op  = 1;
    unlock_it.sem_flg = IPC_NOWAIT;
    pthread_create( & th1, NULL, thRoutine1, NULL);
    pthread_create( & th2, NULL, thRoutine2, NULL);
    pthread_create( & thR, NULL, thRead, NULL);
    while(1) ;
    return 0;
}
I compiled it under cygwin and I receve what I expect i.e. in a random way all the three thread lock the semaphore, update the variable and unlock the semaphore. The access to the resource (the 'a' variable) is equally distribuited between the three thread.
The I compiled under Fedora Core 3 exaclty the same program and I noticed that if i.e. Routine 1 passes the semaphore once, then about 300-400 time again use the resource. If another thread is able to gain the resource, then that thread gain it again for other 300-400 times. I expected, as in cygwin, that the access to the resource would be again equally distribuited.
Did I make any mistake?

Thank's of all.
Davide
 
Old 05-24-2005, 09:23 PM   #2
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,290

Rep: Reputation: 378Reputation: 378Reputation: 378Reputation: 378
Could be your use of IPC_NOWAIT. I ran your program with and without IPC_NOWAIT and I got more reasonable behavior without the NOWAIT. Perhaps because with NOWAIT the semaphore release never actually takes place cleanly (i.e. the thread requests to unlock the semaphore, but with IPC_NOWAIT it returns and grabs the semaphore again before the unlock can be processed, thus starving the other threads. However, this is really just a guess on my part, and I may very well be 100% wrong, in which case I too am curious of the reason.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to test for remaining semaphore? LocalFruit Programming 1 11-25-2005 07:03 AM
difference mutex vs. semaphore? Thinking Programming 1 11-16-2005 05:26 AM
semaphore beginner_84 Programming 5 08-25-2004 06:54 PM
about semaphore dummyagain Programming 1 11-11-2003 11:09 AM
Semaphore setting for Redhat 7.0 bgupta Linux - General 1 01-17-2003 11:04 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration