Semaphore Problem

This is a discussion on Semaphore Problem within the Linux Programming forums, part of the Platform Specific Boards category; Hi All, I am finding problem with the semaphores. Please have a look at the following code. Code: #include <stdio.h> ...

  1. #1
    Registered User cprogrammer_18's Avatar
    Join Date
    Dec 2005
    Posts
    9

    Semaphore Problem

    Hi All,
    I am finding problem with the semaphores. Please have a look at the following code.

    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/sem.h>
    
    int main()
    {
               key_t key;
               key=ftok("test.txt",1);
               printf("Key:%d\n",key);
    
                int semid;
                if(semid=semget(key, 1, IPC_CREAT)) 
                         printf("Semaphore ID:%d\n",semid);
                else
                         perror("Error in creating sem:\n");
    
                semaphore_wait(semid, 1);
                printf("\nInside Semaphore\nHit a Key to release");
                getchar();
                semaphore_signal(semid, 1);
    }
    
    
    int semaphore_wait(int sem_id, int sem_num)
    {
      struct sembuf sem_b;
    
      sem_b.sem_num = sem_num;
      sem_b.sem_op = -1;
      sem_b.sem_flg = SEM_UNDO;
    
           if (semop(sem_id, &sem_b, 1) == -1)
                perror("\nError in sem_wait:");
          else
          {
                 printf("\nSemaphore in wait);
                 return(1);
           }
    }
    
    int semaphore_signal(int sem_id, int sem_num)
    {
      struct sembuf sem_b;
     
      sem_b.sem_num = sem_num;
      sem_b.sem_op = 1;
      sem_b.sem_flg = SEM_UNDO;
      
      if (semop(sem_id, &sem_b, 1) == -1)
            perror("\nError in sem_signal");
      else
            {
             printf("Semaphore release");
             return(1);
             }
    }

    When I executed the following code it gives the Error as
    ------------------------------------------
    Key:16999020
    Semaphore ID:1114113

    Error in sem_wait:: File too large

    Inside Semaphore
    Hit a Key to release
    ------------------------------------------

    Whats the wrong in my code? Can anybody help me out please?

    Thanks in advance.

    Regards,
    Cprogrammer.

  2. #2
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    I don't recall for sure (I don't have a reference handy), but I think the problem is that the semaphore number is 0 based, not 1 based, so doing a semget(key, 1, IPC_CREAT) creates only a single semaphore, which is numbered 0, not 1.

    This may not be the problem, but considering the EFBIG error you're seeing, it's my first guess.
    Insert obnoxious but pithy remark here

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    19
    If you create n semaphores then You have sem_numbers 0 to n-1.
    And in semaphore wait you called sem_op=-1
    Initially when creates semaphores its value is initialized to 0
    and you are decreamenting this value that's why it is not working..
    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/sem.h>
    
    int main()
    {
               key_t key;
               key=ftok("test.txt",1);
               printf("Key:%d\n",key);
    
                int semid;
                if(semid=semget(key, 1, IPC_CREAT)) 
                         printf("Semaphore ID:%d\n",semid);
                else
                         perror("Error in creating sem:\n");
    
                semaphore_wait(semid, 0);
                printf("\nInside Semaphore\nHit a Key to release");
                getchar();
                semaphore_signal(semid, 0);
    }
    
    
    int semaphore_wait(int sem_id, int sem_num)
    {
      struct sembuf sem_b[2];
    
      sem_b[0].sem_num = sem_num;
      sem_b[0].sem_op = 0;
      //this is for it will wait untill the value become Zero
      sem_b[0].sem_flg = 0;
      sem_b[1].sem_num= sem_num;
      sem_b[1].sem_op = 1;
      //It will increment the semaphore value 
      sem_b[1].sem_flg = SEM_UNDO;
    
           if (semop(sem_id, &sem_b, 1) == -1)
                perror("\nError in sem_wait:");
          else
          {
                 printf("\nSemaphore in wait);
                 return(1);
                 exit(0); 
           }
    }
    
    int semaphore_signal(int sem_id, int sem_num)
    {
      struct sembuf sem_b;
     
      sem_b.sem_num = sem_num;
      sem_b.sem_op = -1;
     //It will decrement the semaphore value
      sem_b.sem_flg = SEM_UNDO;
      
      if (semop(sem_id, &sem_b, 1) == -1)
            perror("\nError in sem_signal");
      else
            {
             printf("Semaphore release");
             return(1);
             exit(0);
             }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cross platform semaphore problem
    By jet-plane in forum C Programming
    Replies: 0
    Last Post: 05-24-2008, 01:59 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21