This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RE: spawnv() unlocks files in the calling program


> 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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]