Hi again and again,

  I've solved the problem about the tgoto function, actually, I didn't
use it, instead I used
WINDOW *window;
window=initscr();
move(ren,col);
printw("whatever");
refresh;

and it worked well. Thanks anyway for your help.

Now my problem is another: I'm trying to 'feed the philosophers', you
know, the well known problem of the spaguetti and the forks. I'm solving
it with semaphores, the problem is that the semaphores don't seem to
lock, I mean, there are two contiguos philosophers eating at the same
time, sometimes all of them are eating!! since there aren't enough
forks, that should be impossible.
I'm using the following code:
void down (int i)
{
  locksem(semsetid, i);
}
void up (int i)
{
  unlocksem(semsetid, i);
}

void locksem(int sid, int member)
{
        struct sembuf sem_lock={ 0, -1, IPC_NOWAIT};

        if( member<0 || member>(get_member_count(sid)))
        {
                fprintf(stderr, "semaphore member %d out of range\n",
member);
                return;
        }

        /* Attempt to lock the semaphore set */
        if(!getval(sid, member))
        {
/*                fprintf(stderr, "Semaphore resources exhausted (no
lock)!\n");
*/
                return;
        }

        sem_lock.sem_num = member;

        if((semop(sid, &sem_lock, 1)) == -1)
        {
/*                fprintf(stderr, "Lock failed\n");
*/
                return;
        }
/*        else
                printf("Semaphore %d resources decremented by one
(locked)\n",member);
*/

/*        dispval(sid, member);
*/
}

void unlocksem(int sid, int member)
{
        struct sembuf sem_unlock={ member, 1, IPC_NOWAIT};
        int semval;

        if( member<0 || member>(get_member_count(sid)-1))
        {
                fprintf(stderr, "semaphore member %d out of range\n",
member);
                return;
        }

        /* Is the semaphore set locked? */
        semval = getval(sid, member);
        if(semval == SEM_RESOURCE_MAX) {
                fprintf(stderr, "Semaphore %d not locked!\n",member);
                return;
        }

        sem_unlock.sem_num = member;

        /* Attempt to lock the semaphore set */
        if((semop(sid, &sem_unlock, 1)) == -1)
        {
                fprintf(stderr, "Unlock failed\n");
                return;
        }
/*        else
                printf("Semaphore resources incremented by one
(unlocked)\n");
*/

/*        dispval(sid, member);
*/
}

the functions locksem and unlocksem are on a different file which I
include in the header files as:
#include "semaforo.c"
Can anybody tell me why the semaphores don't lock?
Thanks.
--
********************************
Rodrigo Aceves
[EMAIL PROTECTED]
********************************

Reply via email to