delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2014/02/09/15:06:50

X-Recipient: archive-cygwin AT delorie DOT com
DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id
:list-unsubscribe:list-subscribe:list-archive:list-post
:list-help:sender:reply-to:from:to:references:in-reply-to
:subject:date:message-id:mime-version:content-type
:content-transfer-encoding; q=dns; s=default; b=vQb3A7itBOaah5zv
ILawfA4uZwoqrvibEI1c6jsY0XL997RAvFc+D9sfTH5BvbfCG8+Mn+zuTiSSugxo
rgHW/p5sVk8g6MKiwjjhv2xTt/K5gh8EY1jJ9yzTculfThJp/OfpWJ4tCxwSC5ed
TGnEcPVRKbK+P45OPfGbTmgyDRw=
DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id
:list-unsubscribe:list-subscribe:list-archive:list-post
:list-help:sender:reply-to:from:to:references:in-reply-to
:subject:date:message-id:mime-version:content-type
:content-transfer-encoding; s=default; bh=cSK/rZyEn189xNv+nnfjeN
WrQVg=; b=YdOE4MeCtkuc8J08B0m/13PsF20R7wbmxtSBzZ/De5aHTqgnFee+Wr
rAPOGEa/F3w0oEHnAdYRt7hrO9qD+6KOzYF9n0phDSvX+S7fvcnTu6KtUIzI4IoP
vvWdl5rILsXfKIpLIho9vUepQ/68FgbDG+JQKLNYd6t4yO7kihCW4=
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
Authentication-Results: sourceware.org; auth=none
X-Virus-Found: No
X-Spam-SWARE-Status: No, score=-0.5 required=5.0 tests=AWL,BAYES_00,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.2
X-HELO: p3plsmtpa08-08.prod.phx3.secureserver.net
Reply-To: <sbardwell AT lbmsys DOT com>
From: "Steven Bardwell" <SBardwell AT lbmsys DOT com>
To: <cygwin AT cygwin DOT com>
References: <040201cf25c4$54e9cd20$febd6760$@lbmsys.com> <20140209192859 DOT GG2821 AT calimero DOT vinschen DOT de>
In-Reply-To: <20140209192859.GG2821@calimero.vinschen.de>
Subject: RE: spawnv() unlocks files in the calling program
Date: Sun, 9 Feb 2014 15:06:19 -0500
Message-ID: <041d01cf25d2$6a59e5b0$3f0db110$@lbmsys.com>
MIME-Version: 1.0
X-IsSubscribed: yes
X-MIME-Autoconverted: from quoted-printable to 8bit by delorie.com id s19K6jO4008951

> 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


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019