Access thread information on Dragonfly BSD

On Dragonfly BSD, even it already provides pseudo proc file system, you can’t access thread information like Linux (/proc/$pid/task). You still need to use kvm:

......
kvm_t* kd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
......
struct kinfo_proc *proc = kvm_getprocs(
                                kd,
                                KERN_PROC_PID | KERN_PROC_FLAG_LWP,
                                (int)(pid),
                                &cnt);
......
kvm_close(kd); 

According to man:

The number of processes found is returned in the reference parameter cnt. The processes are returned as a contiguous array of kinfo_proc structures.

There is kp_lwp member in kinfo_proc structure which records information of this thread, i.e., LWP. E.g., To print every thread’s ID:

......
printf("Process (%s) has %d threads, and LWP IDs are:\n", argv[1], cnt);
for (int i = 0; i < cnt; i++)
{
    printf("%d\n",  proc[i].kp_lwp.kl_tid);
}
......

For other BSDs, the principle may be similar.

P.S., the full code is here.

Explore Dragonfly BSD thread model

From Dragonfly BSD‘s official document:

A user process contains one or more LWP (Light Weight Process) entities. Each entity represents a user thread under that process.

I want to explore the thread model myself. So I write a simple program which launches 2 threads in main function, and print process ID, LWP ID and thread id from C++‘s get_id function:

void output_thread_id(const std::string& prefix)
{
    std::stringstream ss;
    ss <<"Process ID is " << getpid() <<
        ", lwp ID is " << lwp_gettid() <<
        ", " << prefix << std::this_thread::get_id() <<'\n';
    std::cout << ss.str();
}

Build and run it:

# ./spawn_threads
Process ID is 16394, lwp ID is 1, main thread ID is 0x8007d00c0
Process ID is 16394, lwp ID is 2, sub thread ID is 0x8007d0240
Process ID is 16394, lwp ID is 3, sub thread ID is 0x8007d03c0

All 3 threads have same process ID, but LWP ID begins with 1, and increases contiguously (this is not same as Linux). By default, top command will only show processes:

Press ‘H‘ can display threads’ information (For ps command, -H option can be used to show threads):

P.S., the full code is here.

Reference:
How to get the thread number and every thread’s ID of a running process? .

 

Allow root to login Dragonfly BSD through SSH

To allow root to login Dragonfly BSD through SSH, you need to modify two parts in /etc/ssh/sshd_config:

......
PermitRootLogin yes
......
PasswordAuthentication yes
......

Otherwise you will bump into following error:

$ ssh root@192.168.35.195
The authenticity of host '192.168.35.195 (192.168.35.195)' can't be established.
......
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.35.195' (ECDSA) to the list of known hosts.
root@192.168.35.195: Permission denied (publickey,keyboard-interactive).

 

First taste of DragonFly BSD

Last week, I needed to pick a BSD Operating System which supports NUMA to do some testing, so I decided to give Dragonfly BSD a shot. Dragonfly BSDonly can run on X86_64 architecture, which reminds me of Arch Linux, and after some tweaking, I feel Dragonfly BSD may be a “developer-friendly” Operating System, at least for me.

I mainly use Dragonfly BSD as a server, so I don’t care whether GUI is fancy or not. But I have high requirements of developer tools, i.e., compiler and debugger. The default compiler of Dragonfly BSD is gcc 8.3, and I can also install clang 8.0.0 from package. This means I can test state-of-the-art features of compilers, and it is really important for me. gdb‘s version is 7.6.1, a little lag behind, but still OK.

Furthermore, the upgradation of Dragonfly BSD is pretty simple and straightforward. I followed document to upgrade my Operating System to 5.6.0 this morning, just copied and pasted, no single error, booted successfully.

Last but not least, there are many out-of-box packages which I can explore in the future.

It sounds interesting, right? Why not try it yourself?

Install DragonFly BSD on VirtualBox

Today I tried to install DragonFly BSD on VirualBox, and the whole progress was pretty smooth, but some caveats need to be noticed:

(1) Select FreeBSD(64-bit) as the Operating System;

(2) I use the simplest “Legacy BIOS“.

If you want to make your feet wet in DragonFly BSD, you can follow above steps yourself. Have fun!