473,786 Members | 2,567 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

flock user defined function

I'm trying to write two C language user defined functions, lockfile() and
unlockfile(), that call flock using LOCK_EX and LOCK_UN respectively. If I
call lockfile from a first psql process it returns successfully. Calling
lockfile from a second psql process blocks. However, when I call unlockfile
from the first psql process, the second process still blocks. The lockfile
call from the second psql proccess doesn't return until I kill the first
psql process.
Any suggestions? Thanks in advance.
Chris Goughnour

PG_FUNCTION_INF O_V1(lockFile);
Datum lockFile(PG_FUN CTION_ARGS){
text *t=PG_GETARG_TE XT_P(0);
char *path=palloc(VA RSIZE(t)-VARHDRSZ+1);
int fileHandle,stat us;
memcpy((void *)path,(void *)VARDATA(t),VA RSIZE(t)-VARHDRSZ);
path[VARSIZE(t)-VARHDRSZ]=0;
fileHandle=open ((const char *)path,O_RDONLY );
if(fileHandle==-1){
PG_RETURN_INT32 (-1);
}
if(flock(fileHa ndle,LOCK_EX)==-1){
PG_RETURN_INT32 (-1);
}
PG_RETURN_INT32 (0);
}

PG_FUNCTION_INF O_V1(unlockFile );
Datum unlockFile(PG_F UNCTION_ARGS){
text *t=PG_GETARG_TE XT_P(0);
char *path=palloc(VA RSIZE(t)-VARHDRSZ+1);
int fileHandle;
memcpy((void *)path,(void *)VARDATA(t),VA RSIZE(t)-VARHDRSZ);
path[VARSIZE(t)-VARHDRSZ]=0;
fileHandle=open ((const char *)path,O_RDONLY );
if(fileHandle==-1){
PG_RETURN_INT32 (-1);
}
if(flock(fileHa ndle,LOCK_UN)==-1){
PG_RETURN_INT32 (-1);
}
PG_RETURN_INT32 (0);
}
---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postg resql.org so that your
message can get through to the mailing list cleanly

Nov 23 '05 #1
4 1287
On Tue, Jun 22, 2004 at 02:49:27PM -0700, Chris Goughnour wrote:
I'm trying to write two C language user defined functions, lockfile() and
unlockfile(), that call flock using LOCK_EX and LOCK_UN respectively. IfI
call lockfile from a first psql process it returns successfully. Calling
lockfile from a second psql process blocks. However, when I call unlockfile
from the first psql process, the second process still blocks. The lockfile
call from the second psql proccess doesn't return until I kill the first
psql process.
Any suggestions? Thanks in advance.
Chris Goughnour
<snip>

Where do you close the file? That might cause some issues.
--
Martijn van Oosterhout <kl*****@svana. org> http://svana.org/kleptog/ Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a
tool for doing 5% of the work and then sitting around waiting for someone
else to do the other 95% so you can sue them.


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQFA2K7GY5T wig3Ge+YRAv3wAJ 0UVrdFN+OlT8jh0 UrFKDS1FQG5PwCf Quz3
kMpd0/j8qyAql8yfqMozF To=
=fVSM
-----END PGP SIGNATURE-----

Nov 23 '05 #2
Martijn van Oosterhout <kl*****@svana. org> writes:
On Tue, Jun 22, 2004 at 02:49:27PM -0700, Chris Goughnour wrote:
I'm trying to write two C language user defined functions, lockfile() and
unlockfile(), that call flock using LOCK_EX and LOCK_UN respectively. If I
call lockfile from a first psql process it returns successfully. Calling
lockfile from a second psql process blocks. However, when I call unlockfile
from the first psql process, the second process still blocks. The lockfile
call from the second psql proccess doesn't return until I kill the first
psql process.
Any suggestions? Thanks in advance.
Chris Goughnour


<snip>

Where do you close the file? That might cause some issues.


Yeah, it's generally best not to call LOCK_UN at all, but just to
close the file (which will release the locks). Otherwise, if you are
using stdio, you can get a situation where the file is unlocked but
its stdio buffer has not been flushed, leading to the corruption you
were trying to avoid by locking the file...

-Doug

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddres sHere" to ma*******@postg resql.org)

Nov 23 '05 #3
Chris Goughnour <cg********@hot p.com> writes:
Any suggestions? Thanks in advance.


I believe locks are associated with file descriptors (what you're
miscalling a handle). The unlock function cannot release a lock
that is held via a different descriptor. What it needs to be doing
is closing the descriptor that lockFile opened. This would also
solve the rather serious descriptor-leak problem you've got.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 23 '05 #4
Thanks. Yeah, I figured that out after Martijn's response. I'm just
returning the file descriptor from lockFile, passing it to unlockFile and
closing the descriptor there. It works fine now. Thanks for edifying a
clueless novice such as myself. :-)
Chris Goughnour <cg********@hot p.com> writes:
Any suggestions? Thanks in advance.


I believe locks are associated with file descriptors (what you're
miscalling a handle). The unlock function cannot release a lock
that is held via a different descriptor. What it needs to be doing
is closing the descriptor that lockFile opened. This would also
solve the rather serious descriptor-leak problem you've got.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2282
by: Marc | last post by:
Hello, when using flock() I get a permission denied error: Warning: fopen("<filename>", "r+") - Permission denied in <pathtofile> on line 7 I do this: $fileToOpen=substr($PHP_SELF, strrpos($PHP_SELF,"/")+1); $fileHandle=fopen($fileToOpen, 'r+') or die($php_erormsg);
14
3178
by: deko | last post by:
Do I need to use flock() when reading a file into an array? It's possible that the file in question could be open at the time the file('filename') request is made. I realize flock() is required when opening a file with fopen() when there is contention for the file: $fp=fopen($ctr, 'w'); //only write if we can get lock on file if (flock($fp, LOCK_EX)) { fwrite($fp, "0");
3
4781
by: Rex Karz | last post by:
Newbie here. I interpret the fine print at http://us2.php.net/manual/en/function.flock.php to mean that flock() does not work where the file being locked is on an NFS filesystem. The commentary following the "official" description of flock() offers numerous circumventions broken flock() where the file needing atomic access is on an NFS filesystem.
4
7909
by: writeson | last post by:
Hi all, I've got a PHP program that I've added flock() to in order to protect multiple scripts trying to write to the same file. After I added the flock() to the code the performance of the code went way down. Can anyone tell me if calling flock() on a file handle is particularly slow? Thanks, Doug
3
6601
by: thakadu | last post by:
The following code works as expected when run in the main body of a python script (ver 2.3.5) on OpenBSD v3.8. but when it is in the body of a function definition it does not work. It does not raise any errors but it simply does not block as expected. I have repeated this in both a cgi envirnoment and a non-cgi environment. I also have repeated it under OpenBSD 3.6 Python Version 2.3.4. To test this you need to run the code from two shells...
7
4488
by: sven.holcombe | last post by:
Hi there, I'm trying to just prevent multiple instances of a script running. I would have thought that the following code would do this for me. <php $fp = fopen("foo.txt", "w"); flock($fp, LOCK_EX + LOCK_NB) or die ("Could not get lock!\n"); echo "Got lock!\n";
4
19168
by: Time Waster | last post by:
Is this a stupid use of flock: FILE *fp=fopen(SOME_FILE_CONSTANT,"r+"); flock(fileno(fp),LOCK_EX); something important here, including reads and a write to fp flock(fileno(fp),LOCK_UN); fclose(fp); Does this accomplish real locking, or just narrow down the race quite a bit? (Race existing between the unlock and the
2
3649
by: xucs007 | last post by:
I ran following 2 programs (lock1, lock2) at almost same time, to write either "123456", or "222" to file "aaa" at the same time. But I often just got "222456" in "aaa" . Is this a bug of python fcntl module ? See 2 programs I ran: #!/usr/bin/env python import fcntl, time
6
4216
by: Bill H | last post by:
I am wondering if this is possible. I have a file that is accessed by multiple users and keeps track of activity (the file is polled by flash every 2 seconds). As users leave, the flash program tells the php program to remove them from the activity file, and as users access this activity file, users who have timed out (haven't been heard from in 60 seconds) are removed. Using a combination of flock, microsecond sleeps, loops and the flash...
0
9650
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9497
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8992
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6748
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.