Quote:
Originally Posted by trist007
Is there just one errno buffer.
|
No, each process and each thread has their own
errno.
Quote:
Originally Posted by trist007
if say I run several processes using fork and they each run a function, each time a child runs that function and returns an error(assuming they all returned an error) errno would get overwritten each time?
|
None of the other processes (or threads, if you used threads instead of processes) would see any change in their
errno. The process or thread that made the call (with the error occurring) is the only one that sees the error.
If you use processes (fork() call), all variables and data structures work that way. At the fork() call, they get copied, that's all. There is nothing strange about
errno, it is just a normal variable; the C library functions just happen to use it to describe the error when an error occurs.
If you use threads and a relatively recent compiler, you can create per-thread variables that work exactly like
errno using the
__thread keyword. For example,
Code:
int __thread my_var = -1;
Each of your threads will see a consistent value (that is, a value that only changes when the thread itself modifies it) for
my_var, but each thread will see their own value. (Also, for example
printf("my_var is at %p\n", &my_var); will print a different address for
my_var for each thread.)
Quote:
Originally Posted by trist007
So say I run httpd and mysqld. And say both error out and set errno, first httpd then mysqld. Are there two errno that I can check say one for httpd and one for mysqld?
|
Yes, httpd has it's own
errno, and
mysqld has one for itself.
(Technically, both httpd and mysqld use either multiple threads or multiple processes, so they have more than one
errno; one for every process and every thread.)
Note that
errno is not Linux-specific. The Linux kernel does not even know it exists. It is purely a C standard thing, and is defined by the C library. (Although it is defined by the C library, and many programs can use the same C library at the same time, each process will still get their own copy: the C library just
defines it. The value is stored separately for each process and thread in their data area.)
I hope this clears it up for you.