A healthy portion of DevOps with a side of Linux. Site currently under construction..

pthreads in C

This tutorial assumes you know the basic concepts of multithreading. It is based on this short video series by Dr. Brian Fraser Creating single threads The pthread library (POSTIX thread) allows C programmers to use multithreading capabilities. First you must define the <pthread.h> header, and link -pthread when compiling the program. The function to create a thread is the pthread_create(): 1 2 <pthread.h> int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg); This takes 4 different parameters:

Socket Programming (TCP)

The C socket API allows a developer to design and build a TCP/IP client/server program. To begin with a socket address structure is needed (<sys/socket.h>): 1 2 3 4 5 6 7 8 9 struct sockaddr_in { sa_family_t sin_family; // AF_INET for TCP/IP in_port_t sin_port; // port number struct in_addr sin_addr; // IP address }; struct in_addr { // internet address (in_addr) uint32_t s_addr; // IP address in network byte order }; for TCP/IP connections sin_family is always set to AF_INET, sin_port contains the port number (in network byte order) and finally sin_addr is the host IP address, also in network byte order.

strace

Strace is a command used to view the system calls made by an executable program during runtime, it is especially helpful when trying to debug a systems program. Basic strace commands: 1 2 3 4 5 6 7 8 9 10 11 strace <program executable> # output to stdout the full output strace -o <file.out> <progex> # output to <file.out> instead of stdout strace -e <command> <progex> # only output specific <command> (i.