How to use errno in embedded linux

Posted on 2012-03-16
Last Modified: 2012-03-16
Hello, we're getting the hang of embedded linux, and we have a call to the POSIX command (in unistd.h) "write" that sometimes returns -1 indicating failure, and we have no idea why (happen to be writing to a USB port).

So I did a little digging and found that a lot of these POSIX commands set something called errno when they fail, which appears to be a centralized error code, which seemed like something we could use.  We included "errno.h" and set the errno to zero before the call that sometimes fails, and then after it fails it is set to 11.

So trying to figure out what that means, consulted this link:

which actually doesn't have an 11, so I assumed that it means 3011, which sorta fits because it is ESTDOUT "The stdout stream cannot be opened.", not very useful but the right direction.  However, consulting this page:

does NOT list ESTDOUT as one of the possible errnos.  


* are we misinterpreting our value of ESTDOUT?
* does that error make sense for the write command
* why would the write command intermittently fail to a USB port?

Thanks very much for any insight!
Question by:RonMexico
  • 3
  • 2
LVL 86

Expert Comment

ID: 37731369
Acutally, errno 11 is EAGAIN (IIRC) or "Resource temporarily unavailable" (

Author Comment

ID: 37731422
Oh... that would be consistent with the intermittent nature.  Your web link didn't have numbers, do you just have that memorized?  That IBM page has EAGAIN as 3406... meaning I will never again use that page.

So we are relatively new to linux, and you are clearly expert (or more accurate guru by the list to the right).  What kinds of things can cause "resource temporarily unavailable" for a USB port?
LVL 86

Accepted Solution

jkr earned 500 total points
ID: 37731444
That could be a lot of things, and given the nature of USB communications, I'd just guess that something else is going on on the bus at that very moment. IMO the best thing is to do what the name of the error code suggest - trying the 'write()' request again until it can be completed successfuly, e.g. (pseudocode):

int ret;

do {

  ret = write(...);

} while (ret == -1 && errno == EAGAIN);

// check other error conditions:
if (ret == -1) {

  // evaluate errno for different values

Open in new window


Author Comment

ID: 37731538
Thanks very much jkr!
LVL 86

Expert Comment

ID: 37731557
You're welcome ;o)

BTW, adding a delay in the above loop would help to make that less CPU-intensive.

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question