LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-10-2011, 12:06 AM   #1
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,052

Rep: Reputation: 70
Got a question about errno in C...


Is there just one errno buffer. For example, if I run a function and the function returns a -1 and sets errno, then that errno will have a value. However, 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?

Basically there's just one errno per memory space?

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? Or is there just one and the value in errno will be that of mysqld since it overwrote it last?
 
Old 07-10-2011, 01:21 AM   #2
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
I'm not a specialist when it comes to processes, threads etc. So I don't know for sure what happens when you start threading and/or forking.

But (in global terms) both httpd and mysqld have their own errno; so a 'bad file descriptor' in the one and a 'file not found' in the other will be correctly reported by the respective programs. And you can not access them using another program (they are internal).
 
Old 07-10-2011, 04:32 AM   #3
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by trist007 View Post
Is there just one errno buffer.
No, each process and each thread has their own errno.

Quote:
Originally Posted by trist007 View Post
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 View Post
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.
 
Old 07-10-2011, 10:50 AM   #4
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,052

Original Poster
Rep: Reputation: 70
Thank you, yes that cleared it up.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] ErrNo 14 and ErrNo 256 in yum server configuration nivedhitha Linux - Server 4 05-22-2011 12:45 AM
errno -2 sha_neb Linux - Software 2 04-04-2011 09:38 AM
some question about kernel-2.4.20/lib/errno.c ao.yuan.young Linux - Kernel 3 06-01-2009 10:10 AM
errno cynthia Linux - Networking 3 09-24-2004 01:58 PM
How to use errno sibtay Programming 4 09-24-2004 09:38 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration