473,418 Members | 2,039 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

`errno' (Dan Pop)

> In <pa****************************@bar.net> "Mac" <fo*@bar.net> writes:
Is it legal to declare errno after you've included errno.h?

For example:

#include<errno.h>

...

int main (void)
{
extern int errno;

...

}

I see in the standard that errno may be a macro, and I see that defining
errno is illegal, but I don't think the declaration above counts as a
definition since it doesn't reserve storage. On the other hand, if errno
IS a macro, the declaration above could easily be a syntax error after
pre-processing.

Please enlighten me.
Your analysis is correct. You cannot even declare errno, because it can
be (and quite often is) defined as a macro in <errno.h>. The common
reason for this is allowing multithreaded applications to have a per
thread errno, rather than sharing a global errno. This is one of the
few places where the C standard cares about multithreading.


[Today I happened to see that this mail was hiding in the "Drafts" folder
for more than a month!]

I am at loss as how defining `errno' as a macro can be helpful in
multithreading applications. Please enlighten me.

--
Vijay
The common macro definition for errno is along the lines:

#define errno (*__errno())

So, if you need to access errno, include <errno.h> and use whatever
definition/declaration it provides.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de

Nov 14 '05 #1
11 2498
In article <2i************@uni-berlin.de>,
"Vijay Kumar R Zanvar" <vi*****@globaledgesoft.com> wrote:
I am at loss as how defining `errno' as a macro can be helpful in
multithreading applications. Please enlighten me.


If "errno" were a plain extern variable, then all threads in a
multithreading application would always see the same "errno" variable.
So if thread A calls a function which might change errno, then checks
errno immediately after the function call, then another thread B could
be calling a different function modifying errno just before thread A
reads the value of errno. So thread A might read the wrong value.

In multithreading applications, you usually have a modified standard
library where every thread has its own "errno" variable, and a library
function called by thread X modifies the errno variable belonging to
thread X. Usually errno is a macro defined as a function call returning
the errno variable of the current thread.
Nov 14 '05 #2
"Vijay Kumar R Zanvar" <vi*****@globaledgesoft.com> writes:
[...]
I am at loss as how defining `errno' as a macro can be helpful in
multithreading applications. Please enlighten me.


As Dan wrote:
The common macro definition for errno is along the lines:

#define errno (*__errno())


Assume each thread has its own data area. The hypothetical __errno()
function returns a pointer into the data area of the current thread.
This way, each thread effectively gets its own unique errno variable.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #3

"Christian Bau" <ch***********@cbau.freeserve.co.uk> wrote in message
news:ch*********************************@slb-newsm1.svr.pol.co.uk...
In article <2i************@uni-berlin.de>,
"Vijay Kumar R Zanvar" <vi*****@globaledgesoft.com> wrote:
I am at loss as how defining `errno' as a macro can be helpful in
multithreading applications. Please enlighten me.


If "errno" were a plain extern variable, then all threads in a
multithreading application would always see the same "errno" variable.
So if thread A calls a function which might change errno, then checks
errno immediately after the function call, then another thread B could
be calling a different function modifying errno just before thread A
reads the value of errno. So thread A might read the wrong value.

In multithreading applications, you usually have a modified standard
library where every thread has its own "errno" variable, and a library
function called by thread X modifies the errno variable belonging to
thread X. Usually errno is a macro defined as a function call returning
the errno variable of the current thread.


Nicely explained. Thanks.
Nov 14 '05 #4

"Keith Thompson" <ks***@mib.org> wrote in message news:ln************@nuthaus.mib.org...
"Vijay Kumar R Zanvar" <vi*****@globaledgesoft.com> writes:
[...]
I am at loss as how defining `errno' as a macro can be helpful in
multithreading applications. Please enlighten me.
As Dan wrote:
The common macro definition for errno is along the lines:

#define errno (*__errno())


Assume each thread has its own data area. The hypothetical __errno()
function returns a pointer into the data area of the current thread.
This way, each thread effectively gets its own unique errno variable.


This paragraph clears my doubt. Thanks.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

Nov 14 '05 #5
in comp.lang.c i read:
In article <2i************@uni-berlin.de>,
"Vijay Kumar R Zanvar" <vi*****@globaledgesoft.com> wrote:

I am at loss as how defining `errno' as a macro can be helpful in
multithreading applications. Please enlighten me.


If "errno" were a plain extern variable, then all threads in a
multithreading application would always see the same "errno" variable.


this is not required or always true (think platform magic). but it's off-
topic so i'll not go into it further.

--
a signature
Nov 14 '05 #6
In <m1*************@usa.net> those who know me have no need of my name <no****************@usa.net> writes:
in comp.lang.c i read:
In article <2i************@uni-berlin.de>,
"Vijay Kumar R Zanvar" <vi*****@globaledgesoft.com> wrote:

I am at loss as how defining `errno' as a macro can be helpful in
multithreading applications. Please enlighten me.


If "errno" were a plain extern variable, then all threads in a ^^^^^^^^^^^^^^^^^^^^^^^multithreading application would always see the same "errno" variable.


this is not required or always true (think platform magic).


Of course it is required and always true. Once implementation magic gets
involved, errno is no longer "a plain extern variable".

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #7
In article <c9**********@sunnews.cern.ch>, Dan Pop <Da*****@cern.ch> wrote:
I am at loss as how defining `errno' as a macro can be helpful in
multithreading applications. Please enlighten me. If "errno" were a plain extern variable, then all threads in a ^^^^^^^^^^^^^^^^^^^^^^^multithreading application would always see the same "errno" variable.
this is not required or always true (think platform magic).
Of course it is required and always true. Once implementation magic gets
involved, errno is no longer "a plain extern variable".


I think by "if errno were a plain extern variable" the poster meant
"if errno's declaration were a plain extern variable declaration", in
contrast to being declared as a macro. At least, that reading is one
that makes his comment comprehensible.

-- Richard
Nov 14 '05 #8

"Dan Pop" <Da*****@cern.ch> wrote in message
news:c9**********@sunnews.cern.ch...
Of course it is required and always true. Once implementation magic gets
involved, errno is no longer "a plain extern variable".

I can be. On the environment I work on, it is. Its value is swapped during
a context switch along with the registers.

DrX
Nov 14 '05 #9
In <c9*********@cui1.lmms.lmco.com> "Xenos" <do**********@spamhate.com> writes:

"Dan Pop" <Da*****@cern.ch> wrote in message
news:c9**********@sunnews.cern.ch...
Of course it is required and always true. Once implementation magic gets
involved, errno is no longer "a plain extern variable".

I can be. On the environment I work on, it is. Its value is swapped during
a context switch along with the registers.


This is another way to say that each context has its own errno, which is
perfectly natural and doesn't require any compiler magic. However, a
context switch and a thread switch are not the same thing.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #10
In <c9***********@pc-news.cogsci.ed.ac.uk> ri*****@cogsci.ed.ac.uk (Richard Tobin) writes:
In article <c9**********@sunnews.cern.ch>, Dan Pop <Da*****@cern.ch> wrote:
> I am at loss as how defining `errno' as a macro can be helpful in
> multithreading applications. Please enlighten me.If "errno" were a plain extern variable, then all threads in a

^^^^^^^^^^^^^^^^^^^^^^^
multithreading application would always see the same "errno" variable.this is not required or always true (think platform magic).

Of course it is required and always true. Once implementation magic gets
involved, errno is no longer "a plain extern variable".


I think by "if errno were a plain extern variable" the poster meant
"if errno's declaration were a plain extern variable declaration", in
contrast to being declared as a macro. At least, that reading is one
that makes his comment comprehensible.


I doubt many implementors would bother making implementation magic work
for ordinarily declared identifiers. It's far more sensible to put the
implementation magic in the declaration of the identifier, like:

extern __thread_local int errno;

in which case errno is no longer a plain extern variable.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #11

"Dan Pop" <Da*****@cern.ch> wrote in message
news:c9**********@sunnews.cern.ch...
In <c9*********@cui1.lmms.lmco.com> "Xenos" <do**********@spamhate.com> writes:
This is another way to say that each context has its own errno, which is
perfectly natural and doesn't require any compiler magic. However, a
context switch and a thread switch are not the same thing.

Dan

That's just arguing sematics. A thread switch is a context switch, though
there are other kinds of context switches.

DrX
Nov 14 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

89
by: Tak-Shing Chan | last post by:
Dear c.l.c regulars, How about codifying a list of acceptable acronyms on c.l.c? <g> <g,d&r> <VBG> AAMOF AFAIAA AFAIAC
4
by: Richard Tobin | last post by:
In a library I am writing, I want to use an errno-like mechanism for error returns. The error would probably be represented as a struct rather than just an integer. I don't have any...
3
by: Mac | last post by:
Is it legal to declare errno after you've included errno.h? For example: #include<errno.h> .... int main (void) {
4
by: Paul Emmons | last post by:
If I am writing a function that sets errno according to a possible error condition, should it also set errno to 0 if it executes normally, or should it leave errno alone on success?
86
by: Walter Roberson | last post by:
If realloc() finds it necessary to move the memory block, then does it free() the previously allocated block? The C89 standard has some reference to undefined behaviour if one realloc()'s memory...
18
by: pete | last post by:
On my system, the following five expressions are true: (HUGE_VAL == HUGE_VAL / 2) (1 / HUGE_VAL == 0) (sqrt(HUGE_VAL) == HUGE_VAL) (sqrt(-1) == HUGE_VAL) (sqrt(-HUGE_VAL) == HUGE_VAL) and
5
by: Urs Beeli | last post by:
I have a question regarding errno. If I understand it correctly, including <errno.h> allows me to check "errno" for error values that some standard library functions may set. This brings up some...
9
by: jacob navia | last post by:
??? Not that I miss his posts, but somehow I find sad that somebody disappears from view and nobody gives a damm. Anybody knows what happened to him? He had lost is job, and was looking for...
13
by: Spiros Bousbouras | last post by:
Assume I'm writing a function which is going to set the value of errno if something went wrong but I also want to guarantee that errno will remain unchanged if the function completed its task...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.