-
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.
-
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?
-
main
I defined each by using #define. I have included my main file in the attachment.
-
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.
-
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
-
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.
-
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?
-
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.