20 Ağustos 2020 Perşembe

/etc/sysctl.conf dosyası - Kernel Ayarları Dosyası

IPv4 Alanları
net.ipv4.ip_unprivileged_port_start
Örnek
Şöyle yaparız. Burada dosya ismi sysctl.conf değil ama yine de örneği not almak istedim.
echo "net.ipv4.ip_unprivileged_port_start=100" > /etc/sysctl.d/privileged_ports.conf
net.ipv4.conf.all.forwarding Alanı
Örnek
Trafiği bir proxy'e yönlendirmek için şöyle yaparız
echo "net.ipv4.conf.all.forwarding = 1" >> /etc/sysctl.conf
sysctl -p
firewall-cmd --permanent --add-masquerade
firewall-cmd --permanent --add-forward-port=port=80:proto=tcp:toport=80:toaddr=TARGET_IP
firewall-cmd --permanent --add-forward-port=port=443:proto=tcp:toport=443:toaddr=TARGET_IP
firewall-cmd --reload
net.ipv4.ping_group_range Alanı
Fedora üzerinde herkesin pin yapabilmesini sağlar. Açıklaması şöyle
Enable the Linux kernel's net.ipv4.ping_group_range parameter to cover all groups. This will let all users on the operating system create ICMP Echo sockets without using setuid binaries, or having the CAP_NET_ADMIN and CAP_NET_RAW file capabilities.
Örnek
Bu alanın değerini görmek için şöyle yaparız
sysctl net.ipv4.ping_group_range

net.ipv4.ping_group_range = 0   2147483647
IPv6 Alanları
IPv6 işlevini kapatmak için şöyle yaparız
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
vm.swappiness Alanı
Linus Torvalds swappiness özelliğini 1992'de eklemiş. Açıklaması şöyle.
I remember that, in December, there was this guy in Germany who only had 2 megabytes of RAM, and he was trying to compile the kernel and he couldn't run GCC because GCC at the time needed more than a megabyte. He asked me if Linux could be compiled with a smaller compiler that wouldn't need as much memory. So I decided that even though I didn't need the particular feature, I would make it happen for him. It's called page-to-disk, and it means that even though someone only has 2 mgs of RAM, he can make it appear to be more using the disk for memory. This was around Christmas 1991.
Böylece mevcut RAM'dan daha fazla belleğe ihtiyaç duyan uygulamalar bile çalıştırabiliyor.

Bu özelliğe paging veya swapping deniliyor. Ancak bence paging daha doğru bir kelime. Çünkü swapping eskiden biraz daha farklı bir anlamına geliyordu. Açıklaması şöyle.
Swapping is a concept predating virtual memory and even memory protection: it just means putting a process on disk to make room for another. The original Unix had two quirks in that regard: "shared text" programs that kept the program code only once in memory and swapped out the data section only. And it had the "fork" system call that swapped out a process to disk while not replacing the memory image and instead keeping a copy (the child) running.
Pagin ise klasik anlamda belleğin sayfalar şeklinde kullanılması anlama geliyor. Açıklaması şöyle.
Page-to-disk, as opposed to swapping, allows for processes to run that do not fit the physical memory. It requires all of protectable memory, memory mapping of virtual addresses to physical addresses, and a restartable page fault mechanism that will allow to change the mapping from an unmapped virtual address to a reasonably selectable physical address and resuming the command that had to be aborted because of the missing mapping.
İlk UNIX'ler MMU olmadan bile bir şekilde pagin yapabiliyomuş.  Açıklaması şöyle.
UNIX was able to run on 68000 processors (including swapping) without MMU, and it made good use of an MMU if available for memory protection, but it took the 68010 to actually have the mechanisms allowing for resuming a program after a page fault.
Bu özellik ile Linux işletim sistemi Minıx'in bir adım önüne geçmiş. Açıklaması şöyle
Page-to-disk was a fairly big thing because it was something Minix had never done. It was included in version 0.12, which was released in the first week of January 1992. Immediately, people started to compare Linux not only to Minix but to Coherent, which was a small Unix clone developed by Mark Williams Company. From the beginning, the act of adding page-to-disk caused Linux to rise above the competition.
That's when Linux took off. Suddenly there were people switching from Minix to Linux.
Aslında bu özellik UNIX'te eskiden beri vardı. Açıklaması şöyle
To be clear, swapping wasn't an innovative feature: most “serious” Unix systems had it, and the feature is older than Unix. What swapping did for Linux was to turn it into a “serious” Unix, whereas MINIX was meant for educational purposes.

Swapping today is still the same concept. The heuristics for deciding which pages to save and when to save them have become a lot more complex, but the basic principle remains.
Açıklaması şöyle.
Even if there is still available RAM, the Linux Kernel will move memory pages which are hardly ever used into swap space.

It’s good to swap out memory pages that have been inactive for a while, keeping often-used data in cache; this is the desired situation of the Kernel.

You can have more control on this by using vfs_cache_pressure (which controls the tendency of the kernel to reclaim the memory which is used for caching of directory and inode objects) and swappiness (which controls how aggressive the kernel will swap memory pages: higher values will increase aggressiveness, lower values decrease the amount of swap).

You can find these settings in /etc/sysctl.conf.
Bir başka açıklama şöyle. Swapiness'ı azaltmak istersek bu değeri 1 yapmak gerekir.
This will determine the tendency of the kernel to swap out memory pages. In may cases, you will want to set this to "1" to keep the swapping to a minimum. A value of "0" will disable it entirely.
Bu değerin şimdiki halini görmek için şöyle yaparız
cat /proc/sys/vm/swappiness
Örnek
Şöyle yaparız.
vm.swappiness=80

19 Ağustos 2020 Çarşamba

pthread_mutex_unlock metodu

Giriş
Mutex üzerinde bekleyen hangi thread'in uyandıralacağı tanımlı değil. Açıklaması şöyle
If there are threads blocked on the mutex object referenced by mutex when pthread_mutex_unlock() is called, resulting in the mutex becoming available, the scheduling policy shall determine which thread shall acquire the mutex.
Örnek
Şöyle yaparız.
pthread_mutex_unlock (&mutex);

18 Ağustos 2020 Salı

bash kodlama - read built-in komutu

Giriş
Açıklaması şöyle.
Read a line from the standard input and split it into fields.
Tek bir satır okur. Eğer birden fazla değişken verilirse satırı kelimelere böler.

Built-in olduğunu görmek için şöyle yaparız.
$ type read
read is a shell builtin
bash ve zsh Farkı
Açıklaması şöyle
BTW, there are lots more differences between read in bash vs zsh (and vs other shells). The POSIX standard only specifies the -r option; everything else is a nonstandard addition, and any similarity between different shells' extensions should be considered a happy accident.
Exit Status
Açıklaması şöyle. EOF olunca 0'dan farklı bir sonuç döner. Böylece while gibi döngüde kullanılabilir.
The exit status is zero, unless end-of-file is encountered
Örnek - while
Şöyle yaparız
while IFS= read -r line; do
  ...
done < text-file
Kullanım
Örnek
Değişkenin başına $ karakteri konulmaz. Şu kod yanlış.
read $input
Örnek
Şöyle yaparız.
echo "Are you there?"  
read input  

if [ $input == yes ]; then  
    echo "Hello!"  
elif [ $input == no ]]; then  
    echo "Are you sure?"  
else  
    echo "Please answer yes or no."  
fi  
Örnek
Dosyada satır satır okumak için şöyle yaparız.
f=myfile.dat

while read line; do
  echo $line
  ...
done < $f > newfile.dat
-a seçeneği
Şöyle yaparız.
IFS=, read -a fields <<< "1,2,3,4,5"
for i in "${fields[@]}"; do echo "$i"; done
-n seçeneği
Okunacak karakter sayısını belirtir
Örnek
Şöyle yaparız
echo "characters" | while IFS= read -d '' -n 1 a; do printf %s "$a-"; done
Çıktı olarak şunu alırız
c-h-a-r-a-c-t-e-r-s-
-p seçeneği
prompt anlamına gelir.
Örnek
Şöyle yaparız.
#!/bin/bash
read -p "Please enter a filename:" filename
-r seçeneği
Açıklaması şöyle.
-r    do not allow backslashes to escape any characters
Açıklaması şöyle
It's very rare that you would want to use read without -r.
Örnek
Dosyadaki her satır şöyle olsun.
file1.fastq.gz file2.fastq.gz foo
Şöyle yaparız.
while read -r file1 file2 trash; do
  something with "$file1" and "$file2"
done < /path/to/input_file
-s seçeneği
silent anlamına gelir. Açıklaması şöyle. Bu seçenek yerine "stty -echo" da kullanılabilir.
-s    do not echo input coming from a terminal
Örnek 
Şöyle yaparız.
read -p 'Password? ' -s password
echo Your password is "$password".


17 Ağustos 2020 Pazartesi

bash brace expansion - Girdiye Süslü Parantez İçindeki Listeyi Ekleyerek Çoklar

Giriş
Açıklaması şöyle. Yani #!/bin/sh şeklinde shebang kullanan kabuklarda çalışmaz.
Brace expansion is not standardized by POSIX. Some, but not all, of the widely used Bourne-style shells support it.
Örnek - Tek Girdi
Brace yani süslü parantez içinde iki tane girdi kullanmak gerekir. Bu girdiler değişken veya sabit olabilir. Elimizde şöyle bir kod olsun. Burada a ve b için açılım yapılıyor
$ echo --hidden-import={a,b}
--hidden-import=a --hidden-import=b
Ancak burada tek girdi olduğu için brace expansion yapılmıyor
echo --hidden-import={$FILES}
--hidden-import={cpu_mon disk_mon mem_mon network_mon}
Açıklaması şöyle
If you have {$x,$y} the shell first processes it into two arguments $x and $y, and only then expands the variables within each argument separately. And if you have only one item, brace expansion doesn't happen at all; {xy} always remains just {xy}.

Örnek
Şöyle yaparız
for i in {2..5}; do cp -r /home/test/DS1 "DS$i"; done
Örnek
Şöyle yaparız.
echo test{1,2}
test1  test2
Örnek
Şöyle yaparız
printf '%s\n' {0..4..2}" "{0..2..2}
Çıktı olarak şunu alırız
0 0
0 2
2 0
2 2
4 0
4 2