C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-05-2005, 09:42 PM   #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.
justgotthis is offline   Reply With Quote
Old 10-05-2005, 09:51 PM   #2
cwr
Registered Luser
 
cwr's Avatar
 
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   Reply With Quote
Old 10-05-2005, 09:57 PM   #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
File Type: c main.c (10.2 KB, 48 views)
justgotthis is offline   Reply With Quote
Old 10-05-2005, 10:01 PM   #4
cwr
Registered Luser
 
cwr's Avatar
 
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   Reply With Quote
Old 10-05-2005, 10:05 PM   #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   Reply With Quote
Old 10-05-2005, 10:13 PM   #6
cwr
Registered Luser
 
cwr's Avatar
 
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   Reply With Quote
Old 10-06-2005, 06:19 PM   #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   Reply With Quote
Old 10-06-2005, 06:35 PM   #8
cwr
Registered Luser
 
cwr's Avatar
 
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   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 11:38 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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