semaphores using up and down

been searching around on how to use an up and down function with semaphores but i can't find an example. i looked into using: "semop" but i have no idea how to use it. I have been able to declared the semaphores using semget and initializing them using semctl.

Up and down? Do you mean a mutex?

Sempahores can have values other than one and zero. Mutexes are "switches" - either on or off (one or zero).

sorry, forgot to update this. i found out that i needed to create a stuct that would implement the function i wanted. increment the semaphore by 1 or decrease it by 1.

here is where i got the idea
http://docs.linux.cz/programming/c/unix_examples/semab.html

void main() is a terrible idea... noticed it on your link.

should be as

int main() {
 return <n>;
}

I noticed the semaphore resource acquired is not released using

semctl + IPC_RMID;
soemthing like,
semctl(sem_id, 0, IPC_RMID, <union>);

what i did was just define a number of KEYs equal to the number of semaphores i wanted and then i created 2 struct to create the functions up and down. i didn't use a void main, just main. Why would void main be bad?

you have to release the resources??? how do you do that? aren't they released as soon as the program is done?

But Still, :slight_smile:

use int main and make the code to return a value !

With IPC, the data structures that are created remain in memory till explicitly removed. Any shared memory, semaphores or message queues have to be removed by the program that creates them or they will stay in memory till the system is rebooted.