# C programming

WARNING

This page is actually in active development state. Many information will be available soon.

DANGER

I am not a C expert. This language is kinda hard to master, even more when you are using other high level language at the same time. During the past years, I used C for my own personal projects, but never feel really comfortable to use it in daily basis. Anyway, because it is not an easy language, there is lot of things to talk about it. I hope these few notes will help you to avoid some of my mistakes I did in the past.

# Introduction

# Programming

TODO

# Concurrent and Parallel Programming

TODO

# Network Programming

TODO

# Testing

Test suites and test cases are probably the most important part of any language. It will not remove bugs, but will help you to define your needs and create a stable software. I mainly used two open-source framework, atf and minunit. Both are really light and can help you to define strong test suites.

Another important thing to test when developping in C is the memory allocation. Unfortunately, I only used valgrind few times, to ensure all my memory allocations were working find. On OpenBSD and other system, you can also find different malloc implementation which can help you during development phase. Here a few list of them:

A curated list of actively maintained malloc can be found there:

# Documenting

TODO

# Debugging

TODO

# Tracing

TODO

# Deploying

TODO

# Open Source Project Development

TODO

# FreeBSD

# OpenBSD

# NetBSD

# Linux

# FAQ

# How to define a global data structure?

#include <stdio.h>

struct mystruct {
    int id;
    char *data;
}
    
int main (void) {
    struct mystruct test;
}

# How to define a global type?

#include <stdio.h>

typedef struct mystruct {
    ind id;
    char *data;
} mystruct;
    
int main (void) {
    mystruct test;
}

# How to manage memory?

Managing memory is probably one of the main issue with C. Many different libraries exist to help developers to manage the memory with simple routines.

# Do you have a list of alternative C compilers?

# How to use enable posix thread during compilation?

cc -l pthread -o yourfile yourfile.c

# How to insert raw assembly in your C code?

__asm__("movl %edx, %eax\n\t"
        "addl $2, %eax\n\t");

# How to trace a function call?

# How to debug a child or a parent after calling fork?

  • http://www.sourceware.org/gdb/onlinedocs/gdb/Forks.html
  • http://stackoverflow.com/questions/6199270/how-do-i-debug-the-child-process-after-fork-in-gdb
  • http://visualgdb.com/gdbreference/commands/set_follow-fork-mode
  • http://reverseengineering.stackexchange.com/questions/2241/gdb-on-freebsd-and-follow-fork-mode-child

# How to debug threads?

‘thread thread-id’, a command to switch among threads ‘info threads’, a command to inquire about existing threads ‘thread apply [thread-id-list] [all] args’, a command to apply a command to a list of threads thread-specific breakpoints ‘set print thread-events’, which controls printing of messages on thread start and exit. ‘set libthread-db-search-path path’, which lets the user specify which libthread_db to use if the default choice isn't compatible with the program.

  • https://www.sourceware.org/gdb/onlinedocs/gdb/Threads.html#Threads

# How to use Valgrind?

  • http://valgrind.org/
  • http://valgrind.org/docs/manual/manual.html

# References and Resources