[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Problem with semaphores and such.




Hi list.

I've jsut tried to compile a text book example for using semaphores on
linux, and I've failed measrably. looking in google for answers produced
nothing coherant, so I'm turning to you guys for help -

This code
--------------------
#include <sys/sem.h>
#include <assert.h>


class CCritical {

key_t m_Key;
    int m_Semaphore;

public:
	CCritical(char* pathname) {
		union semun semopts;
		m_Key = ftok(pathname,'l');
		m_Semaphore = semget(m_Key, 1, IPC_CREAT|0666);
		assert(m_Semaphore);
		semopts.val = 0;
		semctl(m_Semaphore, 0, SETVAL, semopts);
	};


	~CCritical() {
		semctl(m_Key, 0, IPC_RMID, 0);
	};

	void lock() {
		struct sembuf sem_lock={ 0, -1, IPC_NOWAIT};
		sem_lock.sem_num = 0;
		assert(semop(m_Semaphore, &sem_lock, 1) >= 0);
	};

	void unlock() {
		struct sembuf sem_unlock={ 0, 1, IPC_NOWAIT};
		sem_unlock.sem_num = 0;
		assert(semop(m_Semaphore, &sem_unlock, 1) >= 0);
	};
};

----------------

Got me this message :
LoggerStub.h:155: aggregate `semun semopts' has incomplete type and cannot
be initialized
LoggerStub.h:160: confused by earlier errors, bailing out

(don't mind line numbers - I've trimmed the file quite a bit)

I'm really confused as to what this means, and I'm clueless.

TIA

Oded



=================================================================
To unsubscribe, send mail to linux-il-request@linux.org.il with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail linux-il-request@linux.org.il