This is the mail archive of the
cygwin
mailing list for the Cygwin project.
RE: spawnv() unlocks files in the calling program
- From: "Steven Bardwell" <SBardwell at lbmsys dot com>
- To: <cygwin at cygwin dot com>
- Date: Sun, 9 Feb 2014 15:06:19 -0500
- Subject: RE: spawnv() unlocks files in the calling program
- Authentication-results: sourceware.org; auth=none
- References: <040201cf25c4$54e9cd20$febd6760$ at lbmsys dot com> <20140209192859 dot GG2821 at calimero dot vinschen dot de>
- Reply-to: <sbardwell at lbmsys dot com>
> How do you test that? You're calling fcntl(F_SETLKW) exactly once at
> the start of your test application, but never again later. We're
> talking advisory file locking here, so, where's the next fcntl call
> waiting for the lock?
>
> I debugged your test app and the lock still exists after the spawn call.
>
>
>
> Corinna
To test this, I start this program to check for the lock:
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <time.h>
main(argc, argv)
int argc;
char *argv[];
{
int rc;
FILE *pid_file=NULL;
struct flock lock;
char ctbuf[90];
time_t t,time(time_t *);
while (1) {
t=time((time_t*)0); /* make a time stamp */
strcpy(ctbuf,(char *)ctime(&t));
if (ctbuf[strlen(ctbuf)-1]=='\n') ctbuf[strlen(ctbuf)-1]=(char)0;
pid_file=fopen("/tmp/yyy", "r");
if (!pid_file) {
strcat(ctbuf, ": could not open file\n");
fprintf(stderr, ctbuf);
sleep(2);
continue;
}
lock.l_type = F_WRLCK;
lock.l_start = 0;
lock.l_whence = 0;
lock.l_len = 0;
errno = 0;
if ((fcntl(fileno(pid_file), F_GETLK, &lock)) < 0) {
fprintf(stderr, "fcntl() error %d\n", strerror(errno));
exit(1);
}
if (lock.l_type == F_UNLCK) {
strcat(ctbuf, ": File is not locked\n");
fprintf(stderr, ctbuf);
} else {
strcat(ctbuf, ": File is locked\n");
fprintf(stderr, ctbuf);
}
sleep(2);
}
exit(0);
}
It loops until it can open the file (/tmp/yyy), then every two seconds it checks the locked status of the file. Then I start the other program (in a second window). What I see is:
TESTSPAWN:
Sun Feb 9 14:57:47 2014: /tmp/yyy is locked ... sleeping for 10 seconds
Sun Feb 9 14:57:57 2014: spawnv() succeeded
Sun Feb 9 14:57:57 2014: spawned program completed
Sun Feb 9 14:57:57 2014: main program idle
Sun Feb 9 14:57:59 2014: main program idle
Sun Feb 9 14:58:01 2014: main program idle
Sun Feb 9 14:58:03 2014: main program idle
TESTLOCK:
Sun Feb 9 14:57:40 2014: File is not locked
Sun Feb 9 14:57:42 2014: File is not locked
Sun Feb 9 14:57:44 2014: File is not locked
Sun Feb 9 14:57:46 2014: File is not locked
Sun Feb 9 14:57:48 2014: File is locked
Sun Feb 9 14:57:50 2014: File is locked
Sun Feb 9 14:57:52 2014: File is locked
Sun Feb 9 14:57:54 2014: File is locked
Sun Feb 9 14:57:56 2014: File is locked
Sun Feb 9 14:57:58 2014: File is not locked
Sun Feb 9 14:58:00 2014: File is not locked
Sun Feb 9 14:58:02 2014: File is not locked
Sun Feb 9 14:58:04 2014: File is not locked
Sun Feb 9 14:58:06 2014: File is not locked
Steve
~
--
Problem reports: http://cygwin.com/problems.html
FAQ: http://cygwin.com/faq/
Documentation: http://cygwin.com/docs.html
Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple