![]() |
| | #1 |
| Registered User Join Date: Oct 2005
Posts: 14
| Semaphores 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;
}
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);
}
|
| justgotthis is offline | |
| | #2 |
| Registered Luser Join Date: Jul 2005 Location: Singapore
Posts: 867
| 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? |
| cwr is offline | |
| | #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. |
| justgotthis is offline | |
| | #4 |
| Registered Luser Join Date: Jul 2005 Location: Singapore
Posts: 867
| 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. |
| cwr is offline | |
| | #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 |
| justgotthis is offline | |
| | #6 |
| Registered Luser Join Date: Jul 2005 Location: Singapore
Posts: 867
| 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. |
| cwr is offline | |
| | #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? |
| justgotthis is offline | |
| | #8 |
| Registered Luser Join Date: Jul 2005 Location: Singapore
Posts: 867
| 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. |
| cwr is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Semaphores, need advice on implementation. | Swerve | C++ Programming | 2 | 01-13-2009 01:54 AM |
| semaphores | Dr Spud | C Programming | 7 | 09-22-2007 12:45 PM |
| Semaphores Problems | mhelal | Linux Programming | 2 | 05-06-2007 10:36 PM |
| Semaphores | Jules | C Programming | 4 | 01-18-2004 01:58 PM |
| Semaphores | edk | C++ Programming | 1 | 11-25-2001 03:55 PM |