As a system programming language, C still deserves learning today

C is a system programming language which is old but with “bad” fame: undefined behavior, notorious memory related bugs, etc. Especially with Go and Rust having gone viral right now, C seems already forgotten by people. Nevertheless, IMHO, C is still a thing which is worth for you spending some time on it.

Whether you are are a C novice or a C veteran,I highly recommend you read Modern C if you haven’t read it before. Then you will find C also evolves stealthily and is not as primitive as you think. E.g., C11 has defined standard thread APIs like C++ has done, which makes C more like a “modern” language, not an outdated thing. You may get a new perspective of C from this book.

Regardless if you are a systems language programmer, DevOps, performance engineer or wear other hats, the more you know about the Operating System, the more you can do your job better. Take all prevailing Unix-like Operating Systems as an example, from kernel to command line tools, they are almost implemented in C. To study related source code can make you delve into Operating System internal deeper. E.g., I knew there is a taskset command which can bind specified process of a dedicated CPU, but I wanted to know the magic behind it, so I went through its code. Then I learned 2 things:
a) There is a “/proc/%pid/task” folder which records thread information of process;
b) taskset actually calls sched_setaffinity and sched_getaffinity APIs to attain its goal.

Last but not least, because C is so “low-level”, you can leverage it to write highly performant code to squeeze out CPU when performance is critical in some scenarios.

Although I have listed many reasons which I think you should learn C, you may have sufficient excuses to refuse to dabble into C too. It doesn’t matter. But if you want to give C a shot, firstly you should not be scared of it. There are many ways of writing wrong C code, but you only need to make sure what you write is correct and in defined behavior, that’s all about programming.

First taste of C thread APIs

Since C11, C provides standard thread APIs like what C++ does. It means technically, you should use C‘s standard thread APIs to do multi-thread stuff, not pthread APIs. Below is a simple example:

#include <threads.h>
#include <stdio.h>

int print_thread(void* s)
{
    printf("%s\n", (char*)s);
    thrd_exit(0);
}
int main()
{
    thrd_t tid;
    if (thrd_success != thrd_create(&tid, print_thread, "Hello world"))
    {
        fprintf(stderr, "Create thread error\n");
        return 1;
    }
    thrd_join(tid, NULL);
    return 0;
}

Check thrd_create implementation in glibc:

#include "thrd_priv.h"

int
thrd_create (thrd_t *thr, thrd_start_t func, void *arg)
{
  _Static_assert (sizeof (thr) == sizeof (pthread_t),
          "sizeof (thr) != sizeof (pthread_t)");

  int err_code = __pthread_create_2_1 (thr, ATTR_C11_THREAD,
                       (void* (*) (void*))func, arg);
  return thrd_err_map (err_code);
}

You can see thrd_create just encapsulates __pthread_create_2_1, so you can guess in glibc, the standard C thread APIs are just wrappers of pthreadimplementation. It means you need to link pthread library during compiling. Otherwise you will meet following errors:

main.c:(.text+0x1e): undefined reference to `thrd_exit'
......
main.c:(.text+0x4f): undefined reference to `thrd_create'
/usr/bin/ld: main.c:(.text+0x60): undefined reference to `thrd_join'

P.S., the full code is here.