Building an F# Control Plane with a C Data Plane on Linux

Search for a command to run...

No comments yet. Be the first to comment.
Introduction Engineering software often combines multiple programming languages to leverage their individual strengths. A common approach is to implement computational algorithms in native C or C++ wh

Deadlocks are one of the most common synchronization problems encountered in operating systems and concurrent programming. Although the concept is frequently introduced in textbooks, observing it insi

Memory and resource allocation are fundamental operations inside the Linux kernel. Whether assigning device IDs, managing CPU masks, allocating interrupt vectors, or tracking hardware resources, the k

The Linux scheduler is one of the most important components of the operating system. Every running program, background service, and kernel thread eventually interacts with the scheduler. In this artic

Modern software systems often separate control logic from high-performance execution logic. This design is common in networking, distributed systems, operating systems, storage engines, and embedded software.
In this project, we build a simple Linux application that demonstrates this architectural pattern using:
Although the example is intentionally simple, the same design scales to much larger systems.
Different programming languages excel at different tasks.
| Control Plane | Data Plane |
|---|---|
| User interaction | High-performance computation |
| Input validation | Native execution |
| Business logic | Memory-efficient processing |
| Workflow orchestration | Optimized algorithms |
| F# | C |
In this project:
This demonstrates a clean separation of responsibilities.
The project consists of two independent components.
Responsibilities:
Technology:
Responsibilities:
Technology:
terrain_grid/
│
├── control_plane/
│ ├── Program.fs
│ ├── Terrain.fs
│ └── control_plane.fsproj
│
├── data_plane/
│ ├── terrain.c
│ ├── terrain.h
│ └── libterrain.so
│
└── README.md
The control plane is responsible for gathering input and forwarding it to the native library.
A simplified version:
let terrain = Array.zeroCreate<double>(rows * cols)
Terrain.display_terrain(
terrain,
rows,
cols)
Notice that the F# code never performs the display itself.
Its only responsibility is orchestration.
The bridge between managed and native code is created with DllImport.
[<DllImport("libterrain.so",
CallingConvention = CallingConvention.Cdecl)>]
extern void display_terrain(
double[] terrain,
int rows,
int cols)
P/Invoke automatically marshals the managed array into a native pointer that the C library can consume.
The C library receives a pointer to the terrain array and prints the values.
void display_terrain(
const double* terrain,
int rows,
int cols)
{
printf("%8.2f",
terrain[i * cols + j]);
}
Because the array is stored contiguously, indexing is straightforward.
Although the terrain logically represents a matrix, it is stored as a linear array.
Advantages include:
The index calculation is:
index = row * columns + column
This technique is widely used in:
Compile the native library using GCC.
gcc -shared -fPIC terrain.c -o libterrain.so
dotnet build
Run:
dotnet run
Rows : 2
Cols : 2
Elevation [0][0] : 2
Elevation [0][1] : 3
Elevation [1][0] : 4
Elevation [1][1] : 5
Terrain Grid (Elevation in meters)
----------------------------------
2.00 3.00
4.00 5.00
This small application introduces several important concepts.
The same architecture appears in many production systems.
The complexity may increase, but the architectural principle remains the same.
This project can easily be expanded by adding features such as:
This project demonstrates how managed and native languages can work together effectively.
F# provides expressive and concise application logic, while C delivers predictable native execution. By combining the two through P/Invoke, developers can build applications that are both productive and performant.
Although the terrain grid application is intentionally small, it illustrates a design pattern that scales to much larger systems in systems programming, embedded development, and high-performance computing.
terrain_grid
https://github.com/aj333git/Civil_Mech_apps/tree/main/terrain_grid