Today my colleague fixed one bug related to out-of-boundary access of array: a hash function returns the selected index of the array, but the hash function’s return value is int
, so in corner case, when the hash value is overflow, it can become negative, and this will cause access an invalid element of the array. The lessons I learnt from this bug:
(1) Review the return value of hash function;
(2) Pay attention to the index when accessing array, is it possible to cause out-of-boundary access?
Author: nanxiao
How to obtain a big-endian CPU machine
Last week, I wanted to test whether a trivial function works OK on big-endian CPU. I have ARM
and X86_64
machines at hand, but both them are little-endian. After searching online, I come across Running a emulated SparcStation 20 with qemu-sparc, though I heard about qemu
before, but never used it, so wanted to give it a spin.
The installation of qemu
is straightforward, then I created a NetBSD-10.1-sparc
machine in just 3
steps (omit some configurations unneeded for me):
$ qemu-img create -f qcow2 ss20.image 4G
$ qemu-system-sparc -M SS-20 -m 256 -drive file=NetBSD-10.1-sparc.iso,bus=0,unit=2,media=cdrom,readonly=on -drive file=ss20.image,bus=0,unit=0,media=disk -full-screen -boot d
$ qemu-system-sparc -M SS-20 -m 256 -drive file=ss20.image,bus=0,unit=0,media=disk -full-screen -boot c
Then the machine booted successfully and met my requirement perfectly!
My telegram channel
I created a telegram channel: https://t.me/anoldprogrammer, and want to share following things:
(1) My experience as a software engineer;
(2) The tutorials, notes and tips about system programming, performance tuning and software engineering;
(3) Others, all related to software.
In summary, the channel will be a superset of my personal blog, and welcome to subscribe!
What is it like in my working day?
In the morning of every working day, the first thing I will do is synchronizing all the code repositories, and check the modifications committed by colleagues from other timezones when I was sleep, especially pay attention to the changes relevant to my work. At the same time, I also need to process the unread mails and slack messages.
After that, I will begin my daily work, mostly 3
kinds of tasks:
a) Develop new features;
b) Fix bugs;
c) Review other people’s PRs (Pull Requests). The reason that I list reviewing PRs separately is I find this task really costs significant time and energy since you need to understand how other people thought when coding.
Definitely, sometimes there will be an interrupt, e.g., someone asks me one question, and I need to pause current work and answer it. This is inevitable in a cooperated environment.
The above is my typical daily routine, and that’s it.
Test multi-thread program on one CPU
Today, I tested a multi-thread program on one CPU. The testbed is a FreeBSD
virtual machine, and from lscpu command, it has indeed one CPU:
$ lscpu
Architecture: aarch64
Byte Order: Little Endian
Total CPU(s): 1
Model name: Apple Unknown CPU r0p0 (midr: 610f0000)
The multi-thread program is simple too, just 4
threads add one global variable, and the correct result should be 400000
in every run. If the result is not 400000
, exit the program:
#include <pthread.h>
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include <string.h>
#define THREAD_NUM 4
#define SUM_LOOP_SIZE 100000
uint64_t sum;
void *
thread(void *arg)
{
for (int i = 0; i < SUM_LOOP_SIZE; i++) {
sum++;
}
return NULL;
}
int
main()
{
pthread_t tid[THREAD_NUM];
uint64_t counter = 0;
while (1) {
counter++;
for (int i = 0; i < THREAD_NUM; i++) {
int ret = pthread_create(&tid[i], NULL, thread, NULL);
if (ret != 0) {
fprintf(stderr, "Create thread error: %s", strerror(ret));
return 1;
}
}
for (int i = 0; i < THREAD_NUM; i++) {
int ret = pthread_join(tid[i], NULL);
if (ret != 0) {
fprintf(stderr, "Join thread error: %s", strerror(ret));
return 1;
}
}
if (sum != THREAD_NUM * SUM_LOOP_SIZE) {
fprintf(stderr, "Exit after running %" PRIu64 " times, sum=%" PRIu64 "\n", counter, sum);
return 1;
}
sum = 0;
}
return 0;
}
Built and run the program:
$ ./multi_thread_one_cpu
Exit after running 17273076 times, sum=200000
$ ./multi_thread_one_cpu
Exit after running 1539708 times, sum=100000
Change “uint64_t sum;
” to “volatile uint64_t sum;
“, compile and run again:
$ ./multi_thread_one_cpu
Exit after running 20 times, sum=200000
$ ./multi_thread_one_cpu
Exit after running 50 times, sum=200000
Exit much faster.
In summary, when there are multiple threads access same variable, always use lock. P.S., the code can be found here.