- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- File locking in HPUX
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-05-2007 11:40 PM
тАО06-05-2007 11:40 PM
I am trying have a write lock on the same binary which I am running. My sample program runs fine on Solaris but fails on AIX and HPUX with error message "Text file busy". Also I noticed on AIX that if I take a lock on any other executable I am not able to execute it while the lock is held. Could someone suggest if this is a standard POSIX behaviour ??
Below is my sample program
############################################################
#include
#include
#include
#include
#include
#define FILENAME "/home/cics/katiyar/a.out"
int main(int argc, char *argv[])
{
/* l_type l_whence l_start l_len l_pid */
struct flock fl = { F_WRLCK, SEEK_SET, 0, 0, 0 };
int fd;
fl.l_pid = getpid();
if (argc > 1)
fl.l_type = F_RDLCK;
if ((fd = open(FILENAME, O_WRONLY|O_NONBLOCK|O_CREAT|O_EXCL)) == -1) {
if(errno != EEXIST ){
perror("open");
exit(1);
}
else{
if ((fd = open(FILENAME, O_WRONLY|O_NONBLOCK)) == -1) {
perror("open here");
exit(1);
}
}
}
chmod(FILENAME,S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
printf("Press
getchar();
printf("Trying to get lock...");
if (fcntl(fd, F_SETLK, &fl) == -1) {
perror("fcntl");
printf("errno = %d\n",errno);
exit(1);
}
printf("got lock\n");
printf("Press
getchar();
fl.l_type = F_UNLCK; /* set to unlock same region */
if (fcntl(fd, F_SETLK, &fl) == -1) {
perror("fcntl");
exit(1);
}
printf("Unlocked.\n");
close(fd);
}
############################################################
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-06-2007 01:34 AM
тАО06-06-2007 01:34 AM
SolutionWhen you page out program code, it's inefficient to actually write the code to swap, because you already have the program code on disk. You can optimize by just discarding the pages and reading them back from the original binary file if/when needed.
If this optimization is done, it would be a good idea to make the VM subsystem hold a lock on the binary to disallow any changes to it.
If a program's binary could be modified while it's executing, after some page-ins the program code in memory would be corrupted: some of the code would be from the original version and some from the modified version. This would probably make the program crash sooner or later.
In AIX, the "vice-versa" situation is apparently taken into account too: if someone is holding a write lock on a binary, it might not be a good idea to execute it, as the holder of the lock might be modifying the binary.
MK
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-06-2007 04:29 PM
тАО06-06-2007 04:29 PM
Re: File locking in HPUX
You should instead lock some data file.
You didn't mention but I assume you got ETXTBSY when you opened the file.
- Tags:
- ETXTBSY
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-06-2007 10:23 PM
тАО06-06-2007 10:23 PM
Re: File locking in HPUX
We have a reqmnt where there should not be two instances of a daemon. Since we bind our daemon to dynamic port, it allows starting two instances. Normally what daemons do is that they keep their pid in a file and the other process checks if that pid is available. but since pids can be reused this is not full proof. So we thought of having a temp file on which we take a write lock and when other process starts it also tries and fails. This had a problem that a user can delete the temp file. So we thought of having the lock on the daemon itself. So that our issue is resolved.
but this method works on Solaris, but fails on HPUX and AIX.
Hope i am clear. Is there any other full proof method that anyone can suggest ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-06-2007 11:47 PM
тАО06-06-2007 11:47 PM
Re: File locking in HPUX
Well the demon can use ps to look to see if it already running. Or you can put the tempfile in a directory with a sticky bit so nobody but the owner or root can remove the file.
Or you can create a very small shared memory area. And if it exists, you don't start. Of course if you abort, some admin would have to clean up that file or memory area. Of course with that PID file, you could just see if that PID was there. Or check farther and see if that PID is running your demon:
$ UNIX95= ps -C demon-name -opid= | grep -w -f pid-file
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-06-2007 11:57 PM
тАО06-06-2007 11:57 PM
Re: File locking in HPUX
yes each of these methods are correct, but as you see each of them fails in some particular conditions....specially in case of aborts you cannot be sure and some manual intervention is needed. Also we don't run as root but still want to have full control that is why we chose this method.
As you can see , if this works we will not have any issues. Also since our application is large multi-threaded and creates lots of shared memories, we don't want to introduce any more.
Thanks a lot
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-07-2007 12:21 AM
тАО06-07-2007 12:21 AM
Re: File locking in HPUX
The PID file doesn't have this problem. (With checking with ps(1).)
>Also since our application is large multi-threaded and creates lots of shared memories, we don't want to introduce any more.
If you have named the shared memory, just use an existing name.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-07-2007 12:33 AM
тАО06-07-2007 12:33 AM
Re: File locking in HPUX
I would allocate a named semiphore in memory and when the program starts it locks the semiphore and if another program starts it exits since the semiphore is locked.
Here is a example of semlock which was from the linux programmers guide but works on HPUX
This example does a block and makes the program wait if the semiphore is locked. Change the code to make it just exit if the semiphore is locked.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-07-2007 02:23 AM
тАО06-07-2007 02:23 AM
Re: File locking in HPUX
Of course, all this assumes that you don't use kill -9 --- which you should be doing anyway.