How to make thread in C without using POSIX library <pthread.h>

11,933

Solution 1

See:

for UNIX like systems.

Also see:

for BSDs and modern UNIXes.

This page gives many examples of barebone implementations using these primitives and more.

You can use atomic instructions to implement the locking primitives (mutex, semaphores).

I also suggest looking at actual implementations of userland thread libraries to get some hints. See this page which gives a list of implementations for Linux.

Finally, you might want to get some information on coroutines and perhaps trampolines, although the later isn't as closely related.

Solution 2

A thread in Linux is essentially a process that shares memory and resources with its parent. The Linux Kernel does not distinguish between a process and a thread, in other words, there's no concept of a light weight process in Linux like in some other operating systems. Threads in Linux are implemented as standard processes, so it's possible to create a thread using just clone() which is normally called by fork() in the following way:

clone(SIGCHLD, 0);

This clones the signal handlers only, however, with the appropriate flags you can create a thread:

clone(CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND, 0);

This is identical to the previous call, except that the address space, filesystem resources, file descriptors and signal handlers are shared by the two processes.

A different approach is to use user-level threads (also called fibres) those are threads of execution implemented at the user-level, which means the OS is unaware of those threads and the scheduling or context switching has to be done at the user-level. Most user-level schedulers are implemented as cooperative schedulers, but it is also possible to implement a preemptive scheduler with a simple round robin scheduling.

Check the clone(2) man page for details and if you want more information I recommend the Linux Kernel Development 3rd edition By Robert Love, (not affiliated with the author in any way) there's a look inside link there you could read some of it online. As for the user-level threads, there's a minimal package written by me, called libutask, that implements both a cooperative and a preemptive scheduler, you can check the source code if you like.

Note1: I have not mentioned UNIX, as far as I know, this is Linux-specific implementation.

Note2: Creating your own threads with clone is not a real world solution, read the comments for the some problems you may have to deal with, it's only an answer to the question is it possible to create threads without using pthreads, in this case the answer is yes.

Solution 3

You can also check out the new <threads.h> header from the C standard library. (C11)

It's got what you need like int thrd_create(thrd_t *thr, thrd_start_t func, void *arg); as well as mutex functions and condition variables.

Solution 4

One can most certainly make at least a co-operative microkernel with plain c on top of pretty much any operating system. Fundamentally it only requires cloning of the stack frame (and adjusting a few pointers accordingly -- especially the return address from a function to the other threads current return address). And a few utility functions, such as "context switch" the stack to heap and back.

If a timer interrupt with a callback is allowed, one can do a pre-emptive microkernel.

At least Dr Dobbs and IOCCC have presented options along these lines.

Share:
11,933
Rahul Sharma
Author by

Rahul Sharma

.

Updated on June 12, 2022

Comments

  • Rahul Sharma
    Rahul Sharma almost 2 years

    I want to implement the multiple threading in C without using any of the POSIX library. Any help would be appreciated.

    Not : Don't use fork() or vfork().