• Navigation überspringen
  • Zur Navigation
  • Zum Seitenende
Organisationsmenü öffnen Organisationsmenü schließen
Lehrstuhl für Informatik 4 & 16
  • FAUZur zentralen FAU Website
  1. Friedrich-Alexander-Universität
  2. Technische Fakultät
  3. Department Informatik
  • English
  • Campo
  • UnivIS
  • Stellenangebote
  • Lageplan
  • Hilfe im Notfall
  1. Friedrich-Alexander-Universität
  2. Technische Fakultät
  3. Department Informatik

Lehrstuhl für Informatik 4 & 16

Menu Menu schließen
  • Lehrstuhl
    • Leitbild
    • Team
    • Kontakt und Anfahrt
    • 50-jähriges Jubiläum
    Portal Lehrstuhl
  • Forschung
    • Forschungsbereiche
      • Betriebssysteme
      • Echtzeitsysteme
      • Energiebewusste Systeme
      • Verteilte Systeme
    • Projekte
      • BFT2Chain
      • e.LARN
      • NEON
      • PAVE
      • PRIMaTE
      • REFIT
      • ResPECT
      • SURESOFT
      • TRR 89 C1: iRTSS
      • TRR 89 C5
      • Watwa
    • Projektkampagnen
      • DOSS
      • maRE
    • Forschungsgruppen
      • ergoo
    Portal Forschung
  • Publikationen
  • Lehre
    • Sommersemester 2023
      • Ausgewählte Kapitel der Systemsoftware
      • Betriebssystemtechnik
      • Projekt angewandte Systemsoftwaretechnik
      • Systemnahe Programmierung in C
      • Systemprogrammierung 1
      • Verteilte Systeme
    • Wintersemester 2023/24
      • Ausgewählte Kapitel der Systemsoftware
      • Betriebssysteme
      • Systemprogrammierung 2
      • Middleware – Cloud Computing
      • Virtuelle Maschinen
      • Web-basierte Systeme
    Portal Lehre
  • Examensarbeiten
  1. Startseite
  2. Extern

Extern

Bereichsnavigation: Lehre
  • Systemnahe Programmierung in C
    • Vorlesung
      • Übung
        • Folien
        • Aufgaben
        • SPiCboard
          • Bauanleitung
            • Programmieren im CIP
              • Programmieren von zu Hause
                • libspicboard-Doku
                  • SPiC-IDE
                    • SPiCsim
                      • FAQ
                        • Projekte
                        • Linux libc-Doku
                        • Prüfung
                          • Klausurergebnisse
                          • Evaluation
                            • Intern

                            Linux libc-Doku

                            Files | Variables
                            Errno Variable

                            Files

                            file  errno.h
                             

                            Variables

                            int errno
                             Error code set by various library functions.
                             

                            Detailed Description

                            The errno variable is an integer variable and set by system calls and library functions to indicate the source of an error. The errno is undefined, except when a system call or library function indicates an error (e.g., by a special return value) and the corresponding manpage states that in case of an error the errno variable is set. From this follows that the value of the errno is undefined after a successful call to a system call or library function. Furthermore, no system call or library functions sets the errno to 0.

                            There are rare cases, where the errno is not only used to indicate the source of an error, but is also used to detect an error (e.g., readdir()). If the errno is used to detect an error (and not, as usually, the return value) it is explicitly stated in the manpages. In this case the errno must be manually set to 0 before calling the function to be able to check if the function changed the value. Except for these rare cases setting the errno manually is never correct, unless one is writing a library function (e.g., malloc()).

                            The errno variable is a thread-local variable, which means every POSIX thread has a separate errno. Hence, the access of errno must not be synchronized against other POSIX threads.

                            Wrong usage of errno:

                            char *s = malloc(1024);
                            if (s == NULL) {
                            fprintf(stderr, "malloc: ");
                            // now the errno is undefined, because of a successful
                            // or unsuccessful call to fprintf()
                            // wrong: strerror() uses an undefined value to generate the string
                            fprintf(stderr, "%s\n", strerror(errno));
                            exit(EXIT_FAILURE);
                            }
                            errno
                            int errno
                            Error code set by various library functions.
                            fprintf
                            int fprintf(FILE *stream, const char *format,...)
                            Print formatted data to stream.
                            malloc
                            void * malloc(size_t size)
                            Allocate memory.
                            exit
                            void exit(int status)
                            Terminate process.
                            char *s = malloc(1024);
                            if (s == NULL) {
                            // wrong: original errno value is overwritten!
                            errno = ENOMEM;
                            perror("malloc");
                            exit(EXIT_FAILURE);
                            }
                            perror
                            void perror(const char *s)
                            Print an error message.
                            errno = 0; // wrong: setting errno has no effect here
                            char *s = malloc(1024);
                            if (errno != 0) {
                            // wrong: errno can be != 0 even if malloc() is successful
                            // NOTE: There are rare exceptions (e.g., readdir()).
                            perror("malloc");
                            exit(EXIT_FAILURE);
                            }

                            Correct usage of errno:

                            char *s = malloc(1024);
                            if (s == NULL) {
                            perror("malloc");
                            exit(EXIT_FAILURE);
                            }
                            errno = 0;
                            struct dirent *entry = readdir(dirp);
                            if (entry != NULL) {
                            // process entry
                            } else if (errno != 0) { // explicitly stated in man page
                            perror("readdir");
                            exit(EXIT_FAILURE);
                            }
                            readdir
                            struct dirent * readdir(DIR *dirp)
                            Read an entry of a directory.
                            Friedrich-Alexander-Universität
                            Erlangen-Nürnberg

                            Schlossplatz 4
                            91054 Erlangen
                            • Impressum
                            • Datenschutz
                            • Barrierefreiheit
                            • Facebook
                            • RSS Feed
                            • Twitter
                            • Xing
                            Nach oben