Si prosegue con le modifiche per l'inclusione degli esempi di codice.
[gapil.git] / system.tex
index c5e182262fd1bae8914290c840143f5274eafe79..4d980b22464cad62524effaebccb781dc9bd7778 100644 (file)
@@ -42,7 +42,7 @@ quelle della gestione dei file.
 \subsection{Limiti e parametri di sistema}
 \label{sec:sys_limits}
 
-Quando si devono determinare le le caratteristiche generali del sistema ci si
+Quando si devono determinare le caratteristiche generali del sistema ci si
 trova di fronte a diverse possibilità; alcune di queste infatti possono
 dipendere dall'architettura dell'hardware (come le dimensioni dei tipi
 interi), o dal sistema operativo (come la presenza o meno del gruppo degli
@@ -157,7 +157,7 @@ file, riportate in \tabref{tab:sys_file_macro}).
 \begin{table}[htb]
   \centering
   \footnotesize
-  \begin{tabular}[c]{|l|r|p{8cm}|}
+  \begin{tabular}[c]{|l|r|p{7cm}|}
     \hline
     \textbf{Costante}&\textbf{Valore}&\textbf{Significato}\\
     \hline
@@ -201,7 +201,7 @@ riportati in \tabref{tab:sys_posix1_general}.
 \begin{table}[htb]
   \centering
   \footnotesize
-  \begin{tabular}[c]{|l|r|p{8cm}|}
+  \begin{tabular}[c]{|l|r|p{7cm}|}
     \hline
     \textbf{Costante}&\textbf{Valore}&\textbf{Significato}\\
     \hline
@@ -365,22 +365,7 @@ sostituendolo a \code{\_POSIX\_} per le macro definite dagli gli altri due.
 
 In generale si dovrebbe fare uso di \func{sysconf} solo quando la relativa
 macro non è definita, quindi con un codice analogo al seguente:
-%\footnotesize
-\begin{lstlisting}[labelstep=0,frame=,indent=1cm]{}
-get_child_max(void)
-{
-#ifdef CHILD_MAX
-    return CHILD_MAX;
-#else
-    int val = sysconf(_SC_CHILD_MAX);
-    if (val < 0) {
-        perror("fatal error");
-        exit(-1);
-    }
-    return val;
-}
-\end{lstlisting}
-%\normalsize 
+\includecodesnip{listati/get_child_max.c}
 ma in realtà in Linux queste macro sono comunque definite, indicando però un
 limite generico. Per questo motivo è sempre meglio usare i valori restituiti
 da \func{sysconf}.
@@ -521,18 +506,7 @@ solo definendo \macro{\_GNU\_SOURCE}.
 \begin{figure}[!htb]
   \footnotesize \centering
   \begin{minipage}[c]{15cm}
-  \begin{lstlisting}[labelstep=0]{}%,frame=,indent=1cm]{}
-struct utsname {
-    char sysname[];
-    char nodename[];
-    char release[];
-    char version[];
-    char machine[];
-#ifdef _GNU_SOURCE
-    char domainname[];
-#endif
-};
-  \end{lstlisting}
+    \includestruct{listati/ustname.h}
   \end{minipage}
   \normalsize 
   \caption{La struttura \structd{utsname}.} 
@@ -606,7 +580,7 @@ Legge o scrive uno dei parametri di sistema.
 \end{functions}
 
 I parametri a cui la funzione permettere di accedere sono organizzati in
-maniera gerarchica all'interno un albero;\footnote{si tenga presente che
+maniera gerarchica all'interno di un albero;\footnote{si tenga presente che
   includendo solo \file{unistd.h}, saranno definiti solo i parametri generici;
   dato che ce ne sono molti specifici dell'implementazione, nel caso di Linux
   occorrerà includere anche i file \file{linux/unistd.h} e
@@ -620,7 +594,7 @@ arriva ad identificare un parametro specifico 
 attraverso l'array \param{name}, di lunghezza \param{nlen}, che contiene la
 sequenza dei vari nodi da attraversare. Ogni parametro ha un valore in un
 formato specifico che può essere un intero, una stringa o anche una struttura
-complessa, per questo motivo il valori vengono passati come puntatori
+complessa, per questo motivo i valori vengono passati come puntatori
 \ctyp{void}.
 
 L'indirizzo a cui il valore corrente del parametro deve essere letto è
@@ -896,20 +870,7 @@ genere 
 \begin{figure}[!htb]
   \footnotesize \centering
   \begin{minipage}[c]{15cm}
-  \begin{lstlisting}[labelstep=0]{}%,frame=,indent=1cm]{}
-struct statfs {
-   long    f_type;     /* tipo di filesystem */
-   long    f_bsize;    /* dimensione ottimale dei blocchi di I/O */
-   long    f_blocks;   /* blocchi totali nel filesystem */
-   long    f_bfree;    /* blocchi liberi nel filesystem */
-   long    f_bavail;   /* blocchi liberi agli utenti normali */
-   long    f_files;    /* inode totali nel filesystem */
-   long    f_ffree;    /* inode liberi nel filesystem */
-   fsid_t  f_fsid;     /* filesystem id */
-   long    f_namelen;  /* lunghezza massima dei nomi dei file */
-   long    f_spare[6]; /* riservati per uso futuro */
-};
-\end{lstlisting}
+    \includestruct{listati/statfs.h}
   \end{minipage}
   \normalsize 
   \caption{La struttura \structd{statfs}.} 
@@ -983,17 +944,7 @@ il significato dei vari campi.
   \footnotesize
   \centering
   \begin{minipage}[c]{15cm}
-    \begin{lstlisting}[labelstep=0]{}%,frame=,indent=1cm]{}
-struct passwd {
-    char    *pw_name;       /* user name */
-    char    *pw_passwd;     /* user password */
-    uid_t   pw_uid;         /* user id */
-    gid_t   pw_gid;         /* group id */
-    char    *pw_gecos;      /* real name */
-    char    *pw_dir;        /* home directory */
-    char    *pw_shell;      /* shell program */
-};
-    \end{lstlisting}
+    \includestruct{listati/passwd.h}
   \end{minipage} 
   \normalsize 
   \caption{La struttura \structd{passwd} contenente le informazioni relative ad
@@ -1070,14 +1021,7 @@ in questo caso le informazioni vengono restituite in una struttura di tipo
   \footnotesize
   \centering
   \begin{minipage}[c]{15cm}
-    \begin{lstlisting}[labelstep=0]{}%,frame=,indent=1cm]{}
-struct group {
-    char    *gr_name;        /* group name */
-    char    *gr_passwd;      /* group password */
-    gid_t   gr_gid;          /* group id */
-    char    **gr_mem;        /* group members */
-};
-    \end{lstlisting}
+    \includestruct{listati/group.h}
   \end{minipage} 
   \normalsize 
   \caption{La struttura \structd{group} contenente le informazioni relative ad
@@ -1206,6 +1150,18 @@ standard \const{\_PATH\_UTMP} (che 
 corrispondenti ai file \file{/var/run/utmp} e \file{/var/log/wtmp} visti in
 precedenza.
 
+\begin{figure}[!htb]
+  \footnotesize
+  \centering
+  \begin{minipage}[c]{15cm}
+    \includestruct{listati/utmp.h}
+  \end{minipage} 
+  \normalsize 
+  \caption{La struttura \structd{utmp} contenente le informazioni di una voce
+    del database di \textit{accounting}.}
+  \label{fig:sys_utmp_struct}
+\end{figure}
+
 Una volta aperto il file si può eseguire una scansione leggendo o scrivendo
 una voce con le funzioni \funcd{getutent}, \funcd{getutid}, \funcd{getutline}
 e \funcd{pututline}, i cui prototipi sono:
@@ -1230,39 +1186,11 @@ e \funcd{pututline}, i cui prototipi sono:
 \end{functions}
 
 Tutte queste funzioni fanno riferimento ad una struttura di tipo \struct{utmp},
-la cui definizione in Linux è riportata in \secref{fig:sys_utmp_struct}. Le
+la cui definizione in Linux è riportata in \figref{fig:sys_utmp_struct}. Le
 prime tre funzioni servono per leggere una voce dal database; \func{getutent}
 legge semplicemente la prima voce disponibile; le altre due permettono di
 eseguire una ricerca.
 
-\begin{figure}[!htb]
-  \footnotesize
-  \centering
-  \begin{minipage}[c]{15cm}
-    \begin{lstlisting}[labelstep=0]{}%,frame=,indent=1cm]{}
-struct utmp
-{
-    short int ut_type;            /* Type of login.  */
-    pid_t ut_pid;                 /* Process ID of login process.  */
-    char ut_line[UT_LINESIZE];    /* Devicename.  */
-    char ut_id[4];                /* Inittab ID.  */
-    char ut_user[UT_NAMESIZE];    /* Username.  */
-    char ut_host[UT_HOSTSIZE];    /* Hostname for remote login.  */
-    struct exit_status ut_exit;   /* Exit status of a process marked
-                                     as DEAD_PROCESS.  */
-    long int ut_session;          /* Session ID, used for windowing.  */
-    struct timeval ut_tv;         /* Time entry was made.  */
-    int32_t ut_addr_v6[4];        /* Internet address of remote host.  */
-    char __unused[20];            /* Reserved for future use.  */
-};
-    \end{lstlisting}
-  \end{minipage} 
-  \normalsize 
-  \caption{La struttura \structd{utmp} contenente le informazioni di una voce
-    del database di \textit{accounting}.}
-  \label{fig:sys_utmp_struct}
-\end{figure}
-
 Con \func{getutid} si può cercare una voce specifica, a seconda del valore del
 campo \var{ut\_type} dell'argomento \param{ut}.  Questo può assumere i valori
 riportati in \tabref{tab:sys_ut_type}, quando assume i valori
@@ -1377,26 +1305,7 @@ di tipo \struct{rusage}, la cui definizione (che si trova in
   \footnotesize
   \centering
   \begin{minipage}[c]{15cm}
-    \begin{lstlisting}[labelstep=0]{}%,frame=,indent=1cm]{}
-struct rusage {
-    struct timeval ru_utime; /* user time used */
-    struct timeval ru_stime; /* system time used */
-    long ru_maxrss;          /* maximum resident set size */
-    long ru_ixrss;           /* integral shared memory size */
-    long ru_idrss;           /* integral unshared data size */
-    long ru_isrss;           /* integral unshared stack size */
-    long ru_minflt;          /* page reclaims */
-    long ru_majflt;          /* page faults */
-    long ru_nswap;           /* swaps */
-    long ru_inblock;         /* block input operations */
-    long ru_oublock;         /* block output operations */
-    long ru_msgsnd;          /* messages sent */
-    long ru_msgrcv;          /* messages received */
-    long ru_nsignals;   ;    /* signals received */
-    long ru_nvcsw;           /* voluntary context switches */
-    long ru_nivcsw;          /* involuntary context switches */
-};
-    \end{lstlisting}
+    \includestruct{listati/rusage.h}
   \end{minipage} 
   \normalsize 
   \caption{La struttura \structd{rusage} per la lettura delle informazioni dei 
@@ -1467,24 +1376,7 @@ detto \textsl{duro} (o \textit{hard limit}), in quanto un processo normale non
 può modificarne il valore. Il valore di questi limiti è mantenuto in una
 struttura \struct{rlimit}, la cui definizione è riportata in
 \figref{fig:sys_rlimit_struct}, ed i cui campi corrispondono appunto a limite
-corrente e massimo.
-
-\begin{figure}[!htb]
-  \footnotesize
-  \centering
-  \begin{minipage}[c]{15cm}
-    \begin{lstlisting}[labelstep=0]{}%,frame=,indent=1cm]{}
-struct rlimit {
-     rlim_t    rlim_cur;
-     rlim_t    rlim_max;
-};
-    \end{lstlisting}
-  \end{minipage} 
-  \normalsize 
-  \caption{La struttura \structd{rlimit} per impostare i limiti di utilizzo 
-    delle risorse usate da un processo.}
-  \label{fig:sys_rlimit_struct}
-\end{figure}
+corrente e limite massimo.
 
 In genere il superamento di un limite comporta o l'emissione di un segnale o
 il fallimento della system call che lo ha provocato; per permettere di leggere
@@ -1514,6 +1406,20 @@ prototipi sono:
   ed \errval{EFAULT}.}
 \end{functions}
 
+\begin{figure}[!htb]
+  \footnotesize
+  \centering
+  \begin{minipage}[c]{15cm}
+    \includestruct{listati/rlimit.h}
+  \end{minipage} 
+  \normalsize 
+  \caption{La struttura \structd{rlimit} per impostare i limiti di utilizzo 
+    delle risorse usate da un processo.}
+  \label{fig:sys_rlimit_struct}
+\end{figure}
+
+
+
 Entrambe le funzioni permettono di specificare, attraverso l'argomento
 \param{resource}, su quale risorsa si vuole operare: i possibili valori di
 questo argomento sono elencati in \secref{tab:sys_rlimit_values}. L'acceso
@@ -1578,13 +1484,13 @@ poi effettuato attraverso la struttura \struct{rlimit} puntata da
   evitare la creazione di \file{core} file (al proposito si veda
   \secref{sec:sig_prog_error}).}
 
-Nello specificare un limite, oltre a dei valori specifici, si può anche usare
-la costante \const{RLIM\_INFINITY} che permette di sbloccare l'uso di una
-risorsa; ma si ricordi che solo un processo con i privilegi di amministratore
-può innalzare un limite al di sopra del valore corrente del limite massimo. Si
-tenga conto infine che tutti i limiti vengono ereditati dal processo padre
-attraverso una \func{fork} (vedi \secref{sec:proc_fork}) e mantenuti per gli
-altri programmi eseguiti attraverso una \func{exec} (vedi
+Nello specificare un limite, oltre a fornire dei valori specifici, si può
+anche usare la costante \const{RLIM\_INFINITY} che permette di sbloccare l'uso
+di una risorsa; ma si ricordi che solo un processo con i privilegi di
+amministratore può innalzare un limite al di sopra del valore corrente del
+limite massimo. Si tenga conto infine che tutti i limiti vengono ereditati dal
+processo padre attraverso una \func{fork} (vedi \secref{sec:proc_fork}) e
+mantenuti per gli altri programmi eseguiti attraverso una \func{exec} (vedi
 \secref{sec:proc_exec}).
 
 
@@ -1808,14 +1714,7 @@ struttura di tipo \struct{tms}, la cui definizione 
   \footnotesize
   \centering
   \begin{minipage}[c]{15cm}
-    \begin{lstlisting}[labelstep=0]{}%,frame=,indent=1cm]{}
-struct tms {
-       clock_t tms_utime;  /* user time */
-       clock_t tms_stime;  /* system time */
-       clock_t tms_cutime; /* user time of children */
-       clock_t tms_cstime; /* system time of children */
-};
-    \end{lstlisting}
+    \includestruct{listati/tms.h}
   \end{minipage} 
   \normalsize 
   \caption{La struttura \structd{tms} dei tempi di processore associati a un
@@ -1836,7 +1735,6 @@ lo stato di terminazione di tutti i suoi figli, questi processi ``nipoti'' non
 verranno considerati nel calcolo di questi tempi.
 
 
-
 \subsection{Le funzioni per il \textit{calendar time}}
 \label{sec:sys_time_base}
 
@@ -1845,8 +1743,8 @@ mantenuto dal kernel in una variabile di tipo \type{time\_t}, che usualmente
 corrisponde ad un tipo elementare (in Linux è definito come \ctyp{long int},
 che di norma corrisponde a 32 bit).  Il valore corrente del \textit{calendar
   time}, che indicheremo come \textsl{tempo di sistema}, può essere ottenuto
-con la funzione \funcd{time} che lo restituisce in nel suddetto formato; il
-suo prototipo è:
+con la funzione \funcd{time} che lo restituisce nel suddetto formato; il suo
+prototipo è:
 \begin{prototype}{time.h}{time\_t time(time\_t *t)}
   Legge il valore corrente del \textit{calendar time}.
   
@@ -1909,17 +1807,7 @@ nanosecondo.\footnote{la precisione 
 \begin{figure}[!htb]
   \footnotesize \centering
   \begin{minipage}[c]{15cm}
-    \begin{lstlisting}[labelstep=0]{}%,frame=,indent=1cm]{}
-struct timeval 
-{
-    long tv_sec;            /* seconds */
-    long tv_usec;           /* microseconds */
-};
-struct timespec {
-    time_t  tv_sec;         /* seconds */
-    long    tv_nsec;        /* nanoseconds */
-};
-    \end{lstlisting}
+    \includestruct{listati/timeval.h}
   \end{minipage} 
   \normalsize 
   \caption{Le strutture \structd{timeval} e \structd{timespec} usate per una
@@ -1972,29 +1860,7 @@ effettuato.
 \begin{figure}[!htb]
   \footnotesize \centering
   \begin{minipage}[c]{15cm}
-    \begin{lstlisting}[labelstep=0]{}%,frame=,indent=1cm]{}
-struct timex {
-    unsigned int modes;   /* mode selector */
-    long int offset;      /* time offset (usec) */
-    long int freq;        /* frequency offset (scaled ppm) */
-    long int maxerror;    /* maximum error (usec) */
-    long int esterror;    /* estimated error (usec) */
-    int status;           /* clock command/status */
-    long int constant;    /* pll time constant */
-    long int precision;   /* clock precision (usec) (read only) */
-    long int tolerance;   /* clock frequency tolerance (ppm) (read only) */
-    struct timeval time;  /* (read only) */
-    long int tick;        /* (modified) usecs between clock ticks */
-    long int ppsfreq;     /* pps frequency (scaled ppm) (ro) */
-    long int jitter;      /* pps jitter (us) (ro) */
-    int shift;            /* interval duration (s) (shift) (ro) */
-    long int stabil;      /* pps stability (scaled ppm) (ro) */
-    long int jitcnt;      /* jitter limit exceeded (ro) */
-    long int calcnt;      /* calibration intervals (ro) */
-    long int errcnt;      /* calibration errors (ro) */
-    long int stbcnt;      /* stability limit exceeded (ro) */
-};
-    \end{lstlisting}
+    \includestruct{listati/timex.h}
   \end{minipage} 
   \normalsize 
   \caption{La struttura \structd{timex} per il controllo dell'orologio di
@@ -2129,31 +1995,6 @@ stata introdotta una ulteriore rappresentazione, detta \textit{broken-down
   time}, che permette appunto di \textsl{suddividere} il \textit{calendar
   time} usuale in ore, minuti, secondi, ecc.
 
-\begin{figure}[!htb]
-  \footnotesize \centering
-  \begin{minipage}[c]{15cm}
-    \begin{lstlisting}[labelstep=0]{}%,frame=,indent=1cm]{}
-struct tm {
-        int     tm_sec;         /* seconds */
-        int     tm_min;         /* minutes */
-        int     tm_hour;        /* hours */
-        int     tm_mday;        /* day of the month */
-        int     tm_mon;         /* month */
-        int     tm_year;        /* year */
-        int     tm_wday;        /* day of the week */
-        int     tm_yday;        /* day in the year */
-        int     tm_isdst;       /* daylight saving time */
-        long int tm_gmtoff;     /* Seconds east of UTC.  */
-        cost char *tm_zone;     /* Timezone abbreviation.  */
-};
-    \end{lstlisting}
-  \end{minipage} 
-  \normalsize 
-  \caption{La struttura \structd{tm} per una rappresentazione del tempo in
-    termini di ora, minuti, secondi, ecc.}
-  \label{fig:sys_tm_struct}
-\end{figure}
-
 Questo viene effettuato attraverso una opportuna struttura \struct{tm}, la cui
 definizione è riportata in \figref{fig:sys_tm_struct}, ed è in genere questa
 struttura che si utilizza quando si deve specificare un tempo a partire dai
@@ -2193,6 +2034,19 @@ tempo in una stringa contenente data ed ora, i loro prototipi sono:
   restituisce direttamente il valore o -1 in caso di errore.}
 \end{functions}
 
+\begin{figure}[!htb]
+  \footnotesize \centering
+  \begin{minipage}[c]{15cm}
+    \includestruct{listati/tm.h}
+  \end{minipage} 
+  \normalsize 
+  \caption{La struttura \structd{tm} per una rappresentazione del tempo in
+    termini di ora, minuti, secondi, ecc.}
+  \label{fig:sys_tm_struct}
+\end{figure}
+
+
+
 Le prime due funzioni, \func{asctime} e \func{ctime} servono per poter
 stampare in forma leggibile un tempo; esse restituiscono il puntatore ad una
 stringa, allocata staticamente, nella forma:
@@ -2243,11 +2097,7 @@ verr
   \footnotesize
   \centering
   \begin{minipage}[c]{15cm}
-    \begin{lstlisting}[labelstep=0]{}%,frame=,indent=1cm]{}
-extern char *tzname[2];
-extern long timezone;
-extern int daylight;
-    \end{lstlisting}
+    \includestruct{listati/time_zone_var.c}
   \end{minipage} 
   \normalsize 
   \caption{Le variabili globali usate per la gestione delle \textit{time
@@ -2265,7 +2115,7 @@ solare, la seconda per l'ora legale.\footnote{anche se sono indicati come
 Benché la funzione \func{asctime} fornisca la modalità più immediata per
 stampare un tempo o una data, la flessibilità non fa parte delle sue
 caratteristiche; quando si vuole poter stampare solo una parte (l'ora, o il
-gionrno) di un tempo si può ricorrere alla più sofisticata \funcd{strftime},
+giorno) di un tempo si può ricorrere alla più sofisticata \funcd{strftime},
 il cui prototipo è:
 \begin{prototype}{time.h}
 {size\_t strftime(char *s, size\_t max, const char *format, 
@@ -2275,7 +2125,7 @@ Stampa il tempo \param{tm} nella stringa \param{s} secondo il formato
 \param{format}.
   
   \bodydesc{La funzione ritorna il numero di caratteri stampati in \param{s},
-  altrimenti restuisce 0.}
+  altrimenti restituisce 0.}
 \end{prototype}
 
 La funzione converte opportunamente il tempo \param{tm} in una stringa di
@@ -2332,7 +2182,7 @@ viene utilizzato come modificatore; alcuni\footnote{per la precisione quelli
   le \acr{glibc} provvedono tutte le estensioni introdotte da POSIX.2 per il
   comando \cmd{date}, i valori introdotti da SVID3 e ulteriori estensioni GNU;
   l'elenco completo dei possibili valori è riportato nella pagina di manuale
-  della funzione.} dei possibili valori che esso può assumere sono ripotati in
+  della funzione.} dei possibili valori che esso può assumere sono riportati in
 \tabref{tab:sys_strftime_format}. La funzione tiene conto anche della presenza
 di una localizzazione per stampare in maniera adeguata i vari nomi.
 
@@ -2461,36 +2311,18 @@ punti e da uno spazio, il messaggio 
 
 Il messaggio può essere riportato anche usando altre variabili globali
 dichiarate in \file{errno.h}:
-\begin{verbatim}
-   const char *sys_errlist[];
-   int sys_nerr;
-\end{verbatim}
+\includecodesnip{listati/errlist.c}
 la prima contiene i puntatori alle stringhe di errore indicizzati da
 \var{errno}; la seconda esprime il valore più alto per un codice di errore,
 l'utilizzo di questa stringa è sostanzialmente equivalente a quello di
 \func{strerror}.
 
 \begin{figure}[!htb]
-  \footnotesize
-  \begin{lstlisting}{}
-    /* convert string to number */
-    err = strtol(argv[optind], NULL, 10);
-    /* testing error condition on conversion */
-    if (err==LONG_MIN) {
-        perror("Underflow on error code");
-        return 1;
-    } else if (err==LONG_MIN) {
-        perror("Overflow on error code");
-        return 1;
-    }
-    /* conversion is fine */
-    if (message) {
-        printf("Error message for %d is %s\n", err, strerror(err));
-    }
-    if (label) {
-        printf("Error label for %d is %s\n", err, err_code[err]);
-    }
-  \end{lstlisting}
+  \footnotesize \centering
+  \begin{minipage}[c]{15cm}
+    \includecodesample{listati/errcode_mess.c}
+  \end{minipage}
+  \normalsize
   \caption{Codice per la stampa del messaggio di errore standard.}
   \label{fig:sys_err_mess}
 \end{figure}