LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-22-2012, 04:43 PM   #1
mohd2012
LQ Newbie
 
Registered: Apr 2012
Posts: 8

Rep: Reputation: Disabled
Semaphores initialised correctly


Hi ...
I nedd your help to understand
How Semaphores initialised correctly ....

what will or at least can go wrong when running several of semaphores at the same
time. ??and why ?

mutex is initialised to 1


// begin entry section
sem_wait(&mutex);
// end entry section
// critical section
// begin exit section
// end exit section


This a part of homework... i need only to understand this part and how i can came to solution, then i could solve the others 5 example I have in the homework .
 
Old 05-22-2012, 07:09 PM   #2
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
First of all, semaphores are not mutexes.

There are three basic synchronization primitives provided by pthreads in C: mutexes, semaphores, and rwlocks. Of these, only sem_post() is safe to use in a signal handler function. You cannot mix the operations and types: each type has their own sets of operations, and they won't work with any other types. (By that I mean you cannot cast a semaphore to a mutex and expect it to work.)

To initialize a semaphore, you use sem_init(&semaphore, shared, value) where semaphore is a semaphore of the sem_t type (declared in semaphore.h, provided by pthread or rt library), shared is 0 if it is accessed only by threads in the same process (otherwise semaphore must be located in shared memory and shared set to nonzero), and value is the initial value set for the semaphore. Remember to check the return value: it is zero if successful, -1 otherwise (with errno set to indicate the error). Also remember to destroy the semaphore after you no longer access it. It is especially important if the semaphore is in a POSIX shared memory segment (which may persist after the process exits), but it is good practice to always destroy your semaphores before the program exits.

See the sem_init, sem_destroy, sem_wait, sem_post and sem_overview man pages for semaphore details.

For mutexes, look at pthread_mutex_init and related manpages. For rwlocks, look at pthread_rwlock_init and related manpages. Unlike semaphores, both mutexes and rwlocks can be initialized at declaration time by setting them to PTHREAD_MUTEX_INITIALIZER or PTHREAD_RWLOCK_INITIALIZER; the man pages will tell you more.

Last edited by Nominal Animal; 05-22-2012 at 07:10 PM.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] C - Semaphores, initiate semaphores seems to be failing golmschenk Programming 3 06-28-2010 09:32 PM
Applet not initialised - possible sollution ButterflyMelissa Programming 0 05-01-2009 03:00 PM
KDE not initialised baba_gorakhnath Linux - Newbie 3 03-14-2005 12:24 AM
Rezound gives soundplayer not initialised BACTRATE Mandriva 15 02-09-2005 06:38 AM
sessionid.tdb not initialised yat Linux - Networking 0 03-05-2004 09:33 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration