![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| PRo*C program for SQL queries using threading concept | user71408 | UNIX for Dummies Questions & Answers | 0 | 05-21-2008 02:11 AM |
| LUN concept | Prem | HP-UX | 1 | 02-22-2007 05:02 PM |
| Having difficulty with UNIX concept. Please help! | ALon | UNIX for Dummies Questions & Answers | 4 | 01-11-2007 11:42 AM |
| IPTABLES Firewall concept help | Bradley Porter | UNIX for Dummies Questions & Answers | 1 | 01-02-2007 05:39 AM |
| concept of mount point | Vikas Sood | UNIX for Dummies Questions & Answers | 3 | 06-08-2006 06:31 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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 11:35 AM.. Reason: remove my name in the signature |
|
||||
|
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. |
|
||||
|
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 |
|
|||||
|
I think the wikipedia entry on Semaphore programming answers your question.....
|
|
||||
|
Quote:
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. |
|
||||
|
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 05:15 AM.. Reason: removed the smiley URL |
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Tags |
| linux |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|