Change to a lower version of the ubuntu kernel

When changing the kernel version in ubuntu, proceed in the following order.

Install the kernel to change

sudo apt install linux-image-5.15.0-70-generic

change grub order

Change GRUB_DEFAULT="1>4" in /etc/default/grub file
The meaning of "1>4" is to select 4 (5th item) after moving 1 (advanced, second) in the grub menu.
The value can change depending on the kernel installed situation. (/boot/grub/grub.cfg)

Apply grub changes

sudo update-grub

If the driver does not operate normally after booting, install the following two packages additionally

If already installed, reinstall with reinstall instead of install

sudo apt install linux-headers-5.15.0-70-generic
sudo apt install linux-modules-extra-5.15.0-70-generic

Checking when lock and unlock are called in std::lock_guard and std::unique_lock

std::lock_guard calls lock at creation time and unlock at destruction time (end of block).


std::unique_lock calls lock at creation time and unlock at destruction time (end of block).

In addition, lock and unlock can be called (maintaining the state value for lock).


#include <iostream>
#include <string>
#include <mutex>
 
class TestMutex {
    public:
        void lock() {
            std::cout << "call lock" << std::endl;
        }
        void unlock() {
            std::cout << "call unlock" << std::endl;
        }
};
 
TestMutex mutex1;
 
int main() {
    std::cout << "###### lock_guard start" << std::endl;
    {
        std::lock_guard lockGuard(mutex1);
        std::cout << "lock_guard created" << std::endl;
    }
    std::cout << "###### lock_guard end" << std::endl << std::endl;
 
    std::cout << "###### unique_lock start" << std::endl;
    {
        std::unique_lock uniqueLock(mutex1);
        std::cout << "unique_lock created" << std::endl;
    }
    std::cout << "###### unique_lock end" << std::endl << std::endl;
 
    std::cout << "###### unique_lock start 2" << std::endl;
    {
        std::unique_lock uniqueLock(mutex1);
        std::cout << "unique_lock created" << std::endl;
        std::cout << "manual call unlock ++" << std::endl;
        uniqueLock.unlock();
        std::cout << "manual call unlock --" << std::endl;
    }
    std::cout << "###### unique_lock end 2" << std::endl << std::endl;
 
    std::cout << "###### unique_lock start 3" << std::endl;
    {
        std::unique_lock uniqueLock(mutex1);
        std::cout << "unique_lock created" << std::endl;
        std::cout << "manual call unlock ++" << std::endl;
        uniqueLock.unlock();
        std::cout << "manual call unlock --" << std::endl;
        std::cout << "manual call lock ++" << std::endl;
        uniqueLock.lock();
        std::cout << "manual call lock --" << std::endl;
    }
    std::cout << "###### unique_lock end 3" << std::endl << std::endl;
 
 
    return 0;
}

Execution result


$ ./a.out
###### lock_guard start
call lock
lock_guard created
call unlock
###### lock_guard end

###### unique_lock start
call lock
unique_lock created
call unlock
###### unique_lock end

###### unique_lock start 2
call lock
unique_lock created
manual call unlock ++
call unlock
manual call unlock --
###### unique_lock end 2

###### unique_lock start 3
call lock
unique_lock created
manual call unlock ++
call unlock
manual call unlock --
manual call lock ++
call lock
manual call lock --
call unlock
###### unique_lock end 3

grep previous line next line


grep -B 1 TEXT file      // previous line
grep -A 1 TEXT file      // next line
grep -a 1 TEXT file      // previous and next lines

When programming is hard

There are no coding rules.
It's a code that many people see, but there is no response 
even if you shout coding rules, and the code is getting harder and harder to see.
The result is that I am the only one who is sensitive.

Copy the repeated logic.
The code will have the same logic A, A', A'', A'''. 
You can fix the problem quickly, but it's hard later.
Later, there was a problem with A, so I fixed it, and after a while, I am fixing A'... After a while...
Let's subtract with a function what we think is repeated.

Programming languages also change.
C++ 20 years ago is different from C++ now.
Most, but not all, newer versions will find an easier way.
When you ctrl-c ctrl-v on old code, see if it can be improved.

Refactoring
"It's working fine. Leave it as it is."
If I change it, it's my responsibility... I can't touch it

install ubuntu 16.04 python 3.6

Run below command


sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.6
python3 --version
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.5 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 2
sudo update-alternatives --config python3

Common mistakes when using buffer sizing functions such as strncpy, snprintf, and strncmp

strncpy, snprintf : specify buffer size as source string size => must specify destination buffer size


There are many cases where the size of src and dst in the same function are the same, so there are few cases where the problem occurs

However, a problem occurs when receiving src as a function argument.


char* src = "012345678";
char dst[5] = { 0, };

strncpy(dst, src, strlen(src)); // buffer overflow
snprintf(dst, strlen(src), "%s", src); // buffer overflow

strncmp: String comparison is used for purposes that do not exceed the buffer size, such as the above function, resulting in unintended results.


When specifying the number, it is used only when only the first n characters of the string need to be compared, and strcmp is used to compare the entire string.


char* str1 = "";
char* str2 = "abcd";

strncmp(str1, str2, strlen(str1)); // return 0 : equal, no character comparison as n value is 0
strncmp(str1, str2, strlen(str2)); // return non-zero value: different

char* str3 = "ab";
char* str4 = "abcd";

strncmp(str3, str4, strlen(str3)); // return 0 : equal, compare the first 2 characters
strncmp(str3, str4, strlen(str4)); // return non-zero value: different

Using strcpy, strncpy, sprintf, and snprintf safely

strcpy: The length of src must be less than the length of dst.


char *src = "AAA";
char dst[10] = { 0, };
if (strlen(src) < sizeof(dst) {
	strcpy(dst, src);
}

strncpy: If the length of src is greater than or equal to dst, the '\0' at the end of dst disappears, so dst length - 1 should be set as n.


char *src = "AAA";
char dst[10] = { 0, };
strncpy(dst, src, sizeof(dst) - 1);

sprintf : The format string must not exceed dst.

snprintf : Set the length value keeping in mind that '\0' is entered at the end of the length n.


char *src = "aaaaa"
int src2 = 10000

char dst[10] = { 0, };

snprintf(dst, sizeof(dst), "%s%d", src, src2); // "aaaaa1000" The last character is truncated.

Values used by crontab

cron is a command provided in Linux to periodically schedule specific tasks. Set up tasks through contab. editor settings When executin...