Qustion: sigset_t operation.

F

Franklin Li

Hi All,

In Richard's book AUEP, one example as below.

But I compile with gcc in Soalris 9. It will report error.
gcc -I/usr/include -I/opt/exp/include -I/opt/exp/gnu/include -Wall -Dsun -c
setops.c
setops.c: In function `sigaddset':
setops.c:12: error: invalid operands to binary |
*** Error code 1
make: Fatal error: Command failed for target `setops.o'


Could you tell me the reason and how to fix it.

Thanks.

Franklin
***********************************************************
#define SIGBAD(signo) ((signo) <= 0 || (signo) >= NSIG)
/* <signal.h> usually defines NSIG to include signal number 0 */

int
sigaddset(sigset_t *set, int signo)
{
if (SIGBAD(signo)) { errno = EINVAL; return(-1); }

*set |= (1 << (signo - 1)); /* turn bit on */
return(0);
}

int
sigdelset(sigset_t *set, int signo)
{
if (SIGBAD(signo)) { errno = EINVAL; return(-1); }

*set &= ~(1 << (signo - 1)); /* turn bit off */
return(0);
}

int
sigismember(const sigset_t *set, int signo)
{
if (SIGBAD(signo)) { errno = EINVAL; return(-1); }

return( (*set & (1 << (signo - 1))) != 0 );
return(0);
}

**********************************************************
 
C

Chris Torek

In Richard's book AUEP, one example as below.

Which Richard, and what does "AUEP" stand for? (I can guess,
but you should not force me to guess -- and my guess is that
you mean "W. Richard Stevens", and a book that would be better
abbreviated "APUE" or "APitUE", and to shorten his name to a single
word, one would normally use "Stevens", not "Richard", so perhaps
my guess is not such a good one.)

In any case:
setops.c:12: error: invalid operands to binary |

where line 12 is apparently the "|=" line in:
int
sigaddset(sigset_t *set, int signo)
{
if (SIGBAD(signo)) { errno = EINVAL; return(-1); }

*set |= (1 << (signo - 1)); /* turn bit on */
return(0);
}

What type does "sigset_t" have? The only thing that is fairly
obvious is that it must not be an integral type. Since sigset_t
is not a basic C type, you will have to tell us more about it
before we can answer this as a C question.

Although it is off topic here, I will also note that sigaddset(),
sigdelset(), and sigismember() are standard POSIX functions, and
questions about them therefore belong in comp.unix.programmer.
 
R

Ravi Uday

Franklin said:
Hi All,

In Richard's book AUEP, one example as below.

But I compile with gcc in Soalris 9. It will report error.
gcc -I/usr/include -I/opt/exp/include -I/opt/exp/gnu/include -Wall -Dsun -c
setops.c
setops.c: In function `sigaddset':
setops.c:12: error: invalid operands to binary |
*** Error code 1
make: Fatal error: Command failed for target `setops.o'


Could you tell me the reason and how to fix it.

Thanks.

Franklin
***********************************************************
#define SIGBAD(signo) ((signo) <= 0 || (signo) >= NSIG)
/* <signal.h> usually defines NSIG to include signal number 0 */

int
sigaddset(sigset_t *set, int signo)
^^^^^^^^^^

What is 'sigset_t' ? Its not a standard data type.
So i guess its off-topic here and you need to seek in a solaris/sun
group.
 
C

CBFalconer

Chris said:
Which Richard, and what does "AUEP" stand for? (I can guess,
but you should not force me to guess -- and my guess is that
you mean "W. Richard Stevens", and a book that would be better
abbreviated "APUE" or "APitUE", and to shorten his name to a
single word, one would normally use "Stevens", not "Richard",
so perhaps my guess is not such a good one.)

I always wonder why people assume we are mind readers.
In any case:


where line 12 is apparently the "|=" line in:


What type does "sigset_t" have? The only thing that is fairly
obvious is that it must not be an integral type. Since sigset_t
is not a basic C type, you will have to tell us more about it
before we can answer this as a C question.

Surely you jest. To use the |= operator it should be an integral
type, with a heavy preference for an unsigned type.

I wonder if he pasted or typed the code. If the prototype used a
stray 'const' it could explain the problem. Another function in
his original posting did so, but didn't modify the set. The error
return of -1 is another source of problems.
Although it is off topic here, I will also note that sigaddset(),
sigdelset(), and sigismember() are standard POSIX functions, and
questions about them therefore belong in comp.unix.programmer.

Yet those names are firmly in the users namespace, so there is no
reason not to use them. Thus questions that include their source
are valid here.
 
C

Chris Torek

[various snippage]
Surely you jest. To use the |= operator it should be an integral
type, with a heavy preference for an unsigned type.

You misunderstand: the compiler is complaining that the "|" is
invalid, therefore I conclude that sigset_t is in fact not (i.e.,
"must not be") an integral type. The error lies in trying to
manipulate the bits of this non-integral type.

The fix is not "define sigset_t as an integral type", because the
sigset_t type is pre-defined as part of the (POSIX) implementation --
for the programmer to change it is like an attempt by the programmer
to re-#define RAND_MAX in an attempt to influence the numbers coming
out of rand(). It simply does not work.
Yet those names are firmly in the users namespace, so there is no
reason not to use them. Thus questions that include their source
are valid here.

Again, these things are actually supplied by the implementation.

Just for completeness, here is the actual definition of sigset_t on
his system, with some of the names and/or types changed to protect
the guilty :) :

typedef struct { unsigned char bits[SOME_CONSTANT]; } sigset_t;

The reason the compiler gripes about the assignment to *set is now
clear. The code should read something like:

set->bits[signo / SOME_DIVISOR] |= 1 << (signo % SOME_MODULUS);

but his system already provides these routines, so he should not
write them at all!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top