Skip to content
Snippets Groups Projects
Commit 77aba381 authored by Peter Gerwinski's avatar Peter Gerwinski
Browse files

Notizen und Beispiele 10.6.2024

parent 3f675746
No related branches found
No related tags found
No related merge requests found
Showing
with 623 additions and 0 deletions
Massenspeicherverwaltung: Dateisysteme
======================================
:) FAT-Dateisystem (MS-DOS)
:) Bootvorgang
:) Datenrettung
ext2-Dateisystem (Linux)
- Einteilung des Datenträgers in "Blöcke"
- separat: Inode-Liste
- Superblock
- Inode: "everything is a file"
- Inode enthält Array mit Zeigern auf Datenblöcke
Wikipedia:
Für das Wurzelverzeichnis eines Dateisystems sind die beiden Verknüpfungen
„.“ und „..“ identisch:
cassini/> ls -dlai . ..
2 drwxr-xr-x 18 root root 4096 7. Mai 21:46 .
2 drwxr-xr-x 18 root root 4096 7. Mai 21:46 ..
Beide Inodes haben die Nr. 2, sind also derselbe.
Diese Unterverzeichnisse hingegen wurden mit "mount" eingehängt:
cassini/> ls -dlai dev proc run sys
1 drwxr-xr-x 19 root root 3760 10. Jun 14:15 dev
1 dr-xr-xr-x 399 root root 0 7. Mai 21:47 proc
1 drwxr-xr-x 35 root root 1080 10. Jun 13:24 run
1 dr-xr-xr-x 13 root root 0 7. Mai 21:47 sys
cassini/> cat /proc/mounts
sysfs /sys sysfs rw,nosuid,nodev,noexec,relatime 0 0
proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0
udev /dev devtmpfs rw,nosuid,relatime,size=1929468k,nr_inodes=482367,mode=755,inode64 0 0
devpts /dev/pts devpts rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000 0 0
tmpfs /run tmpfs rw,nosuid,nodev,noexec,relatime,size=389216k,mode=755,inode64 0 0
/dev/mapper/cassini--vg-root / ext4 rw,relatime,errors=remount-ro 0 0
[...]
cassini/> lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sda 8:0 0 465,8G 0 disk
├─sda1 8:1 0 487M 0 part /boot
├─sda2 8:2 0 1K 0 part
└─sda5 8:5 0 465,3G 0 part
└─sda5_crypt 254:0 0 465,3G 0 crypt
├─cassini--vg-root 254:1 0 464,3G 0 lvm /
└─cassini--vg-swap_1 254:2 0 980M 0 lvm [SWAP]
Sie befinden sich auf unterschiedlichen Dateisystemen.
Jeder Inode Nr. 1 ist insbesondere ein anderer.
Named Pipes (FIFOs):
Sender:
cassini/home/peter/bo/2024ss/bs/20240610> mknod test.pipe p
cassini/home/peter/bo/2024ss/bs/20240610> ls -l
insgesamt 8
-rw-r--r-- 1 peter peter 2039 10. Jun 16:30 bs-20240610.txt
-rw-r--r-- 1 peter peter 100 9. Jun 22:01 SCRATCH
prw-r--r-- 1 peter peter 0 10. Jun 16:41 test.pipe
cassini/home/peter/bo/2024ss/bs/20240610> echo "Hello, world!" > test.pipe
cassini/home/peter/bo/2024ss/bs/20240610>
Empfänger:
cassini/home/peter/bo/2024ss/bs/20240610> ls -l
insgesamt 8
-rw-r--r-- 1 peter peter 2039 10. Jun 16:30 bs-20240610.txt
-rw-r--r-- 1 peter peter 100 9. Jun 22:01 SCRATCH
prw-r--r-- 1 peter peter 0 10. Jun 16:41 test.pipe
cassini/home/peter/bo/2024ss/bs/20240610> cat test.pipe
Hello, world!
cassini/home/peter/bo/2024ss/bs/20240610>
Gleitkommazahlen
================
1/1² + 1/2² + 1/3² + ... = pi²/6
1/1² = 1
1/2² = 0.25
1/3² = 0.111111111...
1/4² = 0.0625
1/5² = 0.04
1/6² = 0.027777777...
Rechnen mit 2 Ziffern Genauigkeit (Abschneiden statt Runden):
1/5² = 0.04
1/6² = 0.027
Summe: 0.067
1.0 + 0.067 = 1.0 · 10^0 + 6.7 · 10^-2 = 1.0
--> Beim Addieren darauf achten, möglichst gleich große Zahlen zu addieren.
Summen: mit den kleinsten Zahlen anfangen
--> Subtrahieren: möglichst vermeiden
Interne Darstellung von Gleitkommazahlen
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
https://de.wikipedia.org/wiki/IEEE_754-2008
Die Zahl 2.351 · 10^5
kann man auch 0.2351 · 10^6
oder 0.02351 · 10^7
schreiben.
Welche dieser Varianten ist hinsichtlich Speicherplatz am ineffizientesten?
0.02351 · 10^7 --> benötigt 7 Zeichen für die Mantisse
Welche dieser Varianten ist hinsichtlich Speicherplatz am effizientesten?
Beide:
2.351 · 10^5 4 Ziffern für die Mantisse (Konvention: immer eine Ziffer vor dem Komma)
0.2351 · 10^6 4 Ziffern für die Mantisse (Konvention: immer eine 0 vor dem Komma)
Bei Binärzahlen:
1.11011 · 2^101 (Konvention: immer eine 1 vor dem Komma) "normalisierte Zahl"
0.111011 · 2^110 (Konvention: immer eine 0 vor dem Komma)
--> Man braucht die Ziffer vor dem Komma nicht mit zu speichern (wegen Konvention).
Wenn vor dem Komma eine 0 steht, muß dahinter eine 1 stehen (wegen Konvention).
--> Wieder beide gleich. (In beiden Fällen brauchen wir nur 5 Binärziffern zu speichern.)
Standard: IEEE 754 (2008)
Gleitkommazahlen immer normalisiert speichern, die führende 1 nicht mitspeichern.
Beispiel: Kodierung einer 32-Bit-Gleitkommazahl:
https://upload.wikimedia.org/wikipedia/commons/5/56/IEEE-754-single.svg
V EEEEEEEE MMMMMMMMMMMMMMMMMMMMMMM
V = Vorzeichen-Bit
E = "Charakteristik" Siehe: https://de.wikipedia.org/wiki/IEEE_754
M = Mantisse (für 32-Bit-floats: Exponent = Charakteristik - 127)
Aufgabe: Schreibe ein Progrämmchen, das eine float-Variable lesbar ausgibt,
ohne printf() mit "%f" zu verwenden, also durch Analysieren der Bits.
Die Aufgabe ist erfüllt, wenn wir die Mantisse (mit Vorzeichen) und den (binären)
Exponenten kennen. Um danach die Dezimalzahl korrekt auszugeben, ist auch
Gleitkommaarithmetik sowie printf() mit "%f" (oder besser: "%e") erlaubt.
#include <stdio.h>
#include <stdint.h>
#define B 127
int main (void)
{
float G = 6.6743e-11;
printf ("Fließkommazahl: %e\n", G);
uint32_t *g = (uint32_t *) &G;
printf ("Bitmuster: 0x%08x\n", *g);
int8_t sign = !!(*g & (1 << 31));
printf ("Vorzeichen: %d\n", sign);
uint8_t ch = ((*g & 0x7fffffff) >> 23);
printf ("Charakteristik: %d\n", ch);
int ex = ch - B;
printf ("Exponent, bezogen auf das zweitoberste Bit der Mantisse: %d\n", ex);
printf ("Exponent, bezogen auf das unterste Bit der Mantisse: %d\n", ex - 23);
int32_t mant = (*g & 0x7fffff) | 0x800000;
printf ("Mantisse: %d\n", mant);
if (sign)
mant *= -1;
printf ("Ergebnis: %d·2^%d\n", mant, ex - 23);
return 0;
}
#include <stdio.h>
#include <stdint.h>
#define B 127
int main (void)
{
float G = 42.0;
printf ("Fließkommazahl: %e\n", G);
uint32_t *g = (uint32_t *) &G;
printf ("Bitmuster: 0x%08x\n", *g);
int8_t sign = !!(*g & (1 << 31));
printf ("Vorzeichen: %d\n", sign);
uint8_t ch = ((*g & 0x7fffffff) >> 23);
printf ("Charakteristik: %d\n", ch);
int ex = ch - B;
printf ("Exponent, bezogen auf das zweitoberste Bit der Mantisse: %d\n", ex);
printf ("Exponent, bezogen auf das unterste Bit der Mantisse: %d\n", ex - 23);
int32_t mant = (*g & 0x7fffff) | 0x800000;
printf ("Mantisse: %d\n", mant);
if (sign)
mant *= -1;
printf ("Ergebnis: %d·2^%d\n", mant, ex - 23);
return 0;
}
#include <stdio.h>
#include <stdint.h>
#include <math.h>
#define B 127
int main (void)
{
float G = 6.6743e-11;
uint32_t *g = (uint32_t *) &G;
int8_t sign = !!(*g & (1 << 31));
uint8_t ch = (*g >> 23);
int ex = ch - B - 23;
int32_t mant = (*g & 0x7fffff) | 0x800000;
if (sign)
mant *= -1;
printf ("%d·2^%d\n", mant, ex);
printf ("= %e\n", mant * pow (2.0, ex));
return 0;
}
#include <stdio.h>
#include <stdint.h>
#include <math.h>
#define B 127
int main (void)
{
float G = 42.0;
uint32_t *g = (uint32_t *) &G;
int8_t sign = !!(*g & (1 << 31));
uint8_t ch = (*g >> 23);
int ex = ch - B - 23;
int32_t mant = (*g & 0x7fffff) | 0x800000;
if (sign)
mant *= -1;
printf ("%d·2^%d\n", mant, ex);
printf ("= %e\n", mant * pow (2.0, ex));
return 0;
}
#include <stdio.h>
void printBits (size_t const size, void const *const ptr)
{
unsigned char *b = (unsigned char *) ptr;
unsigned char byte;
int i, j;
int counter = 0;
printf ("\nV = ");
for (i = size - 1; i >= 0; i--)
{
for (j = 7; j >= 0; j--)
{
byte = (b[i] >> j) & 1;
printf ("%u", byte);
counter++;
if (counter == 1)
{
printf ("\nE = ");
}
else if (counter == 9)
{
printf ("\nM = ");
}
}
}
printf ("\n");
}
int main ()
{
int x = 42;
printf ("Bits von %d: ", x);
printBits (sizeof (x), &x);
return 0;
}
#include <stdio.h>
void printBits (size_t const size, void const *const ptr)
{
unsigned char *b = (unsigned char *) ptr;
unsigned char byte;
int i, j;
int counter = 0;
printf ("\nV = ");
for (i = size - 1; i >= 0; i--)
{
for (j = 7; j >= 0; j--)
{
byte = (b[i] >> j) & 1;
printf ("%u", byte);
counter++;
if (counter == 1)
{
printf ("\nE = ");
}
else if (counter == 9)
{
printf ("\nM = ");
}
}
}
printf ("\n");
}
int main ()
{
float x = 42;
printf ("Bits von %f: ", x);
printBits (sizeof (x), &x);
return 0;
}
#include <stdio.h>
float f (float x)
{
return x * x;
}
float df_a (float x)
{
return 2.0 * x;
}
float df_n (float x)
{
float dx = 0.0000001;
float df = f (x + 0.5 * dx) - f (x - 0.5 * dx);
return df / dx;
}
int main (void)
{
for (float x = -3.0; x <= 3.0; x += 0.1)
printf ("%10f%10f%10f%10f\n", x, f (x), df_a (x), df_n (x));
return 0;
}
-3.000000 -27.000000 27.000000 0.000000
-2.900000 -24.389004 25.230001 0.000000
-2.800000 -21.952005 23.520002 0.000000
-2.700000 -19.683006 21.870005 0.000000
-2.600000 -17.576008 20.280006 0.000000
-2.500000 -15.625009 18.750008 0.000000
-2.400001 -13.824010 17.280008 0.000000
-2.300001 -12.167011 15.870009 0.000000
-2.200001 -10.648011 14.520010 0.000000
-2.100001 -9.261011 13.230011 0.000000
-2.000001 -8.000011 12.000011 0.000000
-1.900001 -6.859010 10.830010 0.000000
-1.800001 -5.832009 9.720010 0.000000
-1.700001 -4.913008 8.670009 0.000000
-1.600001 -4.096007 7.680008 0.000000
-1.500001 -3.375006 6.750008 0.000000
-1.400001 -2.744005 5.880007 0.000000
-1.300001 -2.197004 5.070006 0.000000
-1.200001 -1.728003 4.320005 0.000000
-1.100001 -1.331003 3.630005 0.000000
-1.000001 -1.000002 3.000004 0.000000
-0.900001 -0.729002 2.430004 2.980232
-0.800001 -0.512001 1.920003 1.788139
-0.700001 -0.343001 1.470003 1.788139
-0.600001 -0.216001 1.080002 1.341105
-0.500001 -0.125000 0.750002 0.894070
-0.400001 -0.064000 0.480001 0.596046
-0.300001 -0.027000 0.270001 0.316650
-0.200001 -0.008000 0.120001 0.102445
-0.100001 -0.001000 0.030000 0.031432
-0.000001 -0.000000 0.000000 0.000000
0.099999 0.001000 0.030000 0.031432
0.199999 0.008000 0.119999 0.111759
0.299999 0.027000 0.269999 0.316650
0.399999 0.064000 0.479998 0.521541
0.499999 0.125000 0.749998 0.894070
0.599999 0.215999 1.079998 1.192093
0.699999 0.342999 1.469997 1.788139
0.799999 0.511999 1.919997 2.384186
0.899999 0.728999 2.429997 2.980232
0.999999 0.999998 2.999997 3.576279
1.099999 1.330998 3.629996 0.000000
1.199999 1.727998 4.319996 0.000000
1.299999 2.196997 5.069996 0.000000
1.399999 2.743997 5.879996 0.000000
1.500000 3.374997 6.749996 0.000000
1.600000 4.095996 7.679996 0.000000
1.700000 4.912996 8.669995 0.000000
1.800000 5.831996 9.719995 0.000000
1.900000 6.858996 10.829996 0.000000
2.000000 7.999996 11.999996 0.000000
2.100000 9.260995 13.229996 0.000000
2.200000 10.647994 14.519995 0.000000
2.299999 12.166991 15.869993 0.000000
2.399999 13.823989 17.279991 0.000000
2.499999 15.624988 18.749989 0.000000
2.599999 17.575983 20.279987 0.000000
2.699999 19.682980 21.869986 0.000000
2.799999 21.951977 23.519983 0.000000
2.899999 24.388971 25.229980 0.000000
2.999999 26.999969 26.999979 0.000000
plot "deriv-01.dat" using 1:2 with lines title "f(x)", \
"deriv-01.dat" using 1:3 with lines title "df_a(x)", \
"deriv-01.dat" using 1:4 with lines lc "red" dashtype "-" title "df_n(x)"
pause -1
#include <stdio.h>
float f (float x)
{
return x * x * x;
}
float df_a (float x)
{
return 3.0 * x * x;
}
float df_n (float x)
{
float dx = 0.0000001;
float df = f (x + 0.5 * dx) - f (x - 0.5 * dx);
return df / dx;
}
int main (void)
{
for (float x = -3.0; x <= 3.0; x += 0.1)
printf ("%10f%10f%10f%10f\n", x, f (x), df_a (x), df_n (x));
return 0;
}
#include <stdio.h>
float f (float x)
{
return x * x;
}
float df_a (float x)
{
return 2.0 * x;
}
float df_n (float x)
{
float dx = 0.1;
float df = f (x + 0.5 * dx) - f (x - 0.5 * dx);
return df / dx;
}
int main (void)
{
for (float x = -3.0; x <= 3.0; x += 0.1)
printf ("%11f %11f %11f %11f\n", x, f (x), df_a (x), df_n (x));
return 0;
}
-3.000000 -27.000000 27.000000 27.002468
-2.900000 -24.389004 25.230001 25.232487
-2.800000 -21.952005 23.520002 23.522472
-2.700000 -19.683006 21.870005 21.872482
-2.600000 -17.576008 20.280006 20.282497
-2.500000 -15.625009 18.750008 18.752489
-2.400001 -13.824010 17.280008 17.282486
-2.300001 -12.167011 15.870009 15.872488
-2.200001 -10.648011 14.520010 14.522505
-2.100001 -9.261011 13.230011 13.232498
-2.000001 -8.000011 12.000011 12.002497
-1.900001 -6.859010 10.830010 10.832500
-1.800001 -5.832009 9.720010 9.722505
-1.700001 -4.913008 8.670009 8.672500
-1.600001 -4.096007 7.680008 7.682502
-1.500001 -3.375006 6.750008 6.752501
-1.400001 -2.744005 5.880007 5.882499
-1.300001 -2.197004 5.070006 5.072502
-1.200001 -1.728003 4.320005 4.322501
-1.100001 -1.331003 3.630005 3.632501
-1.000001 -1.000002 3.000004 3.002503
-0.900001 -0.729002 2.430004 2.432504
-0.800001 -0.512001 1.920003 1.922504
-0.700001 -0.343001 1.470003 1.472503
-0.600001 -0.216001 1.080002 1.082503
-0.500001 -0.125000 0.750002 0.752502
-0.400001 -0.064000 0.480001 0.482502
-0.300001 -0.027000 0.270001 0.272501
-0.200001 -0.008000 0.120001 0.122501
-0.100001 -0.001000 0.030000 0.032500
-0.000001 -0.000000 0.000000 0.002500
0.099999 0.001000 0.030000 0.032500
0.199999 0.008000 0.119999 0.122499
0.299999 0.027000 0.269999 0.272499
0.399999 0.064000 0.479998 0.482499
0.499999 0.125000 0.749998 0.752498
0.599999 0.215999 1.079998 1.082498
0.699999 0.342999 1.469997 1.472498
0.799999 0.511999 1.919997 1.922498
0.899999 0.728999 2.429997 2.432498
0.999999 0.999998 2.999997 3.002497
1.099999 1.330998 3.629996 3.632492
1.199999 1.727998 4.319996 4.322493
1.299999 2.196997 5.069996 5.072492
1.399999 2.743997 5.879996 5.882490
1.500000 3.374997 6.749996 6.752491
1.600000 4.095996 7.679996 7.682493
1.700000 4.912996 8.669995 8.672490
1.800000 5.831996 9.719995 9.722486
1.900000 6.858996 10.829996 10.832486
2.000000 7.999996 11.999996 12.002501
2.100000 9.260995 13.229996 13.232479
2.200000 10.647994 14.519995 14.522486
2.299999 12.166991 15.869993 15.872469
2.399999 13.823989 17.279991 17.282476
2.499999 15.624988 18.749989 18.752470
2.599999 17.575983 20.279987 20.282459
2.699999 19.682980 21.869986 21.872482
2.799999 21.951977 23.519983 23.522453
2.899999 24.388971 25.229980 25.232468
2.999999 26.999969 26.999979 27.002430
plot "deriv-02.dat" using 1:2 with lines title "f(x)", \
"deriv-02.dat" using 1:3 with lines title "df_a(x)", \
"deriv-02.dat" using 1:4 with lines lc "red" dashtype "-" title "df_n(x)"
pause -1
#include <stdio.h>
float f (float x)
{
return x * x * x;
}
float df_a (float x)
{
return 3.0 * x * x;
}
float df_n (float x)
{
float dx = 0.1;
float df = f (x + 0.5 * dx) - f (x - 0.5 * dx);
return df / dx;
}
int main (void)
{
for (float x = -3.0; x <= 3.0; x += 0.1)
printf ("%11f %11f %11f %11f\n", x, f (x), df_a (x), df_n (x));
return 0;
}
#include <stdio.h>
#include <math.h>
/* 1/1² + 1/2² + 1/3² + ... = pi²/6 */
int main (void)
{
float pi = M_PI;
printf ("%0.9f\n", pi * pi / 6.0);
float S = 0.0;
for (float x = 1; x <= 100; x++)
S += 1.0 / (x * x);
printf ("%0.9f\n", S);
return 0;
}
#include <stdio.h>
#include <math.h>
/* 1/1² + 1/2² + 1/3² + ... = pi²/6 */
int main (void)
{
float pi = M_PI;
printf ("%0.9f\n", pi * pi / 6.0);
float S = 0.0;
for (float x = 1; x <= 1000000; x++)
S += 1.0 / (x * x);
printf ("%0.9f\n", S);
return 0;
}
#include <stdio.h>
#include <math.h>
/* 1/1² + 1/2² + 1/3² + ... = pi²/6 */
int main (void)
{
float pi = M_PI;
printf ("%0.9f\n", pi * pi / 6.0);
float S = 0.0;
for (float x = 1; x <= 10000000; x++) /* ca. 1/20 s Rechenzeit */
S += 1.0 / (x * x);
printf ("%0.9f\n", S);
return 0;
}
#include <stdio.h>
#include <math.h>
/* 1/1² + 1/2² + 1/3² + ... = pi²/6 */
int main (void)
{
float pi = M_PI;
printf ("%0.9f\n", pi * pi / 6.0);
float S = 0.0;
for (float x = 1; x <= 10000000; x++)
S += 1.0 / (x * x);
printf ("%0.9f\n", S);
S = 0.0;
for (float x = 10000000; x > 0; x--)
S += 1.0 / (x * x);
printf ("%0.9f\n", S);
return 0;
}
#include <stdio.h>
#include <math.h>
/* 1/1² + 1/2² + 1/3² + ... = pi²/6 */
int main (void)
{
float pi = M_PI;
printf ("%0.9f\n", pi * pi / 6.0);
float S = 0.0;
for (float x = 1; x <= 1000000; x++)
S += 1.0 / (x * x);
printf ("%0.9f\n", S);
S = 0.0;
for (float x = 1000000; x > 0; x--)
S += 1.0 / (x * x);
printf ("%0.9f\n", S);
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment