semaphore concept


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users semaphore concept
# 1  
Old 08-28-2007
semaphore concept

Hi All,

I am going through the semaphore concept and have a doubt regarding the same and hope to get a resolution here.

I have a file which has a number of records.
I want to write an application (in C) which will be able to do concurrent read/write on these records.

Of what I have understood about semaphores, I know that a process can obtain a lock on the critical section of code, read/write into records, then release the lock so the other processes waiting to access the data can obtain a lock.

I am confused and tend to think that "Wouldn't this concept lead to a more serious problem?" as another process, actually wanting to read/write a different record on the same file has to wait for the lock to get released by the process which has obtained the lock. If this is the case, think of a situation where we have a large number of processes (say, more than 1000) wanting to do a read/write on same/different records. Wouldn't this concept slow down the application?

Please let me know if I have misunderstood the concept and throw some light on this...

Regards
maverix

Last edited by maverix; 08-28-2007 at 12:35 PM.. Reason: remove my name in the signature
# 2  
Old 08-28-2007
You don't say what UNIX you are on but consider file locks instead of semaphores.

start with 'man flock' if you're on Linux.
Otherwise, man fcntl, then look for FD_SETLK or FD_GETLK to start off.

Depending on your OS you can get file locks at different levels; struct flock can provide locking a part of a file.

Semaphores are mostly used for access to shared memory.
# 3  
Old 08-28-2007
Semaphores are not inherently linked to memory, processes, code or files.

Semaphores are flags that processes, including processes that access files or shared memory, use to know a guaranteed status, which is often referred to as "atomic".

Let's say you are writing a shared memory application. You don't want to read or write to shared memory when another process is writing to it (if that process has write permissions). Also, you don't want to read a file that is being changed, etc. So, you create a semaphore and you designate two states:

- semaphore set, means that a process with write permission has a file descriptor open.

- semaphore clear, means that there is no process with write permissions accessing the shared memory.

Now, each process that accesses the shared memory must check the status of the semaphore when they (it) open the file (in this case shared memory) for access.

There are many applications for semaphores for interprocess communications, not limited to shared memory; but shared memory certainly required atomic flags for interprocess communications.

Think of this like a person on a single track of train track holding a flag. One train is flying down the track at 200 kph in one direction and other train is heading toward the same track segment from the opposite direction. There is a switch at one section of track where one train can wait while the other passes. A switchman, in the old days, would hold up a flag (later done with lights) to give status to both trains.

You can see that trusting the flags by the switchman is critically important, folks could dies if the flag is incorrect or the conductor of the train does not watch it.

The same is true in interprocess communications, and semaphores are guaranteed by the UNIX / Linux kernel to be atomic, dead right on.

File locks are not guaranteed by the kernel to be atomic, as I recall, so use them with caution.
# 4  
Old 08-29-2007
Hi Jim and Neo,

Thank you for your replies...

I am using RHEL 4.
I have used lockf() and flock() system calls and it seems to work fine.

However, I want to use semaphores to get the same effect. FYI...I am doing these just to understand and learn the concepts. Its not a live/industry project.

Using lockf() and flock(), I was able to lock certain sections of files depending on the file position we provide. Say, for example, the application is a simple banking application and the database I use is a simple file which has number of records which contains account information. Using lockf(), I was able to lock a certain record by obtaining the account number from the user so that another process wanting to access a different record didn't have any issues in reading/writing to a different record.

My question was how to get the same effect using semaphores. If the number of concurrent processes are more, then each process will be waiting to obtain the semaphore to enter the critical section of code, isn't that so?

regards
maverix
# 5  
Old 08-29-2007
I think the wikipedia entry on Semaphore programming answers your question.....
# 6  
Old 08-29-2007
Quote:
Originally Posted by maverix
I have used lockf() and flock() system calls and it seems to work fine.

However, I want to use semaphores to get the same effect. FYI...I am doing these just to understand and learn the concepts. Its not a live/industry project.
One thing to be aware of is the type of thing you are guarding determines the type of semphore.

If it's a file, then file locking is preferred, this way even if the file is on a NFS volume, the locking and sharing will still work.

If the thing is in a single process and the sharing is between threads then using pthread_mutex/pthread_cond et al.

If the thing is shared memory or similar that is being shared by processes that are on the same machine then you can use the kernel provided semahores.

I suggest reading anything by W.Richard Stevens on the subject.
# 7  
Old 08-30-2007
Computer

Hi All,

Thank you for your inputs.

As suggested by porter, I think I'll have to settle with file locking mechanisms as I am using a simple file.

Let me explore other IPC facilities (yet to start studying :-) ) and will get back to you guys with more doubts...

regards
maverix

Last edited by maverix; 08-30-2007 at 06:15 AM.. Reason: removed the smiley URL
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Confusion with the concept of wc -c and wc -m

Is wc -c and wc -m same ? Shellscript::cat file1 hello Shellscript::cat file1 | wc -c 6 Shellscript::cat file1 | wc -m 6 Shellscript::file file1 file1: ASCII text Shellscript::uname -a Linux was85host 2.6.27.45-0.1-vmi #1 SMP 2010-02-22 16:49:47 +0100 i686 i686 i386 GNU/LinuxAtleast... (5 Replies)
Discussion started by: shellscripting
5 Replies

2. UNIX for Dummies Questions & Answers

ACL concept

Hi.. Could someone explain about setfacl,getfacl in unix and its uses. Regards, Suresh (1 Reply)
Discussion started by: suresh sunkara
1 Replies

3. UNIX for Advanced & Expert Users

Looping concept please help

Hi Gurus, Please help me in below requirement. Instance =5 (it is user parameter) total=52 (it is user parameter i need to split this to 5 and reminder as 1 instances totally 6 for example i need to splitt to each 52/5=10.4 1-10 11-20 21-30 31-40 41-50 (2 Replies)
Discussion started by: ragu.selvaraj
2 Replies

4. UNIX for Dummies Questions & Answers

help me in RAID concept...

i couldn't get what does the metainit command represents in numeric values. (i.e) #metainit d66 2 1 c0t0d0s4 1 c0t0d0s5 ??here 2 1 1 represnts what ?? can some one tell clearly about this... (6 Replies)
Discussion started by: sriniv666
6 Replies

5. AIX

Use of mirroring concept....

hi.... Friends... Why using mirroring ? what is the use of mirroring? just any one tell about clearly.... thanks.... (4 Replies)
Discussion started by: Kannan841
4 Replies

6. Shell Programming and Scripting

Concept Of Array

Hi all, I used array a lot in C,VB,C# and java but now i am very new to shell programming,so i need a start of array in shell programming. All i want to do is read a string and put it into a character type array. For reading the string,i did this: $ read a now i want to put the content of a... (1 Reply)
Discussion started by: gautamshaw
1 Replies

7. UNIX for Dummies Questions & Answers

about concept of Interrupts.

Hi all, I am new here ,i want to know about interrupts in detail.What r Interrupts .how they r handeled. Thanx in adavnce. (1 Reply)
Discussion started by: vishwasrao
1 Replies

8. UNIX for Dummies Questions & Answers

The Concept of thread

Hi all This is my first thread here.i confused with the concept of thread.Can anyone tell me this concept in detail.my Quation may be at primary level. Thanx in advance for help. (1 Reply)
Discussion started by: vishwasrao
1 Replies

9. HP-UX

LUN concept

In my location we have Hp storage (san),from there they created luns, so could u plz send me clear document for understanding LUN. (1 Reply)
Discussion started by: Prem
1 Replies
Login or Register to Ask a Question