Mail Archives: cygwin/2016/03/09/03:38:52
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:mime-version:in-reply-to:references:date
|
| :message-id:subject:from:to:content-type; q=dns; s=default; b=Ck
|
| 68ozBjS4xWHEFrH3OzFjCCGE1l+QKBJRor//GH+PIr1PpMhTrsizA5WNBsUm4rbW
|
| PsgYBaK+dO/V+xJXCQiIfqMp77X0MrFcxhtS7BGox82ES/JSkJ0dV77uQQUj/8Se
|
| SpTwSl1sCMr846UJKONgENTPJdfIVB492gnr9+cP4=
|
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:mime-version:in-reply-to:references:date
|
| :message-id:subject:from:to:content-type; s=default; bh=Onl4dXhy
|
| tcKpntTSJJT5LpHY3Do=; b=dGQClMN2yhlsGMJVWLVdNsHfRHK2fmKsKNq74hDr
|
| WI3Z2RGD4YOtJewBfjTXQgvcKKKzGYRv2xuLxWTELoMsRKWu1MyTf7sp+IuXZ9h0
|
| qvrBZdpJePyyofnSOy5iB3HNVmrQi/77SQtqWOafWoCcu1GSxO+EimJji2HIdlrW
|
| M/I=
|
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.8 required=5.0 tests=AWL,BAYES_00,FREEMAIL_ENVFROM_END_DIGIT,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW,SPF_PASS autolearn=ham version=3.3.2 spammy=sk:meenaks, meenakshi DOT pant28 AT gmail DOT com, meenakshipant28gmailcom, U*meenakshi.pant28
|
X-HELO: | mail-wm0-f47.google.com
|
X-Google-DKIM-Signature: | v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to; bh=O65XEwOW2xW4tLKTyPYIC4PCrqEITbej7MCqAsKO+j0=; b=hhDcYwp7H/I/GK2zHaOBNR1EGmOSaHAARy5BncLUkk7pUb++I7SCN2epLLQxbcGb8I 5YF2BIb03SaGp5qChztO+E6D7bLo5CrVyEyaLVNptBFI1sf33ODofQrLKbMGGCf3feqC /y0g8BNPucyO4voVEfjIGVGc/bAy6VGIC5JjQ1ALg8UyrSIde+nLkgQ/JiQX4+4jYMDj Hl8YDQoi/u8ZSQJ517N/ZJg4NfumtkIoEpfK9KqN5MAVm/kYamxwHj7Wr8FKrEdGbJ/N wCyQpiY8/18k53wD4rb7NZxRkRPOPHc1ERHuufcs0ouzJ0VxyUrZTgX3v2PjIazR+slq GN+Q==
|
X-Gm-Message-State: | AD7BkJLMfOIwwSbpTkGiedQMXdyxGYrPzeztSehGndxwZ2QHOGm/EHCVOJUpxGxoHovnZIuF3528sp4FNOz92A==
|
MIME-Version: | 1.0
|
X-Received: | by 10.194.112.98 with SMTP id ip2mr34303354wjb.24.1457512699967; Wed, 09 Mar 2016 00:38:19 -0800 (PST)
|
In-Reply-To: | <CAAeFwS0z=rd7PHG9NEgCZqN2747j9uVQPeizFAH3x2atPaLiSQ@mail.gmail.com>
|
References: | <CAAeFwS3owmU++VppE_pPbX7aFcNC9a8qAvoVMLqO+jr3MAxWuQ AT mail DOT gmail DOT com> <56DE911A DOT 1010506 AT gmail DOT com> <CAAeFwS0z=rd7PHG9NEgCZqN2747j9uVQPeizFAH3x2atPaLiSQ AT mail DOT gmail DOT com>
|
Date: | Wed, 9 Mar 2016 14:08:19 +0530
|
Message-ID: | <CAAeFwS3Bq=6=L4k1hOg=ZUk6J6WSKPnCjuMtLPQBnnnySNRA_w@mail.gmail.com>
|
Subject: | Re: Semaphore Lock Failed Error
|
From: | Meenakshi Pant <meenakshi DOT pant28 AT gmail DOT com>
|
To: | cygwin AT cygwin DOT com
|
X-IsSubscribed: | yes
|
Our program is failing in creating and acquiring lock on the
semaphore. Created a test program 'semtest.c'. It is creating the
semaphore using the program semaphore.c. Compiled and executed using
the latest CYGWIN- 2.4.1. It is failing at line
if ((semval = semctl(*id, 1, GETVAL, semctl_arg)) < 0 )
with error no 22.
Code for semtest.c and semaphore.c are as given below
*********************************************************************
/* semtest.c */
#define PROG_NAME "SEMTEST"
#define TITLE "SEMTEST Semaphore Test Stub - Version 0.1 (10-Dec-1992)"
#define USAGE "Usage: semtest c|o"
#ifndef lint
static char *sccs_id="$Id: semtest.c,v 7.0 2000/08/24 15:59:12 growe Exp $";
#endif
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#ifndef LOCAL_INC
#include <kinsys.h>
#include <semaphore.h>
#include <errormsg.h>
#else
#include <kinsys.h>
#include <semaphore.h>
#include <errormsg.h>
#endif
static int sem = SEM_NULL;
static key_t key = 0x123d4a;
int main(int argc, char *argv[])
{
printf("\n%s\n", TITLE);
printf("\n%s\n", USAGE);
if (argc != 2)
{
printf("\n%s\n", USAGE);
exit(EXIT_FAILURE);
}
switch (argv[1][0])
{
case 'c':
if (SemCreate(&sem, key, SEM_BINARY) EQ FAIL)
goto ErrExit;
printf("Semaphore Created key: 0x%x sem: %d\n", key, sem);
break;
case 'o':
if (SemOpen(&sem, key) EQ FAIL)
goto ErrExit;
printf("Semaphore Opened key: 0x%x sem: %d\n", key, sem);
break;
default:
printf("\n%s\n", USAGE);
exit(EXIT_FAILURE);
}
printf("About to Wait/lock on semaphore\n");
if (SemWait(sem) EQ FAIL)
goto ErrExit;
printf("semaphore locked\n");
printf("Press [RETURN] to continue and unlock semaphore\n");
(void) getchar();
printf("About to Signal/unlock on semaphore\n");
if (SemSignal(sem) EQ FAIL)
goto ErrExit;
printf("semaphore unlocked\n");
printf("About to Close on semaphore\n");
if (SemClose(sem) EQ FAIL)
goto ErrExit;
sem = SEM_NULL;
printf("semaphore closed\n");
(void)MsgClose(mhDefault);
printf("Program Terminated Successfully\n");
exit(EXIT_SUCCESS);
return(SUCCESS);
ErrExit:
(void) EsTraceBack();
(void)MsgClose(mhDefault);
printf("Program Terminated with error\n");
exit(EXIT_FAILURE);
return(FAIL);
}
**************************************************************************************
Semaphore.c
/* semaphore.c
* Description: Provides a simpler and safer interface to the UNIX
* System V Release 4 semaphores. Seven functions are
* provided.
*
* 1. id = SemCreate(key, value);
* 2. id = SemOpen(key);
* 3. SemWait(id);
* 4. SemSignal(id);
* 5. SemOp(id, amount);
* 6. SemClose(id);
* 7. SemRm(id);
*
* Portability Issues: This implementation requires System V semaphore
* facility.
*
* Edit History:
*
*
*/
#ifndef lint
static char *sccs_id="$Id: semaphore.c,v 7.2 2001/03/26 10:54:28 asealy Exp $";
#endif
#include <stdio.h>
#include <stdlib.h>
#include <machine.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <errno.h>
extern int errno;
#define SEM_
#include <kinsys.h>
#include <debug.h>
#include <errormsg.h>
#include <semaphore.h>
#include <semaphore.mh>
#include <ux.h> /* General UNIX messages */
#include <ux.mh> /* General UNIX messages */
#define BIGCOUNT 10000 /* Initial value of process counter */
#if !SEMUN_DEFINED
union semun {
int val;
struct semid_ds *buf;
ushort *array;
} semctl_arg;
#else
union semun semctl_arg;
#endif
typedef struct sembuf SEMBUF;
/* Prototypes for local functions */
/*
* Define the semaphore operation arrays for the semop() calls.
*/
static SEMBUF op_lock[2] = {
2, 0, 0, /* wait for [2] (lock) to equal 0 */
2, 1, SEM_UNDO /* then incrrement [2] to 1 - this locks it */
/* UNDO to release the lock it process exits
before explicitly unblocking */
};
static SEMBUF op_endcreate[2] = {
1, -1, SEM_UNDO,
2, -1, SEM_UNDO
};
static SEMBUF op_open[1] = {
1, -1, SEM_UNDO
};
static SEMBUF op_close[3] = {
2, 0, 0,
2, 1, SEM_UNDO,
1, 1, SEM_UNDO
};
static SEMBUF op_unlock[1] = {
2, -1, SEM_UNDO
};
static SEMBUF op_op[1] = {
0, 99, SEM_UNDO
};
/************************************************************************
* Name: SemCreate
*
* Function:
*
* Input: key - the IPC key that identifies the semaphore.
* initval - initial value of the semaphore.
*
* Output *id - the identifier of this semaphore.
*
* Return: SUCCESS or FAIL
*
* Side Effects: none
*
* Procedure: 1.
* 2.
* 3.
************************************************************************/
STATUS
SemCreate(int *id, key_t key, int initval)
{
printf(" \n in semcretae");
printf("\n SEMUN_DEFINED = %i " , SEMUN_DEFINED);
register int semval;
printf(" \n After Register = %i ", semval);
if (key EQ IPC_PRIVATE)
{
printf(" \n SEM, Not intended for private semaphores");
goto ErrExit;
}
else if (key EQ (key_t) -1)
{
printf("\nSEM, probably an ftok() error by caller");
goto ErrExit;
}
again:
if ( (*id = semget(key, 3, 0666 | IPC_CREAT)) < 0)
{
printf("\nSEM, SemCreate, semget access/limits errno %d", errno);
goto ErrExit;
}
if (semop(*id, &op_lock[0], 2) < 0)
{
if (errno EQ EINVAL)
goto again;
printf("\nSEM, Can't lock...: errno = %d", errno);
goto ErrExit;
}
/*
* Get the value of the process counter. If it equals zero
* then no one has initialised the semaphore yet.
*/
semctl_arg.val = 0;
if ( (semval = semctl(*id, 1, GETVAL, semctl_arg)) < 0)
{
printf("\n SEM, can't GETVAL..: errno = %d", errno);
goto ErrExit;
}
if (semval EQ 0)
{
/* We could initialise by doing a SETALL, but that
* would clear the adjust value.
*/
semctl_arg.val = initval;
if (semctl(*id, 0, SETVAL, semctl_arg) < 0)
{
printf("\nSEM, can SETVAL[0]..: errno = %d", errno);
goto ErrExit;
}
semctl_arg.val = BIGCOUNT;
if (semctl(*id, 1, SETVAL, semctl_arg) < 0)
{
printf("\nSEM, can SETVAL[1]..: errno = %d", errno);
goto ErrExit;
}
}
/*
* Decrement the process counter and then release the lock.
*/
if (semop(*id, &op_endcreate[0], 2) < 0)
{
printf("\nSEM", "can't endcreate..: errno = %d", errno);
goto ErrExit;
}
printf("\nSEM", "SemCreate SUCCESS id:%d tok:%x", *id, key);
return(SUCCESS);
ErrExit:
*id = SEM_NULL;
printf("\n SEM, SemCreate FAIL id:%d tok:%x", *id, key);
return(FAIL);
}
/************************************************************************
* Name: SemOpen
*
* Function:
*
* Input: id - the identifier of this semaphore.
*
* Output: none.
*
* Return: SUCCESS or FAIL
*
* Side Effects: none
*
* Procedure: 1.
* 2.
* 3.
************************************************************************/
STATUS
SemOpen(int *id, key_t key)
{
printf("\n SEM", "SemOpen Token/key tok:%x", key);
if (key EQ IPC_PRIVATE)
{
printf("\n SEM, Not intended for private semaphores");
goto ErrExit;
}
else if (key EQ (key_t) -1)
{
printf("\n SEM", "probably an ftok() error by caller");
goto ErrExit;
}
if ( (*id = semget(key, 3, 0)) < 0)
{
printf("\n SEM, SemOpen, semget prot or tables full:..: errno = %d", errno);
goto ErrExit;
}
/*
* Decrement the process counter - we don't need a lock.
*/
if (semop(*id, &op_open[0], 1) < 0)
{
printf("\n SEM, can't open..: errno = %d", errno);
goto ErrExit;
}
printf("\n SEM, SemOpen SUCCESS id:%d tok:%x", *id, key);
return(SUCCESS);
ErrExit:
*id = SEM_NULL;
printf("\n SEM, SemOpen FAIL id:%d tok:%x", *id, key);
return(FAIL);
}
/************************************************************************
* Name: SemRm
*
* Function:
*
* Input: id - the identifier of this semaphore.
*
* Output: none.
*
* Return: SUCCESS or FAIL
*
* Side Effects: none
*
* Procedure: 1.
* 2.
* 3.
************************************************************************/
STATUS
SemRm(int id)
{
printf("\n SEM", "SemRm( id:%d)",id);
semctl_arg.val = 0;
if (semctl(id, 0, IPC_RMID, semctl_arg) < 0)
{
printf("\n SEM", "can't IPC_RMID..: errno = %d", errno);
goto ErrExit;
}
printf("\n SemRm Error");
return(SUCCESS);
ErrExit:
printf("\n SemRm Error");
return(FAIL);
}
/************************************************************************
* Name: SemClose
*
* Function:
*
* Input: id - the identifier of this semaphore.
*
* Output: none.
*
* Return: SUCCESS or FAIL
*
* Side Effects: none
*
* Procedure: 1.
* 2.
* 3.
************************************************************************/
STATUS
SemClose(int id)
{
register int semval;
/* the followung semop() first gets a lock on the semaphore,
* then increments [1] - the process conuter.
*/
printf(" \n SEM, SemClose( id:%d)",id);
if (semop(id, &op_close[0], 3) < 0)
{
printf("\n SEM, SemClose can't sem_op..: errno = %d", errno);
goto ErrExit;
}
/*
* Now that we have a lock, read the value of the process
* counter to see if this is the last reference to the
* semaphore.
* There is a race condition here - see the comment in
* SemCreate().
*/
semctl_arg.val = 0;
if ( (semval = semctl(id, 1, GETVAL, semctl_arg)) < 0)
{
printf("\n SEM, SemClose can't GETVAL..: errno = %d", errno);
goto ErrExit;
}
if (semval > BIGCOUNT)
{
printf("\nSEM, sem[1] > BIGCOUNT");
goto ErrExit;
}
else if (semval EQ BIGCOUNT)
{
if ( SemRm(id) EQ FAIL)
goto ErrExit;
}
else if (semop(id, &op_unlock[0], 1) < 0)
{
printf("\nSEM, can't unlock..: errno = %d", errno);
goto ErrExit;
}
printf("\n SemClose Error");
return(SUCCESS);
ErrExit:
printf("\n SemClose Error FAIL id:%d",id);
return(FAIL);
}
/************************************************************************
* Name: SemWait
*
* Function:
*
* Input: id - the identifier of this semaphore.
*
* Output: none.
*
* Return: SUCCESS or FAIL
*
* Side Effects: none
*
* Procedure: 1.
* 2.
* 3.
************************************************************************/
STATUS
SemWait(int id)
{
if (SemOp(id, -1) EQ FAIL)
goto ErrExit;
printf("\n SemWait Error");
return(SUCCESS);
ErrExit:
printf("\n SemWait Error");
return(FAIL);
}
/************************************************************************
* Name: SemSignal
*
* Function:
*
* Input: id - the identifier of this semaphore.
*
* Output: none.
*
* Return: SUCCESS or FAIL
*
* Side Effects: none
*
* Procedure: 1.
* 2.
* 3.
************************************************************************/
STATUS
SemSignal(int id)
{
if ( SemOp(id, 1) EQ FAIL)
goto ErrExit;
printf("\n SemSignal Error");
return(SUCCESS);
ErrExit:
printf("\n SemSignal Error");
return(FAIL);
}
/************************************************************************
* Name: SemOp
*
* Function:
*
* Input: id - the identifier of this semaphore.
*
* Output: none.
*
* Return: SUCCESS or FAIL
*
* Side Effects: none
*
* Procedure: 1.
* 2.
* 3.
************************************************************************/
STATUS
SemOp(int id, int value)
{
if ( (op_op[0].sem_op = value) EQ 0)
{
printf("\n SEM, can't have value EQ 0");
goto ErrExit;
}
if (semop(id, &op_op[0], 1) < 0)
{
printf("\n SEM, semop error..: errno = %d", errno);
goto ErrExit;
}
printf("\n SemOp Success");
return(SUCCESS);
ErrExit:
printf("\n SemOp Error");
return(FAIL);
}
/* End of file: semaphore.c */
/**********************************************************/
On Wed, Mar 9, 2016 at 2:02 PM, Meenakshi Pant
<meenakshi DOT pant28 AT gmail DOT com> wrote:
> Our program is failing in creating and acquiring lock on the
> semaphore. Created a test program 'semtest.c'. It is creating the
> semaphore using the program semaphore.c. Compiled and executed using
> the latest CYGWIN- 2.4.1. It is failing at line
>
> if ((semval = semctl(*id, 1, GETVAL, semctl_arg)) < 0 )
>
> with error no 22.
>
> Code for semtest.c and semaphore.c are attached here.
--
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 -