Semaphores

This is a discussion on Semaphores within the Linux Programming forums, part of the Platform Specific Boards category; I am trying to implement semaphores. The include file is as follows: Code: #include <stdio.h> #include <sys/types.h> #include <sys/ipc.h> #include ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    14

    Semaphores

    I am trying to implement semaphores. The include file is as follows:

    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/ipc.h>
    #include <sys/sem.h>
    
    #define SEM_MODE 0666
    
    int global_semid = 3;
    
    void P (int semid)
    {
    	struct sembuf sbuf;
    
    	sbuf.sem_num = semid;
    	sbuf.sem_op = -1;
    	sbuf.sem_flg = 0;
    	if (semop (global_semid, &sbuf, 1) < 0)
    		perror ("semop P");
    	return;
    }
    
    void V (int semid)
    {
    	struct sembuf sbuf;
    
    	sbuf.sem_num = semid;
    	sbuf.sem_op = 1;
    	sbuf.sem_flg = 0;
    	if (semop (global_semid, &sbuf, 1) < 0)
    		perror ("semop V");
    	return;
    }
    
    void Init (int* semid, int value)
    {
    	union {
    		int val;
    		struct semid_ds *buf;
    		u_short *array;
    	} arg;
    
    	arg.val = value;
    	if (semctl (global_semid, semid, SETVAL, arg) < 0)
    		perror ("semctl SETVAL");
    	return;
    }
    
    void Print (int semid)
    {
    	int val;
    
    	if ((val = semctl (global_semid, semid, GETVAL)) < 0)
    		perror ("semctl GETVAL");
    	printf ("Semaphore %d's value = %d\n", semid, val);
    	return;
    }
    and then I call it in my main file:
    Code:
    void initialize()
    {
      initoutput();
      queueInit(qSize);
      Init(MUTEX_SEM, 1);
      Init(EMPTY_SEM, qSize);
      Init(FULL_SEM, 0);
      Init(ITEM_ID_MUTEX_SEM, 1);
      Init(INCR_CONS_COUNT_SEM, 1);
    }
    I keep getting a segmentation fault on this. I have tried to troubleshoot it but to no avail.

  2. #2
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    What are the type of MUTEX_SEM, et al.? Are they pointers? Have you allocated memory for them? I don't think you've posted enough code to enable us to ascertain the problem? Have you tried using a debugger to stack trace and ascertain where the segfault occured?

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    14

    main

    I defined each by using #define. I have included my main file in the attachment.
    Attached Files Attached Files

  4. #4
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    So MUTEX_SEM is #defined as the value 1, but you are passing it to the "Init" function as the first parameter, which expects a pointer. The int is being converted to an address, not likely addressable by your program.

    Not being familiar with posix/sysv semaphores, looking at the manpage, I can see that you're passing this id to semctl, which also expects an int, so you should be using a straight int for semid, not a pointer.

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    14
    well i just changed the int * from originally int in the Init function because I was getting "invalid argument" when the init function gets called. I still get these messages without the pointer in the init function

  6. #6
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    When exactly do you get "invalid argument"? The semctl call? If you do, there is something wrong with what you're passing, but it's not a type problem. The type is clearly int. I'm not familiar with semaphores to determine what constitutes valid data to semctl.

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    14
    From the same code, I compile the program and it gives me the error message:
    "semget IPC_CREAT: No space left on device". This is weird because I have more than enough space on my drive. What is the problem?

  8. #8
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    From man semget:

    ERRORS
    On failure errno will be set to one of the following:

    ENOSPC A semaphore set has to be created but the system limit for the maximum number of semaphore sets (SEMMNI), or the system wide maximum number of semaphores (SEMMNS), would be exceeded.

    This isn't really to do with the C language, I've requested this thread be moved to Linux programming.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Semaphores, need advice on implementation.
    By Swerve in forum C++ Programming
    Replies: 2
    Last Post: 01-13-2009, 12:54 AM
  2. semaphores
    By Dr Spud in forum C Programming
    Replies: 7
    Last Post: 09-22-2007, 12:45 PM
  3. Semaphores Problems
    By mhelal in forum Linux Programming
    Replies: 2
    Last Post: 05-06-2007, 10:36 PM
  4. Semaphores
    By Jules in forum C Programming
    Replies: 4
    Last Post: 01-18-2004, 12:58 PM
  5. Semaphores
    By edk in forum C++ Programming
    Replies: 1
    Last Post: 11-25-2001, 02:55 PM

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