Sponsored Content
Full Discussion: semaphore concept
Top Forums UNIX for Advanced & Expert Users semaphore concept Post 302134159 by maverix on Wednesday 29th of August 2007 07:02:38 AM
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
 

9 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

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. 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

7. 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

8. 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

9. 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
LOCKF(3)						   BSD Library Functions Manual 						  LOCKF(3)

NAME
lockf -- record locking on files LIBRARY
Standard C Library (libc, -lc) SYNOPSIS
#include <unistd.h> int lockf(int fildes, int function, off_t size); DESCRIPTION
The lockf() function allows sections of a file to be locked with advisory-mode locks. Calls to lockf() from other processes which attempt to lock the locked file section will either return an error value or block until the section becomes unlocked. All of the locks for a process are removed when the process terminates. The argument fildes is an open file descriptor. The file descriptor must have been opened either for write-only (O_WRONLY) or read/write (O_RDWR) operation. The function argument is a control value which specifies the action to be taken. The permissible values for function are as follows: Function Description F_ULOCK unlock locked sections F_LOCK lock a section for exclusive use F_TLOCK test and lock a section for exclusive use F_TEST test a section for locks by other processes F_ULOCK removes locks from a section of the file; F_LOCK and F_TLOCK both lock a section of a file if the section is available; F_TEST detects if a lock by another process is present on the specified section. The size argument is the number of contiguous bytes to be locked or unlocked. The section to be locked or unlocked starts at the current offset in the file and extends forward for a positive size or backward for a negative size (the preceding bytes up to but not including the current offset). However, it is not permitted to lock a section that starts or extends before the beginning of the file. If size is 0, the section from the current offset through the largest possible file offset is locked (that is, from the current offset through the present or any future end-of-file). The sections locked with F_LOCK or F_TLOCK may, in whole or in part, contain or be contained by a previously locked section for the same process. When this occurs, or if adjacent locked sections would occur, the sections are combined into a single locked section. If the request would cause the number of locks to exceed a system-imposed limit, the request will fail. F_LOCK and F_TLOCK requests differ only by the action taken if the section is not available. F_LOCK blocks the calling process until the section is available. F_TLOCK makes the function fail if the section is already locked by another process. File locks are released on first close by the locking process of any file descriptor for the file. F_ULOCK requests release (wholly or in part) one or more locked sections controlled by the process. Locked sections will be unlocked start- ing at the current file offset through size bytes or to the end of file if size is 0. When all of a locked section is not released (that is, when the beginning or end of the area to be unlocked falls within a locked section), the remaining portions of that section are still locked by the process. Releasing the center portion of a locked section will cause the remaining locked beginning and end portions to become two separate locked sections. If the request would cause the number of locks in the system to exceed a system-imposed limit, the request will fail. An F_ULOCK request in which size is non-zero and the offset of the last byte of the requested section is the maximum value for an object of type off_t, when the process has an existing lock in which size is 0 and which includes the last byte of the requested section, will be treated as a request to unlock from the start of the requested section with a size equal to 0. Otherwise an F_ULOCK request will attempt to unlock only the requested section. A potential for deadlock occurs if a process controlling a locked region is put to sleep by attempting to lock the locked region of another process. This implementation detects that sleeping until a locked region is unlocked would cause a deadlock and fails with an EDEADLK error. The lockf(), fcntl(2), and flock(2) locks are compatible. Processes using different locking interfaces can cooperate over the same file safely. However, only one of such interfaces should be used within the same process. If a file is locked by a process through flock(2), any record within the file will be seen as locked from the viewpoint of another process using fcntl(2) or lockf(), and vice versa. Blocking on a section is interrupted by any signal. RETURN VALUES
The lockf() function returns the value 0 if successful; otherwise the value -1 is returned and the global variable errno is set to indicate the error. In the case of a failure, existing locks are not changed. ERRORS
The lockf() function will fail if: [EAGAIN] The argument function is F_TLOCK or F_TEST and the section is already locked by another process. [EBADF] The argument fildes is not a valid open file descriptor. The argument function is F_LOCK or F_TLOCK, and fildes is not a valid file descriptor open for writing. [EDEADLK] The argument function is F_LOCK and a deadlock is detected. [EINTR] The argument function is F_LOCK and lockf() was interrupted by the delivery of a signal. [EINVAL] The argument function is not one of F_LOCK, F_TEST, F_TLOCK, or F_ULOCK. The argument fildes refers to a file that does not support advisory locking. [ENOLCK] The argument function is F_LOCK, F_TLOCK, or F_ULOCK and satisfying the lock or unlock request would result in the number of locked regions in the system exceeding a system-imposed limit. [EOPNOTSUPP] The argument fildes refers to a socket; these do not support advisory locking. SEE ALSO
fcntl(2), flock(2) STANDARDS
The lockf() function conforms to X/Open Portability Guide Issue 4, Version 2 (``XPG4.2''). BSD
December 19, 1997 BSD
All times are GMT -4. The time now is 06:37 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy