<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title>dino-996 blog</title>
  <subtitle>Developer portfolio e blog tecnico. Condivido articoli su programmazione, web development e tecnologie moderne.</subtitle>
  <link href="https://dino-996.github.io/feed.xml" rel="self" type="application/atom+xml"/>
  <link href="https://dino-996.github.io" rel="alternate" type="text/html"/>
  <id>https://dino-996.github.io/</id>
  <updated>2026-03-18T00:00:00.000Z</updated>
  <author>
    <name>Davide Sabia</name>
    <email>davidesabia22@gmail.com</email>
    <uri>https://dino-996.github.io</uri>
  </author>

  
  <entry>
    <title>Il Filesystem di Linux</title>
    <link href="https://dino-996.github.io/blog/il-filesystem-di-linux/" rel="alternate" type="text/html"/>
    <id>https://dino-996.github.io/blog/il-filesystem-di-linux/</id>
    <published>2026-03-18T00:00:00.000Z</published>
    <updated>2026-03-18T00:00:00.000Z</updated>
    
    <summary>In questo articolo esploriamo come funziona il filesystem Linux; partiremo dai concetti fondamentali, capiremo perché tutto è un file, e poi percorreremo le directory principali del sistema una per una.</summary>
    
    
    <content type="html"><![CDATA[&lt;h1&gt;Filesystem: Struttura e Organizzazione&lt;/h1&gt;
&lt;p&gt;Se hai mai aperto un terminale Linux e digitato &lt;code&gt;ls /&lt;/code&gt; (se non l&#39;hai mai fatto questa è la volta buona: &lt;a href=&quot;https://dino-996.github.io/blog/comandi-gnu-linux&quot;&gt;Comandi di base della Shell Linux&lt;/a&gt;), probabilmente ti sei trovato di fronte a una lista di cartelle dai nomi enigmatici: &lt;code&gt;/bin&lt;/code&gt;, &lt;code&gt;/etc&lt;/code&gt;, &lt;code&gt;/proc&lt;/code&gt;, &lt;code&gt;/dev&lt;/code&gt;… Dietro quella lista si nasconde un sistema di organizzazione preciso e coerente, ereditato dalla tradizione Unix e standardizzato nel corso degli anni.&lt;/p&gt;
&lt;h2&gt;Filesystem e Filesystem Hierarchy Standard&lt;/h2&gt;
&lt;p&gt;Prima di tutto, è utile distinguere due concetti che spesso vengono confusi.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Filesystem&lt;/strong&gt; (nel senso tecnico) indica il formato con cui i dati vengono organizzati fisicamente su un supporto di memoria, come un HDD, un SSD o una chiavetta USB. I più comuni su Linux sono &lt;strong&gt;ext4&lt;/strong&gt;, &lt;strong&gt;ext3&lt;/strong&gt; ed &lt;strong&gt;ext2&lt;/strong&gt;; su Windows troviamo &lt;strong&gt;FAT32&lt;/strong&gt; e &lt;strong&gt;NTFS&lt;/strong&gt;; su macOS &lt;strong&gt;HFS+&lt;/strong&gt; e il più recente &lt;strong&gt;APFS&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Filesystem Hierarchy Standard (FHS)&lt;/strong&gt; è invece una specifica che definisce &lt;em&gt;quali&lt;/em&gt; directory devono esistere in un sistema Linux e &lt;em&gt;cosa&lt;/em&gt; devono contenere. In pratica, è una convenzione condivisa tra le distribuzioni per garantire coerenza e portabilità: grazie all&#39;FHS, un amministratore di sistema sa già dove cercare i log, i file di configurazione o i binari, indipendentemente dalla distribuzione che sta usando.&lt;/p&gt;
&lt;h2&gt;La struttura ad albero e la directory radice&lt;/h2&gt;
&lt;p&gt;Le directory di un filesystem Linux sono organizzate secondo uno schema &lt;strong&gt;ad albero&lt;/strong&gt;: ogni cartella può contenere file e altre cartelle, creando una gerarchia che si ramifica verso il basso. Al vertice di quest&#39;albero si trova la &lt;strong&gt;directory radice&lt;/strong&gt;, indicata dal simbolo &lt;code&gt;/&lt;/code&gt; e chiamata &lt;em&gt;root&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Attenzione: la directory radice &lt;code&gt;/&lt;/code&gt; non va confusa con l&#39;utente amministratore, che si chiama anch&#39;esso &lt;em&gt;root&lt;/em&gt; e ha come home directory &lt;code&gt;/root&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Tutti i percorsi che partono da &lt;code&gt;/&lt;/code&gt; sono detti &lt;strong&gt;percorsi assoluti&lt;/strong&gt;, perché identificano un file in modo univoco a partire dalla radice, indipendentemente da dove ci si trova nel filesystem. Ad esempio:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;/home/mario/documenti/relazione.txt
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Al contrario, un &lt;strong&gt;percorso relativo&lt;/strong&gt; non inizia con &lt;code&gt;/&lt;/code&gt; e fa riferimento alla posizione corrente. Se ci troviamo già in &lt;code&gt;/home/mario&lt;/code&gt;, possiamo scrivere semplicemente &lt;code&gt;documenti/relazione.txt&lt;/code&gt; per riferirci allo stesso file.&lt;/p&gt;
&lt;p&gt;I nomi dei file in Linux possono contenere quasi tutti i caratteri, con alcune eccezioni: il carattere &lt;code&gt;/&lt;/code&gt; è riservato come separatore di percorso e non può comparire nei nomi. È inoltre buona norma evitare caratteri speciali come spazi, &lt;code&gt;*&lt;/code&gt;, &lt;code&gt;?&lt;/code&gt; o &lt;code&gt;!&lt;/code&gt;, poiché possono avere significati particolari nella shell e causare comportamenti inattesi. La lunghezza massima di un nome file è generalmente di &lt;strong&gt;255 caratteri&lt;/strong&gt; sui filesystem moderni.&lt;/p&gt;
&lt;p&gt;Una caratteristica fondamentale: Linux è &lt;strong&gt;case sensitive&lt;/strong&gt;. &lt;code&gt;File.txt&lt;/code&gt;, &lt;code&gt;file.txt&lt;/code&gt; e &lt;code&gt;FILE.TXT&lt;/code&gt; sono tre file completamente distinti. Questo è diverso da Windows, dove i nomi file sono trattati in modo insensibile alle maiuscole.&lt;/p&gt;
&lt;h2&gt;&amp;quot;In Linux, tutto è un file&amp;quot;&lt;/h2&gt;
&lt;p&gt;Uno dei principi fondamentali di Linux (e dei sistemi Unix in generale) è che &lt;strong&gt;tutto è un file&lt;/strong&gt;. Questo non significa solo che documenti e programmi sono file, ma che quasi ogni risorsa del sistema è rappresentata attraverso questa astrazione uniforme. Esistono diverse categorie:&lt;/p&gt;
&lt;div class=&quot;table-responsive shadow-sm rounded mb-3&quot;&gt;
  &lt;table class=&quot;table table-bordered table-striped table-hover align-middle mb-0&quot;&gt;
    &lt;thead class=&quot;table-dark&quot;&gt;
      &lt;tr&gt;
        &lt;th&gt;Tipo&lt;/th&gt;
        &lt;th&gt;Descrizione&lt;/th&gt;
        &lt;th&gt;Esempio&lt;/th&gt;
      &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
      &lt;tr&gt;
        &lt;td class=&quot;fw-semibold&quot;&gt;File regolare&lt;/td&gt;
        &lt;td&gt;Documenti, binari, script&lt;/td&gt;
        &lt;td&gt;&lt;code&gt;/etc/hosts&lt;/code&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td class=&quot;fw-semibold&quot;&gt;Directory&lt;/td&gt;
        &lt;td&gt;Contenitore per altri file&lt;/td&gt;
        &lt;td&gt;&lt;code&gt;/home/mario/&lt;/code&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td class=&quot;fw-semibold&quot;&gt;File di dispositivo&lt;/td&gt;
        &lt;td&gt;Interfaccia verso periferiche hardware&lt;/td&gt;
        &lt;td&gt;&lt;code&gt;/dev/sda&lt;/code&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td class=&quot;fw-semibold&quot;&gt;Link simbolico&lt;/td&gt;
        &lt;td&gt;Puntatore a un altro file o directory&lt;/td&gt;
        &lt;td&gt;&lt;code&gt;/usr/bin/python&lt;/code&gt; → &lt;code&gt;/usr/bin/python3&lt;/code&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td class=&quot;fw-semibold&quot;&gt;Socket&lt;/td&gt;
        &lt;td&gt;Canale di comunicazione tra processi (di rete o locali)&lt;/td&gt;
        &lt;td&gt;&lt;code&gt;/run/docker.sock&lt;/code&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td class=&quot;fw-semibold&quot;&gt;FIFO (named pipe)&lt;/td&gt;
        &lt;td&gt;Canale di comunicazione unidirezionale tra processi&lt;/td&gt;
        &lt;td&gt;creato con &lt;code&gt;mkfifo&lt;/code&gt;&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;
&lt;/div&gt;
&lt;p&gt;Questa astrazione è uno dei punti di forza di Linux: strumenti e programmi possono interagire con risorse eterogenee usando sempre le stesse operazioni di lettura e scrittura.&lt;/p&gt;
&lt;h2&gt;Montaggio e smontaggio dei filesystem&lt;/h2&gt;
&lt;p&gt;Prima di esplorare le singole directory, è utile capire il concetto di &lt;strong&gt;mount&lt;/strong&gt; (montaggio), perché spiega come vengono integrate le varie risorse di memoria nell&#39;albero del filesystem.&lt;/p&gt;
&lt;p&gt;Di norma, la directory &lt;code&gt;/&lt;/code&gt; corrisponde al disco da cui Linux è stato avviato. Per accedere a supporti aggiuntivi, come una chiavetta USB, un secondo disco o una partizione di rete, è necessario &amp;quot;agganciare&amp;quot; il dispositivo all&#39;albero del filesystem in un punto specifico, detto &lt;strong&gt;mount point&lt;/strong&gt;:&lt;/p&gt;
&lt;pre class=&quot;language-bash&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Monta la partizione /dev/sdb1 nella directory /mnt/chiavetta&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;mount&lt;/span&gt; /dev/sdb1 /mnt/chiavetta&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Da quel momento, i file della chiavetta saranno accessibili navigando in &lt;code&gt;/mnt/chiavetta&lt;/code&gt;. Per rimuovere il collegamento (l&#39;equivalente dell&#39;espulsione sicura), si usa &lt;code&gt;umount&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;language-bash&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token function&quot;&gt;umount&lt;/span&gt; /mnt/chiavetta&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;L&#39;elenco di tutti i filesystem attualmente montati è consultabile in &lt;code&gt;/proc/mounts&lt;/code&gt;.&lt;/p&gt;
&lt;h2&gt;Le directory principali del sistema&lt;/h2&gt;
&lt;h3&gt;&lt;code&gt;/home&lt;/code&gt; — Le directory degli utenti&lt;/h3&gt;
&lt;p&gt;Contiene le cartelle personali di ciascun utente. Se il nome utente è &lt;em&gt;mario&lt;/em&gt;, la sua home sarà &lt;code&gt;/home/mario&lt;/code&gt;. Qui vengono salvati documenti, immagini, download e le configurazioni personali delle applicazioni (spesso in cartelle nascoste che iniziano con un punto, come &lt;code&gt;~/.config&lt;/code&gt; o &lt;code&gt;~/.bashrc&lt;/code&gt;). Le distribuzioni moderne proteggono queste directory impedendo l&#39;accesso agli altri utenti non privilegiati.&lt;/p&gt;
&lt;h3&gt;&lt;code&gt;/root&lt;/code&gt; — La home dell&#39;amministratore&lt;/h3&gt;
&lt;p&gt;La directory personale dell&#39;utente &lt;em&gt;root&lt;/em&gt; è separata da &lt;code&gt;/home&lt;/code&gt; per una ragione precisa: deve essere accessibile anche quando il filesystem degli utenti non è ancora disponibile, ad esempio durante le prime fasi di avvio o in modalità di recupero.&lt;/p&gt;
&lt;h3&gt;&lt;code&gt;/bin&lt;/code&gt; e &lt;code&gt;/usr/bin&lt;/code&gt; — I programmi essenziali&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;/bin&lt;/code&gt; contiene gli eseguibili fondamentali del sistema, come &lt;code&gt;ls&lt;/code&gt;, &lt;code&gt;cp&lt;/code&gt;, &lt;code&gt;mv&lt;/code&gt;, &lt;code&gt;cat&lt;/code&gt; o &lt;code&gt;bash&lt;/code&gt;. Devono essere disponibili già nelle primissime fasi di avvio, prima che altri filesystem vengano montati. &lt;code&gt;/usr/bin&lt;/code&gt; ospita invece i comandi per gli utenti normali che non sono strettamente necessari all&#39;avvio.&lt;/p&gt;
&lt;p&gt;Nota: su molte distribuzioni moderne (Ubuntu, Fedora, Arch…) &lt;code&gt;/bin&lt;/code&gt; è diventato un link simbolico che punta a &lt;code&gt;/usr/bin&lt;/code&gt;, nell&#39;ambito di una semplificazione chiamata &lt;em&gt;usrmerge&lt;/em&gt;.&lt;/p&gt;
&lt;h3&gt;&lt;code&gt;/usr&lt;/code&gt; — Programmi e risorse di sistema&lt;/h3&gt;
&lt;p&gt;È una delle directory più ricche del sistema. Contiene eseguibili, librerie, documentazione e risorse della maggior parte dei programmi installati. È tipicamente in &lt;strong&gt;sola lettura&lt;/strong&gt; per gli utenti normali. Al suo interno troviamo tra l&#39;altro &lt;code&gt;/usr/sbin&lt;/code&gt; per i comandi aggiuntivi dell&#39;amministratore, &lt;code&gt;/usr/lib&lt;/code&gt; per le librerie condivise usate dai programmi, &lt;code&gt;/usr/share&lt;/code&gt; per le risorse condivise come documentazione e icone, e &lt;code&gt;/usr/share/man&lt;/code&gt; per i testi delle &lt;strong&gt;manpage&lt;/strong&gt; accessibili tramite il comando &lt;code&gt;man&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;&lt;code&gt;/etc&lt;/code&gt; — Le configurazioni di sistema&lt;/h3&gt;
&lt;p&gt;Contiene i file di configurazione dell&#39;intero sistema: impostazioni di rete, elenco degli utenti, configurazione dei servizi e molto altro. La maggior parte dei file è leggibile da tutti, ma modificabile solo dall&#39;amministratore. Alcuni file notevoli sono &lt;code&gt;/etc/passwd&lt;/code&gt; per le informazioni sugli account utente (nome, shell, home directory…), &lt;code&gt;/etc/shadow&lt;/code&gt; per le password degli utenti in forma cifrata (accessibile solo da root), &lt;code&gt;/etc/fstab&lt;/code&gt; per la tabella dei filesystem da montare automaticamente all&#39;avvio e &lt;code&gt;/etc/hosts&lt;/code&gt; per la mappatura manuale tra nomi host e indirizzi IP.&lt;/p&gt;
&lt;h3&gt;&lt;code&gt;/boot&lt;/code&gt; — Il kernel e i file di avvio&lt;/h3&gt;
&lt;p&gt;Contiene il kernel Linux, il file &lt;code&gt;initrd&lt;/code&gt; (initial RAM disk, usato nelle prime fasi di avvio) e la configurazione del bootloader &lt;strong&gt;GRUB&lt;/strong&gt;. Si consiglia spesso di dedicare a &lt;code&gt;/boot&lt;/code&gt; una partizione separata all&#39;inizio del disco, per garantire la compatibilità con alcune configurazioni hardware e semplificare il ripristino del sistema.&lt;/p&gt;
&lt;h3&gt;&lt;code&gt;/var&lt;/code&gt; — Dati variabili&lt;/h3&gt;
&lt;p&gt;Contiene file che cambiano frequentemente durante il normale funzionamento del sistema: log, code di stampa, database, cache dei package manager. La sottodirectory più consultata è &lt;code&gt;/var/log&lt;/code&gt;, che raccoglie i log generati dal kernel, dai servizi e dalle applicazioni. Tra i più utili troviamo &lt;code&gt;/var/log/syslog&lt;/code&gt; per gli eventi generali del sistema, &lt;code&gt;/var/log/auth.log&lt;/code&gt; per i tentativi di autenticazione e uso di &lt;code&gt;sudo&lt;/code&gt;, e &lt;code&gt;/var/log/kern.log&lt;/code&gt; per i messaggi del kernel.&lt;/p&gt;
&lt;h3&gt;&lt;code&gt;/tmp&lt;/code&gt; — File temporanei&lt;/h3&gt;
&lt;p&gt;Directory scrivibile da tutti gli utenti, usata per i file temporanei creati da applicazioni e processi. Il suo contenuto viene cancellato ad ogni riavvio. Su molte distribuzioni moderne è montata come &lt;code&gt;tmpfs&lt;/code&gt;, ovvero direttamente in RAM, il che la rende molto veloce ma ne limita la capacità alla memoria disponibile.&lt;/p&gt;
&lt;h2&gt;&lt;code&gt;/dev&lt;/code&gt; — I file di dispositivo&lt;/h2&gt;
&lt;p&gt;La directory &lt;code&gt;/dev&lt;/code&gt; contiene file speciali che rappresentano le periferiche hardware e alcune risorse virtuali del kernel. Non occupano spazio fisico sul disco: sono interfacce verso il kernel, gestite dal demone &lt;strong&gt;udev&lt;/strong&gt; che li crea e li rimuove dinamicamente al collegamento o alla disconnessione dei dispositivi.&lt;/p&gt;
&lt;p&gt;Alcuni esempi particolarmente interessanti:&lt;/p&gt;
&lt;div class=&quot;table-responsive shadow-sm rounded mb-3&quot;&gt;
  &lt;table class=&quot;table table-bordered table-striped table-hover align-middle mb-0&quot;&gt;
    &lt;thead class=&quot;table-dark&quot;&gt;
      &lt;tr&gt;
        &lt;th&gt;File&lt;/th&gt;
        &lt;th&gt;Descrizione&lt;/th&gt;
      &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
      &lt;tr&gt;
        &lt;td class=&quot;fw-semibold&quot;&gt;&lt;code&gt;/dev/null&lt;/code&gt;&lt;/td&gt;
        &lt;td&gt;&lt;i&gt;Buco nero&lt;/i&gt;: qualsiasi dato scritto qui viene scartato silenziosamente&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td class=&quot;fw-semibold&quot;&gt;&lt;code&gt;/dev/zero&lt;/code&gt;&lt;/td&gt;
        &lt;td&gt;Genera una sequenza infinita di byte a zero, utile per sovrascrivere dati o creare file di dimensione fissa&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td class=&quot;fw-semibold&quot;&gt;&lt;code&gt;/dev/random&lt;/code&gt;&lt;/td&gt;
        &lt;td&gt;Genera numeri casuali usando l&#39;entropia raccolta dal kernel&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td class=&quot;fw-semibold&quot;&gt;&lt;code&gt;/dev/urandom&lt;/code&gt;&lt;/td&gt;
        &lt;td&gt;Come &lt;code&gt;/dev/random&lt;/code&gt;, ma non si blocca se l&#39;entropia disponibile è poca; consigliato per uso generale&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td class=&quot;fw-semibold&quot;&gt;&lt;code&gt;/dev/sda&lt;/code&gt;, &lt;code&gt;/dev/nvme0n1&lt;/code&gt;&lt;/td&gt;
        &lt;td&gt;Immagine di un intero disco fisico (SATA o NVMe); le partizioni sono &lt;code&gt;/dev/sda1&lt;/code&gt;, &lt;code&gt;/dev/sda2&lt;/code&gt;…&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td class=&quot;fw-semibold&quot;&gt;&lt;code&gt;/dev/tty&lt;/code&gt;&lt;/td&gt;
        &lt;td&gt;Il terminale corrente&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;
&lt;/div&gt;
&lt;h2&gt;&lt;code&gt;/proc&lt;/code&gt; — La finestra sul kernel&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;/proc&lt;/code&gt; è un &lt;strong&gt;filesystem virtuale&lt;/strong&gt;: non esiste fisicamente su disco, ma viene popolato dal kernel in tempo reale con informazioni sui processi attivi e sullo stato del sistema. Ogni processo in esecuzione ha una sottodirectory identificata dal suo PID (es. &lt;code&gt;/proc/1234&lt;/code&gt;), contenente informazioni su quel processo: file aperti, memoria usata, variabili d&#39;ambiente e altro.&lt;/p&gt;
&lt;p&gt;Tra i file più utili a livello di sistema troviamo &lt;code&gt;/proc/cpuinfo&lt;/code&gt; per le informazioni dettagliate sul processore, &lt;code&gt;/proc/meminfo&lt;/code&gt; per lo stato della memoria RAM e swap, &lt;code&gt;/proc/mounts&lt;/code&gt; per l&#39;elenco dei filesystem attualmente montati e &lt;code&gt;/proc/uptime&lt;/code&gt; per il tempo trascorso dall&#39;avvio del sistema.&lt;/p&gt;
&lt;h2&gt;Link: hard e soft&lt;/h2&gt;
&lt;p&gt;Linux supporta due tipi di collegamento tra file, creati entrambi con il comando &lt;code&gt;ln&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Gli &lt;strong&gt;hard link&lt;/strong&gt; puntano direttamente all&#39;&lt;strong&gt;inode&lt;/strong&gt; del file, ovvero la struttura dati interna al filesystem che descrive un file (permessi, dimensione, posizione dei dati sul disco). Due hard link allo stesso file condividono esattamente i medesimi dati: modificare uno significa modificare l&#39;altro. Il file viene eliminato dal disco solo quando l&#39;ultimo hard link che lo punta viene rimosso. Hanno però dei limiti: non funzionano tra filesystem diversi e non possono puntare a directory.&lt;/p&gt;
&lt;p&gt;I &lt;strong&gt;soft link&lt;/strong&gt; (o link simbolici) sono invece semplici puntatori al &lt;em&gt;percorso&lt;/em&gt; di un altro file. Si creano con:&lt;/p&gt;
&lt;pre class=&quot;language-bash&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token function&quot;&gt;ln&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-s&lt;/span&gt; /percorso/origine /percorso/destinazione&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Sono più flessibili degli hard link, possono attraversare filesystem diversi e puntare a directory, ma se il file originale viene spostato o eliminato il link diventa &lt;em&gt;rotto&lt;/em&gt; (dangling link) e accedervi produrrà un errore.&lt;/p&gt;
&lt;p&gt;Nel listato di &lt;code&gt;ls -l&lt;/code&gt;, i soft link sono identificati dalla lettera &lt;code&gt;l&lt;/code&gt; all&#39;inizio della stringa dei permessi, e mostrano esplicitamente il percorso a cui puntano:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;lrwxrwxrwx 1 root root 7 gen 10 12:00 /bin -&amp;gt; usr/bin
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;FIFO — Le named pipe&lt;/h2&gt;
&lt;p&gt;Le &lt;strong&gt;FIFO&lt;/strong&gt; (First In, First Out), dette anche &lt;em&gt;named pipe&lt;/em&gt;, sono un meccanismo di comunicazione tra processi. A differenza delle pipe anonime, quelle usate con &lt;code&gt;|&lt;/code&gt; nel terminale che esistono solo per la durata del comando, le FIFO hanno un nome nel filesystem e possono essere usate da processi non correlati, anche avviati in momenti diversi.&lt;/p&gt;
&lt;p&gt;Si creano con il comando &lt;code&gt;mkfifo&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;language-bash&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token function&quot;&gt;mkfifo&lt;/span&gt; /tmp/mia_pipe&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Un processo può scrivere sulla pipe e un altro può leggerla: i dati fluiscono in ordine FIFO, senza passare dal disco. Come tutti i file virtuali di questo tipo, le FIFO non sopravvivono al riavvio della macchina.&lt;/p&gt;
]]></content>
    
    
      
    
      
    <category term="linux"/>
      
    
      
    <category term="sistemi operativi"/>
      
    
      
    <category term="filesystem"/>
      
    
  </entry>
  
  <entry>
    <title>Comandi di base della Shell Linux</title>
    <link href="https://dino-996.github.io/blog/comandi-di-base-della-shell-linux/" rel="alternate" type="text/html"/>
    <id>https://dino-996.github.io/blog/comandi-di-base-della-shell-linux/</id>
    <published>2026-03-17T00:00:00.000Z</published>
    <updated>2026-03-17T00:00:00.000Z</updated>
    
    <summary>Una guida pratica ai comandi fondamentali della shell Linux; dalla lettura del prompt alla navigazione nel filesystem, dalla gestione di file e directory all&#39;elaborazione dell&#39;output con pipe e redirezioni. Ogni comando è spiegato con esempi reali, opzioni utili e criteri chiari per scegliere quando e perché usarlo.</summary>
    
    
    <content type="html"><![CDATA[&lt;h1&gt;Comandi di base della Shell&lt;/h1&gt;
&lt;p&gt;Lavorare dalla shell significa comunicare direttamente con il sistema operativo attraverso istruzioni testuali. Un &lt;strong&gt;comando&lt;/strong&gt; può essere un &lt;strong&gt;programma esterno&lt;/strong&gt; (come &lt;code&gt;ls&lt;/code&gt;), una &lt;strong&gt;funzione interna&lt;/strong&gt; della shell, o una combinazione di più comandi collegati tra loro.&lt;/p&gt;
&lt;p&gt;Quando digiti un comando, la shell lo interpreta, lo avvia e gestisce il suo input/output: il risultato può comparire a schermo, essere salvato su file o essere passato ad altri comandi tramite &lt;strong&gt;pipe&lt;/strong&gt;. Conoscere questi strumenti, il loro significato, le possibilità e i limiti, è essenziale per lavorare in modo efficace e sicuro.&lt;/p&gt;
&lt;h2&gt;Anatomia del terminale&lt;/h2&gt;
&lt;p&gt;Prima di digitare qualsiasi comando, vale la pena capire cosa stai guardando. Quando apri un terminale, la prima cosa che vedi non è una pagina bianca, è un &lt;strong&gt;prompt&lt;/strong&gt;, ovvero un invito della shell a ricevere istruzioni. Ogni elemento di quella riga ha un significato preciso.&lt;/p&gt;
&lt;p&gt;Un prompt tipico su una distribuzione Linux si presenta così:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;davide@ubuntu:~/progetti$
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Pensala come una mappa: ti dice chi sei, dove sei e con quali permessi stai operando.&lt;/p&gt;
&lt;h3&gt;Gli elementi del prompt&lt;/h3&gt;
&lt;pre&gt;&lt;code&gt;davide   @   ubuntu   :   ~/progetti   $   _
  │      │     │      │       │        │   │
  │      │     │      │       │        │   └── Cursore
  │      │     │      │       │        └────── Modalità di esecuzione
  │      │     │      │       └─────────────── Directory corrente
  │      │     │      └─────────────────────── Separatore
  │      │     └────────────────────────────── Nome host
  │      └──────────────────────────────────── Separatore
  └─────────────────────────────────────────── Nome utente
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Nome utente&lt;/strong&gt; — Il nome dell&#39;account con cui hai effettuato l&#39;accesso. È importante perché determina quali file puoi leggere, scrivere o eseguire in base ai permessi del filesystem.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Nome host&lt;/strong&gt; — Il nome della macchina su cui stai lavorando. Diventa fondamentale quando lavori su più server in remoto via SSH: ti impedisce di confonderti su &lt;em&gt;quale&lt;/em&gt; macchina stai operando.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Directory corrente&lt;/strong&gt; — Il percorso della directory in cui ti trovi in quel momento. La tilde (&lt;code&gt;~&lt;/code&gt;) è una scorciatoia per la tua home directory (es. &lt;code&gt;/home/davide&lt;/code&gt;). Questa informazione cambia ogni volta che usi &lt;code&gt;cd&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Modalità di esecuzione&lt;/strong&gt; — Il simbolo finale è probabilmente il più importante:&lt;/p&gt;
&lt;div class=&quot;table-responsive shadow-sm rounded mb-3&quot;&gt;
  &lt;table class=&quot;table table-bordered table-striped table-hover align-middle mb-0&quot;&gt;
    &lt;thead class=&quot;table-dark&quot;&gt;
      &lt;tr&gt;
        &lt;th&gt;Simbolo&lt;/th&gt;
        &lt;th&gt;Significato&lt;/th&gt;
        &lt;th&gt;Quando compare&lt;/th&gt;
      &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
      &lt;tr&gt;
        &lt;td class=&quot;fw-semibold&quot;&gt;&lt;code&gt;$&lt;/code&gt;&lt;/td&gt;
        &lt;td&gt;Utente normale&lt;/td&gt;
        &lt;td&gt;
          Operazioni quotidiane, permessi limitati
        &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td class=&quot;fw-semibold&quot;&gt;&lt;code&gt;#&lt;/code&gt;&lt;/td&gt;
        &lt;td&gt;Utente root (superutente)&lt;/td&gt;
        &lt;td&gt;Accesso completo al sistema&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;
&lt;/div&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Attenzione:&lt;/strong&gt; Quando vedi &lt;code&gt;#&lt;/code&gt;, stai operando come root: hai permessi illimitati. Qualsiasi comando eseguito in questa modalità può modificare o danneggiare file di sistema senza alcuna protezione. Usa l&#39;accesso root solo quando strettamente necessario, e preferisci &lt;code&gt;sudo&lt;/code&gt; per i singoli comandi che lo richiedono.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;strong&gt;Cursore&lt;/strong&gt; — Il trattino lampeggiante (o il blocco, a seconda della configurazione del terminale) che segue il prompt indica che la shell è in attesa del tuo input. Se il cursore non compare o il prompt non ritorna dopo aver eseguito un comando, significa che la shell è ancora occupata, o che il processo è in attesa di input da tastiera.&lt;/p&gt;
&lt;h3&gt;Il prompt è personalizzabile&lt;/h3&gt;
&lt;p&gt;Quello che hai visto sopra è il formato più comune, ma il prompt può avere forme molto diverse. Alcune shell e configurazioni mostrano informazioni aggiuntive come il ramo Git corrente, l&#39;ambiente virtuale Python attivo, o l&#39;ora dell&#39;ultimo comando. Tutto questo è controllato dalla variabile d&#39;ambiente &lt;code&gt;PS1&lt;/code&gt; (e &lt;code&gt;PS2&lt;/code&gt; per i prompt di continuazione). Non devi modificarla ora, ma sapere che esiste ti aiuta a capire perché prompt su macchine diverse possono sembrare così diversi.&lt;/p&gt;
&lt;h2&gt;Anatomia di un comando&lt;/h2&gt;
&lt;p&gt;Ogni comando segue una struttura formale precisa:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;lt;programma&amp;gt; [-opzione &amp;lt;valore&amp;gt;] &amp;lt;parametro1&amp;gt; &amp;lt;parametro2&amp;gt; …
&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Programma&lt;/strong&gt; — il nome del comando da eseguire (es. &lt;code&gt;ls&lt;/code&gt;, &lt;code&gt;cp&lt;/code&gt;, &lt;code&gt;grep&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Opzioni&lt;/strong&gt; — modificano il comportamento rispetto alle impostazioni di default. Si identificano con &lt;code&gt;-&lt;/code&gt; (forma breve) o &lt;code&gt;--&lt;/code&gt; (forma lunga) e possono richiedere un valore associato (es. &lt;code&gt;-n 5&lt;/code&gt; o &lt;code&gt;--lines=5&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Flag&lt;/strong&gt; — opzioni booleane senza valore associato: la loro semplice presenza attiva un comportamento (es. &lt;code&gt;-r&lt;/code&gt;, &lt;code&gt;-a&lt;/code&gt;, &lt;code&gt;-i&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Parametri&lt;/strong&gt; — i valori posizionali su cui il comando opera (percorsi, nomi file, stringhe). Il numero di parametri richiesti varia da comando a comando.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Per conoscere tutte le opzioni e i parametri disponibili per qualsiasi programma, usa sempre &lt;code&gt;man &amp;lt;programma&amp;gt;&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;Esempio scomposto&lt;/h3&gt;
&lt;pre class=&quot;language-bash&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token function&quot;&gt;ls&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-l&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-a&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;--color&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;always /home/davide&lt;/code&gt;&lt;/pre&gt;
&lt;div class=&quot;table-responsive shadow-sm rounded mb-3&quot;&gt;
  &lt;table class=&quot;table table-bordered table-striped table-hover align-middle mb-0&quot;&gt;
    &lt;thead class=&quot;table-dark&quot;&gt;
      &lt;tr&gt;
        &lt;th&gt;Parte&lt;/th&gt;
        &lt;th&gt;Tipo&lt;/th&gt;
        &lt;th&gt;Significato&lt;/th&gt;
      &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
      &lt;tr&gt;
        &lt;td class=&quot;fw-semibold&quot;&gt;&lt;code&gt;ls&lt;/code&gt;&lt;/td&gt;
        &lt;td&gt;Programma&lt;/td&gt;
        &lt;td&gt;
          Il comando da eseguire
        &lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td class=&quot;fw-semibold&quot;&gt;&lt;code&gt;-l&lt;/code&gt;&lt;/td&gt;
        &lt;td&gt;Flag&lt;/td&gt;
        &lt;td&gt;Formato lungo&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td class=&quot;fw-semibold&quot;&gt;&lt;code&gt;-a&lt;/code&gt;&lt;/td&gt;
        &lt;td&gt;Flag&lt;/td&gt;
        &lt;td&gt;Include file nascosti&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td class=&quot;fw-semibold&quot;&gt;&lt;code&gt;--color=always&lt;/code&gt;&lt;/td&gt;
        &lt;td&gt;Opzione&lt;/td&gt;
        &lt;td&gt;Forza l&#39;output colorato&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td class=&quot;fw-semibold&quot;&gt;&lt;code&gt;/home/davide&lt;/code&gt;&lt;/td&gt;
        &lt;td&gt;Parametro&lt;/td&gt;
        &lt;td&gt;La directory da listare&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;
&lt;/div&gt;
&lt;h2&gt;I comandi fondamentali&lt;/h2&gt;
&lt;h3&gt;&lt;code&gt;pwd&lt;/code&gt; — Dove mi trovo?&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;pwd&lt;/code&gt; sta per &lt;em&gt;print working directory&lt;/em&gt; e mostra il percorso assoluto della directory in cui ti trovi in quel momento.&lt;/p&gt;
&lt;pre class=&quot;language-bash&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;$ &lt;span class=&quot;token builtin class-name&quot;&gt;pwd&lt;/span&gt;
/home/davide/progetto&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Quando usarlo:&lt;/strong&gt; ogni volta che non sei sicuro della tua posizione nel filesystem, soprattutto dopo una serie di &lt;code&gt;cd&lt;/code&gt; o prima di eseguire comandi che operano su percorsi relativi.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Perché sceglierlo:&lt;/strong&gt; è il modo più diretto per evitare errori di percorso. Sapere dove sei prima di cancellare o modificare file può fare la differenza tra un&#39;operazione riuscita e un disastro.&lt;/p&gt;
&lt;h3&gt;&lt;code&gt;ls&lt;/code&gt; — Cosa c&#39;è qui dentro?&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;ls&lt;/code&gt; elenca il contenuto di una directory. Senza opzioni mostra solo i nomi; con le opzioni giuste fornisce dettagli su permessi, proprietario, dimensione e timestamp.&lt;/p&gt;
&lt;pre class=&quot;language-bash&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;$ &lt;span class=&quot;token function&quot;&gt;ls&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-la&lt;/span&gt; /home/davide&lt;/code&gt;&lt;/pre&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Opzione&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;-l&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Formato lungo con dettagli (permessi, dimensione, data)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;-a&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Include i file nascosti (quelli che iniziano con &lt;code&gt;.&lt;/code&gt;)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;-h&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Dimensioni leggibili (es. &lt;code&gt;4.2K&lt;/code&gt; invece di &lt;code&gt;4312&lt;/code&gt;)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;-t&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Ordina per data di modifica&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;br&gt;
&lt;p&gt;&lt;strong&gt;Quando usarlo:&lt;/strong&gt; per esplorare una directory, verificare la presenza di file, controllare i permessi o capire cosa è stato modificato di recente.&lt;/p&gt;
&lt;h3&gt;&lt;code&gt;cd&lt;/code&gt; — Spostati nel filesystem&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;cd&lt;/code&gt; (&lt;em&gt;change directory&lt;/em&gt;) cambia la directory di lavoro corrente della shell.&lt;/p&gt;
&lt;pre class=&quot;language-bash&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;$ &lt;span class=&quot;token builtin class-name&quot;&gt;cd&lt;/span&gt; /etc          &lt;span class=&quot;token comment&quot;&gt;# percorso assoluto&lt;/span&gt;
$ &lt;span class=&quot;token builtin class-name&quot;&gt;cd&lt;/span&gt; ~/progetti    &lt;span class=&quot;token comment&quot;&gt;# ~ è la shortcut per la home&lt;/span&gt;
$ &lt;span class=&quot;token builtin class-name&quot;&gt;cd&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;..&lt;/span&gt;            &lt;span class=&quot;token comment&quot;&gt;# sale di un livello&lt;/span&gt;
$ &lt;span class=&quot;token builtin class-name&quot;&gt;cd&lt;/span&gt; -             &lt;span class=&quot;token comment&quot;&gt;# torna alla directory precedente&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Quando usarlo:&lt;/strong&gt; prima di operare su file in una directory diversa da quella corrente — editare, copiare, eseguire script.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Perché sceglierlo:&lt;/strong&gt; lavorare dalla directory giusta consente di usare percorsi relativi più brevi. Le scorciatoie (&lt;code&gt;~&lt;/code&gt;, &lt;code&gt;-&lt;/code&gt;, &lt;code&gt;..&lt;/code&gt;) velocizzano molto la navigazione una volta che le si conosce.&lt;/p&gt;
&lt;h3&gt;&lt;code&gt;touch&lt;/code&gt; — Crea un file o aggiorna i timestamp&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;touch&lt;/code&gt; crea un file vuoto se non esiste; se esiste già, aggiorna la sua data/ora di modifica senza toccare il contenuto.&lt;/p&gt;
&lt;pre class=&quot;language-bash&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;$ &lt;span class=&quot;token function&quot;&gt;touch&lt;/span&gt; file.txt
$ &lt;span class=&quot;token function&quot;&gt;touch&lt;/span&gt; src/index.ts&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Quando usarlo:&lt;/strong&gt; per creare rapidamente file, per forzare la ricompilazione in build system basati su timestamp, o per aggiornare i metadati di un file.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Perché sceglierlo:&lt;/strong&gt; è non distruttivo — non sovrascrive mai il contenuto esistente.&lt;/p&gt;
&lt;h3&gt;&lt;code&gt;mkdir -p&lt;/code&gt; — Crea directory (anche annidate)&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;mkdir&lt;/code&gt; crea una o più directory. L&#39;opzione &lt;code&gt;-p&lt;/code&gt; crea anche tutte le directory intermedie mancanti, senza errori se esistono già.&lt;/p&gt;
&lt;pre class=&quot;language-bash&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;$ &lt;span class=&quot;token function&quot;&gt;mkdir&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-p&lt;/span&gt; progetto/src/components&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Senza &lt;code&gt;-p&lt;/code&gt;, se &lt;code&gt;progetto/src&lt;/code&gt; non esiste il comando fallisce. Con &lt;code&gt;-p&lt;/code&gt;, crea l&#39;intera struttura in un colpo solo.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Quando usarlo:&lt;/strong&gt; ogni volta che devi creare strutture di cartelle annidate, specialmente in script che vuoi poter rieseguire senza problemi.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Perché sceglierlo:&lt;/strong&gt; rende gli script &lt;strong&gt;idempotenti&lt;/strong&gt; — possono essere eseguiti più volte senza effetti collaterali indesiderati.&lt;/p&gt;
&lt;h3&gt;&lt;code&gt;cp -r&lt;/code&gt; e &lt;code&gt;mv&lt;/code&gt; — Copia e sposta&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;cp&lt;/code&gt; copia file e, con &lt;code&gt;-r&lt;/code&gt;, intere directory in modo ricorsivo. &lt;code&gt;mv&lt;/code&gt; sposta o rinomina file e directory.&lt;/p&gt;
&lt;pre class=&quot;language-bash&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;$ &lt;span class=&quot;token function&quot;&gt;cp&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-r&lt;/span&gt; src/ backup/src/      &lt;span class=&quot;token comment&quot;&gt;# copia ricorsiva&lt;/span&gt;
$ &lt;span class=&quot;token function&quot;&gt;mv&lt;/span&gt; report.txt archivio/     &lt;span class=&quot;token comment&quot;&gt;# sposta&lt;/span&gt;
$ &lt;span class=&quot;token function&quot;&gt;mv&lt;/span&gt; vecchio_nome nuovo_nome  &lt;span class=&quot;token comment&quot;&gt;# rinomina&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Quando usare &lt;code&gt;cp&lt;/code&gt;:&lt;/strong&gt; quando vuoi duplicare mantenendo l&#39;originale intatto.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Quando usare &lt;code&gt;mv&lt;/code&gt;:&lt;/strong&gt; quando basta spostare o rinominare. All&#39;interno dello stesso filesystem, &lt;code&gt;mv&lt;/code&gt; è molto più efficiente di &lt;code&gt;cp&lt;/code&gt; + &lt;code&gt;rm&lt;/code&gt; perché non copia fisicamente i dati — aggiorna solo i metadati del filesystem.&lt;/p&gt;
&lt;h3&gt;&lt;code&gt;rm -r&lt;/code&gt; / &lt;code&gt;rm -f&lt;/code&gt; — Rimuovi file e directory&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;rm&lt;/code&gt; elimina file in modo definitivo (non finiscono nel cestino). Con &lt;code&gt;-r&lt;/code&gt; rimuove ricorsivamente directory intere; con &lt;code&gt;-f&lt;/code&gt; forza l&#39;operazione senza chiedere conferma.&lt;/p&gt;
&lt;pre class=&quot;language-bash&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;$ &lt;span class=&quot;token function&quot;&gt;rm&lt;/span&gt; file.txt          &lt;span class=&quot;token comment&quot;&gt;# rimuove un singolo file&lt;/span&gt;
$ &lt;span class=&quot;token function&quot;&gt;rm&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-r&lt;/span&gt; dist/          &lt;span class=&quot;token comment&quot;&gt;# rimuove la directory dist/ e tutto il suo contenuto&lt;/span&gt;
$ &lt;span class=&quot;token function&quot;&gt;rm&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-i&lt;/span&gt; *.log          &lt;span class=&quot;token comment&quot;&gt;# chiede conferma per ogni file (consigliato)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Attenzione:&lt;/strong&gt; &lt;code&gt;rm -rf /&lt;/code&gt; cancella l&#39;intero filesystem se eseguito con i privilegi giusti. Verifica sempre il percorso prima di usare &lt;code&gt;-rf&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;strong&gt;Quando usarlo:&lt;/strong&gt; per pulire file o directory non più necessari, in script di build o deploy.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Consiglio:&lt;/strong&gt; in caso di dubbio usa &lt;code&gt;-i&lt;/code&gt; per ricevere una conferma interattiva prima di ogni eliminazione.&lt;/p&gt;
&lt;h3&gt;&lt;code&gt;cat&lt;/code&gt; e &lt;code&gt;less&lt;/code&gt; — Leggi il contenuto di un file&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;cat&lt;/code&gt; stampa il contenuto di un file direttamente nel terminale. &lt;code&gt;less&lt;/code&gt; apre un visualizzatore interattivo che permette di scorrere, cercare e navigare.&lt;/p&gt;
&lt;pre class=&quot;language-bash&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;$ &lt;span class=&quot;token function&quot;&gt;cat&lt;/span&gt; config.json          &lt;span class=&quot;token comment&quot;&gt;# output immediato, tutto in una volta&lt;/span&gt;
$ &lt;span class=&quot;token function&quot;&gt;less&lt;/span&gt; /var/log/syslog     &lt;span class=&quot;token comment&quot;&gt;# visualizzazione paginata e navigabile&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In &lt;code&gt;less&lt;/code&gt;: usa le frecce o &lt;code&gt;j&lt;/code&gt;/&lt;code&gt;k&lt;/code&gt; per scorrere, &lt;code&gt;/termine&lt;/code&gt; per cercare, &lt;code&gt;q&lt;/code&gt; per uscire.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Quando usare &lt;code&gt;cat&lt;/code&gt;:&lt;/strong&gt; per file brevi o per concatenare più file in pipeline (&lt;code&gt;cat a.txt b.txt | sort&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Quando usare &lt;code&gt;less&lt;/code&gt;:&lt;/strong&gt; per file lunghi come log di sistema — eviti di intasare il terminale con migliaia di righe.&lt;/p&gt;
&lt;h3&gt;&lt;code&gt;man&lt;/code&gt; — Il manuale integrato&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;man&lt;/code&gt; (&lt;em&gt;manual&lt;/em&gt;) apre la pagina di documentazione ufficiale di un comando, direttamente nel terminale. Ogni pagina descrive scopo, sintassi, opzioni e, spesso, esempi d&#39;uso.&lt;/p&gt;
&lt;pre class=&quot;language-bash&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;$ &lt;span class=&quot;token function&quot;&gt;man&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;ls&lt;/span&gt;       &lt;span class=&quot;token comment&quot;&gt;# apre il manuale di ls&lt;/span&gt;
$ &lt;span class=&quot;token function&quot;&gt;man&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;cp&lt;/span&gt;       &lt;span class=&quot;token comment&quot;&gt;# apre il manuale di cp&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;La navigazione in &lt;code&gt;man&lt;/code&gt; usa le stesse scorciatoie di &lt;code&gt;less&lt;/code&gt;: frecce o &lt;code&gt;j&lt;/code&gt;/&lt;code&gt;k&lt;/code&gt; per scorrere, &lt;code&gt;/termine&lt;/code&gt; per cercare, &lt;code&gt;q&lt;/code&gt; per uscire.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Quando usarlo:&lt;/strong&gt; ogni volta che non ricordi un&#39;opzione specifica o vuoi capire il comportamento esatto di un comando senza cercare su internet. È la fonte più affidabile perché è sempre aggiornata alla versione installata sul tuo sistema.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Perché sceglierlo:&lt;/strong&gt; &lt;code&gt;man&lt;/code&gt; non richiede connessione, non ha pubblicità e non ti porta su Stack Overflow. È lento da leggere all&#39;inizio, ma imparare a navigarlo è un investimento che ripaga nel tempo.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;💡 Se vuoi una versione più concisa, prova &lt;code&gt;man -k termine&lt;/code&gt; per cercare tra tutte le pagine di manuale che menzionano una parola chiave, oppure comandi come &lt;code&gt;tldr&lt;/code&gt; (da installare separatamente) per riassunti pratici e orientati agli esempi.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;&lt;code&gt;clear&lt;/code&gt; — Pulisci lo schermo&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;clear&lt;/code&gt; svuota il terminale rimuovendo tutto l&#39;output precedente, riportando il prompt in cima allo schermo.&lt;/p&gt;
&lt;pre class=&quot;language-bash&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;$ &lt;span class=&quot;token function&quot;&gt;clear&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In alternativa, la scorciatoia da tastiera &lt;code&gt;Ctrl + L&lt;/code&gt; produce lo stesso effetto senza dover digitare nulla.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Quando usarlo:&lt;/strong&gt; quando lo schermo è affollato di output e vuoi ricominciare da una vista pulita, oppure prima di eseguire un comando il cui output vuoi leggere senza distrazioni.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Perché sceglierlo:&lt;/strong&gt; non cancella la cronologia dei comandi (&lt;code&gt;history&lt;/code&gt; rimane intatta) né termina processi in esecuzione — è puramente visivo. Se vuoi scorrere l&#39;output precedente dopo un &lt;code&gt;clear&lt;/code&gt;, nella maggior parte dei terminali puoi farlo con la rotella del mouse o con &lt;code&gt;Shift + PgUp&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;&lt;code&gt;exit&lt;/code&gt; — Chiudi la sessione&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;exit&lt;/code&gt; termina la sessione della shell corrente. Se stai lavorando in un terminale normale, chiude la finestra o la scheda. Se sei connesso via SSH a un server remoto, disconnette la sessione remota riportandoti alla shell locale.&lt;/p&gt;
&lt;pre class=&quot;language-bash&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;$ &lt;span class=&quot;token builtin class-name&quot;&gt;exit&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Puoi ottenere lo stesso risultato con la scorciatoia &lt;code&gt;Ctrl + D&lt;/code&gt;, che invia il segnale di fine input alla shell.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Quando usarlo:&lt;/strong&gt; per chiudere sessioni SSH in modo pulito, per uscire da una subshell avviata manualmente, o semplicemente per terminare il lavoro nel terminale.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Perché sceglierlo:&lt;/strong&gt; chiudere una sessione con &lt;code&gt;exit&lt;/code&gt; è più corretto che chiudere brutalmente la finestra del terminale, soprattutto su connessioni remote: assicura che i processi associati alla sessione vengano terminati correttamente.&lt;/p&gt;
&lt;h2&gt;Canali di comunicazione di un comando&lt;/h2&gt;
&lt;p&gt;Ogni comando ha a disposizione tre canali di comunicazione con il resto del sistema:&lt;/p&gt;
&lt;div class=&quot;table-responsive shadow-sm rounded mb-3&quot;&gt;
  &lt;table class=&quot;table table-bordered table-striped table-hover align-middle mb-0&quot;&gt;
    &lt;thead class=&quot;table-dark&quot;&gt;
      &lt;tr&gt;
        &lt;th&gt;Canale&lt;/th&gt;
        &lt;th&gt;Nome&lt;/th&gt;
        &lt;th&gt;Descrizione&lt;/th&gt;
      &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
      &lt;tr&gt;
        &lt;td class=&quot;fw-semibold&quot;&gt;&lt;code&gt;stdin&lt;/code&gt;&lt;/td&gt;
        &lt;td&gt;Standard input&lt;/td&gt;
        &lt;td&gt;L&#39;ingresso del programma. Di default è collegato alla tastiera.&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td class=&quot;fw-semibold&quot;&gt;&lt;code&gt;stdout&lt;/code&gt;&lt;/td&gt;
        &lt;td&gt;Standard output&lt;/td&gt;
        &lt;td&gt;L&#39;uscita principale del programma. Di default viene mostrata nel terminale.&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td class=&quot;fw-semibold&quot;&gt;&lt;code&gt;stderr&lt;/code&gt;&lt;/td&gt;
        &lt;td&gt;Standard error&lt;/td&gt;
        &lt;td&gt;L&#39;uscita dedicata agli errori e ai messaggi di stato. Tenuta su un canale separato per evitare che si mescoli con l&#39;output normale. Di default appare anch&#39;essa nel terminale.&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;
&lt;/div&gt;
&lt;p&gt;Separare &lt;code&gt;stdout&lt;/code&gt; e &lt;code&gt;stderr&lt;/code&gt; è una scelta progettuale importante: permette di filtrare, reindirizzare o loggare errori e output in modo indipendente, senza che si contaminino a vicenda.&lt;/p&gt;
&lt;h2&gt;Redirezione dell&#39;output&lt;/h2&gt;
&lt;p&gt;La shell permette di collegare i canali di comunicazione tra comandi e file, costruendo catene di elaborazione anche complesse. Queste operazioni si chiamano &lt;strong&gt;redirezioni&lt;/strong&gt;.&lt;/p&gt;
&lt;div class=&quot;table-responsive shadow-sm rounded mb-3&quot;&gt;
  &lt;table class=&quot;table table-bordered table-striped table-hover align-middle mb-0&quot;&gt;
    &lt;thead class=&quot;table-dark&quot;&gt;
      &lt;tr&gt;
        &lt;th&gt;Sinstassi&lt;/th&gt;
        &lt;th&gt;Effetto&lt;/th&gt;
      &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
      &lt;tr&gt;
        &lt;td class=&quot;fw-semibold&quot;&gt;&lt;code&gt;comando &gt; file&lt;/code&gt;&lt;/td&gt;
        &lt;td&gt;Scrive &lt;code&gt;stdout&lt;/code&gt; nel file (sovrascrive se esiste)&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td class=&quot;fw-semibold&quot;&gt;&lt;code&gt;comando &gt;&gt; file&lt;/code&gt;&lt;/td&gt;
        &lt;td&gt;Appende &lt;code&gt;stdout&lt;/code&gt; al file (non sovrascrive)&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td class=&quot;fw-semibold&quot;&gt;&lt;code&gt;comando &lt; file&lt;/code&gt;&lt;/td&gt;
        &lt;td&gt;Usa il file come &lt;code&gt;stdin&lt;/code&gt; del comando&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td class=&quot;fw-semibold&quot;&gt;&lt;code&gt;comando 2&gt; file&lt;/code&gt;&lt;/td&gt;
        &lt;td&gt;Scrive &lt;code&gt;stderr&lt;/code&gt; nel file&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td class=&quot;fw-semibold text-nowrap&quot;&gt;&lt;code&gt;comando1 | comando2&lt;/code&gt;&lt;/td&gt;
        &lt;td&gt;Trasforma l&#39;output del primo comando (&lt;code&gt;stdout&lt;/code&gt;) &lt;code&gt;comando1&lt;/code&gt; in input (&lt;code&gt;stdin&lt;/code&gt;) del secondo comando &lt;code&gt;comando2&lt;/code&gt;&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;
  &lt;/table&gt;
&lt;/div&gt;
&lt;h3&gt;Esempi pratici&lt;/h3&gt;
&lt;pre class=&quot;language-bash&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Salva la lista dei file in un log&lt;/span&gt;
$ &lt;span class=&quot;token function&quot;&gt;ls&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-la&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; lista_file.txt

&lt;span class=&quot;token comment&quot;&gt;# Appende nuovi risultati senza sovrascrivere&lt;/span&gt;
$ &lt;span class=&quot;token function&quot;&gt;ls&lt;/span&gt; ~/downloads &lt;span class=&quot;token operator&quot;&gt;&gt;&gt;&lt;/span&gt; lista_file.txt

&lt;span class=&quot;token comment&quot;&gt;# Filtra solo gli errori in un file separato&lt;/span&gt;
$ &lt;span class=&quot;token function&quot;&gt;make&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&lt;span class=&quot;token file-descriptor important&quot;&gt;2&lt;/span&gt;&gt;&lt;/span&gt; errori.log

&lt;span class=&quot;token comment&quot;&gt;# Concatena due file e cerca una stringa&lt;/span&gt;
$ &lt;span class=&quot;token function&quot;&gt;cat&lt;/span&gt; a.txt b.txt &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;grep&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;TODO&quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;La pipe (&lt;code&gt;|&lt;/code&gt;) è uno degli strumenti più potenti della shell: permette di comporre programmi semplici in pipeline sofisticate, dove l&#39;output di uno diventa l&#39;input del successivo.&lt;/p&gt;
&lt;h2&gt;Comandi di elaborazione dell&#39;output&lt;/h2&gt;
&lt;p&gt;Questi comandi sono progettati per trasformare, filtrare e analizzare dati testuali. Lavorano spesso in combinazione tramite pipe, e il loro punto di forza è proprio la componibilità: incatenati insieme, permettono di estrarre esattamente le informazioni che servono da flussi di dati anche molto grandi.&lt;/p&gt;
&lt;h3&gt;&lt;code&gt;echo&lt;/code&gt; — Stampa una stringa&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;echo&lt;/code&gt; scrive una stringa su &lt;code&gt;stdout&lt;/code&gt;. Semplice, ma molto utile in script e pipeline.&lt;/p&gt;
&lt;pre class=&quot;language-bash&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;$ &lt;span class=&quot;token builtin class-name&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Ciao mondo&quot;&lt;/span&gt;
Ciao mondo

$ &lt;span class=&quot;token builtin class-name&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-n&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;senza newline&quot;&lt;/span&gt;   &lt;span class=&quot;token comment&quot;&gt;# non va a capo alla fine&lt;/span&gt;
$ &lt;span class=&quot;token builtin class-name&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-e&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;&lt;span class=&quot;token entity&quot; title=&quot;&#92;e&quot;&gt;&#92;e&lt;/span&gt;[36;1mTesto ciano grassetto&lt;span class=&quot;token entity&quot; title=&quot;&#92;e&quot;&gt;&#92;e&lt;/span&gt;[0m&quot;&lt;/span&gt;   &lt;span class=&quot;token comment&quot;&gt;# interpreta escape ANSI&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Opzione&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;-n&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Non aggiunge il carattere di newline finale&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;-e&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Interpreta i caratteri di escape (es. colori ANSI, &lt;code&gt;&#92;t&lt;/code&gt;, &lt;code&gt;&#92;n&lt;/code&gt;)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;br&gt;
&lt;p&gt;&lt;strong&gt;Quando usarlo:&lt;/strong&gt; per stampare messaggi in script, per passare stringhe a pipeline, o per testare rapidamente l&#39;output di variabili d&#39;ambiente (&lt;code&gt;echo $PATH&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Perché sceglierlo:&lt;/strong&gt; è universalmente disponibile e non ha dipendenze. Con &lt;code&gt;-e&lt;/code&gt; permette anche di formattare l&#39;output con colori e stili (vedi &lt;code&gt;man console_codes&lt;/code&gt; per i codici ANSI).&lt;/p&gt;
&lt;h3&gt;&lt;code&gt;head&lt;/code&gt; e &lt;code&gt;tail&lt;/code&gt; — Prime e ultime righe&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;head&lt;/code&gt; mostra le prime righe di un file o di uno stream; &lt;code&gt;tail&lt;/code&gt; mostra le ultime. Per default entrambi mostrano 10 righe.&lt;/p&gt;
&lt;pre class=&quot;language-bash&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;$ &lt;span class=&quot;token function&quot;&gt;head&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-n&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt; access.log      &lt;span class=&quot;token comment&quot;&gt;# prime 5 righe&lt;/span&gt;
$ &lt;span class=&quot;token function&quot;&gt;tail&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-n&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;20&lt;/span&gt; error.log      &lt;span class=&quot;token comment&quot;&gt;# ultime 20 righe&lt;/span&gt;
$ &lt;span class=&quot;token function&quot;&gt;tail&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-f&lt;/span&gt; /var/log/syslog   &lt;span class=&quot;token comment&quot;&gt;# segue il file in tempo reale (live)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Opzione&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;-n &amp;lt;numero&amp;gt;&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Specifica il numero di righe da mostrare&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;br&gt;
&lt;p&gt;&lt;strong&gt;Quando usarli:&lt;/strong&gt; &lt;code&gt;head&lt;/code&gt; per ispezionare l&#39;intestazione di un file (es. CSV con colonne); &lt;code&gt;tail&lt;/code&gt; per leggere le righe più recenti di un log. &lt;code&gt;tail -f&lt;/code&gt; è particolarmente utile per monitorare log applicativi in tempo reale.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Perché sceglierli:&lt;/strong&gt; evitano di caricare l&#39;intero file quando serve solo una porzione. Su file di log da gigabyte, la differenza è sostanziale.&lt;/p&gt;
&lt;h3&gt;&lt;code&gt;grep&lt;/code&gt; — Cerca nel testo&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;grep&lt;/code&gt; (&lt;em&gt;General Regular Expression Print&lt;/em&gt;) cerca righe che corrispondono a un pattern all&#39;interno di file o stream. Restituisce solo le righe che contengono la corrispondenza.&lt;/p&gt;
&lt;pre class=&quot;language-bash&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;$ &lt;span class=&quot;token function&quot;&gt;grep&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;errore&quot;&lt;/span&gt; app.log               &lt;span class=&quot;token comment&quot;&gt;# cerca la stringa &quot;errore&quot;&lt;/span&gt;
$ &lt;span class=&quot;token function&quot;&gt;grep&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-i&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;warning&quot;&lt;/span&gt; app.log           &lt;span class=&quot;token comment&quot;&gt;# case insensitive&lt;/span&gt;
$ &lt;span class=&quot;token function&quot;&gt;grep&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-n&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;TODO&quot;&lt;/span&gt; src/main.ts          &lt;span class=&quot;token comment&quot;&gt;# mostra il numero di riga&lt;/span&gt;
$ &lt;span class=&quot;token function&quot;&gt;grep&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-c&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;404&quot;&lt;/span&gt; access.log            &lt;span class=&quot;token comment&quot;&gt;# conta le righe corrispondenti&lt;/span&gt;
$ &lt;span class=&quot;token function&quot;&gt;cat&lt;/span&gt; app.log &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;grep&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-i&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;error&quot;&lt;/span&gt;       &lt;span class=&quot;token comment&quot;&gt;# in pipeline&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Opzione&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;-i&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Ignora maiuscole/minuscole&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;-n&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Precede ogni riga con il suo numero&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;-c&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Restituisce solo il conteggio delle righe corrispondenti&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;br&gt;
&lt;p&gt;&lt;strong&gt;Quando usarlo:&lt;/strong&gt; per filtrare log, cercare occorrenze di una stringa in un file, o come filtro in una pipeline (&lt;code&gt;comando | grep &amp;quot;pattern&amp;quot;&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Perché sceglierlo:&lt;/strong&gt; è lo strumento standard per la ricerca testuale su stream e file. Supporta anche le espressioni regolari, che aprono possibilità di ricerca molto più avanzate della semplice stringa fissa.&lt;/p&gt;
&lt;h3&gt;&lt;code&gt;wc&lt;/code&gt; — Conta righe, parole e caratteri&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;wc&lt;/code&gt; (&lt;em&gt;Word Count&lt;/em&gt;) conta righe, parole e caratteri di un file o stream.&lt;/p&gt;
&lt;pre class=&quot;language-bash&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;$ &lt;span class=&quot;token function&quot;&gt;wc&lt;/span&gt; file.txt              &lt;span class=&quot;token comment&quot;&gt;# righe, parole e caratteri&lt;/span&gt;
$ &lt;span class=&quot;token function&quot;&gt;wc&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-l&lt;/span&gt; *.log              &lt;span class=&quot;token comment&quot;&gt;# conta solo le righe di ogni file .log&lt;/span&gt;
$ &lt;span class=&quot;token function&quot;&gt;cat&lt;/span&gt; file.txt &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;wc&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-w&lt;/span&gt;     &lt;span class=&quot;token comment&quot;&gt;# conta le parole via pipe&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Opzione&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;-l&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Conta solo le righe&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;-w&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Conta solo le parole&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;-m&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Conta solo i caratteri&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;br&gt;
&lt;p&gt;&lt;strong&gt;Quando usarlo:&lt;/strong&gt; per sapere quante righe ha un file di log, quante voci contiene un CSV, o quante occorrenze ha restituito un &lt;code&gt;grep&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Perché sceglierlo:&lt;/strong&gt; è veloce e componibile. &lt;code&gt;grep &amp;quot;errore&amp;quot; app.log | wc -l&lt;/code&gt; risponde in un colpo solo a &amp;quot;quanti errori ci sono nel log?&amp;quot;.&lt;/p&gt;
&lt;h3&gt;&lt;code&gt;sort&lt;/code&gt; — Ordina le righe&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;sort&lt;/code&gt; riordina le righe di un file o stream secondo la regola specificata. Di default ordina lessicograficamente.&lt;/p&gt;
&lt;pre class=&quot;language-bash&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;$ &lt;span class=&quot;token function&quot;&gt;sort&lt;/span&gt; nomi.txt                  &lt;span class=&quot;token comment&quot;&gt;# ordine alfabetico&lt;/span&gt;
$ &lt;span class=&quot;token function&quot;&gt;sort&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-r&lt;/span&gt; nomi.txt               &lt;span class=&quot;token comment&quot;&gt;# ordine inverso&lt;/span&gt;
$ &lt;span class=&quot;token function&quot;&gt;sort&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-g&lt;/span&gt; numeri.txt             &lt;span class=&quot;token comment&quot;&gt;# ordine numerico&lt;/span&gt;
$ &lt;span class=&quot;token function&quot;&gt;sort&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-R&lt;/span&gt; lista.txt              &lt;span class=&quot;token comment&quot;&gt;# ordine casuale&lt;/span&gt;
$ &lt;span class=&quot;token function&quot;&gt;cat&lt;/span&gt; log.txt &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;sort&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-f&lt;/span&gt;          &lt;span class=&quot;token comment&quot;&gt;# case insensitive via pipe&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Opzione&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;-f&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Ignora maiuscole/minuscole&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;-g&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Ordine numerico&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;-d&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Ordine alfabetico (dizionario, ignora punteggiatura)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;-r&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Ordine inverso&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;-R&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Ordine casuale&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;br&gt;
&lt;p&gt;&lt;strong&gt;Quando usarlo:&lt;/strong&gt; per ordinare output prima di confrontarlo, per preparare dati per &lt;code&gt;uniq&lt;/code&gt;, o per trovare rapidamente il valore massimo/minimo in una lista numerica.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Perché sceglierlo:&lt;/strong&gt; è spesso il passaggio intermedio necessario in una pipeline prima di elaborazioni successive — &lt;code&gt;sort&lt;/code&gt; + &lt;code&gt;uniq&lt;/code&gt; è una combinazione classica.&lt;/p&gt;
&lt;h3&gt;&lt;code&gt;uniq&lt;/code&gt; — Rimuovi o conta i duplicati&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;uniq&lt;/code&gt; (&lt;em&gt;UNIQue&lt;/em&gt;) opera su righe adiacenti identiche: le rimuove, le conta o le filtra. Per funzionare correttamente su file non ordinati, va quasi sempre preceduto da &lt;code&gt;sort&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;language-bash&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;$ &lt;span class=&quot;token function&quot;&gt;sort&lt;/span&gt; lista.txt &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;uniq&lt;/span&gt;          &lt;span class=&quot;token comment&quot;&gt;# rimuove i duplicati&lt;/span&gt;
$ &lt;span class=&quot;token function&quot;&gt;sort&lt;/span&gt; lista.txt &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;uniq&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-c&lt;/span&gt;       &lt;span class=&quot;token comment&quot;&gt;# conta le occorrenze di ogni valore&lt;/span&gt;
$ &lt;span class=&quot;token function&quot;&gt;sort&lt;/span&gt; lista.txt &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;uniq&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-u&lt;/span&gt;       &lt;span class=&quot;token comment&quot;&gt;# mostra solo i valori univoci&lt;/span&gt;
$ &lt;span class=&quot;token function&quot;&gt;sort&lt;/span&gt; lista.txt &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;uniq&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-d&lt;/span&gt;       &lt;span class=&quot;token comment&quot;&gt;# mostra solo i valori duplicati&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Opzione&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;-i&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Ignora maiuscole/minuscole&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;-c&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Precede ogni riga con il numero di occorrenze&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;-u&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Mostra solo le righe che appaiono una volta sola&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;-d&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Mostra solo le righe che appaiono più di una volta&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;br&gt;
&lt;p&gt;&lt;strong&gt;Quando usarlo:&lt;/strong&gt; per deduplicare liste, per trovare valori ripetuti in un log, o per costruire un conteggio delle frequenze (&lt;code&gt;sort | uniq -c | sort -rg&lt;/code&gt; restituisce una classifica delle occorrenze).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Perché sceglierlo:&lt;/strong&gt; in combinazione con &lt;code&gt;sort&lt;/code&gt;, &lt;code&gt;grep&lt;/code&gt; e &lt;code&gt;wc&lt;/code&gt; copre la maggior parte dei casi d&#39;uso di analisi testuale rapida senza bisogno di script complessi.&lt;/p&gt;
&lt;h2&gt;Come scegliere il comando giusto&lt;/h2&gt;
&lt;p&gt;La scelta dipende da tre fattori principali:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Scopo (Identifica l&#39;azione)&lt;/strong&gt;: leggere, copiare, spostare, eliminare. Scegli il comando che fa esattamente quello e niente di più.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Efficienza&lt;/strong&gt;: &lt;code&gt;mv&lt;/code&gt; è più veloce di &lt;code&gt;cp&lt;/code&gt; + &lt;code&gt;rm&lt;/code&gt; per spostamenti sullo stesso filesystem. Conoscere queste differenze evita operazioni inutili.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Sicurezza&lt;/strong&gt;: Preferisci comandi che richiedono conferma (&lt;code&gt;rm -i&lt;/code&gt;) per operazioni distruttive. In uno script, verifica sempre i percorsi prima di eseguire cancellazioni.&lt;/li&gt;
&lt;/ul&gt;
]]></content>
    
    
      
    
      
    <category term="linux"/>
      
    
      
    <category term="sistemi operativi"/>
      
    
      
    <category term="shell"/>
      
    
  </entry>
  
  <entry>
    <title>GNU/Linux</title>
    <link href="https://dino-996.github.io/blog/gnulinux/" rel="alternate" type="text/html"/>
    <id>https://dino-996.github.io/blog/gnulinux/</id>
    <published>2026-03-16T00:00:00.000Z</published>
    <updated>2026-03-16T00:00:00.000Z</updated>
    
    <summary>GNU/Linux è un sistema operativo libero e open source, nato dall&#39;unione del kernel Linux e gli strumenti di sistema del progetto GNU. È stabile, sicuro e altamente personalizzabile, utilizzato in svariati ambiti, dai server ai computer desktop. In questo articolo introduttivo proviamo a capirne di più partendo dall&#39;architettura dell&#39;ecosistema fino ai comandi più utili.</summary>
    
    
    <content type="html"><![CDATA[&lt;h1&gt;Un po&#39; di storia&lt;/h1&gt;
&lt;p&gt;Lo so: appena hai visto il titolo ti è venuta voglia di saltare questa parte. Ma fidati, ne vale la pena. La storia di Linux non è solo interessante: è fondamentale per capire perché oggi domina ovunque, dai server agli smartphone.&lt;/p&gt;
&lt;h2&gt;Più di un semplice software&lt;/h2&gt;
&lt;p&gt;Partiamo da un punto chiave: Linux è un &lt;strong&gt;kernel&lt;/strong&gt;, non un sistema operativo completo.&lt;/p&gt;
&lt;p&gt;Puoi immaginarlo come il &lt;strong&gt;motore di un&#39;auto&lt;/strong&gt;: essenziale per farla muovere, ma inutile senza tutto il resto (interfaccia grafica, programmi, strumenti di sistema). Il suo successo nasce da un mix unico di scelte tecniche brillanti, collaborazione globale e una filosofia di sviluppo aperta.&lt;/p&gt;
&lt;h2&gt;Le origini&lt;/h2&gt;
&lt;p&gt;Nel &lt;strong&gt;1991&lt;/strong&gt;, lo studente finlandese &lt;a href=&quot;https://www.linkedin.com/in/linustorvalds/&quot;&gt;&lt;strong&gt;Linus Torvalds&lt;/strong&gt;&lt;/a&gt; creò Linux per pura curiosità. Voleva un sistema simile a &lt;a href=&quot;https://www.unix.org/&quot;&gt;Unix&lt;/a&gt; per il suo PC e, non potendoselo permettere, decise di scriverselo da solo. Non lo fece per soldi né per crociata ideologica: lo fece per divertimento.&lt;/p&gt;
&lt;p&gt;Tuttavia, Torvalds ha il vizio di creare rivoluzioni quasi per caso. Nel &lt;strong&gt;2005&lt;/strong&gt;, dopo una rottura con &lt;a href=&quot;https://www.bitkeeper.org/&quot;&gt;BitKeeper&lt;/a&gt; (il software di controllo versione usato per gestire il codice di Linux), Torvalds si chiuse in una stanza e &lt;strong&gt;scrisse Git in soli 10 giorni&lt;/strong&gt;. Oggi Git è lo standard mondiale per lo sviluppo software. Torvalds lo ha poi affidato a &lt;a href=&quot;https://www.linkedin.com/in/gitster/&quot;&gt;&lt;strong&gt;Junio Hamano&lt;/strong&gt;&lt;/a&gt;, che ne è tuttora il maintainer, dimostrando che le sue creazioni migliori diventano presto beni comuni.&lt;/p&gt;
&lt;h2&gt;Il lato umano&lt;/h2&gt;
&lt;p&gt;Gestire un progetto globale non è facile, specialmente se hai un carattere fumantino. Torvalds è noto per non avere peli sulla lingua: memorabili sono le sue critiche a &lt;strong&gt;NVIDIA&lt;/strong&gt; (con tanto di &lt;a href=&quot;https://www.youtube.com/watch?v=iYWzMvlj2RQ&quot;&gt;dito medio in diretta&lt;/a&gt;) o i suoi scontri su progetti come &lt;strong&gt;systemd&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Tuttavia, il &lt;strong&gt;2018&lt;/strong&gt; ha segnato un punto di svolta:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Il ritiro temporaneo:&lt;/strong&gt; Torvalds ha ammesso che il suo stile aggressivo aveva reso l&#39;ambiente tossico e si è preso una pausa di un mese per lavorare sulla propria empatia.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Il Codice di Condotta:&lt;/strong&gt; Nel &lt;strong&gt;settembre 2018&lt;/strong&gt;, il progetto ha adottato un &lt;em&gt;Code of Conduct&lt;/em&gt; formale (basato sul &lt;a href=&quot;https://www.contributor-covenant.org/&quot;&gt;&lt;em&gt;Contributor Covenant&lt;/em&gt;&lt;/a&gt;). È stato un passo cruciale per rendere la community più inclusiva, anche se non ha mancato di sollevare polemiche tra i veterani.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;La Community&lt;/h2&gt;
&lt;p&gt;Oggi Linux è mantenuto da migliaia di sviluppatori e colossi come &lt;strong&gt;Google, IBM, Red Hat e Intel&lt;/strong&gt;. Non è più solo il progetto di un singolo uomo, ma una collaborazione globale dove migliaia di persone contribuiscono ogni giorno per migliorare il codice che fa girare internet.&lt;/p&gt;
&lt;h2&gt;Filosofia&lt;/h2&gt;
&lt;p&gt;Il mondo &lt;strong&gt;open source&lt;/strong&gt; vive di un dualismo affascinante:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;L&#39;idealismo di Stallman&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Il software libero è un diritto etico e morale.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Il suo idealismo si basa su quattro libertà fondamentali:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Eseguire il programma per qualunque scopo.&lt;/li&gt;
&lt;li&gt;Studiare come funziona e modificarlo.&lt;/li&gt;
&lt;li&gt;Ridistribuire copie.&lt;/li&gt;
&lt;li&gt;Migliorarlo e condividere le modifiche.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Per &lt;a href=&quot;https://stallman.org/&quot;&gt;Stallman&lt;/a&gt;, il software proprietario è &lt;strong&gt;eticamente sbagliato&lt;/strong&gt; perché nega queste libertà, creando dipendenza e isolamento. La sua battaglia non è tecnica, ma morale: vuole una società in cui la condivisione del sapere sia normale e protetta.&lt;/p&gt;
&lt;p&gt;Come disse dopo la morte di Steve Jobs nel 2011:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Steve Jobs è stato un pioniere dei computer costruiti come belle prigioni.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Il suo approccio è intransigente: rifiuta compromessi, insiste sull&#39;uso del termine &lt;em&gt;software libero&lt;/em&gt; (distinto sia dal software proprietario che dal semplice software di dominio pubblico) e promuove il copyleft tramite la licenza GPL per garantire che la libertà del codice sia perpetua anche nei lavori derivati.&lt;/p&gt;
&lt;p&gt;È nel &lt;strong&gt;1983&lt;/strong&gt; che ha fondato il progetto &lt;a href=&quot;https://www.gnu.org/home.it.html&quot;&gt;&lt;strong&gt;GNU&lt;/strong&gt;&lt;/a&gt;, nato per creare un sistema operativo completamente libero e compatibile con Unix, dando così vita al movimento del &lt;strong&gt;software libero&lt;/strong&gt;. Il nome GNU è un acronimo ricorsivo che sta per &lt;em&gt;GNU&#39;s Not Unix&lt;/em&gt; (GNU non è Unix), riflettendo l&#39;obiettivo di fornire un&#39;alternativa libera al sistema che ai tempi era di proprietà di &lt;a href=&quot;https://archiveos.org/att-unix-pc/&quot;&gt;AT&amp;amp;T&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Nel &lt;strong&gt;1985&lt;/strong&gt; ha fondato la &lt;strong&gt;Free Software Foundation&lt;/strong&gt; per supportare il progetto e fornire un&#39;infrastruttura legale al movimento. Ha inventato il concetto di &lt;em&gt;copyleft&lt;/em&gt; e ha scritto la &lt;strong&gt;GNU General Public License&lt;/strong&gt; (&lt;strong&gt;GPL&lt;/strong&gt;) nel 1989, una licenza fondamentale che protegge i diritti di modifica e ridistribuzione del software.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Il pragmatismo di Torvalds&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Ciò che conta è che il codice funzioni bene.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Inizialmente Linux si appoggiò a &lt;a href=&quot;https://wiki.minix3.org&quot;&gt;MINIX&lt;/a&gt;, un sistema operativo educativo basato su Unix, per configurare, compilare e installare il kernel nascente. MINIX tuttavia presentava limitazioni significative: la sua licenza non permetteva modifiche e ridistribuzione liberamente, e la sua architettura microkernel mal si adattava alle ambizioni di Torvalds. Solo dopo aver reso pubblico il kernel nel &lt;strong&gt;1991&lt;/strong&gt;, Torvalds decise di sostituire i componenti di MINIX con il software del progetto GNU, per creare un sistema operativo completo. Questo avvenne nel &lt;strong&gt;1992&lt;/strong&gt;, quando il kernel Linux si unì de facto alle utilità GNU, formando il sistema operativo completo noto oggi come &lt;strong&gt;GNU/Linux&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Linux ha vinto perché è stato &lt;strong&gt;aperto senza essere dogmatico&lt;/strong&gt;. Questa flessibilità ha permesso ad aziende e appassionati di adottarlo senza sentirsi intrappolati in una battaglia politica.&lt;/p&gt;
&lt;h2&gt;Tux, il pinguino in smoking&lt;/h2&gt;
&lt;p&gt;Perché un &lt;strong&gt;pinguino&lt;/strong&gt;? Tutto nasce da un aneddoto: Torvalds fu morso da un pinguino durante una visita a uno zoo e ne rimase &lt;em&gt;stregato&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Il nome &lt;strong&gt;Tux&lt;/strong&gt; ha un duplice significato:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;È l&#39;acronimo di &lt;strong&gt;(T)orvalds (U)ni(X)&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;È un gioco di parole con &lt;strong&gt;tuxedo&lt;/strong&gt; (lo smoking), per via del piumaggio che lo fa sembrare vestito per una serata di gala.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Resiliente e un po&#39; buffo, Tux rappresenta perfettamente lo spirito informale e libero di Linux.&lt;/p&gt;
&lt;h2&gt;E il nome invece?&lt;/h2&gt;
&lt;p&gt;Il nome fu scelto da &lt;a href=&quot;https://it.wikipedia.org/wiki/Ari_Lemmke&quot;&gt;Ari Lemmke&lt;/a&gt;, l&#39;amministratore che nel 1991 rese disponibile il sistema sul web.&lt;/p&gt;
&lt;p&gt;Torvalds aveva inizialmente chiamato il progetto &lt;strong&gt;Freax&lt;/strong&gt;, una combinazione tra &lt;em&gt;free&lt;/em&gt;, &lt;em&gt;freak&lt;/em&gt; e &lt;em&gt;x&lt;/em&gt; per indicare le caratteristiche Unix-like. Lemmke ignorò questa scelta e preferì il nome Linux, che Torvalds aveva invece scartato ritenendolo troppo egocentrico.&lt;/p&gt;
&lt;p&gt;Lemmke creò una &lt;strong&gt;directory&lt;/strong&gt; su un server FTP chiamata &lt;code&gt;pub/OS/Linux&lt;/code&gt; per ospitare i file del nuovo sistema.
Poiché i file venivano scaricati da questa directory, il nome &lt;strong&gt;Linux&lt;/strong&gt; (derivato da &lt;strong&gt;(LIN)us (U)ni(X)&lt;/strong&gt;) divenne rapidamente lo standard di fatto.
Torvalds accettò il nome, sebbene in seguito il progetto fosse spesso identificato come &lt;strong&gt;GNU/Linux&lt;/strong&gt; per riconoscere il contributo del sistema GNU.&lt;/p&gt;
&lt;p&gt;Oltre a battezzare il kernel, Lemmke fondò il &lt;a href=&quot;https://groups.google.com/g/comp.os.linux&quot;&gt;newsgroup&lt;/a&gt; nel &lt;strong&gt;1992&lt;/strong&gt; e contribuì in modo significativo alla diffusione e allo sviluppo iniziale di Linux.&lt;/p&gt;
&lt;hr&gt;
&lt;h2&gt;Architettura dell&#39;ecosistema GNU/Linux&lt;/h2&gt;
&lt;p&gt;Passando al lato tecnico, l&#39;infrastruttura si divide in due ambiti operativi principali che formano il sistema operativo:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Kernel (Linux)&lt;/strong&gt;: Gestisce l&#39;astrazione dell&#39;hardware (&lt;em&gt;nascondendo la complessità dei circuiti fisici per farli vedere ai programmi come semplici risorse da usare&lt;/em&gt;), lo scheduling dei processi (&lt;em&gt;decidendo a quale programma in esecuzione dare la precedenza nell&#39;uso del processore, come un vigile urbano&lt;/em&gt;), la gestione della memoria e i file system. Funziona come interfaccia tra il software e il processore/periferiche.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Userland (GNU)&lt;/strong&gt;: Suite di strumenti essenziali per rendere operativo il sistema. Include la libreria C (&lt;em&gt;glibc - insiemi di codice condiviso pronti all&#39;uso&lt;/em&gt;), il compilatore (&lt;em&gt;GCC - un traduttore dal codice scritto dai programmatori al codice macchina comprensibile dal computer&lt;/em&gt;), le utility core (coreutils) e la shell.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Senza lo stack software GNU, il kernel Linux non fornirebbe un ambiente di runtime (&lt;em&gt;l&#39;infrastruttura di base necessaria per far funzionare i programmi&lt;/em&gt;) completo; simmetricamente, gli strumenti GNU necessitano di un kernel per eseguire le syscall (&lt;em&gt;chiamate di sistema che i programmi fanno al kernel per usare l&#39;hardware, come ad esempio chiedere il permesso di salvare un file sul disco&lt;/em&gt;).&lt;/p&gt;
&lt;h2&gt;Gerarchia dello stack software e hardware&lt;/h2&gt;
&lt;p&gt;Il &lt;strong&gt;flusso di esecuzione&lt;/strong&gt; può essere rappresentato come una pila di livelli indipendenti:&lt;/p&gt;
  &lt;div class=&quot;accordion mb-3&quot; id=&quot;stackSoftware&quot;&gt;
    &lt;div class=&quot;accordion-item border border-primary rounded-3 mb-2 shadow-sm&quot;&gt;
      &lt;h2 class=&quot;accordion-header&quot;&gt;
        &lt;button class=&quot;accordion-button bg-primary-subtle&quot; type=&quot;button&quot; data-bs-toggle=&quot;collapse&quot; data-bs-target=&quot;#item1&quot;&gt;
          Applicazione utente
        &lt;/button&gt;
      &lt;/h2&gt;
      &lt;div id=&quot;item1&quot; class=&quot;accordion-collapse collapse show&quot; data-bs-parent=&quot;#stackSoftware&quot;&gt;
        &lt;div class=&quot;accordion-body&quot;&gt;
          Browser, editor di testo, tool di sviluppo. Comunicano con il sistema tramite le API (&lt;i&gt;ponti di comunicazione che permettono a software diversi di scambiarsi informazioni in modo standardizzato&lt;/i&gt;) fornite dal software di base o dai framework grafici.
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class=&quot;accordion-item border border-primary rounded-3 mb-2 shadow-sm&quot;&gt;
      &lt;h2 class=&quot;accordion-header&quot;&gt;
        &lt;button class=&quot;accordion-button collapsed bg-primary-subtle&quot; type=&quot;button&quot; data-bs-toggle=&quot;collapse&quot; data-bs-target=&quot;#item2&quot;&gt;
          Interfaccia grafica (GUI / Display Server)
        &lt;/button&gt;
      &lt;/h2&gt;
      &lt;div id=&quot;item2&quot; class=&quot;accordion-collapse collapse&quot; data-bs-parent=&quot;#stackSoftware&quot;&gt;
        &lt;div class=&quot;accordion-body&quot;&gt;
          &lt;strong&gt;Wayland&lt;/strong&gt; o &lt;strong&gt;X11&lt;/strong&gt; (&lt;i&gt;protocolli di comunicazione che definiscono le regole per far interagire il compositore (il server grafico) con le applicazioni (client)&lt;/i&gt;), insieme ai &lt;strong&gt;Desktop Environment&lt;/strong&gt; (GNOME, KDE). Gestiscono l&#39;output visuale e l&#39;input utente.
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class=&quot;accordion-item border border-primary rounded-3 mb-2 shadow-sm&quot;&gt;
      &lt;h2 class=&quot;accordion-header&quot;&gt;
        &lt;button class=&quot;accordion-button collapsed bg-primary-subtle&quot; type=&quot;button&quot; data-bs-toggle=&quot;collapse&quot; data-bs-target=&quot;#item3&quot;&gt;
          Software di base (Userland GNU)
        &lt;/button&gt;
      &lt;/h2&gt;
      &lt;div id=&quot;item3&quot; class=&quot;accordion-collapse collapse&quot; data-bs-parent=&quot;#stackSoftware&quot;&gt;
        &lt;div class=&quot;accordion-body&quot;&gt;
          La shell (&lt;strong&gt;Bash&lt;/strong&gt;), le librerie di sistema (&lt;strong&gt;glibc&lt;/strong&gt;) e le utility core. Forniscono l&#39;ambiente di runtime necessario alle applicazioni.
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class=&quot;accordion-item border border-primary rounded-3 mb-2 shadow-sm&quot;&gt;
      &lt;h2 class=&quot;accordion-header&quot;&gt;
        &lt;button class=&quot;accordion-button collapsed bg-primary-subtle&quot; type=&quot;button&quot; data-bs-toggle=&quot;collapse&quot; data-bs-target=&quot;#item4&quot;&gt;
          Linux (Kernel)
        &lt;/button&gt;
      &lt;/h2&gt;
      &lt;div id=&quot;item4&quot; class=&quot;accordion-collapse collapse&quot; data-bs-parent=&quot;#stackSoftware&quot;&gt;
        &lt;div class=&quot;accordion-body&quot;&gt;
          Il &lt;i&gt;vigile urbano&lt;/i&gt; delle risorse. Gestisce i driver, la memoria e i processi attraverso le system call.
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class=&quot;accordion-item border border-primary rounded-3 mb-2 shadow-sm&quot;&gt;
      &lt;h2 class=&quot;accordion-header&quot;&gt;
        &lt;button class=&quot;accordion-button collapsed bg-primary-subtle&quot; type=&quot;button&quot; data-bs-toggle=&quot;collapse&quot; data-bs-target=&quot;#item5&quot;&gt;
          Firmware (BIOS / UEFI)
        &lt;/button&gt;
      &lt;/h2&gt;
      &lt;div id=&quot;item5&quot; class=&quot;accordion-collapse collapse&quot; data-bs-parent=&quot;#stackSoftware&quot;&gt;
        &lt;div class=&quot;accordion-body&quot;&gt;
          Software di basso livello integrato nella scheda madre che inizializza l&#39;hardware e passa il controllo al bootloader.
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class=&quot;accordion-item border border-primary rounded-3 shadow-sm&quot;&gt;
      &lt;h2 class=&quot;accordion-header&quot;&gt;
        &lt;button class=&quot;accordion-button collapsed bg-primary-subtle&quot; type=&quot;button&quot; data-bs-toggle=&quot;collapse&quot; data-bs-target=&quot;#item6&quot;&gt;
          Hardware
        &lt;/button&gt;
      &lt;/h2&gt;
      &lt;div id=&quot;item6&quot; class=&quot;accordion-collapse collapse&quot; data-bs-parent=&quot;#stackSoftware&quot;&gt;
        &lt;div class=&quot;accordion-body&quot;&gt;
          Il piano fisico (CPU, RAM, Storage, etc.).
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;h2&gt;Protocolli grafici&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Wayland&lt;/strong&gt; e &lt;strong&gt;X11&lt;/strong&gt; sono protocolli grafici usati in Linux per gestire la comunicazione tra applicazioni e schermo. X11 è il sistema tradizionale, nato negli anni &#39;80, mentre Wayland è la sua evoluzione moderna, progettata per hardware e scenari attuali.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;X11 funziona come un server centrale&lt;/strong&gt;: le applicazioni gli inviano comandi di disegno (per esempio: &lt;em&gt;disegna un cerchio qui&lt;/em&gt;) e lui si occupa di renderizzarli. Questo modello permetteva l&#39;uso remoto delle app (&lt;em&gt;come eseguire un programma su un server e vederlo sul PC&lt;/em&gt;), ma introduce latenza e problemi di sicurezza.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Wayland semplifica il processo&lt;/strong&gt;: le applicazioni disegnano direttamente in un buffer (memoria grafica) e lo passano al compositore che si occupa di mostrare tutto sullo schermo. Il compositore gestisce anche finestre, input e sicurezza.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;La differenza chiave è nell&#39;&lt;strong&gt;architettura&lt;/strong&gt; e nella &lt;strong&gt;sicurezza&lt;/strong&gt;. In X11, ogni applicazione può spiare altre finestre, registrare tasti (&lt;strong&gt;keylogging&lt;/strong&gt;), catturare lo schermo senza permesso, al contrario di Wayland, dove ogni applicazione è isolata! Non può interagire con altre finestre senza esplicito consenso (per esempio se si vuole condivisione lo schermo).&lt;/p&gt;
&lt;h2&gt;Desktop Environment&lt;/h2&gt;
&lt;p&gt;Un &lt;strong&gt;Desktop Environment&lt;/strong&gt; (&lt;strong&gt;DE&lt;/strong&gt;) è un ecosistema di componenti software che lavorano in sinergia per fornire un&#39;interfaccia grafica coerente. A differenza di Windows o macOS, dove l&#39;interfaccia è accoppiata profondamente al kernel, in ambiente Linux il &lt;strong&gt;DE è un &lt;em&gt;plugin&lt;/em&gt; di alto livello&lt;/strong&gt;, quindi intercambiabile.&lt;/p&gt;
&lt;h3&gt;Architettura dello stack grafico&lt;/h3&gt;
&lt;p&gt;Per il &lt;strong&gt;rendering delle finestre&lt;/strong&gt;, il sistema attraversa tre strati principali:&lt;/p&gt;
&lt;div class=&quot;accordion mb-3&quot; id=&quot;stackSoftware&quot;&gt;
    &lt;div class=&quot;accordion-item border border-primary rounded-3 mb-2 shadow-sm&quot;&gt;
      &lt;h2 class=&quot;accordion-header&quot;&gt;
        &lt;button class=&quot;accordion-button bg-primary-subtle&quot; type=&quot;button&quot; data-bs-toggle=&quot;collapse&quot; data-bs-target=&quot;#item1&quot;&gt;
          Display Server (X11 o Wayland)
        &lt;/button&gt;
      &lt;/h2&gt;
      &lt;div id=&quot;item1&quot; class=&quot;accordion-collapse collapse&quot; data-bs-parent=&quot;#stackSoftware&quot;&gt;
        &lt;div class=&quot;accordion-body&quot;&gt;
          Protocollo di base per la comunicazione tra hardware (GPU, input) e client grafici (le applicazioni attualmente aperte e visibili sullo schermo). Gestisce i messaggi di I/O.
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class=&quot;accordion-item border border-primary rounded-3 mb-2 shadow-sm&quot;&gt;
      &lt;h2 class=&quot;accordion-header&quot;&gt;
        &lt;button class=&quot;accordion-button bg-primary-subtle&quot; type=&quot;button&quot; data-bs-toggle=&quot;collapse&quot; data-bs-target=&quot;#item2&quot;&gt;
          Window Manager (WM)
        &lt;/button&gt;
      &lt;/h2&gt;
      &lt;div id=&quot;item2&quot; class=&quot;accordion-collapse collapse&quot; data-bs-parent=&quot;#stackSoftware&quot;&gt;
        &lt;div class=&quot;accordion-body&quot;&gt;
          Software che controlla il posizionamento e l&#39;aspetto delle finestre (bordi, pulsanti, ridimensionamento). Esempi di WM possono essere Mutter (GNOME), KWin (KDE) o gestori stand-alone come i3.
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class=&quot;accordion-item border border-primary rounded-3 mb-2 shadow-sm&quot;&gt;
      &lt;h2 class=&quot;accordion-header&quot;&gt;
        &lt;button class=&quot;accordion-button bg-primary-subtle&quot; type=&quot;button&quot; data-bs-toggle=&quot;collapse&quot; data-bs-target=&quot;#item3&quot;&gt;
          Desktop Environment (DE)
        &lt;/button&gt;
      &lt;/h2&gt;
      &lt;div id=&quot;item3&quot; class=&quot;accordion-collapse collapse show&quot; data-bs-parent=&quot;#stackSoftware&quot;&gt;
        &lt;div class=&quot;accordion-body&quot;&gt;
          Pacchetto completo che include il WM e una suite di applicazioni (file manager, terminale, impostazioni) che condividono librerie grafiche e design logico.
          &lt;br&gt;
          I componenti fondamentali di un DE sono:
          &lt;ul&gt;
            &lt;li&gt;
              &lt;strong&gt;Pannello/Taskbar&lt;/strong&gt;: Gestione finestre e menu di avvio.
            &lt;/li&gt;
            &lt;li&gt;
              &lt;strong&gt;File manager&lt;/strong&gt;: Interfaccia per la navigazione del file system (es. Nautilus o Dolphin).
            &lt;/li&gt;
            &lt;li&gt;
              &lt;strong&gt;System settings&lt;/strong&gt;: Centro di controllo per hardware e reti (evita la modifica manuale dei file in /etc).
            &lt;/li&gt;
            &lt;li&gt;
              &lt;strong&gt;Display manager&lt;/strong&gt;: Gestisce il login grafico (es. GDM o SDDM).
            &lt;/li&gt;
          &lt;/ul&gt;
          &lt;div class=&quot;mb-3&quot;&gt;
            &lt;strong&gt;Principali desktop environment e librerie&lt;/strong&gt;
          &lt;/div&gt;
          &lt;!--TABELLA--&gt;
          &lt;div class=&quot;container mb-3&quot;&gt;
            &lt;div class=&quot;table-responsive shadow-sm rounded&quot;&gt;
              &lt;table class=&quot;table table-bordered table-striped table-hover align-middle mb-0&quot;&gt;
                &lt;thead class=&quot;table-dark&quot;&gt;
                  &lt;tr&gt;
                    &lt;th&gt;DE&lt;/th&gt;
                    &lt;th&gt;Libreria&lt;/th&gt;
                    &lt;th&gt;Filosofia&lt;/th&gt;
                  &lt;/tr&gt;
                &lt;/thead&gt;
                &lt;tbody&gt;
                  &lt;tr&gt;
                    &lt;td class=&quot;fw-semibold&quot;&gt;GNOME&lt;/td&gt;
                    &lt;td&gt;GTK&lt;/td&gt;
                    &lt;td&gt;
                      Minimalismo, workflow orientato alla produttività e astrazione
                    &lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                    &lt;td class=&quot;fw-semibold&quot;&gt;KDE Plasma&lt;/td&gt;
                    &lt;td&gt;Qt&lt;/td&gt;
                    &lt;td&gt;Estrema personalizzazione e modularità&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                    &lt;td class=&quot;fw-semibold&quot;&gt;XFCE&lt;/td&gt;
                    &lt;td&gt;GTK&lt;/td&gt;
                    &lt;td&gt;Leggerezza e stabilità per hardware datato o server&lt;/td&gt;
                  &lt;/tr&gt;
                  &lt;tr&gt;
                    &lt;td class=&quot;fw-semibold&quot;&gt;Pantheon&lt;/td&gt;
                    &lt;td&gt;GTK&lt;/td&gt;
                    &lt;td&gt;Estetica curata e semplicità (derivato da &lt;a href=&quot;https://elementary.io/it/&quot;&gt;elementaryOS&lt;/a&gt;)&lt;/td&gt;
                  &lt;/tr&gt;
                &lt;/tbody&gt;
              &lt;/table&gt;
            &lt;/div&gt;
          &lt;/div&gt;
          &lt;!--FINE TABELLA--&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;h2&gt;Modularità e Server Headless&lt;/h2&gt;
&lt;p&gt;L&#39;assenza di interfaccia grafica definisce un sistema &lt;strong&gt;Headless&lt;/strong&gt;. Questa configurazione è standard in ambito server per ottimizzare:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Superficie di attacco&lt;/strong&gt;: Meno software installato riduce le vulnerabilità potenziali.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Risorse&lt;/strong&gt;: Risparmio di RAM (un DE può occupare da 500MB a 1.5GB in idle, ovvero a riposo e senza eseguire operazioni attive).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dipendenze&lt;/strong&gt;: Sistema più snello e facile da manutenere.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Il ruolo del Bootloader&lt;/h2&gt;
&lt;p&gt;Il &lt;strong&gt;Bootloader&lt;/strong&gt; è il primo software eseguito dal &lt;strong&gt;firmware&lt;/strong&gt; (&lt;strong&gt;UEFI / BIOS&lt;/strong&gt;). Individua il kernel sul disco, lo carica in RAM e gli cede il controllo del processore. Il più diffuso è GRUB2, che supporta il multi-boot e permette di iniettare parametri al kernel (es. recovery mode) prima dell&#39;avvio.&lt;/p&gt;
&lt;hr&gt;
&lt;h2&gt;Proprietà intellettuale (PI): Copyright e Copyleft&lt;/h2&gt;
&lt;p&gt;La proprietà intellettuale è lo &lt;strong&gt;strumento giuridico&lt;/strong&gt; che tutela i frutti dell&#39;inventiva umana. Nel software, essa garantisce che l&#39;autore possa decidere come la sua opera debba essere utilizzata, distribuita e modificata.&lt;/p&gt;
&lt;p&gt;È fondamentale perché senza la protezione della PI, un programmatore o un&#39;azienda non avrebbero alcun controllo sulle proprie creazioni. Chiunque potrebbe appropriarsi del lavoro altrui, spacciandolo per proprio o traendone profitto senza autorizzazione. Grazie alla PI si &lt;strong&gt;riconosce il merito&lt;/strong&gt; all&#39;autore garantendo che sia sempre citato (&lt;em&gt;diritto di paternità&lt;/em&gt;) e si &lt;strong&gt;incentiva l&#39;innovazione&lt;/strong&gt; permettendo a chi investe tempo e risorse di decidere il modello di distribuzione (vendita o condivisione gratuita).&lt;/p&gt;
&lt;p&gt;Non rispettare la PI comporta dei seri rischi, ignorare le licenze può portare a &lt;strong&gt;sanzioni legali e pecuniarie&lt;/strong&gt; in quanto violare il copyright può portare a cause civili estremamente costose, &lt;strong&gt;rimozione del software&lt;/strong&gt;, ovvero piattaforme di distribuzione come GitHub possono rimuovere il codice se non è in regola e &lt;strong&gt;danni alla reputazione&lt;/strong&gt; che per un professionista è un segnale di scarsa affidabilità tecnica e legale.&lt;/p&gt;
&lt;p&gt;Prima di parlare di licenze facciamo un piccolo chiarimento: quando parliamo di &lt;strong&gt;software libero&lt;/strong&gt; non intendiamo &lt;strong&gt;software di dominio pubblico&lt;/strong&gt;. È un errore comune pensare che, poiché il software è &lt;em&gt;libero&lt;/em&gt;, lo si possa manipolare a piacimento senza limiti. In realtà, il software libero è un software con una licenza specifica. Se un autore rilascia un codice sotto &lt;a href=&quot;https://www.gnu.org/licenses/gpl-3.0.html&quot;&gt;licenza GPL&lt;/a&gt; e tu lo modifichi per creare un software privato e chiuso, stai violando la legge. La &lt;em&gt;libertà&lt;/em&gt; del software libero è spesso vincolata all&#39;obbligo di mantenere quella libertà anche per chi verrà dopo di te (&lt;em&gt;principio della reciprocità&lt;/em&gt;).&lt;/p&gt;
&lt;h2&gt;Le licenze Creative Commons (CC)&lt;/h2&gt;
&lt;p&gt;Si tratta di licenze &lt;em&gt;modulari&lt;/em&gt; nate per colmare il divario tra &lt;strong&gt;copyright tradizionale&lt;/strong&gt; (tutti i diritti riservati) e il &lt;strong&gt;pubblico dominio&lt;/strong&gt; (nessun diritto riservato).&lt;/p&gt;
&lt;p&gt;Le CC permettono all&#39;autore di dire al mondo: &lt;em&gt;Puoi usare la mia opera, ma a queste condizioni&lt;/em&gt;, un po&#39; come usare dei mattoncini LEGO: l&#39;autore sceglie quali vincoli applicare per comporre la propria licenza personalizzata.&lt;/p&gt;
&lt;p&gt;Queste licenze non sono un blocco unico, ma una combinazione di &lt;strong&gt;quattro clausole fondamentali&lt;/strong&gt; che definiscono i diritti dell&#39;utente:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;BY (Attribuzione)&lt;/strong&gt;: È il modulo base presente in quasi tutte le licenze. Dice: &lt;em&gt;Puoi usare l&#39;opera, ma devi dire chi è l&#39;autore&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;SA (Condividi allo stesso modo)&lt;/strong&gt;: È la clausola della &lt;em&gt;continuità&lt;/em&gt;. Se modifichi l&#39;opera, devi distribuire il risultato con la stessa identica licenza dell&#39;originale.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;NC (Non Commerciale)&lt;/strong&gt;: Specifica che non puoi usare l&#39;opera per guadagnare soldi (per esempio, non puoi stampare un manuale CC-NC e rivenderlo).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;ND (No Opere Derivate)&lt;/strong&gt;: Ti permette di ridistribuire l&#39;opera così com&#39;è, ma ti vieta di modificarla, tagliarla o rimescolarla.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Combinando questi simboli, si ottengono le diverse &lt;strong&gt;tipologie di licenze&lt;/strong&gt; che ora andremo ad analizzare nel dettaglio.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;CC0&lt;/strong&gt;: Rinuncia volontaria al copyright per quanto possibile per legge; l&#39;opera viene resa quanto più possibile assimilabile al pubblico dominio. Nessuna attribuzione obbligatoria, massima libertà di riuso e integrazione in opere commerciali o derivate. È lo strumento usato quando l&#39;autore vuole eliminare ogni vincolo. &lt;em&gt;Nota: in alcuni ordinamenti, come quello italiano, il diritto morale d&#39;autore è inalienabile per legge, quindi CC0 non equivale in modo assoluto al pubblico dominio.&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;CC BY&lt;/strong&gt;: Licenza più permissiva che richiede solo l&#39;attribuzione. Chiunque può copiare, distribuire, modificare e usare commercialmente l&#39;opera purché citi l&#39;autore e mantenga chiari i riferimenti alla licenza. È ideale per massimizzare la diffusione mantenendo il riconoscimento dell&#39;autore.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;CC BY-SA&lt;/strong&gt;: Come CC BY, ma con obbligo che le opere derivate siano rilasciate con la stessa licenza. Crea una catena di condivisione aperta: chi modifica contribuisce alla stessa comunità di licenze libere. È molto usata in progetti collaborativi e risorse educative aperte.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;CC BY-NC&lt;/strong&gt;: Permette modifiche e condivisione ma vieta l&#39;uso commerciale. Implicazioni pratiche: utile per autori che vogliono consentire remix e diffusione non commerciale ma riservare la possibilità di sfruttamento economico a trattative separate. &lt;strong&gt;Attenzione&lt;/strong&gt;: la definizione di &lt;em&gt;commerciale&lt;/em&gt; può essere ambigua e talvolta richiede valutazione caso per caso.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;CC BY-ND&lt;/strong&gt;: Consente la ridistribuzione solo nella forma originale; non sono permesse opere derivate. È adatta a chi vuole massima diffusione senza alterazioni. Limita però la possibilità di integrazione in progetti che richiedono adattamenti.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;CC BY-NC-SA&lt;/strong&gt;: Combina &lt;strong&gt;Non Commerciale&lt;/strong&gt; e &lt;strong&gt;ShareAlike&lt;/strong&gt; con l&#39;obbligo di attribuzione. Permette &lt;em&gt;remix&lt;/em&gt; non commerciali ma impone che i derivati mantengano la stessa licenza, preservando la natura non commerciale e aperta del materiale. È una scelta conservativa per comunità che vogliono proteggere l&#39;uso non commerciale mantenendo apertura.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;CC BY-NC-ND (la più restrittiva tra le licenze CC standard)&lt;/strong&gt;: attribuzione obbligatoria, nessun uso commerciale e nessuna opera derivata. Consente solo la condivisione dell&#39;opera così com&#39;è per scopi non commerciali; è utile quando l&#39;autore vuole massima protezione pur consentendo la diffusione non commerciale.&lt;/li&gt;
&lt;/ol&gt;
&lt;hr&gt;
&lt;h2&gt;Tipologie di distribuzioni (distro)&lt;/h2&gt;
&lt;p&gt;Come abbiamo visto, una &lt;strong&gt;distribuzione&lt;/strong&gt; è un assemblaggio curato di kernel, software di base e interfaccia grafica. Tuttavia, ciò che definisce davvero l&#39;identità di una distro è la sua gestione dei pacchetti (il sistema con cui installi e aggiorni i programmi) e la sua politica di rilascio (quanto spesso e con quale filosofia vengono pubblicati gli aggiornamenti).&lt;/p&gt;
&lt;p&gt;In base alla politica di aggiornamento
Esistono &lt;strong&gt;due filosofie dominanti&lt;/strong&gt; che determinano l&#39;esperienza dell&#39;utente:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Fixed/Point Release (es. Debian, Ubuntu, Linux Mint)&lt;/strong&gt;: È il modello simile a quello di Windows o macOS. Il sistema operativo viene rilasciato in una versione specifica (es. Ubuntu 24.04). Gli aggiornamenti durante la vita di quella versione riguardano principalmente la sicurezza. Per avere nuove funzionalità massicce, bisogna aspettare la versione successiva.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Vantaggio&lt;/strong&gt;: Massima stabilità. Il software non cambia sotto.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Svantaggio&lt;/strong&gt;: Dopo un po&#39; di tempo, le versioni dei programmi possono diventare datate.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Rolling Release (es. Arch Linux, openSUSE Tumbleweed)&lt;/strong&gt;: Il sistema si aggiorna continuamente, pezzo dopo pezzo. Se oggi esce una nuova versione di un programma, domani è già pronta per l&#39;installazione.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Vantaggio&lt;/strong&gt;: Software sempre all&#39;ultimo grido senza dover mai reinstallare tutto il sistema.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Svantaggio&lt;/strong&gt;: Rischio maggiore. Un aggiornamento troppo recente potrebbe contenere bug non ancora scoperti o creare conflitti con altri componenti.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Famiglie di distribuzioni&lt;/h3&gt;
&lt;p&gt;Le distribuzioni si raggruppano spesso in famiglie basate sul formato dei file di installazione che utilizzano:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href=&quot;https://www.debian.org/index.it.html&quot;&gt;Debian (formato .deb)&lt;/a&gt;&lt;/strong&gt;: Include Ubuntu e tutte le sue derivate. Utilizzano il comando &lt;strong&gt;apt&lt;/strong&gt;. È la famiglia più diffusa al mondo, con la più vasta documentazione disponibile.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href=&quot;https://www.redhat.com/it&quot;&gt;Red Hat (formato .rpm)&lt;/a&gt;&lt;/strong&gt;: Include Fedora e RHEL. È lo standard nell&#39;industria e nelle grandi aziende. Utilizzano il comando &lt;strong&gt;dnf&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href=&quot;https://archlinux.org&quot;&gt;Arch (formato .pkg.tar.zst)&lt;/a&gt;&lt;/strong&gt;: Nota per la sua filosofia &lt;em&gt;KISS&lt;/em&gt; (Keep It Simple, Stupid). L&#39;utente costruisce il sistema da zero, installando solo ciò che serve. Utilizza il comando &lt;strong&gt;pacman&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Distribuzioni specialistiche&lt;/h3&gt;
&lt;p&gt;Esistono distro nate per scopi talmente specifici da alterare lo stack software che abbiamo studiato:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Per la Sicurezza (es. &lt;a href=&quot;https://parrotsec.org&quot;&gt;ParrotOS&lt;/a&gt;)&lt;/strong&gt;: Contengono centinaia di strumenti già pronti per il Penetration Testing (test di vulnerabilità delle reti).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Per la Privacy (es. &lt;a href=&quot;https://tails.net&quot;&gt;Tails&lt;/a&gt;)&lt;/strong&gt;: Progettate per non lasciare tracce sul computer. Tutto il traffico internet passa attraverso la rete Tor e, una volta spento il PC, ogni dato viene cancellato dalla RAM.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Per l&#39;Educazione (es. &lt;a href=&quot;https://endlessglobal.com&quot;&gt;Endless OS&lt;/a&gt;)&lt;/strong&gt;: Pensate per zone con scarsa connettività internet, includono intere enciclopedie e corsi offline.&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;p-2 border border-info rounded-3 bg-primary bg-info-subtle shadow-sm&quot;&gt;&lt;strong&gt;Piccola nota editoriale&lt;/strong&gt;: nei prossimi articoli parlerò di &lt;i&gt;Linux&lt;/i&gt; intendendo sempre il sistema completo GNU/Linux. Stallman probabilmente non sarebbe d&#39;accordo, ma prometto che lo citiamo correttamente ogni volta che conta davvero.&lt;/div&gt;]]></content>
    
    
      
    
      
    <category term="linux"/>
      
    
      
    <category term="sistemi operativi"/>
      
    
      
    <category term="licenze"/>
      
    
  </entry>
  
  <entry>
    <title>L&#39;algoritmo di Luhn</title>
    <link href="https://dino-996.github.io/blog/lalgoritmo-di-luhn/" rel="alternate" type="text/html"/>
    <id>https://dino-996.github.io/blog/lalgoritmo-di-luhn/</id>
    <published>2026-03-08T00:00:00.000Z</published>
    <updated>2026-03-08T00:00:00.000Z</updated>
    
    <summary>L’algoritmo di Luhn è un metodo matematico sviluppato nel 1954 dal ricercatore IBM Hans Peter Luhn per verificare la validità di numeri identificativi attraverso una cifra di controllo. Ancora oggi è utilizzato in moltissimi sistemi informatici, soprattutto per la validazione dei numeri di carte di credito, IMEI dei dispositivi mobili e altri identificatori strutturati.</summary>
    
    
    <content type="html"><![CDATA[&lt;h1&gt;Introduzione&lt;/h1&gt;
&lt;p&gt;L’algoritmo di Luhn è uno dei metodi più diffusi per verificare la validità di numeri identificativi come quelli delle carte di credito. Ideato nel 1954 dal ricercatore IBM Hans Peter Luhn, utilizza una semplice regola matematica per individuare errori di digitazione e garantire la coerenza dei dati prima della loro elaborazione.&lt;/p&gt;
&lt;h2&gt;Cos&#39;è e a cosa serve?&lt;/h2&gt;
&lt;p&gt;L&#39;algoritmo di Luhn è un &lt;strong&gt;semplice sistema di controllo cifre&lt;/strong&gt; inventato negli anni &#39;50 da &lt;a href=&quot;https://en.wikipedia.org/wiki/Hans_Peter_Luhn&quot;&gt;&lt;strong&gt;Hans Peter Luhn&lt;/strong&gt;&lt;/a&gt;, ingegnere &lt;a href=&quot;https://www.ibm.com/it-it&quot;&gt;IBM&lt;/a&gt;. Lo trovi ovunque: numeri di carte di credito, codici fiscali, IBAN, codici seriali. Il suo scopo è rispondere a una domanda pratica:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&amp;quot;Questo numero che ho digitato ha senso, oppure ho fatto un errore di battitura?&amp;quot;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;strong&gt;Non protegge da frodi&lt;/strong&gt;. È solo un modo veloce per stanare gli errori.&lt;/p&gt;
&lt;h2&gt;Come funziona, passo per passo&lt;/h2&gt;
&lt;p&gt;Sia $N = n_1, n_2, &#92;dots, n_{11}$ una matrice righa a 11 cifre, con $n_1$ la &lt;strong&gt;prima cifra a sinistra&lt;/strong&gt;.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Identifica le cifre in &lt;strong&gt;posizione dispari&lt;/strong&gt; ($n_2,n_4,n_6,...$)&lt;/li&gt;
&lt;li&gt;Raddoppia ciascuna cifra: $d_i = 2 &#92;cdot n_i$&lt;/li&gt;
&lt;li&gt;Se $d_i &amp;gt; 9$, somma le due cifre:
$d_i = &#92;lfloor d_i / 10 &#92;rfloor + (d_i &#92;bmod 10)$&lt;/li&gt;
&lt;li&gt;Le cifre in &lt;strong&gt;posizione pari&lt;/strong&gt; restano invariate.&lt;/li&gt;
&lt;li&gt;Somma tutte le cifre elaborate: $S = &#92;sum_{i=1}^{11} d_i$&lt;/li&gt;
&lt;li&gt;Se $S &#92;bmod 10 = 0$, PAN valido secondo l&#39;algoritmo di Luhn&lt;/li&gt;
&lt;/ol&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Nota sugli indici:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;In matematica le posizioni partono da 1&lt;/li&gt;
&lt;li&gt;In Java gli &lt;strong&gt;array partono da 0&lt;/strong&gt;, quindi la seconda cifra (posizione 2) corrisponde all’indice 1 dell’array.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
&lt;h2&gt;Quando si usa?&lt;/h2&gt;
&lt;p&gt;Luhn è progettato per rilevare i due errori più comuni:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Sbagliare una cifra&lt;/strong&gt;, per esempio scrivere 4 invece di 5&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Invertire due cifre adiacenti&lt;/strong&gt;, per esempio scrivere 63 invece di 36&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Nella pratica quotidiana, queste due casistiche coprono la stragrande maggioranza degli sbagli umani. Luhn &lt;strong&gt;non è un sistema di sicurezza serio&lt;/strong&gt;: chi vuole può creare numeri validi rispettando la regola. Il suo scopo è &lt;strong&gt;essere veloce e affidabile&lt;/strong&gt; per evitare errori di battitura, non proteggere soldi o dati.&lt;/p&gt;
&lt;p&gt;Negli anni ’50, quando i computer erano grandi quanto una stanza, questa era una priorità enorme. Al giorno d&#39;oggi questo algoritmo è comunque utile, anche se non essenziale come allora.&lt;/p&gt;
&lt;h2&gt;In Java&lt;/h2&gt;
&lt;pre class=&quot;language-java&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token import&quot;&gt;&lt;span class=&quot;token namespace&quot;&gt;org&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;apache&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;logging&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;log4j&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;LogManager&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token import&quot;&gt;&lt;span class=&quot;token namespace&quot;&gt;org&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;apache&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;logging&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;log4j&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Logger&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token import&quot;&gt;&lt;span class=&quot;token namespace&quot;&gt;java&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;util&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;ArrayList&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token import&quot;&gt;&lt;span class=&quot;token namespace&quot;&gt;java&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;util&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Arrays&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token import&quot;&gt;&lt;span class=&quot;token namespace&quot;&gt;java&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;util&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;List&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;LuhnValidator&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Logger&lt;/span&gt; logger &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;LogManager&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;getLogger&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;LuhnValidator&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; args&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

        &lt;span class=&quot;token class-name&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; a &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Arrays&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;asList&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token class-name&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; b &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ArrayList&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;a&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            logger&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Il PAN deve essere composto da 11 cifre!&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; n &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; n &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; n&lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            
            &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; valoreCorrente &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; a&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

            &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;valoreCorrente &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
                logger&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Il PAN non può contenere valori negativi&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

            &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;n &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Indici dispari&lt;/span&gt;
                &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; p &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; valoreCorrente &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

                &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;p &lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
                    b&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;p &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;p &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Sommo la prima e l&#39;ultima cifra del valore&lt;/span&gt;
                &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
                    b&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;p&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
                &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
                b&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;valoreCorrente&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; somma &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; j &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; j &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; j&lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            somma &lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt; b&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;j&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;somma &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            logger&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Il PAN soddisfa l&#39;algoritmo di Luhn&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            logger&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Il PAN NON soddisfa l&#39;algoritmo di Luhn&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
]]></content>
    
    
      
    
      
    <category term="algoritmi"/>
      
    
      
    <category term="java"/>
      
    
  </entry>
  
  <entry>
    <title>Guida completa a PowerShell</title>
    <link href="https://dino-996.github.io/blog/guida-completa-a-powershell/" rel="alternate" type="text/html"/>
    <id>https://dino-996.github.io/blog/guida-completa-a-powershell/</id>
    <published>2026-01-01T00:00:00.000Z</published>
    <updated>2026-01-01T00:00:00.000Z</updated>
    
    <summary>Una guida teorica e pratica per iniziare a utilizzare powershell in modo efficace e rapido.</summary>
    
    
    <content type="html"><![CDATA[&lt;h1&gt;Introduzione&lt;/h1&gt;
&lt;p&gt;In questo guida per tutti troverete spiegazioni chiare, esempi pratici e suggerimenti per l&#39;autoapprendimento.&lt;/p&gt;
&lt;h2&gt;Che cos&#39;è PowerShell?&lt;/h2&gt;
&lt;p&gt;​PowerShell è una​ &lt;strong&gt;​shell di scripting&lt;/strong&gt;​ ​e un ​​&lt;strong&gt;linguaggio ​​di scripting&lt;/strong&gt; ​​sviluppato da Microsoft. A​ ​differenza delle tradizionali shell a riga di comando che si basano sul testo PowerShell è​ &lt;strong&gt;​orientato agli oggetti&lt;/strong&gt;​. Ciò significa che i comandi​​ (chiamati &lt;strong&gt;​​cmdlet&lt;/strong&gt;​) manipolano &lt;strong&gt;​​oggetti​ ​strutturati&lt;/strong&gt;, non solo stringhe di testo. Questa caratteristica lo rende estremamente potente​ per l&#39;automazione, la gestione di sistemi e la configurazione.​&lt;/p&gt;
&lt;h3&gt;Windows PowerShell vs PowerShell 7+&lt;/h3&gt;
&lt;blockquote&gt;
&lt;p&gt;Distinzione fondamentale che molti ignorano.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;div class=&quot;container mb-3&quot;&gt;
  &lt;div class=&quot;table-responsive shadow-sm rounded&quot;&gt;
    &lt;table class=&quot;table table-bordered table-striped table-hover align-middle mb-0&quot;&gt;
      &lt;thead class=&quot;table-dark&quot;&gt;
        &lt;tr&gt;
          &lt;th&gt;Caratteristica&lt;/th&gt;
          &lt;th&gt;Windows PowerShell 5.1&lt;/th&gt;
          &lt;th&gt;
              PowerShell 7+
            &lt;/th&gt;
        &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td class=&quot;fw-semibold&quot;&gt;Stato&lt;/td&gt;
          &lt;td&gt;
            &lt;span class=&quot;badge text-bg-warning&quot;&gt;Legacy&lt;/span&gt;
          &lt;/td&gt;
          &lt;td&gt;
            &lt;span class=&quot;badge text-bg-success&quot;&gt;Attivo&lt;/span&gt;
          &lt;/td&gt;
          &lt;/tr&gt;
        &lt;tr&gt;
        &lt;td class=&quot;fw-semibold&quot;&gt;Open Source&lt;/td&gt;
          &lt;td&gt;&lt;span class=&quot;text-danger fw-bold&quot;&gt;✕&lt;/span&gt;&lt;/td&gt;
          &lt;td&gt;GitHub
        &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
          &lt;td class=&quot;fw-semibold&quot;&gt;Cross-platform&lt;/td&gt;
          &lt;td&gt;Solo Windows&lt;/td&gt;
          &lt;td&gt;Windows · Linux · macOS&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
          &lt;td class=&quot;fw-semibold&quot;&gt;Eseguibile&lt;/td&gt;
          &lt;td&gt;&lt;code&gt;powershell.exe&lt;/code&gt;&lt;/td&gt;     &lt;td&gt;&lt;code&gt;pwsh&lt;/code&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
          &lt;td class=&quot;fw-semibold&quot;&gt;Runtime&lt;/td&gt;
          &lt;td&gt;.NET Framework&lt;/td&gt;
          &lt;td&gt;.NET 6 / 7 / 8&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
          &lt;td class=&quot;fw-semibold&quot;&gt;Incluso in Windows&lt;/td&gt;
          &lt;td&gt;Win 10 / 11&lt;/td&gt;
          &lt;td&gt;Da installare separatamente&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Raccomandazione:&lt;/strong&gt; Usa sempre &lt;strong&gt;PowerShell 7+&lt;/strong&gt; (&lt;code&gt;pwsh&lt;/code&gt;) per i nuovi progetti.&lt;/p&gt;
&lt;h3&gt;Perché usare PowerShell&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Automazione:&lt;/strong&gt; Elimina attività manuali ripetitive — gestione file, provisioning utenti, deployment applicazioni.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Gestione dei sistemi:&lt;/strong&gt; Strumento fondamentale per l&#39;amministrazione Windows, usato sempre più anche su Linux e macOS.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Efficienza:&lt;/strong&gt; Operazioni complesse in poche righe grazie alla pipeline orientata agli oggetti.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Standard enterprise:&lt;/strong&gt; È lo strumento ufficiale per la gestione di &lt;strong&gt;Microsoft Azure&lt;/strong&gt; e dell&#39;ecosistema Microsoft 365.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Differenze tra PowerShell, CMD e Bash&lt;/h3&gt;
&lt;div class=&quot;container mb-3&quot;&gt;
  &lt;div class=&quot;table-responsive shadow-sm rounded&quot;&gt;
    &lt;table class=&quot;table table-bordered table-striped table-hover align-middle mb-0&quot;&gt;
      &lt;thead class=&quot;table-dark&quot;&gt;
        &lt;tr&gt;
          &lt;th&gt;Caratteristica&lt;/th&gt;
          &lt;th&gt;PowerShell&lt;/th&gt;
          &lt;th&gt;CMD&lt;/th&gt;
          &lt;th&gt;Bash&lt;/th&gt;
        &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td class=&quot;fw-semibold&quot;&gt;Orientamento&lt;/td&gt;
          &lt;td&gt;Oggetti .NET&lt;/td&gt;
          &lt;td&gt;Testo&lt;/td&gt;
          &lt;td&gt;Testo&lt;/td&gt;
          &lt;/tr&gt;
        &lt;tr&gt;
        &lt;td class=&quot;fw-semibold&quot;&gt;Linguaggio&lt;/td&gt;
          &lt;td&gt;Powershell (.NET)&lt;/td&gt;
          &lt;td&gt;Batch&lt;/td&gt;
          &lt;td&gt;Batch scripting&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
          &lt;td class=&quot;fw-semibold&quot;&gt;Output pipeline&lt;/td&gt;
          &lt;td&gt;Oggetti strutturati&lt;/td&gt;
          &lt;td&gt;Stringhe&lt;/td&gt;
          &lt;td&gt;Stringhe&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
          &lt;td class=&quot;fw-semibold&quot;&gt;Cross-platform&lt;/td&gt;
          &lt;td&gt;Win/Linux/MacOS&lt;/td&gt;
          &lt;td&gt;Win&lt;/td&gt;
          &lt;td&gt;Linux/MacOS&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
          &lt;td class=&quot;fw-semibold&quot;&gt;Potenza&lt;/td&gt;
          &lt;td&gt;Alta&lt;/td&gt;
          &lt;td&gt;Bassa&lt;/td&gt;
          &lt;td&gt;Alta&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
          &lt;td class=&quot;fw-semibold&quot;&gt;Curva di apprendimento&lt;/td&gt;
          &lt;td&gt;Media&lt;/td&gt;
          &lt;td&gt;Bassa&lt;/td&gt;
          &lt;td&gt;Media&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;h2&gt;Installazione e Configurazione&lt;/h2&gt;
&lt;h3&gt;Windows&lt;/h3&gt;
&lt;p&gt;Windows 10/11 include &lt;strong&gt;Windows PowerShell 5.1&lt;/strong&gt; (legacy). Per installare &lt;strong&gt;PowerShell 7+&lt;/strong&gt;:&lt;/p&gt;
&lt;pre class=&quot;language-powershell&quot;&gt;&lt;code class=&quot;language-powershell&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Metodo 1: Windows Package Manager (winget)&lt;/span&gt;
winget install &lt;span class=&quot;token operator&quot;&gt;--&lt;/span&gt;id Microsoft&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Powershell &lt;span class=&quot;token operator&quot;&gt;--&lt;/span&gt;source winget

&lt;span class=&quot;token comment&quot;&gt;# Metodo 2: Download diretto&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# https://github.com/PowerShell/PowerShell/releases&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Dopo l&#39;installazione, avvia PowerShell 7 con il comando &lt;code&gt;pwsh&lt;/code&gt; nel terminale.&lt;/p&gt;
&lt;h3&gt;macOS&lt;/h3&gt;
&lt;pre class=&quot;language-bash&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Installa Homebrew se non presente&lt;/span&gt;
/bin/bash &lt;span class=&quot;token parameter variable&quot;&gt;-c&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;&lt;span class=&quot;token variable&quot;&gt;&lt;span class=&quot;token variable&quot;&gt;$(&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;curl&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-fsSL&lt;/span&gt; https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh&lt;span class=&quot;token variable&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&quot;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Installa PowerShell&lt;/span&gt;
brew &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;--cask&lt;/span&gt; powershell

&lt;span class=&quot;token comment&quot;&gt;# Avvia PowerShell&lt;/span&gt;
pwsh&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Linux (Ubuntu/Debian)&lt;/h3&gt;
&lt;pre class=&quot;language-bash&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Importa la chiave Microsoft&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;curl&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-sSL&lt;/span&gt; https://packages.microsoft.com/keys/microsoft.asc &lt;span class=&quot;token operator&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;sudo&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;tee&lt;/span&gt; /etc/apt/trusted.gpg.d/microsoft.asc

&lt;span class=&quot;token comment&quot;&gt;# Aggiungi il repository&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;sudo&lt;/span&gt; add-apt-repository &lt;span class=&quot;token string&quot;&gt;&quot;deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-ubuntu-&lt;span class=&quot;token variable&quot;&gt;&lt;span class=&quot;token variable&quot;&gt;$(&lt;/span&gt;lsb_release &lt;span class=&quot;token parameter variable&quot;&gt;-rs&lt;/span&gt;&lt;span class=&quot;token variable&quot;&gt;)&lt;/span&gt;&lt;/span&gt;-prod &lt;span class=&quot;token variable&quot;&gt;&lt;span class=&quot;token variable&quot;&gt;$(&lt;/span&gt;lsb_release &lt;span class=&quot;token parameter variable&quot;&gt;-cs&lt;/span&gt;&lt;span class=&quot;token variable&quot;&gt;)&lt;/span&gt;&lt;/span&gt; main&quot;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Installa&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;sudo&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;apt&lt;/span&gt; update &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;sudo&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;apt&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;install&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-y&lt;/span&gt; powershell

&lt;span class=&quot;token comment&quot;&gt;# Avvia&lt;/span&gt;
pwsh&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Per altre distribuzioni (RHEL, Fedora, openSUSE), consulta la &lt;a href=&quot;https://learn.microsoft.com/powershell/scripting/install/installing-powershell&quot;&gt;documentazione ufficiale&lt;/a&gt;.&lt;/p&gt;
&lt;h3&gt;Editor consigliato: Visual Studio Code&lt;/h3&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;PowerShell ISE è ufficialmente deprecato.&lt;/strong&gt; Microsoft raccomanda &lt;strong&gt;Visual Studio Code&lt;/strong&gt; con l&#39;estensione PowerShell.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Installa l&#39;estensione direttamente da VS Code:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Ctrl+P → ext install ms-vscode.PowerShell
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;L&#39;estensione offre: IntelliSense, debugging integrato, esecuzione di selezioni di codice e integrazione con PSScriptAnalyzer.&lt;/p&gt;
&lt;h3&gt;Configurazione di Base&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Profilo PowerShell&lt;/strong&gt; — script eseguito automaticamente all&#39;avvio:&lt;/p&gt;
&lt;pre class=&quot;language-powershell&quot;&gt;&lt;code class=&quot;language-powershell&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Visualizza il percorso del profilo&lt;/span&gt;
&lt;span class=&quot;token variable&quot;&gt;$PROFILE&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Crea il file di profilo se non esiste&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;-not&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Test-Path&lt;/span&gt; &lt;span class=&quot;token variable&quot;&gt;$PROFILE&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;New-Item&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;ItemType File &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Path &lt;span class=&quot;token variable&quot;&gt;$PROFILE&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Force
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Apri il profilo in VS Code&lt;/span&gt;
code &lt;span class=&quot;token variable&quot;&gt;$PROFILE&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Execution Policy&lt;/strong&gt; — controlla quali script possono essere eseguiti:&lt;/p&gt;
&lt;pre class=&quot;language-powershell&quot;&gt;&lt;code class=&quot;language-powershell&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Visualizza la policy attuale&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Get-ExecutionPolicy&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Imposta la policy per l&#39;utente corrente (scelta comune per sviluppo locale)&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Set-ExecutionPolicy&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Scope CurrentUser &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;ExecutionPolicy RemoteSigned

&lt;span class=&quot;token comment&quot;&gt;# Valori possibili:&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# Restricted    → Nessuno script (default su Windows)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# RemoteSigned  → Gli script locali sono OK; quelli scaricati richiedono firma&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# Unrestricted  → Tutti gli script (non consigliato in produzione)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# AllSigned     → Solo script con firma digitale (massima sicurezza)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Concetti Fondamentali&lt;/h2&gt;
&lt;h3&gt;Sintassi: Verb-Noun&lt;/h3&gt;
&lt;p&gt;Tutti i cmdlet seguono la convenzione &lt;code&gt;Verbo-Sostantivo&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;language-powershell&quot;&gt;&lt;code class=&quot;language-powershell&quot;&gt;&lt;span class=&quot;token function&quot;&gt;Get-Process&lt;/span&gt;        &lt;span class=&quot;token comment&quot;&gt;# Recupera informazioni sui processi&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Stop-Service&lt;/span&gt;       &lt;span class=&quot;token comment&quot;&gt;# Arresta un servizio&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;New-Item&lt;/span&gt;           &lt;span class=&quot;token comment&quot;&gt;# Crea un nuovo elemento&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Remove-Item&lt;/span&gt;        &lt;span class=&quot;token comment&quot;&gt;# Elimina un elemento&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Set-Location&lt;/span&gt;       &lt;span class=&quot;token comment&quot;&gt;# Cambia directory&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I verbi approvati da Microsoft garantiscono coerenza. Per vederli tutti:&lt;/p&gt;
&lt;pre class=&quot;language-powershell&quot;&gt;&lt;code class=&quot;language-powershell&quot;&gt;&lt;span class=&quot;token function&quot;&gt;Get-Verb&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Sort-Object&lt;/span&gt; Verb&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Il Sistema di Help&lt;/h3&gt;
&lt;p&gt;La documentazione integrata è uno dei punti di forza di PowerShell:&lt;/p&gt;
&lt;pre class=&quot;language-powershell&quot;&gt;&lt;code class=&quot;language-powershell&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Aggiorna l&#39;help (eseguire come amministratore)&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Update-Help&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Help base&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Get-Help&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Get-Process&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Con esempi pratici (molto utile!)&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Get-Help&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Get-Process&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Examples

&lt;span class=&quot;token comment&quot;&gt;# Help completo con tutti i parametri&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Get-Help&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Get-Process&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Full

&lt;span class=&quot;token comment&quot;&gt;# Apre la documentazione online&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Get-Help&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Get-Process&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Online&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Cmdlet Essenziali&lt;/h3&gt;
&lt;pre class=&quot;language-powershell&quot;&gt;&lt;code class=&quot;language-powershell&quot;&gt;&lt;span class=&quot;token function&quot;&gt;Get-Command&lt;/span&gt;               &lt;span class=&quot;token comment&quot;&gt;# Elenca tutti i cmdlet disponibili&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Get-Command&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Verb Get     &lt;span class=&quot;token comment&quot;&gt;# Filtra per verbo&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Get-Command&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;process&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;     &lt;span class=&quot;token comment&quot;&gt;# Ricerca per nome&lt;/span&gt;

&lt;span class=&quot;token function&quot;&gt;Get-Member&lt;/span&gt;                &lt;span class=&quot;token comment&quot;&gt;# Mostra proprietà e metodi di un oggetto&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Get-Process&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Get-Member&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Esempio: scopri cosa puoi fare con un processo&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Variabili e Tipi di Dati&lt;/h2&gt;
&lt;h3&gt;Variabili&lt;/h3&gt;
&lt;p&gt;Le variabili iniziano con &lt;code&gt;$&lt;/code&gt;. PowerShell è &lt;strong&gt;tipizzato dinamicamente&lt;/strong&gt;, ma è possibile (e consigliato per script professionali) dichiarare il tipo esplicitamente:&lt;/p&gt;
&lt;pre class=&quot;language-powershell&quot;&gt;&lt;code class=&quot;language-powershell&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Tipizzazione implicita&lt;/span&gt;
&lt;span class=&quot;token variable&quot;&gt;$nome&lt;/span&gt; = &lt;span class=&quot;token string&quot;&gt;&quot;Mario&quot;&lt;/span&gt;
&lt;span class=&quot;token variable&quot;&gt;$eta&lt;/span&gt; = 30
&lt;span class=&quot;token variable&quot;&gt;$attivo&lt;/span&gt; = &lt;span class=&quot;token boolean&quot;&gt;$true&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Tipizzazione esplicita (best practice per script professionali)&lt;/span&gt;
&lt;span class=&quot;token namespace&quot;&gt;[string]&lt;/span&gt;&lt;span class=&quot;token variable&quot;&gt;$nome&lt;/span&gt; = &lt;span class=&quot;token string&quot;&gt;&quot;Mario&quot;&lt;/span&gt;
&lt;span class=&quot;token namespace&quot;&gt;[int]&lt;/span&gt;&lt;span class=&quot;token variable&quot;&gt;$eta&lt;/span&gt; = 30
&lt;span class=&quot;token namespace&quot;&gt;[bool]&lt;/span&gt;&lt;span class=&quot;token variable&quot;&gt;$attivo&lt;/span&gt; = &lt;span class=&quot;token boolean&quot;&gt;$true&lt;/span&gt;
&lt;span class=&quot;token namespace&quot;&gt;[datetime]&lt;/span&gt;&lt;span class=&quot;token variable&quot;&gt;$oggi&lt;/span&gt; = &lt;span class=&quot;token function&quot;&gt;Get-Date&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Stringhe&lt;/h3&gt;
&lt;pre class=&quot;language-powershell&quot;&gt;&lt;code class=&quot;language-powershell&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Virgolette doppie: interpolazione variabili&lt;/span&gt;
&lt;span class=&quot;token variable&quot;&gt;$nome&lt;/span&gt; = &lt;span class=&quot;token string&quot;&gt;&quot;Mario&quot;&lt;/span&gt;
&lt;span class=&quot;token variable&quot;&gt;$saluto&lt;/span&gt; = &lt;span class=&quot;token string&quot;&gt;&quot;Ciao, &lt;span class=&quot;token variable&quot;&gt;$nome&lt;/span&gt;!&quot;&lt;/span&gt;           &lt;span class=&quot;token comment&quot;&gt;# → &quot;Ciao, Mario!&quot;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Virgolette singole: stringa letterale (no interpolazione)&lt;/span&gt;
&lt;span class=&quot;token variable&quot;&gt;$letterale&lt;/span&gt; = &lt;span class=&quot;token string&quot;&gt;&#39;Ciao, $nome!&#39;&lt;/span&gt;        &lt;span class=&quot;token comment&quot;&gt;# → &quot;Ciao, $nome!&quot;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Stringa multilinea (here-string)&lt;/span&gt;
&lt;span class=&quot;token variable&quot;&gt;$testo&lt;/span&gt; = @&lt;span class=&quot;token string&quot;&gt;&quot;
Questa è una stringa
su più righe con variabili: &lt;span class=&quot;token variable&quot;&gt;$nome&lt;/span&gt;
&quot;&lt;/span&gt;@

&lt;span class=&quot;token comment&quot;&gt;# Metodi delle stringhe (PowerShell espone i metodi .NET)&lt;/span&gt;
&lt;span class=&quot;token string&quot;&gt;&quot;powershell&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ToUpper&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;             &lt;span class=&quot;token comment&quot;&gt;# → &quot;POWERSHELL&quot;&lt;/span&gt;
&lt;span class=&quot;token string&quot;&gt;&quot;  testo  &quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Trim&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;                 &lt;span class=&quot;token comment&quot;&gt;# → &quot;testo&quot;&lt;/span&gt;
&lt;span class=&quot;token string&quot;&gt;&quot;ciao mondo&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Split&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot; &quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;           &lt;span class=&quot;token comment&quot;&gt;# → @(&quot;ciao&quot;, &quot;mondo&quot;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Array e HashTable&lt;/h3&gt;
&lt;pre class=&quot;language-powershell&quot;&gt;&lt;code class=&quot;language-powershell&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Array&lt;/span&gt;
&lt;span class=&quot;token variable&quot;&gt;$frutti&lt;/span&gt; = &lt;span class=&quot;token string&quot;&gt;&quot;mela&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;banana&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;arancia&quot;&lt;/span&gt;
&lt;span class=&quot;token variable&quot;&gt;$numeri&lt;/span&gt; = 1&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;10                    &lt;span class=&quot;token comment&quot;&gt;# Range: da 1 a 10&lt;/span&gt;
&lt;span class=&quot;token variable&quot;&gt;$frutti&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;0&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;                         &lt;span class=&quot;token comment&quot;&gt;# → &quot;mela&quot;&lt;/span&gt;
&lt;span class=&quot;token variable&quot;&gt;$frutti&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Count                      &lt;span class=&quot;token comment&quot;&gt;# → 3&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# HashTable (dizionario chiave-valore)&lt;/span&gt;
&lt;span class=&quot;token variable&quot;&gt;$persona&lt;/span&gt; = @&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    Nome    = &lt;span class=&quot;token string&quot;&gt;&quot;Mario&quot;&lt;/span&gt;
    Cognome = &lt;span class=&quot;token string&quot;&gt;&quot;Rossi&quot;&lt;/span&gt;
    Eta     = 30
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token variable&quot;&gt;$persona&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Nome&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt;                   &lt;span class=&quot;token comment&quot;&gt;# → &quot;Mario&quot;&lt;/span&gt;
&lt;span class=&quot;token variable&quot;&gt;$persona&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Eta                       &lt;span class=&quot;token comment&quot;&gt;# → 30&lt;/span&gt;
&lt;span class=&quot;token variable&quot;&gt;$persona&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Keys                      &lt;span class=&quot;token comment&quot;&gt;# → Nome, Cognome, Eta&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Variabili Automatiche&lt;/h3&gt;
&lt;p&gt;PowerShell fornisce variabili speciali predefinite:&lt;/p&gt;
&lt;pre class=&quot;language-powershell&quot;&gt;&lt;code class=&quot;language-powershell&quot;&gt;&lt;span class=&quot;token variable&quot;&gt;$_&lt;/span&gt;         &lt;span class=&quot;token comment&quot;&gt;# (alias: $PSItem) L&#39;oggetto corrente nella pipeline&lt;/span&gt;
$?         &lt;span class=&quot;token comment&quot;&gt;# True se l&#39;ultimo comando ha avuto successo&lt;/span&gt;
&lt;span class=&quot;token variable&quot;&gt;$LASTEXITCODE&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Codice di uscita dell&#39;ultimo programma esterno&lt;/span&gt;
&lt;span class=&quot;token variable&quot;&gt;$Error&lt;/span&gt;     &lt;span class=&quot;token comment&quot;&gt;# Array degli ultimi errori&lt;/span&gt;
&lt;span class=&quot;token variable&quot;&gt;$HOME&lt;/span&gt;      &lt;span class=&quot;token comment&quot;&gt;# Directory home dell&#39;utente&lt;/span&gt;
&lt;span class=&quot;token variable&quot;&gt;$PSVersionTable&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Informazioni sulla versione di PowerShell&lt;/span&gt;
&lt;span class=&quot;token variable&quot;&gt;$PROFILE&lt;/span&gt;   &lt;span class=&quot;token comment&quot;&gt;# Percorso del profilo utente&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Operatori&lt;/h3&gt;
&lt;pre class=&quot;language-powershell&quot;&gt;&lt;code class=&quot;language-powershell&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Aritmetici&lt;/span&gt;
5 &lt;span class=&quot;token operator&quot;&gt;+&lt;/span&gt; 3      &lt;span class=&quot;token comment&quot;&gt;# 8&lt;/span&gt;
10 &lt;span class=&quot;token operator&quot;&gt;%&lt;/span&gt; 3     &lt;span class=&quot;token comment&quot;&gt;# 1 (modulo)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Confronto&lt;/span&gt;
&lt;span class=&quot;token string&quot;&gt;&quot;a&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-eq&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;a&quot;&lt;/span&gt;   &lt;span class=&quot;token comment&quot;&gt;# True  (equal)&lt;/span&gt;
5 &lt;span class=&quot;token operator&quot;&gt;-ne&lt;/span&gt; 3       &lt;span class=&quot;token comment&quot;&gt;# True  (not equal)&lt;/span&gt;
10 &lt;span class=&quot;token operator&quot;&gt;-gt&lt;/span&gt; 5      &lt;span class=&quot;token comment&quot;&gt;# True  (greater than)&lt;/span&gt;
3 &lt;span class=&quot;token operator&quot;&gt;-lt&lt;/span&gt; 5       &lt;span class=&quot;token comment&quot;&gt;# True  (less than)&lt;/span&gt;
5 &lt;span class=&quot;token operator&quot;&gt;-ge&lt;/span&gt; 5       &lt;span class=&quot;token comment&quot;&gt;# True  (greater or equal)&lt;/span&gt;
&lt;span class=&quot;token string&quot;&gt;&quot;ciao&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-like&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;c*&quot;&lt;/span&gt;   &lt;span class=&quot;token comment&quot;&gt;# True  (wildcard match)&lt;/span&gt;
&lt;span class=&quot;token string&quot;&gt;&quot;hello&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-match&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;^h&quot;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;# True  (regex match)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Logici&lt;/span&gt;
&lt;span class=&quot;token boolean&quot;&gt;$true&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-and&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;$false&lt;/span&gt;   &lt;span class=&quot;token comment&quot;&gt;# False&lt;/span&gt;
&lt;span class=&quot;token boolean&quot;&gt;$true&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-or&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;$false&lt;/span&gt;    &lt;span class=&quot;token comment&quot;&gt;# True&lt;/span&gt;
&lt;span class=&quot;token operator&quot;&gt;-not&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;$true&lt;/span&gt;          &lt;span class=&quot;token comment&quot;&gt;# False&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;La Pipeline&lt;/h2&gt;
&lt;p&gt;La &lt;strong&gt;pipeline&lt;/strong&gt; (&lt;code&gt;|&lt;/code&gt;) è il cuore di PowerShell. Passa &lt;strong&gt;oggetti interi&lt;/strong&gt; (non testo) tra i comandi, preservando tutte le proprietà.&lt;/p&gt;
&lt;pre class=&quot;language-powershell&quot;&gt;&lt;code class=&quot;language-powershell&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Esempio base: filtra i processi che usano più di 100MB di RAM&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Get-Process&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Where-Object&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token variable&quot;&gt;$_&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;WorkingSet &lt;span class=&quot;token operator&quot;&gt;-gt&lt;/span&gt; 100MB &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# $_ rappresenta l&#39;oggetto corrente nella pipeline (ogni processo)&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# .WorkingSet è una proprietà dell&#39;oggetto Process&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Concatena più operazioni&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Get-Process&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;|&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;Where-Object&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token variable&quot;&gt;$_&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;CPU &lt;span class=&quot;token operator&quot;&gt;-gt&lt;/span&gt; 1 &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;|&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;Sort-Object&lt;/span&gt; CPU &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Descending &lt;span class=&quot;token punctuation&quot;&gt;|&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;Select-Object&lt;/span&gt; Name&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; CPU&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Id &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;First 10 &lt;span class=&quot;token punctuation&quot;&gt;|&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;Format-Table&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;AutoSize&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Cmdlet Fondamentali per la Pipeline&lt;/h3&gt;
&lt;pre class=&quot;language-powershell&quot;&gt;&lt;code class=&quot;language-powershell&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Filtra oggetti&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Get-Service&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Where-Object&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token variable&quot;&gt;$_&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Status &lt;span class=&quot;token operator&quot;&gt;-eq&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Running&quot;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Ordina&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Get-Process&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Sort-Object&lt;/span&gt; WorkingSet &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Descending

&lt;span class=&quot;token comment&quot;&gt;# Seleziona proprietà specifiche&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Get-Process&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Select-Object&lt;/span&gt; Name&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Id&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; CPU

&lt;span class=&quot;token comment&quot;&gt;# Aggrega&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Get-Process&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Measure-Object&lt;/span&gt; CPU &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Sum &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Average

&lt;span class=&quot;token comment&quot;&gt;# Raggruppa&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Get-Service&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Group-Object&lt;/span&gt; Status

&lt;span class=&quot;token comment&quot;&gt;# Converte output&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Get-Process&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;ConvertTo-Json&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Get-Process&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;ConvertTo-Csv&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Out-File&lt;/span&gt; processi&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;csv

&lt;span class=&quot;token comment&quot;&gt;# Formatta output (solo per visualizzazione, non usare in pipeline)&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Get-Process&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Format-Table&lt;/span&gt; Name&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; CPU &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;AutoSize
&lt;span class=&quot;token function&quot;&gt;Get-Process&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Format-List&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Gestione File e Directory&lt;/h2&gt;
&lt;h3&gt;Navigazione&lt;/h3&gt;
&lt;pre class=&quot;language-powershell&quot;&gt;&lt;code class=&quot;language-powershell&quot;&gt;&lt;span class=&quot;token function&quot;&gt;Get-Location&lt;/span&gt;              &lt;span class=&quot;token comment&quot;&gt;# Directory corrente (alias: pwd)&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Set-Location&lt;/span&gt; C:&#92;Projects  &lt;span class=&quot;token comment&quot;&gt;# Cambia directory (alias: cd)&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Set-Location&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;           &lt;span class=&quot;token comment&quot;&gt;# Vai alla directory padre&lt;/span&gt;

&lt;span class=&quot;token function&quot;&gt;Get-PSDrive&lt;/span&gt;               &lt;span class=&quot;token comment&quot;&gt;# Mostra tutti i &quot;drive&quot; disponibili (C:, D:, HKLM:, Env:, ecc.)&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Set-Location&lt;/span&gt; HKLM:        &lt;span class=&quot;token comment&quot;&gt;# PowerShell può navigare anche nel Registry!&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Operazioni sui File&lt;/h3&gt;
&lt;pre class=&quot;language-powershell&quot;&gt;&lt;code class=&quot;language-powershell&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Elenca contenuto&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Get-ChildItem&lt;/span&gt;             &lt;span class=&quot;token comment&quot;&gt;# (alias: ls, dir)&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Get-ChildItem&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Recurse    &lt;span class=&quot;token comment&quot;&gt;# Ricorsivo&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Get-ChildItem&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;log       &lt;span class=&quot;token comment&quot;&gt;# Filtra per estensione&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Crea&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;New-Item&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;ItemType File &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Path &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&#92;log&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;txt
&lt;span class=&quot;token function&quot;&gt;New-Item&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;ItemType Directory &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Path &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&#92;backup

&lt;span class=&quot;token comment&quot;&gt;# Leggi&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Get-Content&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&#92;log&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;txt
&lt;span class=&quot;token function&quot;&gt;Get-Content&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&#92;log&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;txt &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Tail 20   &lt;span class=&quot;token comment&quot;&gt;# Ultime 20 righe (come tail -n)&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Get-Content&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&#92;log&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;txt &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Wait      &lt;span class=&quot;token comment&quot;&gt;# Modalità follow (come tail -f)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Scrivi&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Set-Content&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&#92;output&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;txt &lt;span class=&quot;token string&quot;&gt;&quot;Nuovo contenuto&quot;&lt;/span&gt;     &lt;span class=&quot;token comment&quot;&gt;# Sovrascrive&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Add-Content&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&#92;output&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;txt &lt;span class=&quot;token string&quot;&gt;&quot;Riga aggiuntiva&quot;&lt;/span&gt;    &lt;span class=&quot;token comment&quot;&gt;# Appende&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Copia, sposta, elimina&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Copy-Item&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&#92;source&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;txt &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&#92;dest&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;txt
&lt;span class=&quot;token function&quot;&gt;Move-Item&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&#92;old&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;txt &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&#92;new&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;txt
&lt;span class=&quot;token function&quot;&gt;Remove-Item&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&#92;file&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;txt
&lt;span class=&quot;token function&quot;&gt;Remove-Item&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&#92;cartella &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Recurse  &lt;span class=&quot;token comment&quot;&gt;# Elimina cartella con contenuto&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Strutture di Controllo&lt;/h2&gt;
&lt;h3&gt;Condizionali&lt;/h3&gt;
&lt;pre class=&quot;language-powershell&quot;&gt;&lt;code class=&quot;language-powershell&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# If / ElseIf / Else&lt;/span&gt;
&lt;span class=&quot;token variable&quot;&gt;$temperatura&lt;/span&gt; = 25

&lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token variable&quot;&gt;$temperatura&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-gt&lt;/span&gt; 30&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;Write-Host&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Caldo&quot;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;elseif&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token variable&quot;&gt;$temperatura&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-gt&lt;/span&gt; 20&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;Write-Host&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Piacevole&quot;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;Write-Host&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Fresco&quot;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Switch (più efficiente di if/elseif per casi multipli)&lt;/span&gt;
&lt;span class=&quot;token variable&quot;&gt;$giorno&lt;/span&gt; = &lt;span class=&quot;token string&quot;&gt;&quot;Lunedì&quot;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;switch&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token variable&quot;&gt;$giorno&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;Lunedì&quot;&lt;/span&gt;    &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Write-Host&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Inizio settimana&quot;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;Venerdì&quot;&lt;/span&gt;   &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Write-Host&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Fine settimana&quot;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;Sabato&quot;&lt;/span&gt;    &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Write-Host&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Weekend!&quot;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token string&quot;&gt;&quot;Domenica&quot;&lt;/span&gt;  &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Write-Host&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Weekend!&quot;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    default     &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Write-Host&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Giorno lavorativo&quot;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Loop&lt;/h3&gt;
&lt;pre class=&quot;language-powershell&quot;&gt;&lt;code class=&quot;language-powershell&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# For classico&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token variable&quot;&gt;$i&lt;/span&gt; = 0&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token variable&quot;&gt;$i&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-lt&lt;/span&gt; 5&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token variable&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;Write-Host&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Iterazione: &lt;span class=&quot;token variable&quot;&gt;$i&lt;/span&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# ForEach (il più comune in PowerShell)&lt;/span&gt;
&lt;span class=&quot;token variable&quot;&gt;$servizi&lt;/span&gt; = &lt;span class=&quot;token function&quot;&gt;Get-Service&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Where-Object&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token variable&quot;&gt;$_&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Status &lt;span class=&quot;token operator&quot;&gt;-eq&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Stopped&quot;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;foreach&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token variable&quot;&gt;$servizio&lt;/span&gt; in &lt;span class=&quot;token variable&quot;&gt;$servizi&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;Write-Host&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Servizio fermo: &lt;span class=&quot;token function&quot;&gt;$&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token variable&quot;&gt;$servizio&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Name&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# ForEach-Object (nella pipeline)&lt;/span&gt;
1&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;5 &lt;span class=&quot;token punctuation&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;ForEach-Object&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token variable&quot;&gt;$_&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt; 2 &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;   &lt;span class=&quot;token comment&quot;&gt;# Output: 2 4 6 8 10&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# While&lt;/span&gt;
&lt;span class=&quot;token variable&quot;&gt;$contatore&lt;/span&gt; = 0
&lt;span class=&quot;token keyword&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token variable&quot;&gt;$contatore&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-lt&lt;/span&gt; 3&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;Write-Host&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Contatore: &lt;span class=&quot;token variable&quot;&gt;$contatore&lt;/span&gt;&quot;&lt;/span&gt;
    &lt;span class=&quot;token variable&quot;&gt;$contatore&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Gestione Errori&lt;/h3&gt;
&lt;pre class=&quot;language-powershell&quot;&gt;&lt;code class=&quot;language-powershell&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# $ErrorActionPreference controlla il comportamento globale degli errori&lt;/span&gt;
&lt;span class=&quot;token variable&quot;&gt;$ErrorActionPreference&lt;/span&gt; = &lt;span class=&quot;token string&quot;&gt;&quot;Stop&quot;&lt;/span&gt;  &lt;span class=&quot;token comment&quot;&gt;# Trasforma tutti gli errori in eccezioni terminanti&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;try&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token variable&quot;&gt;$contenuto&lt;/span&gt; = &lt;span class=&quot;token function&quot;&gt;Get-Content&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;file-inesistente.txt&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;ErrorAction Stop
    &lt;span class=&quot;token function&quot;&gt;Write-Host&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;File letto con successo&quot;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;catch&lt;/span&gt; &lt;span class=&quot;token namespace&quot;&gt;[System.IO.FileNotFoundException]&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;Write-Warning&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;File non trovato: &lt;span class=&quot;token variable&quot;&gt;$_&lt;/span&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;catch&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# Cattura qualsiasi altro errore&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;Write-Error&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Errore imprevisto: &lt;span class=&quot;token function&quot;&gt;$&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token variable&quot;&gt;$_&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Exception&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Message&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&quot;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;finally&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token comment&quot;&gt;# Eseguito sempre, con o senza errori (utile per cleanup)&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;Write-Host&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Operazione completata.&quot;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Funzioni e Moduli&lt;/h2&gt;
&lt;h3&gt;Funzioni Base&lt;/h3&gt;
&lt;pre class=&quot;language-powershell&quot;&gt;&lt;code class=&quot;language-powershell&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Get-Saluto&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;param&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;token namespace&quot;&gt;[string]&lt;/span&gt;&lt;span class=&quot;token variable&quot;&gt;$Nome&lt;/span&gt; = &lt;span class=&quot;token string&quot;&gt;&quot;Mondo&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;   &lt;span class=&quot;token comment&quot;&gt;# Parametro con valore di default&lt;/span&gt;
        &lt;span class=&quot;token namespace&quot;&gt;[int]&lt;/span&gt;&lt;span class=&quot;token variable&quot;&gt;$Ripetizioni&lt;/span&gt; = 1
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token variable&quot;&gt;$i&lt;/span&gt; = 0&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token variable&quot;&gt;$i&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-lt&lt;/span&gt; &lt;span class=&quot;token variable&quot;&gt;$Ripetizioni&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token variable&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token function&quot;&gt;Write-Output&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Ciao, &lt;span class=&quot;token variable&quot;&gt;$Nome&lt;/span&gt;!&quot;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Chiamata&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Get-Saluto&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Nome &lt;span class=&quot;token string&quot;&gt;&quot;Mario&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Ripetizioni 3&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Funzioni Avanzate (Advanced Functions)&lt;/h3&gt;
&lt;p&gt;Per script professionali, usa &lt;code&gt;CmdletBinding&lt;/code&gt; per ottenere comportamenti da cmdlet nativi (supporto &lt;code&gt;-Verbose&lt;/code&gt;, &lt;code&gt;-WhatIf&lt;/code&gt;, &lt;code&gt;-ErrorAction&lt;/code&gt;, ecc.):&lt;/p&gt;
&lt;pre class=&quot;language-powershell&quot;&gt;&lt;code class=&quot;language-powershell&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Invoke-Backup&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token namespace&quot;&gt;[CmdletBinding(SupportsShouldProcess)]&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;param&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;token namespace&quot;&gt;[Parameter(Mandatory, ValueFromPipeline)]&lt;/span&gt;
        &lt;span class=&quot;token namespace&quot;&gt;[string]&lt;/span&gt;&lt;span class=&quot;token variable&quot;&gt;$SourcePath&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;

        &lt;span class=&quot;token namespace&quot;&gt;[Parameter(Mandatory)]&lt;/span&gt;
        &lt;span class=&quot;token namespace&quot;&gt;[string]&lt;/span&gt;&lt;span class=&quot;token variable&quot;&gt;$DestinationPath&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;

        &lt;span class=&quot;token namespace&quot;&gt;[switch]&lt;/span&gt;&lt;span class=&quot;token variable&quot;&gt;$Compress&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;process&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token variable&quot;&gt;$PSCmdlet&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ShouldProcess&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token variable&quot;&gt;$SourcePath&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Backup&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token function&quot;&gt;Write-Verbose&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Backup di &#39;&lt;span class=&quot;token variable&quot;&gt;$SourcePath&lt;/span&gt;&#39; verso &#39;&lt;span class=&quot;token variable&quot;&gt;$DestinationPath&lt;/span&gt;&#39;&quot;&lt;/span&gt;
            &lt;span class=&quot;token function&quot;&gt;Copy-Item&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Path &lt;span class=&quot;token variable&quot;&gt;$SourcePath&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Destination &lt;span class=&quot;token variable&quot;&gt;$DestinationPath&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Recurse
            &lt;span class=&quot;token function&quot;&gt;Write-Output&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Backup completato.&quot;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Uso con -WhatIf per simulare senza eseguire&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Invoke-Backup&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;SourcePath &lt;span class=&quot;token string&quot;&gt;&quot;C:&#92;dati&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;DestinationPath &lt;span class=&quot;token string&quot;&gt;&quot;D:&#92;backup&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;WhatIf

&lt;span class=&quot;token comment&quot;&gt;# Uso con -Verbose per logging dettagliato&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Invoke-Backup&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;SourcePath &lt;span class=&quot;token string&quot;&gt;&quot;C:&#92;dati&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;DestinationPath &lt;span class=&quot;token string&quot;&gt;&quot;D:&#92;backup&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Verbose&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Gestione Moduli&lt;/h3&gt;
&lt;pre class=&quot;language-powershell&quot;&gt;&lt;code class=&quot;language-powershell&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Trova moduli disponibili nel repository PSGallery&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Find-Module&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Name &lt;span class=&quot;token operator&quot;&gt;*&lt;/span&gt;Azure*

&lt;span class=&quot;token comment&quot;&gt;# Installa un modulo&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Install-Module&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Name Az &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Scope CurrentUser

&lt;span class=&quot;token comment&quot;&gt;# Importa un modulo nella sessione corrente&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Import-Module&lt;/span&gt; Az

&lt;span class=&quot;token comment&quot;&gt;# Elenca i moduli installati&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Get-Module&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;ListAvailable

&lt;span class=&quot;token comment&quot;&gt;# Elenca i cmdlet di un modulo specifico&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Get-Command&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Module Az&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Compute&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Amministrazione di Sistema&lt;/h2&gt;
&lt;h3&gt;Utenti Locali&lt;/h3&gt;
&lt;pre class=&quot;language-powershell&quot;&gt;&lt;code class=&quot;language-powershell&quot;&gt;&lt;span class=&quot;token function&quot;&gt;Get-LocalUser&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;New-LocalUser&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Name &lt;span class=&quot;token string&quot;&gt;&quot;mario&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Password &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;ConvertTo-SecureString&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;P@ss!&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;AsPlainText &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Force&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;FullName &lt;span class=&quot;token string&quot;&gt;&quot;Mario Rossi&quot;&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Add-LocalGroupMember&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Group&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Administrators&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Member &lt;span class=&quot;token string&quot;&gt;&quot;mario&quot;&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Remove-LocalUser&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Name &lt;span class=&quot;token string&quot;&gt;&quot;mario&quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Servizi&lt;/h3&gt;
&lt;pre class=&quot;language-powershell&quot;&gt;&lt;code class=&quot;language-powershell&quot;&gt;&lt;span class=&quot;token function&quot;&gt;Get-Service&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Get-Service&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Name &lt;span class=&quot;token string&quot;&gt;&quot;wuauserv&quot;&lt;/span&gt;           &lt;span class=&quot;token comment&quot;&gt;# Windows Update&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Start-Service&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Name &lt;span class=&quot;token string&quot;&gt;&quot;wuauserv&quot;&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Stop-Service&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Name &lt;span class=&quot;token string&quot;&gt;&quot;wuauserv&quot;&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Restart-Service&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Name &lt;span class=&quot;token string&quot;&gt;&quot;wuauserv&quot;&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Set-Service&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Name &lt;span class=&quot;token string&quot;&gt;&quot;wuauserv&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;StartupType Disabled&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Processi&lt;/h3&gt;
&lt;pre class=&quot;language-powershell&quot;&gt;&lt;code class=&quot;language-powershell&quot;&gt;&lt;span class=&quot;token function&quot;&gt;Get-Process&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Sort-Object&lt;/span&gt; CPU &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Descending &lt;span class=&quot;token punctuation&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Select-Object&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;First 10
&lt;span class=&quot;token function&quot;&gt;Stop-Process&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Name &lt;span class=&quot;token string&quot;&gt;&quot;notepad&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Confirm
&lt;span class=&quot;token function&quot;&gt;Start-Process&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;notepad.exe&quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Registro di Sistema&lt;/h3&gt;
&lt;pre class=&quot;language-powershell&quot;&gt;&lt;code class=&quot;language-powershell&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Naviga nel registro come se fosse un filesystem&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Set-Location&lt;/span&gt; HKLM:&#92;SOFTWARE&#92;Microsoft&#92;Windows&#92;CurrentVersion

&lt;span class=&quot;token function&quot;&gt;Get-ItemProperty&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Path &lt;span class=&quot;token string&quot;&gt;&quot;HKLM:&#92;SOFTWARE&#92;Microsoft&#92;Windows NT&#92;CurrentVersion&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Name &lt;span class=&quot;token string&quot;&gt;&quot;ProductName&quot;&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Set-ItemProperty&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Path &lt;span class=&quot;token string&quot;&gt;&quot;HKCU:&#92;Software&#92;MyApp&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Name &lt;span class=&quot;token string&quot;&gt;&quot;Debug&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Value 1
&lt;span class=&quot;token function&quot;&gt;New-Item&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Path &lt;span class=&quot;token string&quot;&gt;&quot;HKCU:&#92;Software&#92;MyApp&quot;&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Remove-Item&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Path &lt;span class=&quot;token string&quot;&gt;&quot;HKCU:&#92;Software&#92;MyApp&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Recurse&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;PowerShell Remoting&lt;/h2&gt;
&lt;p&gt;PowerShell Remoting consente di eseguire comandi su computer remoti.&lt;/p&gt;
&lt;h3&gt;Tramite WinRM (Windows)&lt;/h3&gt;
&lt;pre class=&quot;language-powershell&quot;&gt;&lt;code class=&quot;language-powershell&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Abilita il remoting (da eseguire come amministratore)&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Enable-PSRemoting&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Force

&lt;span class=&quot;token comment&quot;&gt;# Sessione interattiva&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Enter-PSSession&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;ComputerName Server01 &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Credential &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Get-Credential&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Esegui un comando su un computer remoto&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Invoke-Command&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;ComputerName Server01 &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;ScriptBlock &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;Get-Service&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Where-Object&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token variable&quot;&gt;$_&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Status &lt;span class=&quot;token operator&quot;&gt;-eq&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Stopped&quot;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Esegui uno script locale su computer remoti&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Invoke-Command&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;ComputerName Server01&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; Server02 &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;FilePath &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&#92;script&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ps1

&lt;span class=&quot;token comment&quot;&gt;# Gestione sessioni persistenti&lt;/span&gt;
&lt;span class=&quot;token variable&quot;&gt;$sessione&lt;/span&gt; = &lt;span class=&quot;token function&quot;&gt;New-PSSession&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;ComputerName Server01 &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Credential &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;Get-Credential&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Invoke-Command&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Session &lt;span class=&quot;token variable&quot;&gt;$sessione&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;ScriptBlock &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Get-Process&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Remove-PSSession&lt;/span&gt; &lt;span class=&quot;token variable&quot;&gt;$sessione&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Tramite SSH (Cross-Platform, PowerShell 7+)&lt;/h3&gt;
&lt;pre class=&quot;language-powershell&quot;&gt;&lt;code class=&quot;language-powershell&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Connessione SSH a un server Linux&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Enter-PSSession&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;HostName ubuntu-server &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;UserName admin &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;SSHTransport

&lt;span class=&quot;token comment&quot;&gt;# Esegui comandi remoti via SSH&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Invoke-Command&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;HostName ubuntu-server &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;UserName admin &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;ScriptBlock &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; uname &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;a &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Sicurezza in PowerShell&lt;/h2&gt;
&lt;p&gt;La sicurezza è una priorità per qualsiasi script destinato a produzione o ambienti aziendali.&lt;/p&gt;
&lt;h3&gt;Firma Digitale degli Script (Code Signing)&lt;/h3&gt;
&lt;p&gt;In ambienti con policy &lt;code&gt;AllSigned&lt;/code&gt;, gli script devono essere firmati:&lt;/p&gt;
&lt;pre class=&quot;language-powershell&quot;&gt;&lt;code class=&quot;language-powershell&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Ottieni il certificato di firma (da un&#39;autorità di certificazione aziendale o auto-firmato)&lt;/span&gt;
&lt;span class=&quot;token variable&quot;&gt;$cert&lt;/span&gt; = &lt;span class=&quot;token function&quot;&gt;Get-ChildItem&lt;/span&gt; Cert:&#92;CurrentUser&#92;My &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;CodeSigningCert

&lt;span class=&quot;token comment&quot;&gt;# Firma lo script&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Set-AuthenticodeSignature&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;FilePath &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&#92;script&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ps1 &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Certificate &lt;span class=&quot;token variable&quot;&gt;$cert&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Verifica la firma&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Get-AuthenticodeSignature&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&#92;script&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ps1&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Gestione Sicura delle Credenziali&lt;/h3&gt;
&lt;blockquote&gt;
&lt;p&gt;Non inserire mai password in chiaro negli script!&lt;/p&gt;
&lt;/blockquote&gt;
&lt;pre class=&quot;language-powershell&quot;&gt;&lt;code class=&quot;language-powershell&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Chiedi le credenziali in modo sicuro&lt;/span&gt;
&lt;span class=&quot;token variable&quot;&gt;$cred&lt;/span&gt; = &lt;span class=&quot;token function&quot;&gt;Get-Credential&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Salva una password come SecureString (cifrata, leggibile solo dall&#39;utente corrente)&lt;/span&gt;
&lt;span class=&quot;token variable&quot;&gt;$password&lt;/span&gt; = &lt;span class=&quot;token function&quot;&gt;Read-Host&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Prompt &lt;span class=&quot;token string&quot;&gt;&quot;Inserisci password&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;AsSecureString
&lt;span class=&quot;token variable&quot;&gt;$passwordCifrata&lt;/span&gt; = &lt;span class=&quot;token function&quot;&gt;ConvertFrom-SecureString&lt;/span&gt; &lt;span class=&quot;token variable&quot;&gt;$password&lt;/span&gt;
&lt;span class=&quot;token variable&quot;&gt;$passwordCifrata&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Out-File&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&#92;password&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;enc  &lt;span class=&quot;token comment&quot;&gt;# Salva cifrata su disco&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Ricarica la password&lt;/span&gt;
&lt;span class=&quot;token variable&quot;&gt;$passwordCifrata&lt;/span&gt; = &lt;span class=&quot;token function&quot;&gt;Get-Content&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&#92;password&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;enc &lt;span class=&quot;token punctuation&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;ConvertTo-SecureString&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Script Block Logging e Auditing&lt;/h3&gt;
&lt;p&gt;Per ambienti aziendali, abilita la registrazione degli script tramite Group Policy:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Computer Configuration → Administrative Templates → Windows Components →
Windows PowerShell → Turn on Script Block Logging → Enabled
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Gli script eseguiti vengono registrati nel &lt;strong&gt;Windows Event Log&lt;/strong&gt; (Event ID 4104), fondamentale per audit e incident response.&lt;/p&gt;
&lt;h3&gt;Constrained Language Mode&lt;/h3&gt;
&lt;p&gt;Limita le funzionalità di PowerShell per ridurre la superficie d&#39;attacco:&lt;/p&gt;
&lt;pre class=&quot;language-powershell&quot;&gt;&lt;code class=&quot;language-powershell&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Verifica la modalità corrente&lt;/span&gt;
&lt;span class=&quot;token variable&quot;&gt;$ExecutionContext&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;SessionState&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;LanguageMode

&lt;span class=&quot;token comment&quot;&gt;# In CLM, tipi .NET arbitrari e COM objects sono bloccati&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# Viene tipicamente applicato tramite AppLocker o WDAC&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;PowerShell e il Cloud&lt;/h2&gt;
&lt;p&gt;PowerShell è lo strumento ufficiale per gestire &lt;strong&gt;Microsoft Azure&lt;/strong&gt;:&lt;/p&gt;
&lt;pre class=&quot;language-powershell&quot;&gt;&lt;code class=&quot;language-powershell&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Installa il modulo Az&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Install-Module&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Name Az &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Scope CurrentUser &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Repository PSGallery &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Force

&lt;span class=&quot;token comment&quot;&gt;# Autenticazione ad Azure&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Connect-AzAccount&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Operazioni comuni&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Get-AzResourceGroup&lt;/span&gt;                          &lt;span class=&quot;token comment&quot;&gt;# Elenca i Resource Group&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Get-AzVM&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;ResourceGroupName &lt;span class=&quot;token string&quot;&gt;&quot;MyRG&quot;&lt;/span&gt;           &lt;span class=&quot;token comment&quot;&gt;# Elenca le VM&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Esempio: avvia tutte le VM ferme in un Resource Group&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Get-AzVM&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;ResourceGroupName &lt;span class=&quot;token string&quot;&gt;&quot;MyRG&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Status &lt;span class=&quot;token punctuation&quot;&gt;|&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;Where-Object&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token variable&quot;&gt;$_&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;PowerState &lt;span class=&quot;token operator&quot;&gt;-eq&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;VM deallocated&quot;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;|&lt;/span&gt;
    &lt;span class=&quot;token function&quot;&gt;ForEach-Object&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;Start-AzVM&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;ResourceGroupName &lt;span class=&quot;token string&quot;&gt;&quot;MyRG&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Name &lt;span class=&quot;token variable&quot;&gt;$_&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Name &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Best Practice per Script Professionali&lt;/h2&gt;
&lt;h3&gt;Qualità del Codice con PSScriptAnalyzer&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;PSScriptAnalyzer&lt;/strong&gt; è il linter ufficiale per PowerShell:&lt;/p&gt;
&lt;pre class=&quot;language-powershell&quot;&gt;&lt;code class=&quot;language-powershell&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Installa&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Install-Module&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Name PSScriptAnalyzer &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Scope CurrentUser

&lt;span class=&quot;token comment&quot;&gt;# Analizza uno script&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Invoke-ScriptAnalyzer&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Path &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&#92;script&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ps1

&lt;span class=&quot;token comment&quot;&gt;# Analizza una directory intera&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Invoke-ScriptAnalyzer&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Path &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&#92;scripts&#92; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Recurse&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Testing con Pester&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Pester&lt;/strong&gt; è il framework di testing standard per PowerShell:&lt;/p&gt;
&lt;pre class=&quot;language-powershell&quot;&gt;&lt;code class=&quot;language-powershell&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Installa Pester&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Install-Module&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Name Pester &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Scope CurrentUser &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Force

&lt;span class=&quot;token comment&quot;&gt;# Esempio di test (file: Get-Saluto.Tests.ps1)&lt;/span&gt;
Describe &lt;span class=&quot;token string&quot;&gt;&quot;Get-Saluto&quot;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    It &lt;span class=&quot;token string&quot;&gt;&quot;Restituisce il saluto corretto&quot;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token variable&quot;&gt;$risultato&lt;/span&gt; = &lt;span class=&quot;token function&quot;&gt;Get-Saluto&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Nome &lt;span class=&quot;token string&quot;&gt;&quot;Mario&quot;&lt;/span&gt;
        &lt;span class=&quot;token variable&quot;&gt;$risultato&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;|&lt;/span&gt; Should &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Be &lt;span class=&quot;token string&quot;&gt;&quot;Ciao, Mario!&quot;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    It &lt;span class=&quot;token string&quot;&gt;&quot;Usa &#39;Mondo&#39; come default se nessun nome è fornito&quot;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token function&quot;&gt;Get-Saluto&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;|&lt;/span&gt; Should &lt;span class=&quot;token operator&quot;&gt;-&lt;/span&gt;Be &lt;span class=&quot;token string&quot;&gt;&quot;Ciao, Mondo!&quot;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Esegui i test&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;Invoke-Pester&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&#92;&lt;span class=&quot;token function&quot;&gt;Get-Saluto&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Tests&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;ps1&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Checklist Best Practice&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Usa &lt;code&gt;CmdletBinding&lt;/code&gt;&lt;/strong&gt; nelle funzioni per supportare &lt;code&gt;-Verbose&lt;/code&gt;, &lt;code&gt;-WhatIf&lt;/code&gt;, &lt;code&gt;-ErrorAction&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Tipizza i parametri&lt;/strong&gt; esplicitamente (&lt;code&gt;[string]&lt;/code&gt;, &lt;code&gt;[int]&lt;/code&gt;, &lt;code&gt;[Parameter(Mandatory)]&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Usa &lt;code&gt;$ErrorActionPreference = &amp;quot;Stop&amp;quot;&lt;/code&gt;&lt;/strong&gt; all&#39;inizio degli script critici&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Commenta il codice&lt;/strong&gt; con &lt;code&gt;#&lt;/code&gt; per riga e &lt;code&gt;&amp;lt;# ... #&amp;gt;&lt;/code&gt; per blocchi&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Non usare alias negli script&lt;/strong&gt; (&lt;code&gt;ls&lt;/code&gt;, &lt;code&gt;cd&lt;/code&gt;, &lt;code&gt;?&lt;/code&gt;) — usa i nomi completi per leggibilità&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Gestisci sempre le credenziali&lt;/strong&gt; con &lt;code&gt;SecureString&lt;/code&gt; o &lt;code&gt;Get-Credential&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Usa &lt;code&gt;Write-Verbose&lt;/code&gt;&lt;/strong&gt; per logging, non &lt;code&gt;Write-Host&lt;/code&gt; (che bypassa la pipeline)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Analizza con PSScriptAnalyzer&lt;/strong&gt; prima di distribuire uno script&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Scrivi test con Pester&lt;/strong&gt; per gli script riutilizzati in produzione&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Controlla la Execution Policy&lt;/strong&gt; prima del deployment&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Risorse Utili&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Documentazione ufficiale:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://learn.microsoft.com/powershell&quot;&gt;Microsoft Learn – PowerShell&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.powershellgallery.com&quot;&gt;PowerShell Gallery&lt;/a&gt; — repository di moduli e script&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://github.com/PowerShell/PowerShell&quot;&gt;GitHub – PowerShell&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Community:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https://powershell.org&quot;&gt;PowerShell.org&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://reddit.com/r/PowerShell&quot;&gt;r/PowerShell su Reddit&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Conclusione&lt;/h2&gt;
&lt;p&gt;Hai ora una base teorica e pratica solida per iniziare a usare &lt;strong&gt;PowerShell&lt;/strong&gt; in contesti reali. Gli argomenti coperti — dalla pipeline alla gestione remota, dalla sicurezza al cloud — ti posizionano bene per l&#39;uso professionale di questo strumento.&lt;/p&gt;
&lt;p&gt;La competenza reale arriva con la &lt;strong&gt;pratica costante&lt;/strong&gt;: automatizza le tue attività quotidiane, analizza i tuoi script con PSScriptAnalyzer e, quando sei pronto, scrivi i tuoi primi test con Pester.&lt;/p&gt;
]]></content>
    
    
      
    
      
    <category term="powershell"/>
      
    
  </entry>
  
  <entry>
    <title>Fondamenti di Networking</title>
    <link href="https://dino-996.github.io/blog/fondamenti-di-networking/" rel="alternate" type="text/html"/>
    <id>https://dino-996.github.io/blog/fondamenti-di-networking/</id>
    <published>2020-03-12T00:00:00.000Z</published>
    <updated>2020-03-12T00:00:00.000Z</updated>
    
    <summary>Una prima introduzione sulle basi del networking, le fondamenta che ci permetteranno di capire come funzionano realmente le reti a cui ci connettiamo tutti i giorni.</summary>
    
    
    <content type="html"><![CDATA[&lt;h1&gt;Fondamenti di networking&lt;/h1&gt;
&lt;p&gt;Oggi non ci chiediamo più se siamo connessi a Internet. La connettività è diventata un&#39;infrastruttura critica, al pari dell&#39;elettricità o dell&#39;acqua corrente. Comprendere come funzionano le reti non è più prerogativa esclusiva degli ingegneri informatici, è una competenza fondamentale per chiunque lavori nel mondo digitale, dallo sviluppatore all&#39;IT Manager.&lt;/p&gt;
&lt;h2&gt;Chi possiede internet?&lt;/h2&gt;
&lt;p&gt;Internet non appartiene a nessun singolo gruppo, azienda o governo. È il risultato
dell&#39;interconnessione di migliaia di reti autonome che cooperano attraverso &lt;strong&gt;standard
e protocolli comuni&lt;/strong&gt; (come TCP/IP) per scambiarsi informazioni.&lt;/p&gt;
&lt;p&gt;Le informazioni viaggiano attraverso una varietà di mezzi fisici:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Cavi in rame&lt;/strong&gt; (doppino telefonico, cavo coassiale)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Fibra ottica&lt;/strong&gt; (trasmissione tramite impulsi luminosi)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Connessioni wireless&lt;/strong&gt; (Wi-Fi, 4G/5G, satellite)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I dati scambiati possono assumere forme diverse: testo, video, audio, immagini,
telemetria IoT e molto altro.&lt;/p&gt;
&lt;h2&gt;Reti locali (LAN)&lt;/h2&gt;
&lt;p&gt;Le reti locali (Local Area Network) esistono in tutte le dimensioni e configurazioni.&lt;/p&gt;
&lt;h3&gt;Reti SOHO (Small Office / Home Office)&lt;/h3&gt;
&lt;p&gt;Le reti SOHO collegano un numero ridotto di dispositivi (da 2 a qualche decina) e
consentono la condivisione di risorse come stampanti, file, musica e connessione
Internet. Sono tipicamente basate su un router consumer con funzionalità Wi-Fi
integrate.&lt;/p&gt;
&lt;h3&gt;Reti aziendali&lt;/h3&gt;
&lt;p&gt;Le reti aziendali possono connettere centinaia di migliaia di dispositivi distribuiti
su più sedi geografiche. Supportano applicazioni critiche come:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;E-commerce e CRM&lt;/li&gt;
&lt;li&gt;Comunicazioni interne (email, VoIP, videoconferenza)&lt;/li&gt;
&lt;li&gt;Accesso centralizzato a server e database&lt;/li&gt;
&lt;li&gt;Sistemi ERP e supply chain management&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;La comunicazione in rete è generalmente più efficiente e meno costosa rispetto ai
canali tradizionali. Le reti aziendali e SOHO forniscono tipicamente una &lt;strong&gt;connessione
condivisa a Internet&lt;/strong&gt;, motivo per cui Internet viene spesso definita una
&lt;strong&gt;&amp;quot;rete di reti&amp;quot;&lt;/strong&gt; (&lt;em&gt;network of networks&lt;/em&gt;).&lt;/p&gt;
&lt;h2&gt;Dispositivi mobili e IoT&lt;/h2&gt;
&lt;p&gt;Internet connette oggi miliardi di dispositivi che vanno ben oltre i tradizionali
computer desktop e laptop.&lt;/p&gt;
&lt;h3&gt;Dispositivi mobili&lt;/h3&gt;
&lt;p&gt;Smartphone, tablet e wearable (smartwatch, fitness tracker) sono oggi tra i
principali produttori e consumatori di traffico Internet mondiale.&lt;/p&gt;
&lt;h3&gt;Internet of Things (IoT)&lt;/h3&gt;
&lt;p&gt;L&#39;&lt;strong&gt;IoT (Internet of Things)&lt;/strong&gt; rappresenta l&#39;ecosistema di oggetti fisici dotati di
sensori, software e connettività di rete. L&#39;architettura IoT si articola tipicamente
su tre livelli:&lt;/p&gt;
&lt;div class=&quot;container my-3&quot;&gt;
  &lt;div class=&quot;table-responsive shadow-sm rounded&quot;&gt;
    &lt;table class=&quot;table table-bordered table-striped table-hover align-middle mb-0&quot;&gt;
      &lt;thead class=&quot;table-dark&quot;&gt;
        &lt;tr&gt;
          &lt;th&gt;Cloud / Data Center&lt;/th&gt;
          &lt;th&gt;Edge / Gateway&lt;/th&gt;
          &lt;th&gt;Dispositivi / End Nodes&lt;/th&gt;
        &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td class=&quot;fw-semibold&quot;&gt;Elaborazione, storage, analytics&lt;/td&gt;
          &lt;td class=&quot;fw-semibold&quot;&gt;Pre-elaborazione locale, protocolli (MQTT, CoAP)&lt;/td&gt;
          &lt;td class=&quot;fw-semibold&quot;&gt;&lt;code&gt;Sensori, attuatori, RFID&lt;/code&gt;&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Esempi concreti di dispositivi IoT:&lt;/p&gt;
&lt;div class=&quot;container my-3&quot;&gt;
  &lt;div class=&quot;table-responsive shadow-sm rounded&quot;&gt;
    &lt;table class=&quot;table table-bordered table-striped table-hover align-middle mb-0&quot;&gt;
      &lt;thead class=&quot;table-dark&quot;&gt;
        &lt;tr&gt;
          &lt;th&gt;Tag RFID&lt;/th&gt;
          &lt;th&gt;Sensori industriali&lt;/th&gt;
          &lt;th&gt;Dispositivi medici&lt;/th&gt;
          &lt;th&gt;Domotica&lt;/th&gt;
          &lt;th&gt;Veicoli connessi&lt;/th&gt;
        &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td class=&quot;fw-semibold&quot;&gt;Tracciamento merci, badge aziendali, antifurti&lt;/td&gt;
          &lt;td class=&quot;fw-semibold&quot;&gt;Temperatura, pressione, vibrazione (Industry 4.0)&lt;/td&gt;
          &lt;td class=&quot;fw-semibold&quot;&gt;Glucometri connessi, pacemaker smart, telemedicina&lt;/td&gt;
          &lt;td class=&quot;fw-semibold&quot;&gt;Termostati intelligenti, serrature smart, telecamere IP&lt;/td&gt;
          &lt;td class=&quot;fw-semibold&quot;&gt;Telemetria auto, fleet management, guida assistita&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Nota per sviluppatori&lt;/strong&gt;: i protocolli più diffusi in ambito IoT sono &lt;strong&gt;MQTT&lt;/strong&gt;
(publish/subscribe, leggero, ideale per connessioni instabili) e &lt;strong&gt;CoAP&lt;/strong&gt;
(ottimizzato per dispositivi con risorse limitate). Entrambi operano sopra TCP/IP.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;Il Bit: L&#39;Unità fondamentale dell&#39;informazione&lt;/h2&gt;
&lt;p&gt;I computer e le reti operano esclusivamente con &lt;strong&gt;cifre binarie&lt;/strong&gt;: zeri e uno.
Il termine &lt;strong&gt;bit&lt;/strong&gt; (abbreviazione di &lt;em&gt;binary digit&lt;/em&gt;) rappresenta la più piccola
unità di informazione digitale.&lt;/p&gt;
&lt;p&gt;Un bit può assumere solo due stati fisici distinti:&lt;/p&gt;
&lt;div class=&quot;container my-3&quot;&gt;
  &lt;div class=&quot;table-responsive shadow-sm rounded&quot;&gt;
    &lt;table class=&quot;table table-bordered table-striped table-hover align-middle mb-0&quot;&gt;
      &lt;thead class=&quot;table-dark&quot;&gt;
        &lt;tr&gt;
          &lt;th&gt;Stato fisico&lt;/th&gt;
          &lt;th&gt;Valore binario&lt;/th&gt;
        &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td class=&quot;fw-semibold&quot;&gt;Interruttore aperto&lt;/td&gt;
          &lt;td class=&quot;fw-semibold&quot;&gt;&lt;code&gt;1&lt;/code&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
        &lt;td class=&quot;fw-semibold&quot;&gt;Interruttore chiuso&lt;/td&gt;
          &lt;td class=&quot;fw-semibold&quot;&gt;&lt;code&gt;0&lt;/code&gt;&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;h3&gt;Byte e codifica dei caratteri&lt;/h3&gt;
&lt;p&gt;Otto bit formano un &lt;strong&gt;byte&lt;/strong&gt;, l&#39;unità standard per rappresentare un singolo carattere
o valore.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;ASCII e Unicode&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;⚠️ &lt;strong&gt;Precisazione tecnica&lt;/strong&gt;: l&#39;ASCII &lt;em&gt;originale&lt;/em&gt; (standard ANSI X3.4-1968)
utilizza &lt;strong&gt;7 bit&lt;/strong&gt;, codificando 128 caratteri. La versione &amp;quot;Extended ASCII&amp;quot;
estende a 8 bit (256 caratteri). Tuttavia, per applicazioni moderne e
internazionali, si utilizza &lt;strong&gt;Unicode&lt;/strong&gt; (standard UTF-8), che supporta oltre
1.1 milioni di caratteri e mantiene compatibilità retroattiva con ASCII.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Esempi di codifica ASCII (Extended, 8 bit):&lt;/p&gt;
&lt;div class=&quot;container my-3&quot;&gt;
  &lt;div class=&quot;table-responsive shadow-sm rounded&quot;&gt;
    &lt;table class=&quot;table table-bordered table-striped table-hover align-middle mb-0&quot;&gt;
      &lt;thead class=&quot;table-dark&quot;&gt;
        &lt;tr&gt;
          &lt;th&gt;Carattere&lt;/th&gt;
          &lt;th&gt;Binario&lt;/th&gt;
          &lt;th&gt;Decimale&lt;/th&gt;
          &lt;th&gt;Esadecimale (Hex)&lt;/th&gt;
        &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td class=&quot;fw-semibold&quot;&gt;&lt;code&gt;A&lt;/code&gt;&lt;/td&gt;
          &lt;td class=&quot;fw-semibold&quot;&gt;&lt;code&gt;01000001&lt;/code&gt;&lt;/td&gt;
          &lt;td class=&quot;fw-semibold&quot;&gt;&lt;code&gt;65&lt;/code&gt;&lt;/td&gt;
          &lt;td class=&quot;fw-semibold&quot;&gt;&lt;code&gt;0x41&lt;/code&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
          &lt;td class=&quot;fw-semibold&quot;&gt;&lt;code&gt;9&lt;/code&gt;&lt;/td&gt;
          &lt;td class=&quot;fw-semibold&quot;&gt;&lt;code&gt;00111001&lt;/code&gt;&lt;/td&gt;
          &lt;td class=&quot;fw-semibold&quot;&gt;&lt;code&gt;57&lt;/code&gt;&lt;/td&gt;
          &lt;td class=&quot;fw-semibold&quot;&gt;&lt;code&gt;0x39&lt;/code&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
          &lt;td class=&quot;fw-semibold&quot;&gt;&lt;code&gt;#&lt;/code&gt;&lt;/td&gt;
          &lt;td class=&quot;fw-semibold&quot;&gt;&lt;code&gt;00100011&lt;/code&gt;&lt;/td&gt;
          &lt;td class=&quot;fw-semibold&quot;&gt;&lt;code&gt;35&lt;/code&gt;&lt;/td&gt;
          &lt;td class=&quot;fw-semibold&quot;&gt;&lt;code&gt;0x23&lt;/code&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
          &lt;td class=&quot;fw-semibold&quot;&gt;&lt;code&gt;€&lt;/code&gt;&lt;/td&gt;
          &lt;td&gt;Non codificabile in ASCII. Richiede UTF-8&lt;/td&gt;
          &lt;td&gt;-&lt;/td&gt;
          &lt;td&gt;-&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;h3&gt;Conversione binaria in Python&lt;/h3&gt;
&lt;pre class=&quot;language-python&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Conversione tra carattere, decimale e binario in Python&lt;/span&gt;

carattere &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;A&#39;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Ottieni il valore decimale (code point Unicode)&lt;/span&gt;
decimale &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;ord&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;carattere&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Carattere : &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;carattere&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Decimale  : &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;decimale&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;          &lt;span class=&quot;token comment&quot;&gt;# Output: 65&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Binario   : &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;bin&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;decimale&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;     &lt;span class=&quot;token comment&quot;&gt;# Output: 0b1000001&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Hex       : &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;hex&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;decimale&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;     &lt;span class=&quot;token comment&quot;&gt;# Output: 0x41&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Padding a 8 bit (formato byte completo)&lt;/span&gt;
binario_8bit &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;decimale&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&#39;08b&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Byte      : &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;binario_8bit&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;      &lt;span class=&quot;token comment&quot;&gt;# Output: 01000001&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Operazione inversa: da binario a carattere&lt;/span&gt;
valore &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token builtin&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&#39;01000001&#39;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string-interpolation&quot;&gt;&lt;span class=&quot;token string&quot;&gt;f&quot;Da bin a char: &lt;/span&gt;&lt;span class=&quot;token interpolation&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token builtin&quot;&gt;chr&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;valore&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;    &lt;span class=&quot;token comment&quot;&gt;# Output: A&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Metodi di trasmissione dei dati&lt;/h2&gt;
&lt;p&gt;Dopo che le informazioni sono state convertite in bit, devono essere trasformate in
&lt;strong&gt;segnali fisici&lt;/strong&gt; trasmissibili attraverso un mezzo (&lt;em&gt;medium&lt;/em&gt;). Esistono tre metodi
principali:&lt;/p&gt;
&lt;h3&gt;1. Segnali elettrici&lt;/h3&gt;
&lt;p&gt;Trasmissione tramite impulsi di tensione su &lt;strong&gt;cavi in rame&lt;/strong&gt; (es. cavo Cat5e/Cat6
per reti Ethernet). Economici e diffusi, ma soggetti a interferenze elettromagnetiche
(EMI) e attenuazione sul lungo raggio.&lt;/p&gt;
&lt;h3&gt;2. Segnali ottici&lt;/h3&gt;
&lt;p&gt;Trasmissione tramite &lt;strong&gt;impulsi luminosi&lt;/strong&gt; su cavi in fibra ottica (single-mode o
multi-mode). Offrono larghezza di banda elevatissima, immunità alle interferenze e
distanze di trasmissione fino a centinaia di chilometri.&lt;/p&gt;
&lt;h3&gt;3. Segnali wireless&lt;/h3&gt;
&lt;p&gt;Trasmissione tramite &lt;strong&gt;onde elettromagnetiche&lt;/strong&gt; (radio, microonde, infrarossi).
Elimina la necessità di cablaggio fisico ma è soggetto a interferenze, ostacoli
fisici e limitazioni di sicurezza.&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Mezzo&lt;/th&gt;
&lt;th&gt;Velocità tipica&lt;/th&gt;
&lt;th&gt;Distanza&lt;/th&gt;
&lt;th&gt;Pro&lt;/th&gt;
&lt;th&gt;Contro&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Rame (Cat6)&lt;/td&gt;
&lt;td&gt;1–10 Gbps&lt;/td&gt;
&lt;td&gt;≤100 m&lt;/td&gt;
&lt;td&gt;Economico&lt;/td&gt;
&lt;td&gt;Interferenze, distanza&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Fibra ottica&lt;/td&gt;
&lt;td&gt;10 Gbps–400 Tbps&lt;/td&gt;
&lt;td&gt;km–migliaia di km&lt;/td&gt;
&lt;td&gt;Velocità, sicurezza&lt;/td&gt;
&lt;td&gt;Costo installazione&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Wi-Fi (802.11ax)&lt;/td&gt;
&lt;td&gt;Fino a ~9.6 Gbps teorici&lt;/td&gt;
&lt;td&gt;~50–100 m indoor&lt;/td&gt;
&lt;td&gt;Mobilità&lt;/td&gt;
&lt;td&gt;Interferenze, sicurezza&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5G&lt;/td&gt;
&lt;td&gt;Fino a ~20 Gbps teorici&lt;/td&gt;
&lt;td&gt;Variabile&lt;/td&gt;
&lt;td&gt;Mobilità, copertura&lt;/td&gt;
&lt;td&gt;Infrastruttura&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;blockquote&gt;
&lt;p&gt;I segnali possono essere &lt;strong&gt;convertiti più volte&lt;/strong&gt; lungo il percorso
(es. elettrico → ottico → wireless) attraverso dispositivi come convertitori
di media (&lt;em&gt;media converters&lt;/em&gt;) e access point.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;Larghezza di Banda, Throughput, Latenza e Jitter&lt;/h2&gt;
&lt;p&gt;Questi quattro parametri sono fondamentali per valutare le &lt;strong&gt;prestazioni di una
rete&lt;/strong&gt;. Vengono spesso confusi: ecco le distinzioni precise.&lt;/p&gt;
&lt;h3&gt;Bandwidth (larghezza di banda)&lt;/h3&gt;
&lt;p&gt;La &lt;strong&gt;bandwidth&lt;/strong&gt; rappresenta la &lt;em&gt;capacità teorica massima&lt;/em&gt; di un collegamento,
ovvero quanti bit possono essere trasmessi per unità di tempo in condizioni ideali.
È una proprietà del mezzo fisico e dell&#39;infrastruttura.&lt;/p&gt;
&lt;h3&gt;Throughput (velocità effettiva)&lt;/h3&gt;
&lt;p&gt;Il &lt;strong&gt;throughput&lt;/strong&gt; misura la quantità di dati &lt;em&gt;effettivamente&lt;/em&gt; trasferiti in un dato
intervallo di tempo. È quasi sempre inferiore alla bandwidth dichiarata, a causa di:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Overhead dei protocolli di rete (header TCP/IP, controllo errori)&lt;/li&gt;
&lt;li&gt;Congestione e collisioni&lt;/li&gt;
&lt;li&gt;Qualità del collegamento fisico&lt;/li&gt;
&lt;li&gt;Limitazioni hardware dei dispositivi intermedi&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Latenza&lt;/h3&gt;
&lt;p&gt;La &lt;strong&gt;latenza&lt;/strong&gt; è il tempo necessario affinché un pacchetto viaggi dalla sorgente
alla destinazione. Si misura tipicamente in millisecondi (ms) tramite il comando
&lt;code&gt;ping&lt;/code&gt;. È critica per applicazioni &lt;strong&gt;real-time&lt;/strong&gt; come VoIP, gaming online e
videoconferenze.&lt;/p&gt;
&lt;pre class=&quot;language-bash&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Misurazione latenza verso un host remoto&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;ping&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-c&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;8.8&lt;/span&gt;.8.8

&lt;span class=&quot;token comment&quot;&gt;# Output di esempio:&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# 64 bytes from 8.8.8.8: icmp_seq=1 ttl=118 time=12.4 ms&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# 64 bytes from 8.8.8.8: icmp_seq=2 ttl=118 time=11.8 ms&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# round-trip min/avg/max/stddev = 11.8/12.1/12.4/0.3 ms&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Jitter&lt;/h3&gt;
&lt;p&gt;Il &lt;strong&gt;jitter&lt;/strong&gt; è la &lt;em&gt;variazione&lt;/em&gt; della latenza nel tempo (la deviazione standard
della latenza). Un jitter elevato causa degrado della qualità audio/video nelle
comunicazioni real-time, anche quando la latenza media è accettabile.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Latenza ideale:  10ms — 10ms — 10ms — 10ms  (jitter ≈ 0)
Latenza reale:   10ms — 25ms —  8ms — 30ms  (jitter elevato → qualità VoIP scarsa)
&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Tabella unità di misura della bandwidth&lt;/h3&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Unità&lt;/th&gt;
&lt;th&gt;Simbolo&lt;/th&gt;
&lt;th&gt;Equivalente&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Bit per secondo&lt;/td&gt;
&lt;td&gt;bps&lt;/td&gt;
&lt;td&gt;unità base&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Kilobit per secondo&lt;/td&gt;
&lt;td&gt;Kbps&lt;/td&gt;
&lt;td&gt;10³ bps&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Megabit per secondo&lt;/td&gt;
&lt;td&gt;Mbps&lt;/td&gt;
&lt;td&gt;10⁶ bps&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Gigabit per secondo&lt;/td&gt;
&lt;td&gt;Gbps&lt;/td&gt;
&lt;td&gt;10⁹ bps&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Terabit per secondo&lt;/td&gt;
&lt;td&gt;Tbps&lt;/td&gt;
&lt;td&gt;10¹² bps&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Petabit per secondo&lt;/td&gt;
&lt;td&gt;Pbps&lt;/td&gt;
&lt;td&gt;10¹⁵ bps&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;blockquote&gt;
&lt;p&gt;⚠️ &lt;strong&gt;Attenzione&lt;/strong&gt;: i provider Internet dichiarano la velocità in &lt;strong&gt;Mbps&lt;/strong&gt;
(megabit), mentre i sistemi operativi mostrano spesso la velocità di download
in &lt;strong&gt;MB/s&lt;/strong&gt; (megabyte). Ricorda: 1 byte = 8 bit → 100 Mbps ≈ 12.5 MB/s.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;Il collo di bottiglia (&lt;em&gt;Bottleneck&lt;/em&gt;)&lt;/h3&gt;
&lt;p&gt;In una rete con più segmenti, la &lt;strong&gt;velocità massima effettiva è limitata dal
segmento più lento&lt;/strong&gt; del percorso — il cosiddetto &lt;em&gt;bottleneck&lt;/em&gt;. Questo principio,
noto come &lt;strong&gt;legge dell&#39;anello debole&lt;/strong&gt;, è fondamentale nell&#39;ottimizzazione delle
prestazioni di rete in contesti Cloud e DevOps.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Client (1 Gbps) ──→ Switch (10 Gbps) ──→ Router WAN (100 Mbps) ──→ Server
                                               ↑
                                     BOTTLENECK: throughput massimo = 100 Mbps
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Ottimizzazione delle performance di rete&lt;/h2&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Per IT Manager e DevOps Engineer&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Monitoring continuo&lt;/strong&gt;: usa strumenti come &lt;strong&gt;Prometheus + Grafana&lt;/strong&gt;, &lt;strong&gt;Zabbix&lt;/strong&gt;
o &lt;strong&gt;PRTG&lt;/strong&gt; per monitorare latenza, throughput e packet loss in tempo reale.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Quality of Service (QoS)&lt;/strong&gt;: prioritizza il traffico critico (VoIP, video) su
quello non critico (backup, aggiornamenti) tramite policy QoS su switch e router.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Segmentazione della rete&lt;/strong&gt;: usa VLAN per isolare traffico e migliorare sicurezza
e performance.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;CDN (Content Delivery Network)&lt;/strong&gt;: distribuisci i contenuti statici vicino agli
utenti finali per ridurre latenza e carico sui server centrali.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Test periodici&lt;/strong&gt;: strumenti come &lt;strong&gt;iPerf3&lt;/strong&gt; (throughput interno) e
&lt;strong&gt;Speedtest CLI&lt;/strong&gt; (connettività esterna) aiutano a rilevare degradi prestazionali
prima che diventino problemi critici.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;language-bash&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Test throughput interno con iPerf3&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# Sul server:&lt;/span&gt;
iperf3 &lt;span class=&quot;token parameter variable&quot;&gt;-s&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;# Sul client:&lt;/span&gt;
iperf3 &lt;span class=&quot;token parameter variable&quot;&gt;-c&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt;IP_SERVER&lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-t&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-P&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;4&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# -t 10 = durata 10 secondi&lt;/span&gt;
&lt;span class=&quot;token comment&quot;&gt;# -P 4  = 4 stream paralleli&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Introduzione al modello OSI&lt;/h2&gt;
&lt;p&gt;Tutti i concetti trattati in questo modulo — trasmissione dei dati, segnali,
larghezza di banda — trovano una collocazione precisa nel &lt;strong&gt;Modello OSI&lt;/strong&gt;
(Open Systems Interconnection), il framework a 7 livelli che descrive come i
dati viaggiano da un&#39;applicazione su un dispositivo a un&#39;altra applicazione su
un dispositivo remoto.&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Livello&lt;/th&gt;
&lt;th&gt;Nome&lt;/th&gt;
&lt;th&gt;Funzione chiave&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;Applicazione&lt;/td&gt;
&lt;td&gt;HTTP, DNS, SMTP&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;Presentazione&lt;/td&gt;
&lt;td&gt;Codifica, crittografia, compressione&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;Sessione&lt;/td&gt;
&lt;td&gt;Gestione connessioni&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;Trasporto&lt;/td&gt;
&lt;td&gt;TCP/UDP, porte, controllo flusso&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Rete&lt;/td&gt;
&lt;td&gt;IP, routing&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Data Link&lt;/td&gt;
&lt;td&gt;MAC, switch, Ethernet&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;Fisico&lt;/td&gt;
&lt;td&gt;Bit, segnali, cavi ← &lt;strong&gt;questo modulo&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
]]></content>
    
    
      
    
      
    <category term="networking"/>
      
    
  </entry>
  
  <entry>
    <title>Tecniche algoritmiche per dev Java</title>
    <link href="https://dino-996.github.io/blog/tecniche-algoritmiche-per-dev-java/" rel="alternate" type="text/html"/>
    <id>https://dino-996.github.io/blog/tecniche-algoritmiche-per-dev-java/</id>
    <published>2020-03-05T00:00:00.000Z</published>
    <updated>2020-03-05T00:00:00.000Z</updated>
    
    <summary>Questa guida raccoglie le tecniche algoritmiche più comuni utilizzate nello sviluppo software quotidiano. Le seguenti tecniche sono descritte il linguaggio Java.</summary>
    
    
    <content type="html"><![CDATA[&lt;h1&gt;Introduzione&lt;/h1&gt;
&lt;p&gt;L&#39;obiettivo di questa guida non è una trattazione accademica, ma offrire strumenti pratici per risolvere problemi ricorrenti quando si lavora con collezioni di dati.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Versione di riferimento:&lt;/strong&gt; Java 17 LTS (o superiore). Gli esempi sfruttano feature
moderne come &lt;code&gt;record&lt;/code&gt;, &lt;code&gt;var&lt;/code&gt;, &lt;code&gt;Stream API&lt;/code&gt; e &lt;code&gt;Optional&lt;/code&gt;. Alcuni snippet sono compatibili
anche con Java 11+, dove indicato.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Gli esempi si concentrano sull&#39;uso delle strutture dati della libreria standard:
&lt;code&gt;List&lt;/code&gt;, &lt;code&gt;Set&lt;/code&gt; e &lt;code&gt;Map&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Per ogni tecnica sono presentati:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;il problema da risolvere&lt;/li&gt;
&lt;li&gt;la complessità computazionale (notazione Big-O)&lt;/li&gt;
&lt;li&gt;un&#39;implementazione imperativa&lt;/li&gt;
&lt;li&gt;un&#39;alternativa funzionale con Stream API&lt;/li&gt;
&lt;li&gt;note sui casi limite e le best practice&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h3&gt;La classe &lt;code&gt;Product&lt;/code&gt; usata negli esempi&lt;/h3&gt;
&lt;p&gt;Per mantenere gli esempi concisi e moderni, utilizziamo un &lt;strong&gt;Java Record&lt;/strong&gt; — introdotto
in Java 16 e stabile da Java 17. I record sono ideali per modellare dati immutabili
(Value Object, DTO).&lt;/p&gt;
&lt;pre class=&quot;language-java&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;record&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Product&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; codice&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; nome&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;double&lt;/span&gt; prezzo&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;boolean&lt;/span&gt; disponibile
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Perché un &lt;code&gt;record&lt;/code&gt;?&lt;/strong&gt; Rispetto a una classe tradizionale, il compilatore genera
automaticamente costruttore, getter, &lt;code&gt;equals()&lt;/code&gt;, &lt;code&gt;hashCode()&lt;/code&gt; e &lt;code&gt;toString()&lt;/code&gt;.
Perfetto per oggetti di sola lettura come i prodotti di un catalogo.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;hr&gt;
&lt;h2&gt;Iterazione e Aggregazione&lt;/h2&gt;
&lt;p&gt;Molte operazioni software richiedono di attraversare una collezione per produrre
un risultato aggregato: una somma, una media, un conteggio o una verifica logica.&lt;/p&gt;
&lt;p&gt;Queste operazioni condividono lo stesso schema:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Attraversamento sequenziale&lt;/strong&gt; della collezione&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Accumulazione&lt;/strong&gt; di uno stato&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Restituzione&lt;/strong&gt; di un risultato&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;La complessità è generalmente &lt;strong&gt;O(n)&lt;/strong&gt;, dove &lt;em&gt;n&lt;/em&gt; è il numero di elementi.&lt;/p&gt;
&lt;h2&gt;Somma di una collezione&lt;/h2&gt;
&lt;h3&gt;Problema&lt;/h3&gt;
&lt;p&gt;Calcolare la somma di una lista di valori numerici.&lt;/p&gt;
&lt;h3&gt;Implementazione imperativa&lt;/h3&gt;
&lt;pre class=&quot;language-java&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;calcolaTotale&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Double&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; importi&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;Objects&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;requireNonNull&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;importi&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;La lista degli importi non può essere null&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;double&lt;/span&gt; totale &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;double&lt;/span&gt; importo &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; importi&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        totale &lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt; importo&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; totale&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Complessità: O(n)&lt;/strong&gt;&lt;/p&gt;
&lt;h3&gt;Versione con Stream API&lt;/h3&gt;
&lt;pre class=&quot;language-java&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;calcolaTotale&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Double&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; importi&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;Objects&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;requireNonNull&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;importi&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;La lista degli importi non può essere null&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; importi&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;mapToDouble&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Double&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;doubleValue&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Nota:&lt;/strong&gt; &lt;code&gt;mapToDouble()&lt;/code&gt; restituisce un &lt;code&gt;DoubleStream&lt;/code&gt; primitivo, evitando il boxing/
unboxing di &lt;code&gt;Double&lt;/code&gt; → &lt;code&gt;double&lt;/code&gt;. Preferirlo sempre per operazioni numeriche intensive.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;Calcolo della media&lt;/h2&gt;
&lt;h3&gt;Problema&lt;/h3&gt;
&lt;p&gt;Calcolare la media dei valori presenti in una lista.&lt;/p&gt;
&lt;h3&gt;Implementazione imperativa&lt;/h3&gt;
&lt;pre class=&quot;language-java&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;calcolaMedia&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Double&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; valori&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;Objects&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;requireNonNull&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;valori&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;La lista non può essere null&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;valori&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;isEmpty&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;IllegalArgumentException&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;La lista non può essere vuota&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;double&lt;/span&gt; somma &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;double&lt;/span&gt; valore &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; valori&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        somma &lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt; valore&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; somma &lt;span class=&quot;token operator&quot;&gt;/&lt;/span&gt; valori&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;size&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Versione con Stream API&lt;/h3&gt;
&lt;pre class=&quot;language-java&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;double&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;calcolaMedia&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Double&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; valori&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;Objects&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;requireNonNull&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;valori&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;La lista non può essere null&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; valori&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;mapToDouble&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Double&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;doubleValue&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;average&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;orElseThrow&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;IllegalArgumentException&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Lista vuota&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Conteggio condizionale&lt;/h2&gt;
&lt;h3&gt;Problema&lt;/h3&gt;
&lt;p&gt;Contare quanti elementi soddisfano una certa condizione.
&lt;em&gt;Esempio: quanti importi superano una soglia.&lt;/em&gt;&lt;/p&gt;
&lt;h3&gt;Implementazione imperativa&lt;/h3&gt;
&lt;pre class=&quot;language-java&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;contaImportiMaggioriDi&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Double&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; importi&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;double&lt;/span&gt; soglia&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;Objects&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;requireNonNull&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;importi&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;La lista non può essere null&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;long&lt;/span&gt; conteggio &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;double&lt;/span&gt; importo &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; importi&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;importo &lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; soglia&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            conteggio&lt;span class=&quot;token operator&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; conteggio&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Versione con Stream API&lt;/h3&gt;
&lt;pre class=&quot;language-java&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;contaImportiMaggioriDi&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Double&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; importi&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;double&lt;/span&gt; soglia&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; importi&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;i &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; i &lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; soglia&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Verifiche logiche su collezioni&lt;/h2&gt;
&lt;h3&gt;Esiste almeno un elemento sotto soglia? (&lt;code&gt;anyMatch&lt;/code&gt;)&lt;/h3&gt;
&lt;pre class=&quot;language-java&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Imperativa&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;boolean&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;esisteImportoSottoSoglia&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Double&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; importi&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;double&lt;/span&gt; soglia&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;double&lt;/span&gt; importo &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; importi&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;importo &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; soglia&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// Stream API&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;boolean&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;esisteImportoSottoSoglia&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Double&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; importi&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;double&lt;/span&gt; soglia&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; importi&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;anyMatch&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;importo &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; importo &lt;span class=&quot;token operator&quot;&gt;&amp;lt;&lt;/span&gt; soglia&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Tutti gli elementi sono sopra soglia? (&lt;code&gt;allMatch&lt;/code&gt;)&lt;/h3&gt;
&lt;pre class=&quot;language-java&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Imperativa&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;boolean&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;tuttiSopraSoglia&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Double&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; importi&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;double&lt;/span&gt; soglia&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;double&lt;/span&gt; importo &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; importi&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;importo &lt;span class=&quot;token operator&quot;&gt;&amp;lt;=&lt;/span&gt; soglia&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// Stream API&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;boolean&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;tuttiSopraSoglia&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Double&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; importi&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;double&lt;/span&gt; soglia&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; importi&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;allMatch&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;importo &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; importo &lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; soglia&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Short-circuit evaluation:&lt;/strong&gt; Sia la versione imperativa che &lt;code&gt;anyMatch&lt;/code&gt;/&lt;code&gt;allMatch&lt;/code&gt;
interrompono l&#39;iterazione non appena la condizione è soddisfatta (o falsificata).
Su liste grandi questo può fare una differenza significativa di performance.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;Ricerca e Filtraggio&lt;/h2&gt;
&lt;p&gt;La ricerca di elementi in una collezione è tra le operazioni più frequenti. La scelta
dell&#39;algoritmo e della struttura dati ha un impatto diretto sulle prestazioni.&lt;/p&gt;
&lt;h2&gt;Ricerca lineare&lt;/h2&gt;
&lt;h3&gt;Problema&lt;/h3&gt;
&lt;p&gt;Trovare un elemento in una lista tramite un campo chiave.&lt;/p&gt;
&lt;h3&gt;Implementazione imperativa (con &lt;code&gt;Optional&lt;/code&gt;)&lt;/h3&gt;
&lt;pre class=&quot;language-java&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Optional&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Product&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;trovaPerCodice&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Product&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; prodotti&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; codice&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;Objects&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;requireNonNull&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;codice&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;Il codice non può essere null&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Product&lt;/span&gt; prodotto &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; prodotti&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;prodotto&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;codice&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;equals&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;codice&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Optional&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;of&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;prodotto&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Optional&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;empty&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Evita &lt;code&gt;null&lt;/code&gt; come valore di ritorno.&lt;/strong&gt; Restituire &lt;code&gt;null&lt;/code&gt; da un metodo di ricerca è
una fonte comune di &lt;code&gt;NullPointerException&lt;/code&gt;. Usa sempre &lt;code&gt;Optional&amp;lt;T&amp;gt;&lt;/code&gt; per segnalare
esplicitamente la possibile assenza di un risultato.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;strong&gt;Complessità: O(n)&lt;/strong&gt;&lt;/p&gt;
&lt;h3&gt;Versione con Stream API&lt;/h3&gt;
&lt;pre class=&quot;language-java&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Optional&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Product&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;trovaPerCodice&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Product&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; prodotti&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; codice&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; prodotti&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;p &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;codice&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;equals&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;codice&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;findFirst&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Filtraggio multiplo&lt;/h2&gt;
&lt;h3&gt;Problema&lt;/h3&gt;
&lt;p&gt;Estrarre tutti i prodotti che soddisfano più condizioni contemporaneamente.&lt;/p&gt;
&lt;h3&gt;Implementazione imperativa&lt;/h3&gt;
&lt;pre class=&quot;language-java&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Product&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;filtraProdotti&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Product&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; prodotti&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;double&lt;/span&gt; prezzoMinimo&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Product&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; risultato &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ArrayList&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Product&lt;/span&gt; prodotto &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; prodotti&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;prodotto&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;prezzo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; prezzoMinimo &lt;span class=&quot;token operator&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; prodotto&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;disponibile&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            risultato&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;prodotto&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; risultato&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Versione con Stream API&lt;/h3&gt;
&lt;pre class=&quot;language-java&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Product&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;filtraProdotti&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Product&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; prodotti&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;double&lt;/span&gt; prezzoMinimo&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; prodotti&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;p &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;prezzo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; prezzoMinimo&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Product&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;disponibile&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;toList&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// Restituisce una lista immutabile (Java 16+)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;toList()&lt;/code&gt; vs &lt;code&gt;collect(Collectors.toList())&lt;/code&gt;:&lt;/strong&gt; Da Java 16, &lt;code&gt;Stream.toList()&lt;/code&gt; è il
metodo preferito. Restituisce una lista &lt;strong&gt;immutabile&lt;/strong&gt;, il che rende il codice più
sicuro. Se hai bisogno di una lista mutabile, usa &lt;code&gt;collect(Collectors.toList())&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;Ricerca efficiente con &lt;code&gt;Map&lt;/code&gt;&lt;/h2&gt;
&lt;p&gt;Quando si effettuano ricerche frequenti per chiave univoca, indicizzare i dati in una
&lt;code&gt;Map&lt;/code&gt; è molto più efficiente di una ricerca lineare su &lt;code&gt;List&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&quot;language-java&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Costruzione dell&#39;indice (eseguita una sola volta)&lt;/span&gt;
&lt;span class=&quot;token class-name&quot;&gt;Map&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Product&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; prodottiPerCodice &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;HashMap&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Product&lt;/span&gt; p &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; prodotti&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    prodottiPerCodice&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;put&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;codice&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; p&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// Con Stream API (più compatto)&lt;/span&gt;
&lt;span class=&quot;token class-name&quot;&gt;Map&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Product&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; prodottiPerCodice &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; prodotti&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;collect&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Collectors&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;toMap&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Product&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;codice&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; p &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; p&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// Ricerca O(1)&lt;/span&gt;
&lt;span class=&quot;token class-name&quot;&gt;Optional&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Product&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; trovato &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Optional&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;ofNullable&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;prodottiPerCodice&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;codice&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;div class=&quot;container my-3&quot;&gt;
  &lt;div class=&quot;table-responsive shadow-sm rounded&quot;&gt;
    &lt;table class=&quot;table table-bordered table-striped table-hover align-middle mb-0&quot;&gt;
      &lt;thead class=&quot;table-dark&quot;&gt;
        &lt;tr&gt;
          &lt;th&gt;Approccio&lt;/th&gt;
          &lt;th&gt;Complessità ricerca&lt;/th&gt;
          &lt;th&gt;Note&lt;/th&gt;
        &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td class=&quot;fw-semibold&quot;&gt;&lt;code&gt;List&lt;/code&gt; + loop&lt;/td&gt;
          &lt;td&gt;O(n)&lt;/span&gt;&lt;/td&gt;
          &lt;td&gt;Semplice, nessun pre-processing&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
          &lt;td class=&quot;fw-semibold&quot;&gt;&lt;code&gt;HashMap&lt;/code&gt;&lt;/td&gt;
          &lt;td&gt;O(1) media&lt;/span&gt;&lt;/td&gt;
          &lt;td&gt;Richiede indicizzazione iniziale&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
          &lt;td class=&quot;fw-semibold&quot;&gt;&lt;code&gt;TreeMap&lt;/code&gt;&lt;/td&gt;
          &lt;td&gt;O(log n)&lt;/span&gt;&lt;/td&gt;
          &lt;td&gt;Mantiene l&#39;ordine delle chiavi&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;h2&gt;Selezione e Ordinamento&lt;/h2&gt;
&lt;p&gt;Ordinare una collezione consente di confrontare, visualizzare e cercare dati in modo
prevedibile. Java utilizza &lt;strong&gt;TimSort&lt;/strong&gt; — un algoritmo ibrido (MergeSort + InsertionSort)
ottimizzato per dati parzialmente ordinati, molto comune in scenari reali.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Complessità: O(n log n)&lt;/strong&gt; nel caso medio e peggiore.&lt;/p&gt;
&lt;h2&gt;Ordinamento crescente con &lt;code&gt;Comparable&lt;/code&gt;&lt;/h2&gt;
&lt;p&gt;Se la classe implementa &lt;code&gt;Comparable&amp;lt;T&amp;gt;&lt;/code&gt;, è possibile ordinare direttamente:&lt;/p&gt;
&lt;pre class=&quot;language-java&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;record&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Product&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; codice&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt; nome&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;double&lt;/span&gt; prezzo&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;boolean&lt;/span&gt; disponibile&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;implements&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Comparable&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Product&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;token annotation punctuation&quot;&gt;@Override&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;compareTo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Product&lt;/span&gt; altro&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Double&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;compare&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;prezzo&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; altro&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;prezzo&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// Utilizzo&lt;/span&gt;
prodotti&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;sort&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// usa l&#39;ordinamento naturale definito da compareTo&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Ordinamento con &lt;code&gt;Comparator&lt;/code&gt; (approccio preferito)&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;Comparator&lt;/code&gt; è più flessibile perché non richiede di modificare la classe e permette
criteri multipli di ordinamento.&lt;/p&gt;
&lt;pre class=&quot;language-java&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Per prezzo crescente&lt;/span&gt;
prodotti&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;sort&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Comparator&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;comparingDouble&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Product&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;prezzo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// Per prezzo decrescente&lt;/span&gt;
prodotti&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;sort&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Comparator&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;comparingDouble&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Product&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;prezzo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;reversed&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// Ordinamento multi-livello: prima per disponibilità, poi per prezzo crescente&lt;/span&gt;
prodotti&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;sort&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;Comparator&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;comparing&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Product&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;disponibile&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;reversed&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
              &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;thenComparingDouble&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Product&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;prezzo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;comparingDouble()&lt;/code&gt; vs &lt;code&gt;comparing()&lt;/code&gt;:&lt;/strong&gt; Per campi di tipo &lt;code&gt;double&lt;/code&gt;, usa sempre
&lt;code&gt;Comparator.comparingDouble()&lt;/code&gt;. &lt;code&gt;comparing()&lt;/code&gt; usa il boxing a &lt;code&gt;Double&lt;/code&gt;, introducendo
overhead non necessario in collezioni grandi.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;Gestione dei Duplicati&lt;/h2&gt;
&lt;p&gt;Rilevare e gestire duplicati è un problema comune, ad esempio nella validazione di input
o nel processamento di batch di dati.&lt;/p&gt;
&lt;h2&gt;Verificare la presenza di duplicati&lt;/h2&gt;
&lt;pre class=&quot;language-java&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;boolean&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;contieneDuplicati&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; valori&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;Set&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; valoriUnici &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;HashSet&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Integer&lt;/span&gt; valore &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; valori&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;!&lt;/span&gt;valoriUnici&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;valore&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;token comment&quot;&gt;// add() restituisce false se l&#39;elemento era già presente&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Complessità: O(n)&lt;/strong&gt; — ogni &lt;code&gt;add()&lt;/code&gt; su &lt;code&gt;HashSet&lt;/code&gt; è O(1) in media.&lt;/p&gt;
&lt;h2&gt;Rimuovere i duplicati da una lista&lt;/h2&gt;
&lt;pre class=&quot;language-java&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Metodo 1: tramite Set (non preserva l&#39;ordine)&lt;/span&gt;
&lt;span class=&quot;token class-name&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; senzaDuplicati &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ArrayList&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;HashSet&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;listaConDuplicati&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// Metodo 2: tramite LinkedHashSet (preserva l&#39;ordine di inserimento)&lt;/span&gt;
&lt;span class=&quot;token class-name&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; senzaDuplicatiOrdinati &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ArrayList&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;LinkedHashSet&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;listaConDuplicati&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// Metodo 3: Stream API&lt;/span&gt;
&lt;span class=&quot;token class-name&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; senzaDuplicatiStream &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; listaConDuplicati&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;distinct&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;toList&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Deduplicazione per campo specifico&lt;/h2&gt;
&lt;p&gt;Spesso si vogliono rimuovere oggetti &amp;quot;duplicati&amp;quot; in base a un campo, non per uguaglianza
totale. Un pattern comune usa &lt;code&gt;Collectors.toMap()&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;language-java&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Mantiene il primo prodotto trovato per ogni codice&lt;/span&gt;
&lt;span class=&quot;token class-name&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Product&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; prodottiUnici &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ArrayList&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
    prodotti&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;collect&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Collectors&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;toMap&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
                &lt;span class=&quot;token class-name&quot;&gt;Product&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;codice&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                p &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; p&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;esistente&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; nuovo&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; esistente &lt;span class=&quot;token comment&quot;&gt;// in caso di chiave duplicata, mantieni il primo&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;values&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Trasformazione dei Dati (Mapping)&lt;/h2&gt;
&lt;p&gt;Trasformare una collezione di oggetti in un&#39;altra è un&#39;operazione fondamentale, spesso
chiamata &lt;strong&gt;mapping&lt;/strong&gt;. Con la Stream API, questo processo diventa conciso ed espressivo.&lt;/p&gt;
&lt;h2&gt;Trasformazione semplice: estrarre un campo&lt;/h2&gt;
&lt;pre class=&quot;language-java&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Imperativa&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;estraiNomi&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Product&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; prodotti&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token class-name&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; nomi &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;ArrayList&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Product&lt;/span&gt; prodotto &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; prodotti&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
        nomi&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;add&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;prodotto&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;nome&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; nomi&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;token comment&quot;&gt;// Stream API&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;estraiNomi&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Product&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; prodotti&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;return&lt;/span&gt; prodotti&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Product&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;nome&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;toList&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Collectors avanzati&lt;/h2&gt;
&lt;p&gt;I &lt;code&gt;Collector&lt;/code&gt; di Java permettono aggregazioni sofisticate in una sola passata sulla
collezione. Sono uno strumento essenziale per chi lavora con pipeline di dati.&lt;/p&gt;
&lt;h3&gt;Raggruppamento (&lt;code&gt;groupingBy&lt;/code&gt;)&lt;/h3&gt;
&lt;p&gt;Raggruppa i prodotti per disponibilità:&lt;/p&gt;
&lt;pre class=&quot;language-java&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token class-name&quot;&gt;Map&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Boolean&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Product&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; perDisponibilita &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; prodotti&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;collect&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Collectors&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;groupingBy&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Product&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;disponibile&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token class-name&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Product&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; disponibili   &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; perDisponibilita&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token class-name&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Product&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; nonDisponibili &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; perDisponibilita&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token boolean&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Partizione (&lt;code&gt;partitioningBy&lt;/code&gt;)&lt;/h3&gt;
&lt;p&gt;Versione specializzata di &lt;code&gt;groupingBy&lt;/code&gt; per predicati booleani — più efficiente e
semanticamente più chiara:&lt;/p&gt;
&lt;pre class=&quot;language-java&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token class-name&quot;&gt;Map&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Boolean&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Product&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; partizione &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; prodotti&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;collect&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Collectors&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;partitioningBy&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;p &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;prezzo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;100.0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Costruzione di una Map (&lt;code&gt;toMap&lt;/code&gt;)&lt;/h3&gt;
&lt;pre class=&quot;language-java&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Map&amp;lt;codice, nomeProdotto&gt;&lt;/span&gt;
&lt;span class=&quot;token class-name&quot;&gt;Map&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; codiceToNome &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; prodotti&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;collect&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Collectors&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;toMap&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Product&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;codice&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Product&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;nome&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Conteggio per gruppo (&lt;code&gt;groupingBy&lt;/code&gt; + &lt;code&gt;counting&lt;/code&gt;)&lt;/h3&gt;
&lt;pre class=&quot;language-java&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Quanti prodotti per fascia di prezzo? (esempio semplificato)&lt;/span&gt;
&lt;span class=&quot;token class-name&quot;&gt;Map&lt;/span&gt;&lt;span class=&quot;token generics&quot;&gt;&lt;span class=&quot;token punctuation&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Long&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;&gt;&lt;/span&gt;&lt;/span&gt; conteggioPerFascia &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; prodotti&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;collect&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Collectors&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;groupingBy&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;
            p &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; p&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;prezzo&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;100.0&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;premium&quot;&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;token string&quot;&gt;&quot;standard&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;token class-name&quot;&gt;Collectors&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;counting&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Parallel Streams: quando usarli (e quando no)&lt;/h2&gt;
&lt;p&gt;La Stream API supporta l&#39;elaborazione parallela tramite &lt;code&gt;parallelStream()&lt;/code&gt;. È una
funzionalità potente, ma va usata con cautela.&lt;/p&gt;
&lt;pre class=&quot;language-java&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;// Esempio di parallel stream per somma su lista molto grande&lt;/span&gt;
&lt;span class=&quot;token keyword&quot;&gt;double&lt;/span&gt; totale &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; importiMoltoGrandi&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;parallelStream&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;mapToDouble&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;Double&lt;/span&gt;&lt;span class=&quot;token operator&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;doubleValue&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Quando conviene:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Collezioni con &lt;strong&gt;milioni di elementi&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Operazioni &lt;strong&gt;CPU-intensive&lt;/strong&gt; e &lt;strong&gt;stateless&lt;/strong&gt; (senza effetti collaterali)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Quando non conviene:&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Collezioni piccole (l&#39;overhead di gestione dei thread supera il guadagno)&lt;/li&gt;
&lt;li&gt;Operazioni con &lt;strong&gt;side-effect&lt;/strong&gt; (es. scrittura su variabili condivise)&lt;/li&gt;
&lt;li&gt;Operazioni di I/O (usa &lt;code&gt;CompletableFuture&lt;/code&gt; in questi casi)&lt;/li&gt;
&lt;/ul&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Regola pratica:&lt;/strong&gt; Misura sempre con un benchmark reale (es. JMH) prima di passare
a &lt;code&gt;parallelStream()&lt;/code&gt;. In molti casi la versione sequenziale è più veloce.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;Scelta delle Strutture Dati&lt;/h2&gt;
&lt;p&gt;La struttura dati corretta può fare la differenza tra un&#39;applicazione performante e una
che degrada sotto carico. La tabella seguente riassume le complessità principali.&lt;/p&gt;
&lt;div class=&quot;container mb-3&quot;&gt;
  &lt;div class=&quot;table-responsive shadow-sm rounded&quot;&gt;
    &lt;table class=&quot;table table-bordered table-striped table-hover align-middle mb-0&quot;&gt;
      &lt;thead class=&quot;table-dark&quot;&gt;
        &lt;tr&gt;
          &lt;th&gt;Struttura&lt;/th&gt;
          &lt;th&gt;Accesso&lt;/th&gt;
          &lt;th&gt;Ricerca&lt;/th&gt;
          &lt;th&gt;Inserimento (coda)&lt;/th&gt;
          &lt;th&gt;Inserimento (posizione)&lt;/th&gt;
          &lt;th&gt;Note&lt;/th&gt;
        &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
        &lt;tr&gt;
          &lt;td class=&quot;fw-semibold&quot;&gt;&lt;code&gt;ArrayList&lt;/code&gt;&lt;/td&gt;
          &lt;td&gt;&lt;span class=&quot;badge text-bg-success&quot;&gt;O(1)&lt;/span&gt;&lt;/td&gt;
          &lt;td&gt;&lt;span class=&quot;badge text-bg-warning&quot;&gt;O(n)&lt;/span&gt;&lt;/td&gt;
          &lt;td&gt;&lt;span class=&quot;badge text-bg-success&quot;&gt;O(1) ammortizzato&lt;/span&gt;&lt;/td&gt;
          &lt;td&gt;&lt;span class=&quot;badge text-bg-warning&quot;&gt;O(n)&lt;/span&gt;&lt;/td&gt;
          &lt;td&gt;Preferita per uso generale&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
          &lt;td class=&quot;fw-semibold&quot;&gt;&lt;code&gt;LinkedList&lt;/code&gt;&lt;/td&gt;
          &lt;td&gt;&lt;span class=&quot;badge text-bg-warning&quot;&gt;O(n)&lt;/span&gt;&lt;/td&gt;
          &lt;td&gt;&lt;span class=&quot;badge text-bg-warning&quot;&gt;O(n)&lt;/span&gt;&lt;/td&gt;
          &lt;td&gt;&lt;span class=&quot;badge text-bg-success&quot;&gt;O(1)&lt;/span&gt;&lt;/td&gt;
          &lt;td&gt;&lt;span class=&quot;badge text-bg-success&quot;&gt;O(1) *&lt;/span&gt;&lt;/td&gt;
          &lt;td&gt;*Con riferimento diretto al nodo&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
          &lt;td class=&quot;fw-semibold&quot;&gt;&lt;code&gt;HashSet&lt;/code&gt;&lt;/td&gt;
          &lt;td&gt;&lt;span class=&quot;text-muted&quot;&gt;—&lt;/span&gt;&lt;/td&gt;
          &lt;td&gt;&lt;span class=&quot;badge text-bg-success&quot;&gt;O(1) med.&lt;/span&gt;&lt;/td&gt;
          &lt;td&gt;&lt;span class=&quot;badge text-bg-success&quot;&gt;O(1) med.&lt;/span&gt;&lt;/td&gt;
          &lt;td&gt;&lt;span class=&quot;text-muted&quot;&gt;—&lt;/span&gt;&lt;/td&gt;
          &lt;td&gt;No duplicati, no ordine&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
          &lt;td class=&quot;fw-semibold&quot;&gt;&lt;code&gt;LinkedHashSet&lt;/code&gt;&lt;/td&gt;
          &lt;td&gt;&lt;span class=&quot;text-muted&quot;&gt;—&lt;/span&gt;&lt;/td&gt;
          &lt;td&gt;&lt;span class=&quot;badge text-bg-success&quot;&gt;O(1) med.&lt;/span&gt;&lt;/td&gt;
          &lt;td&gt;&lt;span class=&quot;badge text-bg-success&quot;&gt;O(1) med.&lt;/span&gt;&lt;/td&gt;
          &lt;td&gt;&lt;span class=&quot;text-muted&quot;&gt;—&lt;/span&gt;&lt;/td&gt;
          &lt;td&gt;No duplicati, ordine inserimento&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
          &lt;td class=&quot;fw-semibold&quot;&gt;&lt;code&gt;TreeSet&lt;/code&gt;&lt;/td&gt;
          &lt;td&gt;&lt;span class=&quot;text-muted&quot;&gt;—&lt;/span&gt;&lt;/td&gt;
          &lt;td&gt;&lt;span class=&quot;badge text-bg-info&quot;&gt;O(log n)&lt;/span&gt;&lt;/td&gt;
          &lt;td&gt;&lt;span class=&quot;badge text-bg-info&quot;&gt;O(log n)&lt;/span&gt;&lt;/td&gt;
          &lt;td&gt;&lt;span class=&quot;text-muted&quot;&gt;—&lt;/span&gt;&lt;/td&gt;
          &lt;td&gt;No duplicati, ordinato&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
          &lt;td class=&quot;fw-semibold&quot;&gt;&lt;code&gt;HashMap&lt;/code&gt;&lt;/td&gt;
          &lt;td&gt;&lt;span class=&quot;text-muted&quot;&gt;—&lt;/span&gt;&lt;/td&gt;
          &lt;td&gt;&lt;span class=&quot;badge text-bg-success&quot;&gt;O(1) med.&lt;/span&gt;&lt;/td&gt;
          &lt;td&gt;&lt;span class=&quot;badge text-bg-success&quot;&gt;O(1) med.&lt;/span&gt;&lt;/td&gt;
          &lt;td&gt;&lt;span class=&quot;text-muted&quot;&gt;—&lt;/span&gt;&lt;/td&gt;
          &lt;td&gt;Chiave → valore, no ordine&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
          &lt;td class=&quot;fw-semibold&quot;&gt;&lt;code&gt;LinkedHashMap&lt;/code&gt;&lt;/td&gt;
          &lt;td&gt;&lt;span class=&quot;text-muted&quot;&gt;—&lt;/span&gt;&lt;/td&gt;
          &lt;td&gt;&lt;span class=&quot;badge text-bg-success&quot;&gt;O(1) med.&lt;/span&gt;&lt;/td&gt;
          &lt;td&gt;&lt;span class=&quot;badge text-bg-success&quot;&gt;O(1) med.&lt;/span&gt;&lt;/td&gt;
          &lt;td&gt;&lt;span class=&quot;text-muted&quot;&gt;—&lt;/span&gt;&lt;/td&gt;
          &lt;td&gt;Ordine di inserimento&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
          &lt;td class=&quot;fw-semibold&quot;&gt;&lt;code&gt;TreeMap&lt;/code&gt;&lt;/td&gt;
          &lt;td&gt;&lt;span class=&quot;text-muted&quot;&gt;—&lt;/span&gt;&lt;/td&gt;
          &lt;td&gt;&lt;span class=&quot;badge text-bg-info&quot;&gt;O(log n)&lt;/span&gt;&lt;/td&gt;
          &lt;td&gt;&lt;span class=&quot;badge text-bg-info&quot;&gt;O(log n)&lt;/span&gt;&lt;/td&gt;
          &lt;td&gt;&lt;span class=&quot;text-muted&quot;&gt;—&lt;/span&gt;&lt;/td&gt;
          &lt;td&gt;Ordinato per chiave&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;h3&gt;Linee guida pratiche&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Hai bisogno di accesso casuale per indice?&lt;/strong&gt; → &lt;code&gt;ArrayList&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Inserisci/rimuovi spesso in testa o in coda?&lt;/strong&gt; → &lt;code&gt;ArrayDeque&lt;/code&gt; (non &lt;code&gt;LinkedList&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Devi verificare l&#39;appartenenza di un elemento?&lt;/strong&gt; → &lt;code&gt;HashSet&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Devi iterare in ordine di inserimento?&lt;/strong&gt; → &lt;code&gt;LinkedHashSet&lt;/code&gt; / &lt;code&gt;LinkedHashMap&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Devi mantenere un ordinamento naturale?&lt;/strong&gt; → &lt;code&gt;TreeSet&lt;/code&gt; / &lt;code&gt;TreeMap&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Ricerchi frequentemente per chiave univoca?&lt;/strong&gt; → &lt;code&gt;HashMap&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
]]></content>
    
    
      
    
      
    <category term="algoritmi"/>
      
    
      
    <category term="java"/>
      
    
  </entry>
  
  <entry>
    <title>Allocazione della memoria</title>
    <link href="https://dino-996.github.io/blog/allocazione-della-memoria/" rel="alternate" type="text/html"/>
    <id>https://dino-996.github.io/blog/allocazione-della-memoria/</id>
    <published>2020-02-23T00:00:00.000Z</published>
    <updated>2020-02-23T00:00:00.000Z</updated>
    
    <summary>Questo approfondimento tecnico esplora le sfide dell&#39;allocazione dei frame in sistemi multiprogrammati. Analizzeremo come il modello del working set e la frequenza dei page fault prevengano il collasso del sistema (thrashing), oltre a esaminare le tecniche specifiche del kernel per la gestione di oggetti di dimensioni variabili e fisse.</summary>
    
    
    <content type="html"><![CDATA[&lt;h1&gt;Strategie di allocazione dei frame&lt;/h1&gt;
&lt;p&gt;La gestione efficiente della memoria fisica non si limita alla semplice paginazione; il cuore delle prestazioni risiede nella &lt;strong&gt;strategia di allocazione dei frame&lt;/strong&gt;. Decidere quanti e quali frame assegnare a ogni processo è un gioco di equilibrio tra equità, priorità e throughput del sistema.&lt;/p&gt;
&lt;h2&gt;Modelli di allocazione&lt;/h2&gt;
&lt;p&gt;Esistono due approcci filosofici alla distribuzione della memoria:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Allocazione Uniforme:&lt;/strong&gt; Assegna a ogni processo lo stesso numero di frame ($m/n$), indipendentemente dal carico di lavoro. È una strategia democratica ma inefficiente, poiché penalizza i processi pesanti e spreca risorse con quelli leggeri.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Allocazione Proporzionale:&lt;/strong&gt; Distribuisce la memoria in base alla dimensione virtuale del processo. Se un processo $p_i$ ha una dimensione virtuale $s_i$, riceverà un numero di frame $a_i$ proporzionale alla sua quota sul totale della memoria virtuale richiesta.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Politiche di sostituzione&lt;/h3&gt;
&lt;p&gt;Oltre alla quantità, la &amp;quot;provenienza&amp;quot; dei frame è determinante:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Sostituzione Globale:&lt;/strong&gt; Un processo può sottrarre frame a un altro (spesso a quelli con priorità inferiore). Questo massimizza il throughput ma rende il tempo di risposta del singolo processo imprevedibile.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Sostituzione Locale:&lt;/strong&gt; Un processo può rimpiazzare solo i frame che gli sono già stati assegnati. Questo garantisce isolamento e stabilità, ma può lasciare inutilizzata RAM preziosa se un processo non ne ha immediato bisogno.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Il pericolo del Thrashing&lt;/h2&gt;
&lt;p&gt;Il &lt;strong&gt;Thrashing&lt;/strong&gt; è il &amp;quot;punto di rottura&amp;quot; di un sistema operativo: si verifica quando il sistema spende più tempo a gestire i page fault (I/O) che a eseguire istruzioni utili.&lt;/p&gt;
&lt;p&gt;Questo collasso avviene tipicamente quando il grado di multiprogrammazione aumenta oltre il limite critico. La CPU registra un calo di utilizzo perché i processi sono in attesa dell&#39;I/O; il sistema operativo, interpretando erroneamente il calo come mancanza di carico, avvia nuovi processi. Questo sottrae ulteriori frame ai processi esistenti, innescando una reazione a catena che porta il sistema alla paralisi.&lt;/p&gt;
&lt;h3&gt;Strategie di prevenzione&lt;/h3&gt;
&lt;p&gt;Per scongiurare il thrashing, i kernel moderni adottano due modelli principali:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Modello del Working Set:&lt;/strong&gt; Si basa sul &lt;strong&gt;principio di località&lt;/strong&gt;. Il sistema monitora le pagine usate attivamente in una finestra temporale $&#92;Delta$. Se la somma dei working set di tutti i processi supera la RAM disponibile, il sistema sospende temporaneamente un processo (swap-out) per liberare risorse.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Frequenza dei Page Fault (PFF):&lt;/strong&gt; Un approccio diretto che stabilisce una soglia superiore e inferiore di fault. Se un processo genera troppi fault, riceve più frame; se ne genera troppi pochi, i suoi frame vengono ridotti.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;Gestione della memoria nel kernel&lt;/h2&gt;
&lt;p&gt;A differenza dei processi utente, il kernel richiede spesso allocazioni di memoria &lt;strong&gt;fisicamente contigue&lt;/strong&gt; (per interagire con l&#39;hardware via DMA). Vengono utilizzate due tecniche specifiche:&lt;/p&gt;
&lt;h3&gt;Sistema buddy (Buddy System)&lt;/h3&gt;
&lt;p&gt;Il kernel gestisce la memoria in blocchi di dimensioni pari a potenze di due ($2^n$). Se viene richiesta una quantità di memoria che non corrisponde a una potenza di due, il sistema divide ricorsivamente i blocchi a metà (&amp;quot;buddies&amp;quot;) finché non trova la dimensione minima adatta.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Vantaggio:&lt;/strong&gt; Facilità estrema nel ricompattare i blocchi liberi.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Svantaggio:&lt;/strong&gt; Rischio di &lt;strong&gt;frammentazione interna&lt;/strong&gt; se la richiesta è appena superiore a una potenza di due.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Slab allocator&lt;/h3&gt;
&lt;p&gt;Ideato per gestire oggetti di dimensione fissa (come i descrittori dei processi o i nodi del file system). Organizza la memoria in &lt;strong&gt;Slab&lt;/strong&gt; (lastre) che contengono oggetti pre-allocati.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Cache dedicate:&lt;/strong&gt; Ogni tipo di struttura dati ha la sua cache.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Zero frammentazione:&lt;/strong&gt; Elimina la frammentazione interna ed evita il costo computazionale di allocazione/deallocazione continua.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Ottimizzazioni moderne e il caso Linux&lt;/h2&gt;
&lt;p&gt;Nei sistemi contemporanei, l&#39;efficienza è spinta all&#39;estremo attraverso:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;TLB Reach:&lt;/strong&gt; L&#39;uso di &amp;quot;Huge Pages&amp;quot; (pagine di dimensioni maggiori, es. 2MB o 1GB) per aumentare la porzione di memoria indirizzabile senza causare un flush del TLB.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Compressione della Memoria (ZRAM):&lt;/strong&gt; Comune nei dispositivi mobili; invece di fare swap su disco, le pagine meno usate vengono compresse in una porzione dedicata della RAM.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Come gestisce Linux il rimpiazzo?&lt;/h3&gt;
&lt;p&gt;Linux non usa un algoritmo LRU puro (troppo costoso), ma una sua approssimazione efficace tramite due liste:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Active List:&lt;/strong&gt; Pagine referenziate di recente.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Inactive List:&lt;/strong&gt; Pagine candidate alla rimozione.
Attraverso il bit di accesso, il kernel sposta dinamicamente le pagine tra queste liste (meccanismo a &amp;quot;due mani&amp;quot;), garantendo che le pagine più preziose rimangano in memoria il più a lungo possibile.&lt;/li&gt;
&lt;/ol&gt;
&lt;hr&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Key Takeaway:&lt;/strong&gt; L&#39;allocazione dei frame non è solo una questione di spazio, ma di tempo. Un kernel che sa prevedere il working set dei suoi processi è la differenza tra una workstation fluida e un sistema in costante attesa del disco.&lt;/p&gt;
&lt;/blockquote&gt;
]]></content>
    
    
      
    
      
    <category term="sistemi operativi"/>
      
    
      
    <category term="allocazione della memoria"/>
      
    
  </entry>
  
  <entry>
    <title>La memoria virtuale</title>
    <link href="https://dino-996.github.io/blog/la-memoria-virtuale/" rel="alternate" type="text/html"/>
    <id>https://dino-996.github.io/blog/la-memoria-virtuale/</id>
    <published>2020-02-22T00:00:00.000Z</published>
    <updated>2020-02-22T00:00:00.000Z</updated>
    
    <summary>Scopri come i sistemi operativi riescono a eseguire applicazioni enormi su hardware limitato attraverso la memoria virtuale, la paginazione su richiesta e i sofisticati algoritmi di sostituzione delle pagine.</summary>
    
    
    <content type="html"><![CDATA[&lt;h1&gt;Architettura e strategie della Memoria Virtuale&lt;/h1&gt;
&lt;p&gt;Nei moderni sistemi multiprogrammati, l&#39;esecuzione simultanea di più processi richiede una gestione oculata delle risorse. Tuttavia, la RAM è per definizione una risorsa finita e spesso insufficiente a ospitare l&#39;intero working set di ogni applicazione attiva. Qui entra in gioco la &lt;strong&gt;Memoria Virtuale&lt;/strong&gt;, un&#39;astrazione fondamentale che scollega la memoria logica vista dal programmatore dalla memoria fisica effettivamente disponibile.&lt;/p&gt;
&lt;p&gt;Questa architettura offre vantaggi determinanti:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Scalabilità:&lt;/strong&gt; Consente l&#39;esecuzione di programmi le cui dimensioni superano la capacità della RAM fisica.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Astrazione:&lt;/strong&gt; Il programmatore interagisce con uno spazio di indirizzamento lineare e contiguo, ignorando la frammentazione fisica.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Efficienza:&lt;/strong&gt; Facilita la condivisione di librerie (DLL/Shared Objects) tra processi diversi attraverso la mappatura delle medesime pagine fisiche.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Paginazione su richiesta&lt;/h2&gt;
&lt;p&gt;Il cuore della memoria virtuale risiede nel &lt;strong&gt;Demand Paging&lt;/strong&gt; (paginazione su richiesta). Invece di caricare l&#39;intero binario in fase di startup, il kernel carica le singole pagine solo quando vengono effettivamente referenziate durante l&#39;esecuzione.&lt;/p&gt;
&lt;h3&gt;Il ciclo del page fault&lt;/h3&gt;
&lt;p&gt;Quando un processo tenta di accedere a un indirizzo logico non presente in RAM (identificato da un bit di validità &amp;quot;0&amp;quot; nella Page Table), l&#39;hardware genera un&#39;eccezione nota come &lt;strong&gt;Page Fault&lt;/strong&gt;. Il sistema operativo interviene seguendo una procedura rigorosa:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Verifica:&lt;/strong&gt; Il SO controlla se il riferimento alla memoria era legittimo.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Swap-in:&lt;/strong&gt; Individua la pagina nel disco (area di swap).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Allocazione:&lt;/strong&gt; Cerca un frame libero nella RAM.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Update:&lt;/strong&gt; Carica i dati nel frame, aggiorna la Page Table e setta il bit di validità a &amp;quot;1&amp;quot;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Restart:&lt;/strong&gt; Ripristina l&#39;istruzione che aveva causato l&#39;eccezione.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;Performance e ottimizzazioni&lt;/h2&gt;
&lt;p&gt;L&#39;implementazione della memoria virtuale introduce un overhead inevitabile. Le prestazioni del sistema vengono misurate tramite il &lt;strong&gt;Tempo di Accesso Effettivo (EAT)&lt;/strong&gt;, calcolato come:&lt;/p&gt;
&lt;p&gt;$$EAT = (1 - p) &#92;times ma + p &#92;times pft$$&lt;/p&gt;
&lt;p&gt;Dove:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;$p$ è la probabilità di page fault ($0 &#92;le p &#92;le 1$).&lt;/li&gt;
&lt;li&gt;$ma$ è il tempo di accesso alla memoria (nanosecondi).&lt;/li&gt;
&lt;li&gt;$pft$ è il tempo di gestione del page fault (millisecondi).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Dato che il recupero da disco è ordini di grandezza più lento della RAM, anche un tasso di page fault minimo può degradare sensibilmente le performance.&lt;/p&gt;
&lt;h3&gt;Copy-on-Write (CoW)&lt;/h3&gt;
&lt;p&gt;Per ottimizzare la creazione dei processi (system call &lt;code&gt;fork()&lt;/code&gt;), si utilizza la tecnica &lt;strong&gt;Copy-on-Write&lt;/strong&gt;. Inizialmente, processo padre e figlio condividono le stesse pagine fisiche in sola lettura. Solo quando uno dei due tenta una scrittura, il kernel interviene duplicando la pagina specifica. Questo evita sprechi di memoria per processi che non modificano i dati ereditati.&lt;/p&gt;
&lt;h2&gt;Algoritmi di sostituzione delle pagine&lt;/h2&gt;
&lt;p&gt;Cosa accade quando la memoria fisica è satura? Il sistema deve selezionare una &amp;quot;vittima&amp;quot; da espellere per far spazio a una nuova pagina. Per ottimizzare questo processo, si utilizza il &lt;strong&gt;Dirty Bit&lt;/strong&gt; (o bit di modifica): se una pagina non è stata modificata dal suo caricamento, può essere sovrascritta senza essere riscritta su disco, dimezzando l&#39;I/O.&lt;/p&gt;
&lt;h3&gt;Analisi delle strategie di replacement&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;FIFO (First-In-First-Out):&lt;/strong&gt; Sostituisce la pagina residente da più tempo. Sebbene semplice, è inefficiente e soggetto all&#39;&lt;strong&gt;Anomalia di Belady&lt;/strong&gt;: un fenomeno controintuitivo dove l&#39;aggiunta di frame fisici può aumentare il numero di page fault.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;OPT (Algoritmo Ottimale):&lt;/strong&gt; Sostituisce la pagina che non verrà utilizzata per il periodo di tempo più lungo in assoluto. È il gold standard teorico, ma è &lt;strong&gt;irrealizzabile&lt;/strong&gt; poiché richiederebbe la conoscenza futura degli accessi (non determinismo).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;LRU (Least Recently Used):&lt;/strong&gt; Approssima l&#39;algoritmo ottimale sostituendo la pagina non utilizzata da più tempo. È molto efficace ma richiede supporto hardware dedicato (contatori o stack) per non appesantire la CPU.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Seconda Chance (Clock):&lt;/strong&gt; Un compromesso ingegnoso tra FIFO e LRU. Organizza le pagine in una lista circolare e utilizza un &lt;strong&gt;Reference Bit&lt;/strong&gt;. Se il bit è 1, la pagina riceve una &amp;quot;seconda possibilità&amp;quot; e il bit viene azzerato; se è 0, la pagina viene sostituita.&lt;/li&gt;
&lt;/ol&gt;
]]></content>
    
    
      
    
      
    <category term="sistemi operativi"/>
      
    
      
    <category term="memoria virtuale"/>
      
    
  </entry>
  
  <entry>
    <title>Scheduling della CPU</title>
    <link href="https://dino-996.github.io/blog/scheduling-della-cpu/" rel="alternate" type="text/html"/>
    <id>https://dino-996.github.io/blog/scheduling-della-cpu/</id>
    <published>2020-02-21T00:00:00.000Z</published>
    <updated>2020-02-21T00:00:00.000Z</updated>
    
    <summary>Facciamo un&#39;analisi approfondita dello scheduling della CPU, una funzione vitale dei sistemi operativi progettata per massimizzare l&#39;efficienza attraverso la multiprogrammazione.</summary>
    
    
    <content type="html"><![CDATA[&lt;h1&gt;Introduzione&lt;/h1&gt;
&lt;p&gt;In questo articolo esploriamo lo scheduling della CPU, una funzione vitale dei sistemi operativi progettata per ottimizza throughput e tempi d’attesa attraverso algoritmi come FCFS, SJF e Round Robin. La gestione moderna si estende a thread hardware/software e sistemi multicore, privilegiando la processor affinity per l&#39;efficienza della cache. Il supporto real-time (soft/hard) garantisce il rispetto di scadenze rigide.&lt;/p&gt;
&lt;h2&gt;Cos’è lo scheduling della CPU&lt;/h2&gt;
&lt;p&gt;Lo &lt;strong&gt;scheduling della CPU&lt;/strong&gt; è il meccanismo attraverso il quale il sistema operativo decide quale processo o thread debba essere eseguito in un determinato istante. Poiché un singolo core può eseguire un solo flusso di esecuzione alla volta, lo scheduler ha il compito di distribuire il tempo di CPU tra più entità concorrenti, garantendo efficienza, equità e reattività.&lt;/p&gt;
&lt;p&gt;Nei sistemi moderni lo scheduler è una componente critica del kernel: deve operare con latenza minima, comportamento prevedibile e capacità di scalare su architetture multicore o multiprocessore.&lt;/p&gt;
&lt;h2&gt;Concetti fondamentali&lt;/h2&gt;
&lt;h3&gt;CPU burst e I/O burst&lt;/h3&gt;
&lt;p&gt;L’esecuzione di un processo è caratterizzata dall’alternanza tra:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;CPU burst&lt;/strong&gt;, fasi di elaborazione attiva;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;I/O burst&lt;/strong&gt;, fasi di attesa per operazioni di input/output.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Questa struttura ciclica costituisce il fondamento della multiprogrammazione: mentre un processo è bloccato in attesa di I/O, un altro può utilizzare la CPU, aumentando l’utilizzo complessivo del sistema.&lt;/p&gt;
&lt;h3&gt;Modalità di prelazione (preemption)&lt;/h3&gt;
&lt;p&gt;Si distinguono due modelli principali:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Non preemptive&lt;/strong&gt;: il processo mantiene la CPU fino al completamento o a un evento di blocco (ad esempio una richiesta di I/O).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Preemptive&lt;/strong&gt;: lo scheduler può interrompere un processo attivo, tipicamente tramite interrupt generati da un timer hardware, per assegnare la CPU a un altro processo.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;La preemption migliora la reattività nei sistemi interattivi, ma richiede meccanismi di sincronizzazione accurati per preservare la coerenza dello stato del kernel.&lt;/p&gt;
&lt;h3&gt;Dispatcher e context switch&lt;/h3&gt;
&lt;p&gt;Il &lt;strong&gt;dispatcher&lt;/strong&gt; è il modulo responsabile dell’effettivo trasferimento del controllo della CPU al processo selezionato. Le sue operazioni includono:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;salvataggio e ripristino dei registri (context switch);&lt;/li&gt;
&lt;li&gt;aggiornamento delle strutture di controllo del kernel;&lt;/li&gt;
&lt;li&gt;passaggio dalla modalità kernel alla modalità utente.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Il tempo necessario a eseguire queste operazioni è detto &lt;strong&gt;latenza di dispatch&lt;/strong&gt; e rappresenta un overhead che deve essere contenuto per mantenere elevate prestazioni.&lt;/p&gt;
&lt;h2&gt;Criteri di valutazione&lt;/h2&gt;
&lt;p&gt;Gli algoritmi di scheduling sono confrontati mediante metriche quantitative standard:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Utilizzo della CPU&lt;/strong&gt;: percentuale di tempo in cui la CPU esegue lavoro utile.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Throughput&lt;/strong&gt;: numero di processi completati per unità di tempo.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Turnaround time&lt;/strong&gt;: intervallo tra l’arrivo del processo nel sistema e la sua conclusione.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Tempo di attesa&lt;/strong&gt;: tempo totale trascorso nella ready queue.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Tempo di risposta&lt;/strong&gt;: intervallo tra la richiesta di esecuzione e la prima risposta prodotta, rilevante nei sistemi interattivi.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Queste metriche possono entrare in conflitto: ad esempio, la riduzione del tempo di risposta può aumentare il numero di context switch e quindi l’overhead complessivo.&lt;/p&gt;
&lt;h2&gt;Algoritmi di scheduling&lt;/h2&gt;
&lt;h3&gt;FCFS (First-Come, First-Served)&lt;/h3&gt;
&lt;p&gt;Il processo con ordine di arrivo più basso nella ready queue viene eseguito per primo. È un algoritmo semplice e non preemptive, ma può generare il &lt;strong&gt;convoy effect&lt;/strong&gt;, in cui un processo lungo ritarda l’esecuzione di numerosi processi brevi.&lt;/p&gt;
&lt;h3&gt;SJF (Shortest-Job-First) e SRTF&lt;/h3&gt;
&lt;p&gt;L’algoritmo &lt;strong&gt;SJF&lt;/strong&gt; seleziona il processo con il CPU burst stimato più breve, minimizzando il tempo di attesa medio in condizioni ideali. La variante preemptive è detta &lt;strong&gt;Shortest Remaining Time First (SRTF)&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;La principale criticità consiste nella stima della durata futura dei burst, generalmente ottenuta tramite tecniche di predizione statistica (ad esempio media esponenziale).&lt;/p&gt;
&lt;h3&gt;Round Robin (RR)&lt;/h3&gt;
&lt;p&gt;Ogni processo riceve un intervallo di tempo prefissato, detto &lt;strong&gt;quantum&lt;/strong&gt;. Allo scadere del quantum, il processo viene sospeso e reinserito nella ready queue.&lt;/p&gt;
&lt;p&gt;La scelta del quantum è determinante:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;un valore troppo elevato rende il comportamento simile a FCFS;&lt;/li&gt;
&lt;li&gt;un valore troppo ridotto aumenta l’overhead dovuto ai frequenti context switch.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Scheduling con priorità&lt;/h3&gt;
&lt;p&gt;A ogni processo è assegnato un livello di priorità; la CPU viene concessa al processo con priorità più alta.&lt;/p&gt;
&lt;p&gt;Questo approccio può causare &lt;strong&gt;starvation&lt;/strong&gt; per i processi a bassa priorità. Il fenomeno è mitigabile mediante &lt;strong&gt;aging&lt;/strong&gt;, ossia l’incremento progressivo della priorità dei processi in attesa.&lt;/p&gt;
&lt;h3&gt;Code multilivello e multilevel feedback queue (MLFQ)&lt;/h3&gt;
&lt;p&gt;Le &lt;strong&gt;multilevel queue&lt;/strong&gt; suddividono i processi in code distinte (ad esempio interattivi, batch, di sistema), ciascuna con una propria politica di scheduling.&lt;/p&gt;
&lt;p&gt;Le &lt;strong&gt;multilevel feedback queue (MLFQ)&lt;/strong&gt; consentono ai processi di spostarsi tra code in base al comportamento osservato, realizzando uno scheduling adattativo che favorisce i processi interattivi senza penalizzare eccessivamente quelli computazionali.&lt;/p&gt;
&lt;h2&gt;Scheduling in sistemi multiprocessore&lt;/h2&gt;
&lt;p&gt;Nei sistemi &lt;strong&gt;SMP (Symmetric Multiprocessing)&lt;/strong&gt;, ogni core può gestire una coda locale oppure condividere una coda globale.&lt;/p&gt;
&lt;p&gt;Concetti rilevanti includono:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Processor affinity&lt;/strong&gt;: mantenere un processo sullo stesso core per sfruttare la località della cache e ridurre i costi di migrazione.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Load balancing&lt;/strong&gt;: distribuire il carico in modo uniforme tra i core per evitare squilibri.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Sistemi real-time&lt;/h2&gt;
&lt;p&gt;Nei sistemi real-time il rispetto delle deadline costituisce un requisito primario.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Soft real-time&lt;/strong&gt;: il mancato rispetto di una deadline degrada le prestazioni ma non compromette necessariamente il sistema.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Hard real-time&lt;/strong&gt;: il mancato rispetto di una deadline è considerato un errore critico.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Algoritmi classici includono:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Rate Monotonic Scheduling (RMS)&lt;/strong&gt;: assegna priorità fisse proporzionali alla frequenza dei task periodici.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Earliest Deadline First (EDF)&lt;/strong&gt;: assegna priorità dinamiche in base alla deadline più prossima; è ottimale su singolo processore in condizioni ideali di modello.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Scheduler nei principali sistemi operativi&lt;/h2&gt;
&lt;h3&gt;Linux&lt;/h3&gt;
&lt;p&gt;Linux utilizza più classi di scheduling. Per i task ordinari adotta il &lt;strong&gt;Completely Fair Scheduler (CFS)&lt;/strong&gt;, che modella l’esecuzione come una condivisione equa del tempo di CPU e utilizza strutture dati bilanciate (ad esempio alberi red-black) per selezionare il task con il minor tempo virtuale accumulato.&lt;/p&gt;
&lt;p&gt;Sono inoltre presenti classi dedicate ai task real-time e meccanismi di bilanciamento del carico su sistemi SMP.&lt;/p&gt;
&lt;h3&gt;Windows&lt;/h3&gt;
&lt;p&gt;Windows implementa uno scheduler preemptive basato su priorità, con 32 livelli distinti:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;livelli &lt;strong&gt;variabili&lt;/strong&gt; per applicazioni generiche;&lt;/li&gt;
&lt;li&gt;livelli &lt;strong&gt;real-time&lt;/strong&gt; riservati a task critici.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Le priorità possono essere modificate dinamicamente per migliorare la reattività delle applicazioni interattive e prevenire situazioni di starvation.&lt;/p&gt;
]]></content>
    
    
      
    
      
    <category term="sistemi operativi"/>
      
    
      
    <category term="scheduling"/>
      
    
  </entry>
  
  <entry>
    <title>Thread</title>
    <link href="https://dino-996.github.io/blog/thread/" rel="alternate" type="text/html"/>
    <id>https://dino-996.github.io/blog/thread/</id>
    <published>2020-02-18T00:00:00.000Z</published>
    <updated>2020-02-18T00:00:00.000Z</updated>
    
    <summary>Si descrivono i thread come le unità di base di utilizzo della CPU, evidenziando i vantaggi del multithreading come la condivisione delle risorse e la scalabilità, concludendo con le sfide della cancellazione dei task e le specificità di sistema come la funzione clone() in Linux.</summary>
    
    
    <content type="html"><![CDATA[&lt;h1&gt;Introduzione&lt;/h1&gt;
&lt;p&gt;In questo articolo esploriamo l&#39;architettura dei thread e la programmazione concorrente all&#39;interno dei sistemi operativi moderni.&lt;/p&gt;
&lt;h2&gt;Thread&lt;/h2&gt;
&lt;p&gt;Il thread rappresenta l’unità minima di esecuzione schedulabile dalla CPU all’interno di un processo. Dal punto di vista architetturale, ciascun thread è caratterizzato da:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;un identificatore univoco;&lt;/li&gt;
&lt;li&gt;un Program Counter (PC), che individua l’istruzione corrente;&lt;/li&gt;
&lt;li&gt;un insieme di registri di stato;&lt;/li&gt;
&lt;li&gt;uno stack privato per la gestione delle chiamate di funzione e delle variabili locali.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;All’interno dello stesso &lt;strong&gt;&lt;a href=&quot;https://dino-996.github.io/blog/processi/&quot;&gt;processo&lt;/a&gt;&lt;/strong&gt;, i thread condividono lo spazio di indirizzamento e le risorse globali, tra cui il segmento di codice, il segmento dati (heap e variabili globali) e le risorse di sistema allocate (file descriptor, segnali, socket). Tale condivisione costituisce l’elemento distintivo rispetto al modello multiprocesso tradizionale, nel quale ogni processo possiede uno spazio di memoria isolato.&lt;/p&gt;
&lt;p&gt;L’introduzione del multithreading risponde a esigenze di efficienza computazionale e di progettazione del software concorrente. I principali benefici possono essere formalizzati come segue:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Riduzione della latenza percepita&lt;/strong&gt;: la suddivisione del lavoro in più thread consente la sovrapposizione tra operazioni di I/O e computazione.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Efficienza nell’uso delle risorse&lt;/strong&gt;: il costo di creazione e distruzione di un thread è inferiore rispetto a quello di un processo, così come l’overhead del context switch.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Modularità progettuale&lt;/strong&gt;: la decomposizione di un’applicazione in unità concorrenti favorisce una strutturazione più chiara delle responsabilità.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Scalabilità su architetture multicore&lt;/strong&gt;: la presenza di più unità di esecuzione fisiche permette l’esecuzione realmente parallela dei thread.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Concorrenza, Parallelismo e Architetture Eterogenee&lt;/h2&gt;
&lt;p&gt;Nel contesto dei sistemi moderni è necessario distinguere rigorosamente tra concorrenza e parallelismo, abbiamo già affrontato l&#39;argomento quando parlavamo dei processi ma è bene ripetere i concetti al fine di fissare bene le idee.&lt;/p&gt;
&lt;p&gt;La &lt;strong&gt;concorrenza&lt;/strong&gt; è una proprietà logica del sistema: più task progrediscono nel tempo, anche se non necessariamente in modo simultaneo. In un sistema single-core, ciò avviene tramite time slicing e meccanismi di scheduling preemptive.&lt;/p&gt;
&lt;p&gt;Il &lt;strong&gt;parallelismo&lt;/strong&gt;, invece, è una proprietà fisica dell’hardware: più task sono eseguiti simultaneamente su core distinti. Il parallelismo rappresenta quindi un caso particolare di concorrenza in presenza di risorse computazionali multiple.&lt;/p&gt;
&lt;p&gt;L’evoluzione delle architetture ha introdotto sistemi eterogenei CPU–GPU. Le GPU (Graphics Processing Unit), inizialmente progettate per il rendering grafico, sono oggi impiegate nel paradigma GPGPU (General-Purpose computing on GPU). Tali dispositivi integrano un numero elevato di ALU (Arithmetic Logic Units), ottimizzate per il calcolo massivamente parallelo su grandi insiemi di dati omogenei.&lt;/p&gt;
&lt;p&gt;Applicazioni tipiche includono:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;calcolo scientifico e bioinformatica;&lt;/li&gt;
&lt;li&gt;crittografia e analisi numerica;&lt;/li&gt;
&lt;li&gt;addestramento di modelli di deep learning;&lt;/li&gt;
&lt;li&gt;simulazioni fisiche su larga scala.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Modelli di Implementazione del Multithreading&lt;/h2&gt;
&lt;p&gt;Dal punto di vista sistemistico, si distinguono thread a livello utente e thread a livello kernel.&lt;/p&gt;
&lt;p&gt;I &lt;strong&gt;thread a livello utente&lt;/strong&gt; sono gestiti da librerie in spazio utente, senza intervento diretto del kernel nelle operazioni di scheduling interne al processo.&lt;/p&gt;
&lt;p&gt;I &lt;strong&gt;thread a livello kernel&lt;/strong&gt; sono invece entità direttamente note al sistema operativo, il quale ne gestisce pianificazione, sospensione e sincronizzazione.&lt;/p&gt;
&lt;p&gt;La relazione tra thread utente e thread kernel è formalizzata attraverso modelli di mapping:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Many-to-One&lt;/strong&gt;: più thread utente sono associati a un singolo thread kernel. Il modello minimizza l’overhead (indica il costo aggiuntivo necessario per gestire un’operazione, costo che non contribuisce direttamente al risultato utile, ma è indispensabile per renderla possibile), ma una chiamata bloccante compromette l’intero processo.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;One-to-One&lt;/strong&gt;: ogni thread utente corrisponde a un thread kernel. Garantisce parallelismo reale su sistemi multicore, al prezzo di un maggiore consumo di risorse.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Many-to-Many&lt;/strong&gt;: più thread utente sono mappati su un numero limitato di thread kernel, consentendo un compromesso tra flessibilità e scalabilità.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Two-Level Model&lt;/strong&gt;: estensione del many-to-many che consente l’associazione vincolata di specifici thread utente a determinati thread kernel.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;La scelta del modello influenza direttamente prestazioni, prevedibilità temporale e complessità di gestione.&lt;/p&gt;
&lt;h2&gt;Astrazioni di Programmazione e Threading Implicito&lt;/h2&gt;
&lt;p&gt;Le principali interfacce di programmazione concorrente includono:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;POSIX Threads (Pthreads)&lt;/strong&gt;: standard che definisce primitive per creazione, sincronizzazione e gestione dei thread in ambienti Unix-like.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;API Windows&lt;/strong&gt;: insieme di primitive integrate nel kernel NT per la gestione nativa dei thread.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Java Concurrency Framework&lt;/strong&gt;: insieme di astrazioni ad alto livello (Executor, ExecutorService, Callable, Future) che separano la definizione del task dalla gestione dei thread sottostanti.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Il paradigma del &lt;strong&gt;threading implicito&lt;/strong&gt; trasferisce la responsabilità della gestione concorrente a runtime e librerie specializzate. Tra i principali strumenti:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Thread Pool&lt;/strong&gt;: insieme di thread riutilizzabili per l’esecuzione di task asincroni, con controllo del grado di parallelismo.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Fork/Join Framework&lt;/strong&gt;: modello ricorsivo divide-et-impera basato su work stealing per l’ottimizzazione del bilanciamento del carico.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;OpenMP, Grand Central Dispatch, Intel TBB&lt;/strong&gt;: tecnologie che forniscono direttive e costrutti di alto livello per la parallelizzazione controllata.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Tali astrazioni riducono la probabilità di errori sistemici e favoriscono una gestione più robusta delle risorse concorrenti.&lt;/p&gt;
&lt;h2&gt;Criticità e Differenze tra Sistemi Operativi&lt;/h2&gt;
&lt;p&gt;La programmazione multithread introduce problematiche complesse, tra cui:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;condizioni di race e necessità di meccanismi di sincronizzazione (mutex, semafori, monitor);&lt;/li&gt;
&lt;li&gt;gestione coerente dei segnali in presenza di più flussi di esecuzione;&lt;/li&gt;
&lt;li&gt;utilizzo del Thread-Local Storage (TLS) per mantenere dati isolati per thread;&lt;/li&gt;
&lt;li&gt;politiche di cancellazione e terminazione.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;La cancellazione può essere asincrona, con interruzione immediata del thread, oppure differita, mediante punti di cancellazione espliciti. L’approccio differito è generalmente preferito per preservare la consistenza dello stato condiviso.&lt;/p&gt;
&lt;p&gt;Dal punto di vista implementativo emergono differenze significative:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Windows&lt;/strong&gt; utilizza strutture interne quali ETHREAD, KTHREAD e TEB (Thread Environment Block) per rappresentare e gestire i thread.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Linux&lt;/strong&gt; adotta un modello unificato in cui processi e thread sono rappresentati come task. La system call &lt;code&gt;clone()&lt;/code&gt; consente di specificare, tramite flag, il livello di condivisione delle risorse tra entità padre e figlio, realizzando un modello flessibile di creazione concorrente.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;L’analisi comparativa evidenzia come le scelte progettuali a livello di kernel influenzino direttamente il modello di programmazione esposto agli sviluppatori e le garanzie offerte in termini di isolamento, prestazioni e controllo della concorrenza.&lt;/p&gt;
]]></content>
    
    
      
    
      
    <category term="sistemi operativi"/>
      
    
      
    <category term="thread"/>
      
    
  </entry>
  
  <entry>
    <title>Struttura dei sistemi operativi</title>
    <link href="https://dino-996.github.io/blog/struttura-dei-sistemi-operativi/" rel="alternate" type="text/html"/>
    <id>https://dino-996.github.io/blog/struttura-dei-sistemi-operativi/</id>
    <published>2020-02-14T00:00:00.000Z</published>
    <updated>2020-02-14T00:00:00.000Z</updated>
    
    <summary>Si descrivono le diverse modalità di accesso, dalle interfacce testuali (shell) a quelle grafiche touch-screen, evidenziando come le chiamate di sistema permettono ai programmi di comunicare con il kernel.</summary>
    
    
    <content type="html"><![CDATA[&lt;h1&gt;Introduzione&lt;/h1&gt;
&lt;p&gt;In questo articolo vediamo l&#39;architettura dei sistemi operativi, analizzando il modo in cui gestiscono le risorse hardware e interagiscono con l&#39;utente.&lt;/p&gt;
&lt;h2&gt;Servizi e Interfacce del Sistema Operativo&lt;/h2&gt;
&lt;p&gt;Il sistema operativo (&lt;strong&gt;SO&lt;/strong&gt;) fornisce un ambiente per l&#39;esecuzione dei programmi, offrendo servizi essenziali quali l&#39;&lt;strong&gt;esecuzione di programmi&lt;/strong&gt;, operazioni di &lt;strong&gt;I/O&lt;/strong&gt;, gestione del &lt;strong&gt;file system&lt;/strong&gt;, comunicazioni, rilevamento di errori e allocazione delle risorse.&lt;/p&gt;
&lt;p&gt;L&#39;interazione con l&#39;utente avviene attraverso tre modalità principali:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Interfaccia a riga di comando (CLI):&lt;/strong&gt; utilizza interpreti speciali chiamati &lt;strong&gt;shell&lt;/strong&gt; (come Bash nei sistemi Unix/Linux). È preferita dagli amministratori per l&#39;efficienza e la programmabilità.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Interfaccia grafica (GUI):&lt;/strong&gt; basata sulla metafora del desktop, icone e puntatori.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Interfaccia touch screen:&lt;/strong&gt; basata su gesti (gesture) direttamente sullo schermo.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Interfaccia di Programmazione - System Call e API&lt;/h2&gt;
&lt;p&gt;Le &lt;strong&gt;chiamate di sistema (system call)&lt;/strong&gt; rappresentano l&#39;interfaccia verso i servizi resi disponibili dal SO.&lt;br&gt;
Spesso i programmatori non invocano direttamente le system call, ma utilizzano le &lt;strong&gt;Application Programming Interface (API)&lt;/strong&gt;, come le API di Windows, POSIX (per Unix, Linux, macOS) o le API Java. Questo garantisce la &lt;strong&gt;portabilità delle applicazioni&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Per passare i parametri al sistema operativo durante una chiamata, si utilizzano tre metodi principali:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Attraverso i &lt;strong&gt;registri&lt;/strong&gt; della CPU (metodo più veloce).&lt;/li&gt;
&lt;li&gt;In un &lt;strong&gt;blocco o tabella di memoria&lt;/strong&gt;, passando l&#39;indirizzo del blocco in un registro.&lt;/li&gt;
&lt;li&gt;Nello &lt;strong&gt;stack&lt;/strong&gt;, dove i parametri vengono inseriti (&lt;strong&gt;push&lt;/strong&gt;) e poi prelevati dal SO (&lt;strong&gt;pop&lt;/strong&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Sviluppo ed Esecuzione delle Applicazioni&lt;/h2&gt;
&lt;p&gt;Il processo che porta un codice sorgente alla sua esecuzione prevede due figure chiave:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Linker:&lt;/strong&gt; combina file oggetto rilocabili in un unico &lt;strong&gt;file binario&lt;/strong&gt; eseguibile, includendo eventuali librerie (es. la libreria standard C).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Loader:&lt;/strong&gt; carica il file eseguibile in memoria, assegnando gli indirizzi definitivi (&lt;strong&gt;relocation&lt;/strong&gt;) per permettere l&#39;esecuzione sul core della CPU.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Le applicazioni sono generalmente dipendenti dal SO, poiché ogni sistema fornisce un insieme univoco di chiamate di sistema.&lt;/p&gt;
&lt;h2&gt;Kernel&lt;/h2&gt;
&lt;p&gt;Il kernel è responsabile della gestione delle risorse hardware e dei servizi di base del sistema. Le sue attività principali includono:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Inizializzazione dell&#39;hardware:&lt;/strong&gt; durante il processo di avvio (&lt;strong&gt;boot&lt;/strong&gt;), il kernel viene caricato in memoria e prepara i componenti fisici del computer per l&#39;uso.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Gestione dei processi e della CPU:&lt;/strong&gt; si occupa dello &lt;strong&gt;scheduling&lt;/strong&gt;, decidendo quali programmi devono essere eseguiti dal processore e per quanto tempo.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Gestione della memoria:&lt;/strong&gt; controlla la memoria fisica, la &lt;strong&gt;memoria virtuale&lt;/strong&gt; e la paginazione.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Gestione dei file e dei dispositivi:&lt;/strong&gt; amministra il &lt;strong&gt;file system&lt;/strong&gt; (montando il root file system all&#39;avvio) e i driver necessari per interagire con periferiche, dischi e reti.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Interfaccia per le chiamate di sistema:&lt;/strong&gt; fornisce un&#39;interfaccia protetta tramite la quale le applicazioni possono richiedere servizi al sistema operativo usando le &lt;strong&gt;system call&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Modalità di Esecuzione&lt;/h3&gt;
&lt;p&gt;Il kernel opera in uno spazio di memoria protetto chiamato &lt;strong&gt;kernel space&lt;/strong&gt; (o modalità kernel), distinto dallo spazio utente dove girano le normali applicazioni.&lt;br&gt;
Questa separazione garantisce la stabilità del sistema: un errore critico all&#39;interno del kernel provoca un &lt;strong&gt;crash di sistema&lt;/strong&gt;, non un semplice bug applicativo.&lt;/p&gt;
&lt;h3&gt;Tipologie di Architettura del Kernel&lt;/h3&gt;
&lt;p&gt;Ci sono diversi approcci utilizzati per la progettazione di un kernel:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Kernel Monolitico:&lt;/strong&gt; tutte le funzioni del sistema risiedono in un &lt;strong&gt;unico spazio di indirizzamento&lt;/strong&gt;. Questo approccio (usato da &lt;strong&gt;Unix&lt;/strong&gt; e &lt;strong&gt;Linux&lt;/strong&gt;) offre prestazioni elevate grazie alla comunicazione interna veloce, anche se può risultare più difficile da estendere. Molti kernel monolitici moderni sono comunque &lt;strong&gt;modulari&lt;/strong&gt;, permettendo di caricare funzionalità aggiuntive dinamicamente.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Microkernel&lt;/strong&gt; (es. &lt;strong&gt;Mach&lt;/strong&gt;): fornisce solo le funzioni minime necessarie (come la comunicazione tra processi). La maggior parte dei servizi del sistema operativo viene spostata nello spazio utente per aumentare la modularità e la flessibilità.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Sistemi Ibridi:&lt;/strong&gt; come &lt;strong&gt;Darwin&lt;/strong&gt; (il cuore di macOS e iOS), che combina la struttura a microkernel di Mach con parti del kernel BSD Unix.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Esempi Specifici&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Linux:&lt;/strong&gt; un kernel monolitico ampiamente personalizzabile. Progetti come &lt;em&gt;Linux From Scratch&lt;/em&gt; mostrano come compilare il proprio kernel partendo dal codice sorgente.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Android:&lt;/strong&gt; utilizza un kernel Linux, ma aggiunge uno strato chiamato &lt;strong&gt;HAL&lt;/strong&gt; (Hardware Abstraction Layer) per poter girare su una vasta gamma di dispositivi hardware diversi.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Darwin:&lt;/strong&gt; il kernel alla base dei sistemi Apple, rilasciato come software open-source.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Generazione, Avvio e Debugging&lt;/h2&gt;
&lt;p&gt;Il processo di avvio (&lt;strong&gt;boot&lt;/strong&gt;) segue solitamente questi passaggi:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Un &lt;strong&gt;programma di bootstrap&lt;/strong&gt; (nel firmware BIOS o UEFI) individua e carica il kernel.&lt;/li&gt;
&lt;li&gt;Un bootloader più evoluto, come &lt;strong&gt;GRUB&lt;/strong&gt;, può gestire il caricamento di diversi kernel o parametri di avvio.&lt;/li&gt;
&lt;li&gt;Il kernel inizializza l&#39;hardware e monta il root file system.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Il &lt;strong&gt;debugging&lt;/strong&gt; è l&#39;attività volta a risolvere i &lt;strong&gt;bug&lt;/strong&gt; e ottimizzare le prestazioni.&lt;br&gt;
I sistemi operativi facilitano questo compito tramite:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;file di log&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;core dump&lt;/strong&gt; (immagini della memoria dei processi falliti)&lt;/li&gt;
&lt;li&gt;strumenti di &lt;strong&gt;tracing&lt;/strong&gt; e &lt;strong&gt;contatori&lt;/strong&gt; per monitorare gli eventi e le chiamate di sistema&lt;/li&gt;
&lt;/ul&gt;
]]></content>
    
    
      
    
      
    <category term="sistemi operativi"/>
      
    
      
    <category term="struttura"/>
      
    
  </entry>
  
  <entry>
    <title>Processi</title>
    <link href="https://dino-996.github.io/blog/processi/" rel="alternate" type="text/html"/>
    <id>https://dino-996.github.io/blog/processi/</id>
    <published>2020-02-10T00:00:00.000Z</published>
    <updated>2020-02-10T00:00:00.000Z</updated>
    
    <summary>Vengono esaminati i meccanismi di gestione della memoria, illustrando la differenza tra l&#39;allocazione dinamica nello heap e l&#39;utilizzo temporaneo dello stack per le chiamate di funzione. Process Control Block (PCB), una struttura dati fondamentale che memorizza lo stato, i registri e le informazioni di scheduling necessarie alla CPU.</summary>
    
    
    <content type="html"><![CDATA[&lt;h1&gt;Introduzione&lt;/h1&gt;
&lt;p&gt;In questo articolo approfondiamo l&#39;architettura dei processi all&#39;interno dei sistemi operativi, distinguendo tra il concetto di programma statico e quello di entità attiva in esecuzione.&lt;/p&gt;
&lt;h2&gt;Che cos&#39;è un processo?&lt;/h2&gt;
&lt;p&gt;Un &lt;strong&gt;processo&lt;/strong&gt; è definito come un &lt;strong&gt;programma in esecuzione&lt;/strong&gt;: un’entità attiva dotata di un contatore di programma e di un insieme di risorse associate. Si distingue dal programma, che è invece un’entità passiva, ad esempio un file memorizzato su disco.&lt;/p&gt;
&lt;p&gt;In &lt;strong&gt;memoria&lt;/strong&gt;, un processo è suddiviso in sezioni specifiche:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Testo (Text Segment)&lt;/strong&gt;: contiene il codice eseguibile.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dati (Data Segment)&lt;/strong&gt;: contiene le variabili globali e statiche.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Heap&lt;/strong&gt;: area di memoria allocata dinamicamente durante l’esecuzione.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Stack&lt;/strong&gt;: memoria temporanea utilizzata per le chiamate di funzione e le variabili locali.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Nei sistemi moderni il concetto si estende ai &lt;strong&gt;&lt;a href=&quot;https://dino-996.github.io/blog/thread/&quot;&gt;thread&lt;/a&gt;.&lt;/strong&gt;, che consentono a un singolo processo di avere più percorsi di esecuzione concorrenti, facilitando il parallelismo nei sistemi multicore.&lt;/p&gt;
&lt;h2&gt;Il core e il multicore&lt;/h2&gt;
&lt;p&gt;Un &lt;strong&gt;core&lt;/strong&gt; è un’unità di calcolo fisica indipendente all’interno della CPU.&lt;/p&gt;
&lt;p&gt;Una CPU contiene tipicamente:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Registri&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;ALU (Arithmetic Logic Unit)&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Logica di controllo&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Capacità di eseguire un &lt;strong&gt;flusso di istruzioni indipendente&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Fino ai primi anni 2000, le CPU disponevano generalmente di un solo core. Ciò significava che poteva essere eseguita una sola istruzione alla volta per ciascun ciclo di clock.&lt;/p&gt;
&lt;p&gt;Il sistema operativo utilizzava il &lt;strong&gt;time slicing&lt;/strong&gt;, una tecnica che assegna a ciascun processo un intervallo di tempo limitato di utilizzo della CPU (time slice). Ad esempio:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Processo A per 5 ms&lt;/li&gt;
&lt;li&gt;Processo B per 5 ms&lt;/li&gt;
&lt;li&gt;Processo C per 5 ms&lt;/li&gt;
&lt;li&gt;Ritorno al processo A&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Il cambio tra processi (context switch) avviene così rapidamente da risultare impercettibile all’utente. Tuttavia, il core esegue comunque una sola istruzione per volta: il parallelismo è simulato.&lt;/p&gt;
&lt;p&gt;In un sistema &lt;strong&gt;multicore&lt;/strong&gt;, invece, esistono più core fisici. Di conseguenza:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Il core 1 può eseguire il thread A&lt;/li&gt;
&lt;li&gt;Il core 2 può eseguire il thread B&lt;/li&gt;
&lt;li&gt;Il core 3 può eseguire il thread C&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Tutto nello stesso istante fisico. In questo caso il &lt;strong&gt;parallelismo è reale&lt;/strong&gt;, non simulato.&lt;/p&gt;
&lt;p&gt;Un processo può avere più thread. Ogni thread:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;È un percorso di esecuzione indipendente&lt;/li&gt;
&lt;li&gt;Possiede un proprio program counter&lt;/li&gt;
&lt;li&gt;Ha un proprio stack&lt;/li&gt;
&lt;li&gt;Condivide l’heap e le risorse del processo con gli altri thread&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Ad esempio, con un processo composto da 4 thread e una CPU a 4 core, il sistema operativo può assegnare un thread a ciascun core, eseguendo il programma in parallelo su più unità fisiche.&lt;/p&gt;
&lt;h2&gt;Stato e controllo del processo&lt;/h2&gt;
&lt;p&gt;Ogni processo attraversa diversi stati durante il suo ciclo di vita: &lt;strong&gt;nuovo&lt;/strong&gt;, &lt;strong&gt;pronto&lt;/strong&gt;, &lt;strong&gt;esecuzione&lt;/strong&gt;, &lt;strong&gt;attesa&lt;/strong&gt; e &lt;strong&gt;terminato&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Il sistema operativo rappresenta ogni processo tramite un &lt;strong&gt;PCB (Process Control Block)&lt;/strong&gt;, che memorizza informazioni cruciali quali:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Stato corrente&lt;/li&gt;
&lt;li&gt;PID (Process Identifier)&lt;/li&gt;
&lt;li&gt;Contatore di programma&lt;/li&gt;
&lt;li&gt;Registri della CPU&lt;/li&gt;
&lt;li&gt;Informazioni di memoria&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Il ciclo di vita di un processo&lt;/h2&gt;
&lt;p&gt;Un processo può trovarsi nei seguenti stati:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Nuovo (New)&lt;/strong&gt;: è stato creato ma non è ancora pronto per l’esecuzione.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Pronto (Ready)&lt;/strong&gt;: è in memoria e attende l’assegnazione della CPU.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Esecuzione (Running)&lt;/strong&gt;: sta utilizzando la CPU.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Attesa (Waiting/Blocked)&lt;/strong&gt;: è in attesa di un evento esterno, tipicamente un’operazione di I/O.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Terminato (Terminated)&lt;/strong&gt;: ha completato la propria esecuzione.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Questi stati determinano in quale struttura dati del kernel il processo viene inserito e quali operazioni possono essere effettuate su di esso.&lt;/p&gt;
&lt;h2&gt;Code di scheduling&lt;/h2&gt;
&lt;p&gt;Il sistema operativo mantiene diverse code:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Ready Queue&lt;/strong&gt;: contiene i processi pronti all’esecuzione.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Waiting Queue&lt;/strong&gt;: contiene i processi in attesa di eventi.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Device Queue&lt;/strong&gt;: una coda per ciascun dispositivo di I/O.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Quando un processo richiede un’operazione di I/O:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Viene spostato nella coda del dispositivo.&lt;/li&gt;
&lt;li&gt;Il kernel assegna la CPU a un altro processo pronto.&lt;/li&gt;
&lt;li&gt;Al termine dell’I/O (tramite interrupt), il processo torna nella ready queue.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;Il Process Control Block (PCB)&lt;/h2&gt;
&lt;p&gt;Il &lt;strong&gt;PCB&lt;/strong&gt; è la struttura dati con cui il kernel rappresenta un processo.&lt;/p&gt;
&lt;p&gt;Contiene:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Stato del processo&lt;/li&gt;
&lt;li&gt;PID&lt;/li&gt;
&lt;li&gt;Program counter&lt;/li&gt;
&lt;li&gt;Registri della CPU&lt;/li&gt;
&lt;li&gt;Informazioni di scheduling (priorità, puntatori alle code)&lt;/li&gt;
&lt;li&gt;Informazioni di memoria (tabelle delle pagine, limiti)&lt;/li&gt;
&lt;li&gt;File aperti e risorse allocate&lt;/li&gt;
&lt;li&gt;Informazioni di accounting (tempo di CPU utilizzato)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Il PCB risiede nello spazio kernel ed è accessibile esclusivamente al sistema operativo.&lt;/p&gt;
&lt;h2&gt;Context switch&lt;/h2&gt;
&lt;p&gt;Il &lt;strong&gt;context switch&lt;/strong&gt; è il meccanismo con cui il sistema operativo interrompe un processo e ne attiva un altro.&lt;/p&gt;
&lt;p&gt;Le operazioni principali sono:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Salvare registri e program counter nel PCB del processo corrente.&lt;/li&gt;
&lt;li&gt;Selezionare un processo dalla ready queue.&lt;/li&gt;
&lt;li&gt;Ripristinare lo stato del nuovo processo dal suo PCB.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Il context switch rappresenta un overhead inevitabile del multitasking.&lt;/p&gt;
&lt;h2&gt;Scheduling e politiche decisionali&lt;/h2&gt;
&lt;p&gt;La scelta del processo successivo dipende dall’algoritmo di scheduling:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;FCFS (First Come, First Served)&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;SJF (Shortest Job First)&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Round Robin&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Priority Scheduling&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Ogni algoritmo ottimizza metriche diverse:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Tempo medio di attesa&lt;/li&gt;
&lt;li&gt;Tempo di risposta&lt;/li&gt;
&lt;li&gt;Throughput&lt;/li&gt;
&lt;li&gt;Equità&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Non esiste una soluzione universalmente ottimale: la scelta dipende dal contesto applicativo.&lt;/p&gt;
&lt;h2&gt;Stati estesi&lt;/h2&gt;
&lt;p&gt;Nei sistemi operativi moderni esistono stati aggiuntivi.&lt;/p&gt;
&lt;h3&gt;Suspended&lt;/h3&gt;
&lt;p&gt;Indica che il processo è temporaneamente escluso dalla competizione per la CPU.&lt;/p&gt;
&lt;p&gt;Può accadere per:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Swapping su disco&lt;/li&gt;
&lt;li&gt;Sospensione amministrativa&lt;/li&gt;
&lt;li&gt;Politiche di gestione delle risorse&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Differenza fondamentale:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Waiting&lt;/strong&gt;: attesa di un evento.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Suspended&lt;/strong&gt;: esclusione per decisione del sistema o gestione della memoria.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Zombie&lt;/h3&gt;
&lt;p&gt;Un processo &lt;strong&gt;zombie&lt;/strong&gt; ha terminato l’esecuzione ma il suo PCB non è ancora stato rimosso.&lt;/p&gt;
&lt;p&gt;Il processo:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Rilascia le risorse.&lt;/li&gt;
&lt;li&gt;Conserva nel PCB il codice di uscita.&lt;/li&gt;
&lt;li&gt;Attende che il padre invochi &lt;code&gt;wait()&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Non consuma CPU, ma occupa una voce nella tabella dei processi.&lt;/p&gt;
&lt;h3&gt;Orphan&lt;/h3&gt;
&lt;p&gt;Un processo &lt;strong&gt;orfano&lt;/strong&gt; è un processo il cui padre termina prima di lui.&lt;/p&gt;
&lt;p&gt;Nei sistemi Unix-like viene adottato dal processo &lt;code&gt;init&lt;/code&gt; (PID 1) o da un suo equivalente, che si occuperà di raccoglierne lo stato finale.&lt;/p&gt;
&lt;h2&gt;Coordinamento nei sistemi multicore&lt;/h2&gt;
&lt;p&gt;Nei sistemi multicore:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Più CPU eseguono in parallelo.&lt;/li&gt;
&lt;li&gt;Le strutture dati del kernel sono condivise.&lt;/li&gt;
&lt;li&gt;Le decisioni di scheduling possono avvenire simultaneamente.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Strutture condivise&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Ready queue&lt;/li&gt;
&lt;li&gt;Tabelle dei processi (PCB)&lt;/li&gt;
&lt;li&gt;Tabelle delle pagine&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Accessi concorrenti possono causare condizioni di race.&lt;/p&gt;
&lt;h3&gt;Meccanismi di sincronizzazione&lt;/h3&gt;
&lt;p&gt;Il kernel utilizza:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Spinlock&lt;/li&gt;
&lt;li&gt;Mutex&lt;/li&gt;
&lt;li&gt;Sezioni critiche&lt;/li&gt;
&lt;li&gt;Disabilitazione temporanea degli interrupt&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;per garantire coerenza.&lt;/p&gt;
&lt;h3&gt;Affinità della CPU&lt;/h3&gt;
&lt;p&gt;La &lt;strong&gt;CPU affinity&lt;/strong&gt; consente di associare preferenzialmente un processo a un core specifico per:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Migliorare la località della cache&lt;/li&gt;
&lt;li&gt;Ridurre il costo di migrazione tra core&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;La migrazione tra core comporta invalidazioni di cache e costi aggiuntivi.&lt;/p&gt;
&lt;h2&gt;Comunicazione tra processi (IPC)&lt;/h2&gt;
&lt;p&gt;I processi cooperanti utilizzano meccanismi di &lt;strong&gt;IPC (InterProcess Communication)&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Due modelli principali:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Memoria condivisa&lt;/strong&gt;: comunicazione tramite una regione di memoria comune; è veloce ma richiede sincronizzazione.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Scambio di messaggi&lt;/strong&gt;: basato su primitive &lt;code&gt;send()&lt;/code&gt; e &lt;code&gt;receive()&lt;/code&gt;, può essere sincrono (bloccante) o asincrono (non bloccante).&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Esempi di sistemi IPC e comunicazione client-server&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Pipe&lt;/strong&gt;: canali di comunicazione, tipicamente tra processi correlati.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Socket&lt;/strong&gt;: identificati da indirizzo IP e porta; fondamentali per la comunicazione in rete (TCP e UDP).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;RPC (Remote Procedure Call)&lt;/strong&gt;: permettono di invocare procedure remote come se fossero locali.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Sistemi specifici&lt;/strong&gt;: Mach utilizza porte e messaggi; Windows impiega le &lt;strong&gt;ALPC (Advanced Local Procedure Call)&lt;/strong&gt; per la comunicazione locale.&lt;/li&gt;
&lt;/ul&gt;
]]></content>
    
    
      
    
      
    <category term="sistemi operativi"/>
      
    
      
    <category term="processi"/>
      
    
  </entry>
  
  <entry>
    <title>Paginazione</title>
    <link href="https://dino-996.github.io/blog/paginazione/" rel="alternate" type="text/html"/>
    <id>https://dino-996.github.io/blog/paginazione/</id>
    <published>2020-02-04T00:00:00.000Z</published>
    <updated>2020-02-04T00:00:00.000Z</updated>
    
    <summary>La paginazione è una tecnica di gestione della memoria che permette di allocare i processi in modo non contiguo per eliminare la frammentazione esterna.</summary>
    
    
    <content type="html"><![CDATA[&lt;h1&gt;Introduzione&lt;/h1&gt;
&lt;p&gt;In questo articolo si descrive come la &lt;strong&gt;Memory Management Unit (MMU)&lt;/strong&gt; converta gli indirizzi logici in fisici suddividendo la memoria in pagine e frame, supportata da strutture hardware come il &lt;strong&gt;TLB&lt;/strong&gt; per accelerare le traduzioni. Vengono esaminate diverse configurazioni delle tabelle delle pagine, incluse quelle gerarchiche, invertite e basate su hash, insieme ai meccanismi di protezione e condivisione del codice. Approfondiamo inoltre il concetto di &lt;strong&gt;swapping&lt;/strong&gt;, analizzando le differenze tra l&#39;approccio standard e quello utilizzato nei sistemi mobili come &lt;strong&gt;iOS&lt;/strong&gt; e &lt;strong&gt;Android&lt;/strong&gt;, con esempi concreti di implementazione nelle architetture &lt;strong&gt;Intel IA-32&lt;/strong&gt;, &lt;strong&gt;x86-64&lt;/strong&gt; e &lt;strong&gt;ARMv8&lt;/strong&gt;, evidenziandone le specifiche granularità e i livelli di traduzione.&lt;/p&gt;
&lt;hr&gt;
&lt;h2&gt;Concetti Fondamentali e Meccanismo di Traduzione&lt;/h2&gt;
&lt;p&gt;L&#39;architettura della paginazione suddivide la memoria in blocchi di dimensione fissa:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Pagine&lt;/strong&gt;: blocchi di dimensione fissa nello spazio di indirizzamento &lt;em&gt;logico&lt;/em&gt; del processo.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Frame&lt;/strong&gt;: blocchi di pari dimensione nello spazio di indirizzamento &lt;em&gt;fisico&lt;/em&gt; della RAM.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Ogni indirizzo generato dalla CPU è composto da due parti:&lt;/p&gt;
&lt;table class=&quot;table table-bordered table-hover &quot;&gt;
  &lt;thead class=&quot;table-dark&quot;&gt;
    &lt;tr&gt;
      &lt;th&gt;Campo&lt;/th&gt;
      &lt;th&gt;Simbolo&lt;/th&gt;
      &lt;th&gt;Ruolo&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Numero di pagina&lt;/td&gt;
      &lt;td&gt;&lt;code&gt;p&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Indice nella tabella delle pagine&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Offset di pagina&lt;/td&gt;
      &lt;td&gt;&lt;code&gt;d&lt;/code&gt;&lt;/td&gt;
      &lt;td&gt;Posizione all&#39;interno del frame&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;La MMU traduce un indirizzo logico &lt;code&gt;(p, d)&lt;/code&gt; in un indirizzo fisico &lt;code&gt;(f, d)&lt;/code&gt; estraendo
il numero di frame &lt;code&gt;f&lt;/code&gt; dalla tabella delle pagine e concatenandolo con l&#39;offset.&lt;/p&gt;
&lt;hr&gt;
&lt;div class=&quot;d-flex align-items-center justify-content-center gap-2 flex-wrap my-4&quot;&gt;
  &lt;!-- Indirizzo Logico --&gt;
  &lt;div class=&quot;border rounded px-3 py-2 text-center&quot;&gt;
    &lt;small class=&quot;text-muted d-block&quot;&gt;Indirizzo Logico&lt;/small&gt;
    &lt;span class=&quot;fw-bold font-monospace&quot;&gt;p &amp;nbsp;|&amp;nbsp; d&lt;/span&gt;
  &lt;/div&gt;
  &lt;div class=&quot;text-muted&quot;&gt;→&lt;/div&gt;
  &lt;!-- Tabella delle Pagine --&gt;
  &lt;div class=&quot;border rounded px-3 py-2 bg-primary text-white text-center&quot;&gt;
    &lt;small class=&quot;d-block&quot; style=&quot;opacity:.75&quot;&gt;lookup&lt;/small&gt;
    &lt;span class=&quot;fw-bold&quot;&gt;Page Table&lt;/span&gt;
  &lt;/div&gt;
  &lt;div class=&quot;text-muted&quot;&gt;→&lt;/div&gt;
  &lt;!-- MMU --&gt;
  &lt;div class=&quot;border rounded px-3 py-2 bg-secondary text-white text-center fw-bold&quot;&gt;
    MMU
  &lt;/div&gt;
  &lt;div class=&quot;text-muted&quot;&gt;→&lt;/div&gt;
  &lt;!-- Indirizzo Fisico --&gt;
  &lt;div class=&quot;border rounded px-3 py-2 text-center&quot;&gt;
    &lt;small class=&quot;text-muted d-block&quot;&gt;Indirizzo Fisico&lt;/small&gt;
    &lt;span class=&quot;fw-bold font-monospace&quot;&gt;f &amp;nbsp;|&amp;nbsp; d&lt;/span&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;hr&gt;
&lt;p&gt;La dimensione delle pagine è definita dall&#39;hardware e corrisponde sempre a una potenza
di 2 (tipicamente tra &lt;strong&gt;4 KB e 1 GB&lt;/strong&gt;), il che semplifica il calcolo degli indirizzi a
operazioni di shift e mask sui bit.&lt;/p&gt;
&lt;h2&gt;Frammentazione e Condivisione del Codice&lt;/h2&gt;
&lt;p&gt;La paginazione &lt;strong&gt;elimina la frammentazione esterna&lt;/strong&gt;: qualsiasi frame libero può essere
assegnato a un processo indipendentemente dalla sua posizione fisica. Introduce però la
&lt;strong&gt;frammentazione interna&lt;/strong&gt;: se la memoria richiesta non è multiplo esatto della dimensione
della pagina, l&#39;ultimo frame assegnato risulta parzialmente inutilizzato.&lt;/p&gt;
&lt;p&gt;Un vantaggio significativo è la &lt;strong&gt;condivisione del codice&lt;/strong&gt;: in ambienti multiprocesso è
possibile mantenere in memoria una sola copia di librerie condivise (es. &lt;code&gt;libc&lt;/code&gt;).
Le tabelle delle pagine di processi distinti puntano agli stessi frame marcati come
&lt;em&gt;read-only&lt;/em&gt;, garantendo protezione e risparmio di memoria.&lt;/p&gt;
&lt;h2&gt;Supporto Hardware: il TLB&lt;/h2&gt;
&lt;p&gt;Poiché la tabella delle pagine risiede in memoria principale, senza ottimizzazioni ogni
accesso ai dati richiederebbe &lt;strong&gt;due accessi in memoria&lt;/strong&gt; (uno per la tabella, uno per il
dato). Il &lt;strong&gt;TLB (Translation Lookaside Buffer)&lt;/strong&gt; è una piccola cache hardware ad
altissima velocità che risolve questo problema.&lt;/p&gt;
&lt;hr&gt;
&lt;div class=&quot;d-inline-flex flex-column gap-3&quot;&gt;
  &lt;!-- CPU → TLB → Frame (hit) --&gt;
  &lt;div class=&quot;d-flex align-items-center gap-2&quot;&gt;
    &lt;div class=&quot;border rounded px-3 py-2 text-center fw-bold&quot;&gt;CPU&lt;/div&gt;
    &lt;div class=&quot;text-muted&quot;&gt;→&lt;/div&gt;
    &lt;div class=&quot;border rounded px-3 py-2 bg-primary text-white text-center fw-bold&quot;&gt;TLB&lt;/div&gt;
    &lt;div class=&quot;text-muted&quot;&gt;→&lt;/div&gt;
    &lt;div class=&quot;border rounded px-3 py-2 text-success text-center&quot;&gt;
      &lt;small class=&quot;d-block text-muted&quot;&gt;hit&lt;/small&gt;
      &lt;span class=&quot;fw-bold&quot;&gt;Frame&lt;/span&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;!-- freccia giù + label --&gt;
  &lt;div class=&quot;d-flex align-items-center gap-2&quot;&gt;
    &lt;div style=&quot;width: 106px;&quot;&gt;&lt;/div&gt; &lt;!-- spacer allineato al TLB --&gt;
    &lt;div class=&quot;text-muted d-flex flex-column align-items-center&quot;&gt;
      &lt;small&gt;miss&lt;/small&gt;
      &lt;span&gt;↓&lt;/span&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;!-- Page Table → Frame (miss) --&gt;
  &lt;div class=&quot;d-flex align-items-center gap-2&quot;&gt;
    &lt;div style=&quot;width: 106px;&quot;&gt;&lt;/div&gt; &lt;!-- spacer allineato al TLB --&gt;
    &lt;div class=&quot;border rounded px-3 py-2 bg-warning-subtle text-center&quot;&gt;
      &lt;small class=&quot;text-muted d-block&quot;&gt;miss&lt;/small&gt;
      &lt;span class=&quot;fw-bold&quot;&gt;Page Table&lt;/span&gt;
      &lt;small class=&quot;text-muted d-block&quot;&gt;in RAM&lt;/small&gt;
    &lt;/div&gt;
    &lt;div class=&quot;text-muted&quot;&gt;→&lt;/div&gt;
    &lt;div class=&quot;border rounded px-3 py-2 text-center&quot;&gt;
      &lt;small class=&quot;text-muted d-block&quot;&gt;+ aggiornamento TLB&lt;/small&gt;
      &lt;span class=&quot;fw-bold&quot;&gt;Frame&lt;/span&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;hr&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;TLB hit&lt;/strong&gt;: il numero di frame è immediatamente disponibile, nessun accesso extra.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;TLB miss&lt;/strong&gt;: si consulta la tabella in memoria, con penalità di latenza.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Calcolo del Tempo di Accesso Effettivo (EAT)&lt;/h3&gt;
&lt;p&gt;Dato un hit ratio $h$ e un tempo di accesso alla memoria $t$:&lt;/p&gt;
&lt;p&gt;$&#92;text{EAT}=ht+(1-h)2t=t(2-h)$&lt;/p&gt;
&lt;p&gt;Con $h=0.99$ e $t = 100 &#92;text{ns}$:&lt;/p&gt;
&lt;p&gt;$&#92;text{EAT}=100(2-0.99)=101 &#92;text{ns}$&lt;/p&gt;
&lt;p&gt;Alcuni TLB utilizzano gli &lt;strong&gt;ASID (Address-Space Identifiers)&lt;/strong&gt; per taggare le entry per
processo, evitando il flush completo della cache a ogni cambio di contesto e migliorando
sensibilmente le prestazioni in sistemi multiprocesso.&lt;/p&gt;
&lt;h2&gt;Struttura delle Tabelle delle Pagine&lt;/h2&gt;
&lt;p&gt;Con spazi di indirizzamento a 64 bit, una tabella delle pagine piatta occuperebbe
quantità di memoria proibitive. Si adottano quindi strutture alternative:&lt;/p&gt;
&lt;h3&gt;Paginazione Gerarchica&lt;/h3&gt;
&lt;p&gt;La tabella stessa viene paginata su più livelli (tipicamente 2–4). Su x86-64 si usano
4 livelli: PML4 → PDPT → PD → PT. Il registro &lt;strong&gt;CR3&lt;/strong&gt; punta alla tabella di livello
superiore (PML4).&lt;/p&gt;
&lt;h3&gt;Tabelle Hash&lt;/h3&gt;
&lt;p&gt;Comuni per spazi di indirizzamento superiori a 32 bit. Ogni entry contiene una lista
concatenata per gestire le collisioni. Variante comune: &lt;strong&gt;clustered page tables&lt;/strong&gt;, che
mappano più pagine per entry.&lt;/p&gt;
&lt;h3&gt;Tabella Invertita&lt;/h3&gt;
&lt;p&gt;Un&#39;unica tabella di sistema con un&#39;entry per ogni &lt;strong&gt;frame fisico&lt;/strong&gt; (anziché per ogni
pagina logica). Riduce drasticamente l&#39;occupazione di memoria, ma complica la ricerca
(spesso si affianca una hash table per accelerarla).&lt;/p&gt;
&lt;h2&gt;Huge Pages: un&#39;Ottimizzazione per Sistemi Moderni&lt;/h2&gt;
&lt;p&gt;In scenari ad alte prestazioni (database, JVM, ambienti cloud-native), si utilizzano
&lt;strong&gt;Huge Pages&lt;/strong&gt; (2 MB o 1 GB su x86-64) per ridurre il numero di entry nella tabella
delle pagine e aumentare l&#39;hit rate del TLB.&lt;/p&gt;
&lt;p&gt;Su Linux è possibile abilitarle tramite:&lt;/p&gt;
&lt;pre class=&quot;language-bash&quot;&gt;&lt;code class=&quot;language-bash&quot;&gt;&lt;span class=&quot;token comment&quot;&gt;# Verifica disponibilità&lt;/span&gt;
&lt;span class=&quot;token function&quot;&gt;grep&lt;/span&gt; &lt;span class=&quot;token parameter variable&quot;&gt;-i&lt;/span&gt; hugepages /proc/meminfo

&lt;span class=&quot;token comment&quot;&gt;# Allocazione di 512 huge pages da 2MB (= 1GB)&lt;/span&gt;
&lt;span class=&quot;token builtin class-name&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;512&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;&gt;&lt;/span&gt; /proc/sys/vm/nr_hugepages&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Framework come &lt;strong&gt;Java HotSpot&lt;/strong&gt; (&lt;code&gt;-XX:+UseHugePages&lt;/code&gt;) e &lt;strong&gt;DPDK&lt;/strong&gt; le sfruttano
attivamente per ridurre la latenza.&lt;/p&gt;
&lt;h2&gt;Swapping e Memoria Ausiliaria&lt;/h2&gt;
&lt;p&gt;Lo swapping consente di spostare temporaneamente pagine o interi processi dalla RAM a
una &lt;strong&gt;memoria ausiliaria (backing store)&lt;/strong&gt;, estendendo virtualmente la memoria disponibile.&lt;/p&gt;
&lt;h3&gt;Desktop e Server (Linux/Windows)&lt;/h3&gt;
&lt;p&gt;Supportano lo swapping standard e lo &lt;strong&gt;swapping con paginazione&lt;/strong&gt; (demand paging).
Su Linux, strumenti moderni come &lt;strong&gt;&lt;code&gt;zswap&lt;/code&gt;&lt;/strong&gt; e &lt;strong&gt;&lt;code&gt;zram&lt;/code&gt;&lt;/strong&gt; comprimono le pagine prima
di scriverle su disco, riducendo la latenza e l&#39;usura degli SSD — particolarmente
rilevante in ambienti containerizzati (Docker, Kubernetes).&lt;/p&gt;
&lt;h3&gt;Sistemi Mobili (iOS / Android)&lt;/h3&gt;
&lt;p&gt;Le memorie flash hanno cicli di scrittura limitati e latenze non trascurabili, quindi:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;iOS&lt;/strong&gt; non implementa swapping tradizionale: notifica alle app di liberare memoria
volontariamente tramite i &lt;em&gt;memory pressure notifications&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Android&lt;/strong&gt; salva lo stato dell&#39;app nello storage prima di terminarla (&lt;em&gt;killed process&lt;/em&gt;),
consentendo un riavvio rapido con ripristino dello stato.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Architetture Reali a Confronto&lt;/h2&gt;
&lt;h3&gt;Intel IA-32&lt;/h3&gt;
&lt;p&gt;Utilizza un sistema misto &lt;strong&gt;segmentazione + paginazione a due livelli&lt;/strong&gt;. L&#39;estensione
&lt;strong&gt;PAE (Physical Address Extension)&lt;/strong&gt; espande gli indirizzi fisici da 32 a &lt;strong&gt;36 bit&lt;/strong&gt;,
permettendo ai processori a 32 bit di indirizzare fino a &lt;strong&gt;64 GB&lt;/strong&gt; di RAM fisica (pur
mantenendo uno spazio virtuale per processo di 4 GB).&lt;/p&gt;
&lt;h3&gt;Intel x86-64&lt;/h3&gt;
&lt;p&gt;Adotta una struttura a &lt;strong&gt;4 livelli&lt;/strong&gt; (estendibile a 5 con LA57), gestita dal registro
&lt;strong&gt;CR3&lt;/strong&gt;. Supporta pagine da 4 KB, 2 MB (Large Pages) e 1 GB (Huge Pages).&lt;/p&gt;
&lt;h3&gt;ARMv8 / AArch64&lt;/h3&gt;
&lt;p&gt;Supporta granularità di traduzione di &lt;strong&gt;4 KB, 16 KB e 64 KB&lt;/strong&gt; con fino a &lt;strong&gt;4 livelli&lt;/strong&gt;
di paginazione. Il registro &lt;strong&gt;TTBR0&lt;/strong&gt; punta alla tabella del processo utente, &lt;strong&gt;TTBR1&lt;/strong&gt;
a quella del kernel, separando nettamente i due spazi di indirizzamento.&lt;/p&gt;
&lt;h2&gt;Best Practice per gli Sviluppatori&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;Perché un dev dovrebbe preoccuparsene?&lt;/strong&gt; Le scelte di allocazione impattano direttamente le performance a causa dell&#39;interazione con la paginazione.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Evita allocazioni frammentate&lt;/strong&gt;: preferisci buffer contigui (array vs linked list)
per massimizzare la locality e ridurre i TLB miss.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Valuta Huge Pages&lt;/strong&gt; per applicazioni con working set &amp;gt; 1 GB (JVM, Redis, PostgreSQL).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Attenzione ai memory-mapped file&lt;/strong&gt; (&lt;code&gt;mmap&lt;/code&gt;): sfruttano direttamente la paginazione
e possono ridurre le copie di dati tra kernel e userspace.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Profila i TLB miss&lt;/strong&gt; con &lt;code&gt;perf stat -e dTLB-load-misses ./myapp&lt;/code&gt; su Linux prima
di ottimizzare prematuramente.&lt;/li&gt;
&lt;/ul&gt;
]]></content>
    
    
      
    
      
    <category term="sistemi operativi"/>
      
    
      
    <category term="paginazione"/>
      
    
  </entry>
  
  <entry>
    <title>Sincronizzazione dei processi</title>
    <link href="https://dino-996.github.io/blog/sincronizzazione-dei-processi/" rel="alternate" type="text/html"/>
    <id>https://dino-996.github.io/blog/sincronizzazione-dei-processi/</id>
    <published>2020-02-03T00:00:00.000Z</published>
    <updated>2020-02-03T00:00:00.000Z</updated>
    
    <summary>Viene esaminata la sincronizzazione dei processi nei sistemi operativi, focalizzandosi sulla gestione dell&#39;accesso concorrente a dati condivisi per prevenire le race condition.</summary>
    
    
    <content type="html"><![CDATA[&lt;h1&gt;Introduzione&lt;/h1&gt;
&lt;p&gt;In questo articolo esploriamo la sincronizzazione dei processi per garantire mutua esclusione, progresso e attesa limitata nella sezione critica. Vengono illustrati strumenti che spaziano dal supporto hardware (CAS) a mutex, semafori e monitor, analizzando criticità di liveness come stallo e inversione di priorità. La selezione della tecnica più efficiente viene infine valutata in base al livello di contesa del sistema.&lt;/p&gt;
&lt;h2&gt;Sincronizzazione dei Processi&lt;/h2&gt;
&lt;p&gt;La sincronizzazione dei processi è uno dei problemi fondamentali dell’informatica dei sistemi. Quando più thread o processi accedono a dati condivisi, il rischio di inconsistenza diventa concreto. Comprendere e progettare correttamente la &lt;strong&gt;sezione critica&lt;/strong&gt; è essenziale per garantire sicurezza, affidabilità e scalabilità.&lt;/p&gt;
&lt;h2&gt;Il Problema della Sezione Critica&lt;/h2&gt;
&lt;p&gt;Una &lt;strong&gt;sezione critica (Critical Section, CS)&lt;/strong&gt; è la porzione di codice in cui un &lt;a href=&quot;https://dino-996.github.io/blog/processi/&quot;&gt;processo&lt;/a&gt; o &lt;a href=&quot;https://dino-996.github.io/blog/thread/&quot;&gt;thread&lt;/a&gt; accede e modifica risorse condivise.&lt;/p&gt;
&lt;p&gt;Qualsiasi soluzione corretta deve soddisfare tre proprietà fondamentali:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Mutua esclusione&lt;/strong&gt;: se un processo è nella CS, nessun altro può entrarvi.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Progresso&lt;/strong&gt;: la scelta del prossimo processo da far entrare non può essere rinviata indefinitamente.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Attesa limitata&lt;/strong&gt;: esiste un limite massimo al numero di accessi consentiti ad altri processi prima che una richiesta venga soddisfatta.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Kernel preemptive e non preemptive&lt;/h3&gt;
&lt;p&gt;Nei kernel &lt;strong&gt;non preemptive&lt;/strong&gt;, un processo in modalità kernel non viene interrotto fino al termine della sua esecuzione in tale modalità. Questo riduce il rischio di race condition interne al kernel, ma non costituisce una protezione generale: le condizioni di gara possono comunque verificarsi tra thread in user space o in sistemi multiprocessore.&lt;/p&gt;
&lt;p&gt;Nei sistemi moderni, prevalentemente &lt;strong&gt;preemptive e multi-core&lt;/strong&gt;, la sincronizzazione è un requisito strutturale.&lt;/p&gt;
&lt;h2&gt;Modelli di Memoria e Riordino delle Istruzioni&lt;/h2&gt;
&lt;p&gt;Le architetture moderne (x86, ARM, Power) adottano &lt;strong&gt;modelli di memoria debolmente ordinati&lt;/strong&gt;. Compilatori e CPU possono riordinare le istruzioni per ottimizzazione.&lt;/p&gt;
&lt;p&gt;Senza adeguate &lt;strong&gt;memory barrier&lt;/strong&gt; o &lt;strong&gt;memory fence&lt;/strong&gt; (istruzioni che impongono un vincolo sull’ordine con cui operazioni di lettura e scrittura in memoria possono essere eseguite e rese visibili agli altri thread), un algoritmo teoricamente corretto può fallire nella pratica.&lt;/p&gt;
&lt;p&gt;Questo è il motivo per cui soluzioni puramente software come l’algoritmo di Peterson non sono affidabili su hardware moderno senza primitive di sincronizzazione esplicite.&lt;/p&gt;
&lt;h2&gt;Soluzioni Software e Supporto Hardware&lt;/h2&gt;
&lt;h3&gt;Algoritmo di Peterson&lt;/h3&gt;
&lt;p&gt;Soluzione elegante per due processi basata su variabili condivise (&lt;code&gt;flag&lt;/code&gt; e &lt;code&gt;turn&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;Limiti principali:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Non scala oltre due thread.&lt;/li&gt;
&lt;li&gt;Non sicura su architetture moderne senza memory fence.&lt;/li&gt;
&lt;li&gt;Non adatta a sistemi ad alta contesa.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;È oggi più rilevante come esercizio teorico che come soluzione pratica.&lt;/p&gt;
&lt;h3&gt;Primitive Atomiche Hardware&lt;/h3&gt;
&lt;p&gt;Le CPU moderne offrono istruzioni atomiche come:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;test_and_set&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;compare_and_swap (CAS)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Il &lt;strong&gt;CAS&lt;/strong&gt; è particolarmente importante nei sistemi concorrenti moderni.&lt;/p&gt;
&lt;p&gt;Esempio concettuale:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;CAS(address, expected, new_value)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Aggiorna il valore solo se coincide con &lt;code&gt;expected&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Problemi possibili:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;ABA problem&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Starvation&lt;/strong&gt; in presenza di alta contesa&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Queste primitive sono alla base di strutture dati &lt;strong&gt;lock-free&lt;/strong&gt;.&lt;/p&gt;
&lt;h2&gt;Strumenti di Sincronizzazione di Alto Livello&lt;/h2&gt;
&lt;h3&gt;Mutex&lt;/h3&gt;
&lt;p&gt;Strumento basilare per garantire mutua esclusione.&lt;/p&gt;
&lt;p&gt;Due categorie principali:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Spinlock&lt;/strong&gt;: attesa attiva (busy waiting). Efficiente per sezioni critiche molto brevi su sistemi multi-core.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Mutex bloccanti&lt;/strong&gt;: sospendono il thread, evitando spreco di CPU ma con overhead di context switch.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Semafori&lt;/h3&gt;
&lt;p&gt;Variabili intere gestite tramite operazioni atomiche:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;wait()&lt;/code&gt; (P)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;signal()&lt;/code&gt; (V)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Possono essere:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Binari&lt;/strong&gt; (equivalenti a mutex)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Contatori&lt;/strong&gt; (gestione pool di risorse)&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Monitor&lt;/h3&gt;
&lt;p&gt;Costrutti di alto livello che incapsulano:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Dati condivisi&lt;/li&gt;
&lt;li&gt;Procedure&lt;/li&gt;
&lt;li&gt;Variabili condizionali&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Garantiscono mutua esclusione automatica.&lt;/p&gt;
&lt;h2&gt;Esempi in Python e Java&lt;/h2&gt;
&lt;h3&gt;Python (threading)&lt;/h3&gt;
&lt;pre class=&quot;language-python&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; threading

lock &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; threading&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Lock&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
counter &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;increment&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;global&lt;/span&gt; counter
    &lt;span class=&quot;token keyword&quot;&gt;with&lt;/span&gt; lock&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        counter &lt;span class=&quot;token operator&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Nota: a causa del &lt;strong&gt;GIL (Global Interpreter Lock)&lt;/strong&gt;, il threading in CPython non è realmente parallelo su CPU-bound tasks.&lt;/p&gt;
&lt;hr&gt;
&lt;h3&gt;Java&lt;/h3&gt;
&lt;pre class=&quot;language-java&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;token import&quot;&gt;&lt;span class=&quot;token namespace&quot;&gt;java&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;util&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;concurrent&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;atomic&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;AtomicInteger&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token class-name&quot;&gt;AtomicInteger&lt;/span&gt; counter &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;AtomicInteger&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;increment&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
    counter&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;incrementAndGet&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Java fornisce:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;synchronized&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ReentrantLock&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;package &lt;code&gt;java.util.concurrent&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Il modello di memoria Java definisce formalmente la visibilità delle variabili condivise.&lt;/p&gt;
&lt;h2&gt;Liveness e Problemi Classici&lt;/h2&gt;
&lt;p&gt;La &lt;strong&gt;liveness&lt;/strong&gt; misura la capacità del sistema di garantire progresso.&lt;/p&gt;
&lt;p&gt;Principali criticità:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Deadlock&lt;/strong&gt;: attesa circolare indefinita.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Starvation&lt;/strong&gt;: un processo non ottiene mai accesso alla risorsa.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Priority Inversion&lt;/strong&gt;: un processo ad alta priorità bloccato da uno a bassa priorità.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Caso emblematico: &lt;strong&gt;Mars Pathfinder (1997)&lt;/strong&gt;. Il problema fu risolto abilitando il meccanismo di &lt;em&gt;priority inheritance&lt;/em&gt;.&lt;/p&gt;
&lt;h2&gt;Sincronizzazione e Scalabilità in Ambiente Cloud Native&lt;/h2&gt;
&lt;p&gt;Nei &lt;strong&gt;sistemi distribuiti&lt;/strong&gt; (insieme di computer indipendenti che collaborano tra loro come se fossero un unico sistema coerente agli occhi dell’utente o delle applicazioni), la sincronizzazione non riguarda più solo thread locali.&lt;/p&gt;
&lt;p&gt;Si utilizzano:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Lock distribuiti&lt;/strong&gt; (meccanismo che garantisce mutua esclusione tra processi o servizi che girano su nodi diversi di un sistema distribuito) (&lt;a href=&quot;https://redis.io/&quot;&gt;Redis&lt;/a&gt;, &lt;a href=&quot;https://zookeeper.apache.org/&quot;&gt;ZooKeeper&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Transazioni database&lt;/strong&gt; (insieme di operazioni che vengono trattate con un&#39;unica unità logica di lavoro)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Algoritmi di consenso&lt;/strong&gt; (meccanismo che permette a un insieme di nodi di un sistema distribuito di accordarsi su un singolo valore o decisione, anche in presenza di guasti o comunicazioni ritardate) (&lt;a href=&quot;https://raft.github.io/raft.pdf&quot;&gt;Raft&lt;/a&gt;, &lt;a href=&quot;https://www.cs.cornell.edu/home/rvr/Paxos/paxos.pdf&quot;&gt;Paxos&lt;/a&gt;) - Per approfondire: &lt;a href=&quot;https://arxiv.org/abs/2004.05074&quot;&gt;Paxos vs Raft: Have we reached consensus on distributed consensus?&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In architetture &lt;strong&gt;microservizi&lt;/strong&gt;, l’eccesso di lock distribuiti può compromettere la scalabilità (intesa come capacità di un sistema di gestire un aumento del carico di lavoro senza degradare significativamente le prestazioni.).&lt;/p&gt;
&lt;p&gt;Approcci moderni:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Design &lt;strong&gt;event-driven&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Code di messaggi&lt;/li&gt;
&lt;li&gt;Architetture idempotenti&lt;/li&gt;
&lt;li&gt;Programmazione lock-free&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Valutazione in Base al Livello di Contesa&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Bassa o moderata contesa&lt;/strong&gt;: strutture basate su CAS spesso più performanti.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Alta contesa&lt;/strong&gt;: mutex e meccanismi bloccanti possono risultare più efficienti.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;La scelta dipende da:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Durata della sezione critica&lt;/li&gt;
&lt;li&gt;Numero di core&lt;/li&gt;
&lt;li&gt;Pattern di accesso&lt;/li&gt;
&lt;li&gt;Requisiti di latenza&lt;/li&gt;
&lt;/ul&gt;
]]></content>
    
    
      
    
      
    <category term="sistemi operativi"/>
      
    
      
    <category term="processi"/>
      
    
  </entry>
  
  <entry>
    <title>Memoria centrale</title>
    <link href="https://dino-996.github.io/blog/memoria-centrale/" rel="alternate" type="text/html"/>
    <id>https://dino-996.github.io/blog/memoria-centrale/</id>
    <published>2020-02-02T00:00:00.000Z</published>
    <updated>2020-02-02T00:00:00.000Z</updated>
    
    <summary>Presentiamo la memoria centrale nei sistemi operativi, con particolare focus sulla necessità di ospitare dati e programmi durante l&#39;esecuzione.</summary>
    
    
    <content type="html"><![CDATA[&lt;h1&gt;Introduzione&lt;/h1&gt;
&lt;p&gt;In questo articolo esploriamo le distinzioni tra memorie volatili e non volatili, oltre ai meccanismi hardware, come i registri base e limite, necessari per proteggere le aree riservate del sistema e dei singoli processi. Si illustra come gli indirizzi vengano trasformati da logici a fisici tramite l&#39;unità di gestione della memoria (MMU) e si descrive le diverse fasi di associazione degli indirizzi. Particolare attenzione è rivolta all&#39;allocazione contigua, confrontando strategie come first-fit, best-fit e worst-fit per l&#39;assegnazione dello spazio disponibile. Infine, si parla di criticità legate alla frammentazione interna ed esterna, proponendo soluzioni come la compattazione o la paginazione per ottimizzare l&#39;uso della RAM.&lt;/p&gt;
&lt;h2&gt;Fondamenti della Memoria Centrale&lt;/h2&gt;
&lt;p&gt;Per essere eseguiti, i programmi e i dati devono risiedere, almeno in parte, nella memoria centrale. Le principali tipologie di memoria includono:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;ROM (Read-Only Memory):&lt;/strong&gt; Memoria non volatile che mantiene i dati anche senza alimentazione.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;RAM (Random-Access Memory):&lt;/strong&gt; Memoria volatile utilizzata per l’esecuzione dei programmi; i dati vengono persi allo spegnimento.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cache:&lt;/strong&gt; Memoria volatile molto più veloce della RAM, posta tra CPU e RAM per ottimizzare le prestazioni.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Best practice:&lt;/strong&gt; In applicazioni Python o Java, un uso attento delle strutture dati e della gestione della memoria heap può ridurre il carico sulla RAM e migliorare la scalabilità.&lt;/p&gt;
&lt;h2&gt;Protezione della Memoria&lt;/h2&gt;
&lt;p&gt;Il sistema operativo deve garantire che i processi utente siano isolati tra loro e protetti dagli accessi non autorizzati. La protezione avviene a &lt;strong&gt;livello hardware&lt;/strong&gt;, riducendo l’impatto sulle prestazioni. I meccanismi principali sono:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Registro Base:&lt;/strong&gt; Contiene l’indirizzo fisico più basso accessibile a un processo.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Registro Limite:&lt;/strong&gt; Definisce l’intervallo massimo di indirizzi utilizzabili.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Eccezioni/Trap:&lt;/strong&gt; Tentativi di accesso non autorizzato generano errori hardware che riportano il controllo al sistema operativo.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Associazione degli Indirizzi (Binding)&lt;/h2&gt;
&lt;p&gt;Gli indirizzi di memoria di un programma evolvono lungo le fasi di esecuzione:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Indirizzi simbolici:&lt;/strong&gt; Nomi di variabili nel codice sorgente.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Indirizzi rilocabili:&lt;/strong&gt; Offset rispetto all’inizio di un modulo.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Codice assoluto:&lt;/strong&gt; Se la posizione è nota in fase di compilazione.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Associazione a runtime:&lt;/strong&gt; Necessaria quando un processo può essere spostato durante l’esecuzione.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Spazi di Indirizzamento e MMU&lt;/h2&gt;
&lt;p&gt;Esiste una distinzione fondamentale tra &lt;strong&gt;indirizzo virtuale/logico&lt;/strong&gt; (generato dalla CPU) e &lt;strong&gt;indirizzo fisico&lt;/strong&gt; (usato dalla RAM). La &lt;strong&gt;MMU (Memory Management Unit)&lt;/strong&gt; effettua la traduzione dinamica degli indirizzi logici in fisici, spesso sommando un registro di rilocazione.&lt;/p&gt;
&lt;div class=&quot;d-flex align-items-center gap-2 flex-wrap my-4&quot;&gt;
  &lt;div class=&quot;border rounded px-3 py-2 bg-body text-center fw-bold&quot;&gt;
    CPU
  &lt;/div&gt;
  &lt;div class=&quot;text-muted&quot;&gt;→&lt;/div&gt;
  &lt;div class=&quot;border rounded px-3 py-2 bg-body text-center&quot;&gt;
    &lt;small class=&quot;text-muted d-block&quot;&gt;Indirizzo Logico&lt;/small&gt;
    &lt;span class=&quot;fw-bold&quot;&gt;(Virtuale)&lt;/span&gt;
  &lt;/div&gt;
  &lt;div class=&quot;text-muted&quot;&gt;→&lt;/div&gt;
  &lt;div class=&quot;border rounded px-3 py-2 bg-primary text-white text-center fw-bold&quot;&gt;
    MMU
  &lt;/div&gt;
  &lt;div class=&quot;text-muted&quot;&gt;→&lt;/div&gt;
  &lt;div class=&quot;border rounded px-3 py-2 bg-body text-center&quot;&gt;
    &lt;small class=&quot;text-muted d-block&quot;&gt;Indirizzo Fisico&lt;/small&gt;
    &lt;span class=&quot;fw-bold&quot;&gt;RAM&lt;/span&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Nei sistemi moderni, la MMU supporta paginazione a più livelli e segmentazione, fondamentali per la gestione di grandi spazi di memoria in ambienti cloud.&lt;/p&gt;
&lt;h2&gt;Allocazione Contigua della Memoria&lt;/h2&gt;
&lt;p&gt;Nell’&lt;strong&gt;allocazione contigua&lt;/strong&gt;, ogni processo riceve un blocco unico e continuo di memoria fisica. Questo approccio è semplice e veloce, poiché il calcolo dell’indirizzo fisico è diretto (basta sommare un offset), ma può generare inefficienze se la memoria si frammenta nel tempo.&lt;/p&gt;
&lt;p&gt;Gli algoritmi principali per scegliere quale porzione di memoria assegnare a un processo sono:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;First-fit:&lt;/strong&gt; Cerca il primo spazio libero sufficientemente grande. Vantaggio: velocità di allocazione. Svantaggio: può lasciare molti piccoli blocchi inutilizzabili all’inizio della memoria, aumentando la frammentazione esterna.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Best-fit:&lt;/strong&gt; Seleziona lo spazio più piccolo che soddisfa il requisito. Vantaggio: riduce lo spreco di memoria. Svantaggio: può creare numerosi piccoli buchi che non saranno mai utilizzabili.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Worst-fit:&lt;/strong&gt; Assegna lo spazio più grande disponibile. L’idea è che i blocchi residui possano ancora servire ad altri processi. Svantaggio: spesso spreca memoria e non è comune nei sistemi moderni.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Approfondimento pratico:&lt;/strong&gt; Nei linguaggi gestiti come Java, il garbage collector e lo heap gestiscono automaticamente l’allocazione contigua interna degli oggetti, riducendo la complessità per lo sviluppatore, ma la frammentazione può comunque influire sulle performance.&lt;/p&gt;
&lt;h2&gt;Frammentazione della Memoria&lt;/h2&gt;
&lt;p&gt;L’allocazione contigua può portare a due tipi principali di inefficienza:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Frammentazione esterna:&lt;/strong&gt; La memoria libera totale è sufficiente per un nuovo processo, ma è suddivisa in blocchi non contigui. Esempio: 100 MB liberi totali, ma divisi in 4 blocchi da 25 MB; un processo da 50 MB non può essere allocato. Nei sistemi tradizionali con first-fit si può perdere fino al 30–35% della memoria utile.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Frammentazione interna:&lt;/strong&gt; Si verifica quando la partizione assegnata a un processo è leggermente più grande del necessario, lasciando spazio inutilizzato all’interno della partizione stessa. Questo fenomeno è tipico delle allocazioni fisse e dei sistemi legacy.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Nota tecnica:&lt;/strong&gt; Nei sistemi moderni, la frammentazione interna può essere mitigata tramite &lt;strong&gt;heap dinamici&lt;/strong&gt;, &lt;strong&gt;pool di oggetti&lt;/strong&gt; o allocatori di memoria slab (come quelli usati in kernel Linux) che riducono gli sprechi nei blocchi di piccola dimensione.&lt;/p&gt;
&lt;h2&gt;Soluzioni alla Frammentazione&lt;/h2&gt;
&lt;p&gt;Per ridurre le inefficienze causate dalla frammentazione, i sistemi operativi adottano tecniche avanzate:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Compattazione:&lt;/strong&gt; Riordina la memoria spostando i processi attivi per consolidare i blocchi liberi in un’unica area contigua. Richiede supporto hardware per rilocazione dinamica e può essere costosa in termini di performance se eseguita frequentemente.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Paginazione:&lt;/strong&gt; Divide la memoria in pagine di dimensioni fisse (tipicamente 4 KB o multipli) e permette agli indirizzi logici di un processo di mappare pagine fisiche non contigue. Vantaggi: elimina la frammentazione esterna, semplifica la gestione della memoria virtuale e supporta funzionalità come &lt;strong&gt;swap&lt;/strong&gt; su disco. Svantaggi: frammentazione interna minima sulle ultime pagine.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Segmentazione:&lt;/strong&gt; Divide la memoria in segmenti logici (codice, dati, stack) di dimensioni variabili. Consente una gestione più semantica della memoria, ma può ancora soffrire di frammentazione esterna tra segmenti.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Esempio pratico:&lt;/strong&gt; Nei container Docker, la memoria può essere limitata tramite flag &lt;code&gt;--memory&lt;/code&gt; e monitorata da strumenti come cAdvisor, permettendo ai sistemi di orchestrazione di ribilanciare carichi e ridurre sprechi dovuti a frammentazione.&lt;/p&gt;
]]></content>
    
    
      
    
      
    <category term="sistemi operativi"/>
      
    
      
    <category term="memoria centrale"/>
      
    
  </entry>
  
  <entry>
    <title>Stallo dei processi</title>
    <link href="https://dino-996.github.io/blog/stallo-dei-processi/" rel="alternate" type="text/html"/>
    <id>https://dino-996.github.io/blog/stallo-dei-processi/</id>
    <published>2020-02-01T00:00:00.000Z</published>
    <updated>2020-02-01T00:00:00.000Z</updated>
    
    <summary>Viene esaminato il fenomeno dello stallo dei processi (deadlock) nei sistemi operativi, analizzando come più thread competano per risorse limitate.</summary>
    
    
    <content type="html"><![CDATA[&lt;h1&gt;Introduzione&lt;/h1&gt;
&lt;p&gt;In questo articolo approfondiamo le quattro condizioni necessarie affinché si verifichi un blocco totale, distinguendo tra il classico deadlock e il livelock, o stallo attivo. Vengono illustrati diversi strumenti diagnostici, come il grafo di assegnazione delle risorse, e algoritmi complessi come quello del banchiere per garantire uno stato di esecuzione sicuro. Infine, vengono descritte le tecniche di ripristino, che includono la terminazione forzata dei processi o la prelazione delle risorse per interrompere i cicli di attesa circolare.&lt;/p&gt;
&lt;h2&gt;Deadlock&lt;/h2&gt;
&lt;p&gt;Lo &lt;strong&gt;stallo (deadlock)&lt;/strong&gt; è una condizione in cui un insieme di thread o processi rimane bloccato perché ciascuno attende una risorsa detenuta da un altro elemento del gruppo.&lt;/p&gt;
&lt;p&gt;In un sistema con multiprogrammazione, le entità concorrenti competono per un numero finito di &lt;strong&gt;risorse&lt;/strong&gt; (CPU, memoria, file, socket, lock, connessioni database). Una risorsa viene:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Richiesta&lt;/li&gt;
&lt;li&gt;Assegnata&lt;/li&gt;
&lt;li&gt;Utilizzata&lt;/li&gt;
&lt;li&gt;Rilasciata&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Se una risorsa è già assegnata, il richiedente entra in stato di attesa. In determinate condizioni, questa attesa può diventare permanente.&lt;/p&gt;
&lt;h2&gt;Le quattro condizioni necessarie allo stallo&lt;/h2&gt;
&lt;p&gt;Affinché si verifichi un deadlock, devono coesistere simultaneamente quattro condizioni (Coffman conditions):&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Mutua esclusione&lt;/strong&gt;
Almeno una risorsa deve essere non condivisibile (es. un lock esclusivo).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Possesso e attesa (Hold and Wait)&lt;/strong&gt;
Un thread detiene almeno una risorsa e ne attende altre.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Assenza di prelazione (No Preemption)&lt;/strong&gt;
Le risorse non possono essere sottratte forzatamente; devono essere rilasciate volontariamente.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Attesa circolare (Circular Wait)&lt;/strong&gt;
Esiste una catena chiusa di attese tra thread/processi.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Rimuovere anche solo una di queste condizioni impedisce il verificarsi dello stallo.&lt;/p&gt;
&lt;h2&gt;Deadlock, Livelock e Starvation&lt;/h2&gt;
&lt;p&gt;È essenziale distinguere tre fenomeni diversi:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Deadlock&lt;/strong&gt;: blocco permanente.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Livelock&lt;/strong&gt;: i thread sono attivi ma non progrediscono (ad esempio, rilasciano e riacquisiscono risorse continuamente).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Starvation&lt;/strong&gt;: un thread non ottiene mai una risorsa a causa di politiche di scheduling sbilanciate.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In sistemi ad alta concorrenza, la starvation è spesso più subdola del deadlock.&lt;/p&gt;
&lt;h2&gt;Grafo di Assegnazione delle Risorse (RAG)&lt;/h2&gt;
&lt;p&gt;Le situazioni di stallo possono essere modellate tramite un &lt;strong&gt;Resource Allocation Graph&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Nodi circolari → thread/processi&lt;/li&gt;
&lt;li&gt;Nodi rettangolari → risorse&lt;/li&gt;
&lt;li&gt;Arco da thread a risorsa → richiesta&lt;/li&gt;
&lt;li&gt;Arco da risorsa a thread → assegnazione&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Proprietà fondamentali&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Nessun ciclo → nessun deadlock&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Ciclo con una sola istanza per risorsa → deadlock certo&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Ciclo con più istanze → condizione necessaria ma non sufficiente&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Nel caso di risorse con singola istanza, si utilizza spesso il &lt;strong&gt;grafo di attesa&lt;/strong&gt;, più semplice da analizzare.&lt;/p&gt;
&lt;h2&gt;Strategie di Gestione dello Stallo&lt;/h2&gt;
&lt;h3&gt;Ignorare il problema (Approccio “Ostrich”)&lt;/h3&gt;
&lt;p&gt;Sistemi come Microsoft Windows e Linux non implementano meccanismi generali di prevenzione del deadlock a livello kernel per tutte le risorse applicative.&lt;/p&gt;
&lt;p&gt;La responsabilità è delegata allo sviluppatore. È una scelta pragmatica: prevenzione ed evitamento hanno costi computazionali elevati.&lt;/p&gt;
&lt;h3&gt;Prevenzione&lt;/h3&gt;
&lt;p&gt;Consiste nell’eliminare una delle quattro condizioni necessarie. Un esempio pratico può essere quello di prevenire l’attesa circolare imponendo un &lt;strong&gt;ordinamento globale dei lock&lt;/strong&gt;. Se ogni thread acquisisce sempre le risorse nello stesso ordine, il ciclo non può formarsi.&lt;/p&gt;
&lt;h3&gt;Evitamento&lt;/h3&gt;
&lt;p&gt;Richiede conoscenza preventiva delle richieste massime di risorse. L’&lt;strong&gt;algoritmo del banchiere&lt;/strong&gt; verifica che ogni assegnazione mantenga il sistema in uno &lt;strong&gt;stato sicuro&lt;/strong&gt;, ossia uno stato da cui esiste almeno una sequenza di completamento per tutti i processi. È elegante dal punto di vista teorico, ma raramente applicato nei sistemi general-purpose moderni a causa dell’overhead e della necessità di informazioni complete.&lt;/p&gt;
&lt;h4&gt;Algoritmo del banchiere&lt;/h4&gt;
&lt;p&gt;La complessità di questo algoritmo è $O(n^2 m)$, dove $n$ è il numero di processi e $m$ il numero di tipi di risorse.&lt;/p&gt;
&lt;p&gt;Supponiamo:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;3 processi: $P_0, P_1, P_2$&lt;/li&gt;
&lt;li&gt;3 tipi di risorse: $A, B, C$&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Risorse totali disponibili: $&#92;text{Avaible} = [3,3,2]$&lt;/p&gt;
&lt;h5&gt;Matrice delle richieste massime:&lt;/h5&gt;
&lt;div class=&quot;mb-2&quot;&gt;
$
&#92;text{Max}=
&#92;begin{bmatrix}
7 &amp; 5 &amp; 3 &#92;&#92;
3 &amp; 2 &amp; 2 &#92;&#92;
9 &amp; 0 &amp; 2
&#92;end{bmatrix}
$
&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;indica il &lt;strong&gt;massimo&lt;/strong&gt; che ogni processo potrebbe richiedere per ciascuna risorsa. Ad esempio, $P_0$ potrebbe richiedere al massimo 7 unità di $A$, 5 di $B$ e 3 di $C$.&lt;/li&gt;
&lt;/ul&gt;
&lt;h5&gt;Risorse già assegnate:&lt;/h5&gt;
&lt;div class=&quot;mb-2&quot;&gt;
$
&#92;text{Allocation}=
&#92;begin{bmatrix}
0 &amp; 1 &amp; 0 &#92;&#92;
2 &amp; 0 &amp; 0 &#92;&#92;
3 &amp; 0 &amp; 2
&#92;end{bmatrix}
$
&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;indica quante risorse sono &lt;strong&gt;già state date&lt;/strong&gt; a ciascun processo. Ad esempio, $P_1$ ha già ricevuto 2 unità di $A$, 0 di $B$ e 0 di $C$.&lt;/li&gt;
&lt;/ul&gt;
&lt;h5&gt;Calcolo delle risorse ancora necessarie:&lt;/h5&gt;
&lt;div class=&quot;mb-2&quot;&gt;
$
&#92;text{Need}=&#92;text{Max - Allocation}=
&#92;begin{bmatrix}
7 &amp; 4 &amp; 3 &#92;&#92;
1 &amp; 2 &amp; 2 &#92;&#92;
6 &amp; 0 &amp; 0
&#92;end{bmatrix}
$
&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;Si ottiene sottraendo riga per riga: ad esempio per $P_0$, $&#92;text{Need}[0] = [7,5,3] - [0,1,0] = [7,4,3]$. Rappresenta quante risorse mancano ancora a ciascun processo per completare la propria esecuzione.&lt;/li&gt;
&lt;/ul&gt;
&lt;h5&gt;Verifica dello stato sicuro&lt;/h5&gt;
&lt;p&gt;L&#39;algoritmo cerca iterativamente un processo $P_i$ che soddisfi la condizione $&#92;text{Need}[i] &#92;leq &#92;text{Avaible}$, ovvero un processo che possa essere eseguito con le risorse attualmente disponibili. Se lo trova, simula la sua esecuzione: le risorse che gli erano state assegnate vengono &amp;quot;liberate&amp;quot; e aggiunte ad $&#92;text{Avaible}$. Si ripete finché tutti i processi sono stati eseguiti (stato sicuro) oppure non si trova più nessun processo eseguibile (stato non sicuro).&lt;/p&gt;
&lt;p&gt;Partendo da $P_0$ con $&#92;text{Avaible} = [3,3,2]$:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;$P_0 &#92;text{: } &#92;text{Need[0]} = [7,4,3] &#92;to &#92;text{Non soddisfa le condizioni: } 7 &amp;gt; 3 &#92;text{ già alla prima risorsa.}$&lt;/li&gt;
&lt;li&gt;$P_1 &#92;text{: } &#92;text{Need[1]} = [1,2,2] &#92;to &#92;text{Soddisfa le condizioni: } 1 &#92;leq 3, 2 &#92;leq 3, 2 &#92;leq 2. $&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;$P_1$ viene eseguito e le risorse vengono rilasciate. $&#92;text{Avaible}$ si aggiorna sommando $&#92;text{Allocation}[1] = [2,0,0]$:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;$&#92;text{Avaible} = [3,3,2] + [2,0,0] = [5,3,2]$&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Ripartiamo da $P_0$ con $&#92;text{Avaible} = [5,3,2]$:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;$P_0 &#92;text{: } &#92;text{Need[0]} = [7,4,3] &#92;to &#92;text{Non soddisfa le condizioni: } 7 &amp;gt; 5 &#92;text{ già alla prima risorsa.}$&lt;/li&gt;
&lt;li&gt;$P_2 &#92;text{: } &#92;text{Need[2]} = [6,0,0] &#92;to &#92;text{Non soddisfa le condizioni: } 6 &amp;gt; 5 &#92;text{ già alla prima risorsa.}$&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Nessun processo è eseguibile: non è possibile trovare una sequenza sicura. &lt;strong&gt;Lo stato è non sicuro&lt;/strong&gt;, il che significa che il sistema non può garantire che tutti i processi riescano a completarsi. L&#39;algoritmo del banchiere avrebbe quindi rifiutato l&#39;allocazione che ha portato a questo stato.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Conclusione&lt;/strong&gt;: se provassimo a dare subito le risorse disponibili senza priorità andremmo in contro a uno stato non sicuro.&lt;/p&gt;
&lt;p&gt;Se inizialmente avremmo avuto $&#92;text{Avaible}=[10,5,7]$:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;$P_0 &#92;text{: } &#92;text{Need[0]} = [7,4,3] &#92;to &#92;text{Soddisfa le condizioni: } 7 &#92;leq 12, 4 &#92;leq 5, 3 &#92;leq 7. &#92;quad &#92;text{Avaible} = [12,5,7] + [0,1,0] = [12,6,7]$&lt;/li&gt;
&lt;li&gt;$P_1 &#92;text{: } &#92;text{Need[1]} = [1,2,2] &#92;to &#92;text{Soddisfa le condizioni: } 1 &#92;leq 10, 2 &#92;leq 5, 2 &#92;leq 7. &#92;quad &#92;text{Avaible} = [10,5,7] + [2,0,0] = [12,5,7]$&lt;/li&gt;
&lt;li&gt;$P_2 &#92;text{: } &#92;text{Need[2]} = [6,0,0] &#92;to &#92;text{Soddisfa le condizioni: } 6 &#92;leq 12, 0 &#92;leq 6, 0 &#92;leq 7. &#92;quad &#92;text{Avaible} = [12,6,7] + [3,0,2] = [15,6,9]$&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Sequenza sicura&lt;/strong&gt;: $P_0 &#92;to P_1 &#92;to P_2$&lt;/p&gt;
&lt;h3&gt;Rilevamento e Ripristino&lt;/h3&gt;
&lt;p&gt;Utilizzato soprattutto nei database e nei sistemi transazionali.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Rilevamento periodico tramite analisi del grafo di attesa.&lt;/li&gt;
&lt;li&gt;Ripristino tramite:
&lt;ul&gt;
&lt;li&gt;Terminazione di processi&lt;/li&gt;
&lt;li&gt;Prelazione con rollback&lt;/li&gt;
&lt;li&gt;Selezione di una “vittima” secondo criteri di costo&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h1&gt;Deadlock nella pratica: esempi concreti&lt;/h1&gt;
&lt;h2&gt;Esempio in Python&lt;/h2&gt;
&lt;pre class=&quot;language-python&quot;&gt;&lt;code class=&quot;language-python&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; threading
&lt;span class=&quot;token keyword&quot;&gt;import&lt;/span&gt; time

lock1 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; threading&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Lock&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
lock2 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; threading&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Lock&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;thread1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;with&lt;/span&gt; lock1&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        time&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sleep&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;with&lt;/span&gt; lock2&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Thread 1 acquisito lock2&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;token keyword&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;thread2&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;with&lt;/span&gt; lock2&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
        time&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;sleep&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;token keyword&quot;&gt;with&lt;/span&gt; lock1&lt;span class=&quot;token punctuation&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token string&quot;&gt;&quot;Thread 2 acquisito lock1&quot;&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

t1 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; threading&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Thread&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;target&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;thread1&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
t2 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; threading&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;Thread&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;target&lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt;thread2&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;

t1&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;start&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;
t2&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;start&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Qui:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Thread 1 acquisisce &lt;code&gt;lock1&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Thread 2 acquisisce &lt;code&gt;lock2&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Entrambi attendono il lock detenuto dall’altro&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Deadlock perfetto.&lt;/p&gt;
&lt;h3&gt;Soluzione&lt;/h3&gt;
&lt;p&gt;Imporre un ordine globale di acquisizione dei lock.&lt;/p&gt;
&lt;h2&gt;Esempio in Java&lt;/h2&gt;
&lt;pre class=&quot;language-java&quot;&gt;&lt;code class=&quot;language-java&quot;&gt;&lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;DeadlockExample&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Object&lt;/span&gt; lock1 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token keyword&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Object&lt;/span&gt; lock2 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;token keyword&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;token function&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;]&lt;/span&gt; args&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;

        &lt;span class=&quot;token class-name&quot;&gt;Thread&lt;/span&gt; t1 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Thread&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;synchronized&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lock1&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;token keyword&quot;&gt;try&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; 
                    &lt;span class=&quot;token class-name&quot;&gt;Thread&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; 
                &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;catch&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;InterruptedException&lt;/span&gt; e&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
                &lt;span class=&quot;token keyword&quot;&gt;synchronized&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lock2&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;token class-name&quot;&gt;Thread&lt;/span&gt; t2 &lt;span class=&quot;token operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;token class-name&quot;&gt;Thread&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token operator&quot;&gt;-&gt;&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;token keyword&quot;&gt;synchronized&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lock2&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;
                &lt;span class=&quot;token keyword&quot;&gt;try&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt; 
                    &lt;span class=&quot;token class-name&quot;&gt;Thread&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;sleep&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token number&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt; 
                &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;token keyword&quot;&gt;catch&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token class-name&quot;&gt;InterruptedException&lt;/span&gt; e&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
                &lt;span class=&quot;token keyword&quot;&gt;synchronized&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;lock1&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;token punctuation&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
            &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;

        t1&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
        t2&lt;span class=&quot;token punctuation&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;token function&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;token punctuation&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;token punctuation&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In ambienti enterprise basati su Java, questo errore può compromettere interi application server.&lt;/p&gt;
&lt;h1&gt;Deadlock e Cloud Native&lt;/h1&gt;
&lt;p&gt;Nel paradigma &lt;strong&gt;Cloud Native&lt;/strong&gt;, il deadlock non riguarda solo thread locali.&lt;/p&gt;
&lt;p&gt;Può emergere tra:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Microservizi che attendono risposte reciproche&lt;/li&gt;
&lt;li&gt;Pool di connessioni database saturi&lt;/li&gt;
&lt;li&gt;Lock distribuiti&lt;/li&gt;
&lt;li&gt;Transazioni distribuite&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Un pool di connessioni esaurito può generare un deadlock applicativo che si traduce in:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Saturazione CPU&lt;/li&gt;
&lt;li&gt;Timeout a cascata&lt;/li&gt;
&lt;li&gt;Riduzione della disponibilità (effetto simile a un DoS logico)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Qui il problema diventa anche di &lt;strong&gt;sicurezza e resilienza operativa&lt;/strong&gt;.&lt;/p&gt;
]]></content>
    
    
      
    
      
    <category term="sistemi operativi"/>
      
    
      
    <category term="processi"/>
      
    
  </entry>
  

</feed>