Determining PThread Library Type

Linux kernel versions prior to 2.6 did not offer good internal support for threads. Therefore a threading library called LinuxThreads was used to provide most of the POSIX PThread API.

Since Linux 2.6 good threading support was added to the Linux kernel and a new library, called NPTL for New Posix Threading Library was created to provide scalable, robust POSIX compliant threading support in Linux.

As both libraries use the same POSIX API, code written for one will usually work with the other as is, but not always. It is therefore some time desirable to be able to learn in run time which threading library is being used.

The following code example shows how this can be done:

#define _XOPEN_SOURCE
#include <unistd.h>
#include <stdio.h>
 
int main(void)
{
  char name[128];
  confstr (_CS_GNU_LIBPTHREAD_VERSION,  name, sizeof(name));
  printf ("Pthreads lib: %s\n", name);
  return 0;
}

Using O_DIRECT

It is sometime useful for a user program to request that to be able to read and write directly from a storage device. All DMA operation, if any, will be performed directly into the application memory space, without being going through the kernel page cache.

Here is a small code example showing how this can accomplished if proper support is provided by the file system and storage device.

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <linux/fcntl.h>
#include <sys/mman.h>
 
static char * progname;
#define PAGE_SIZE (4096)
 
void usage(void) {
    printf("Usage: %s [filename]\n", progname);
    return;
}
 
int main(int argc, char * argv[]) {
 
    const char * filename;
    int fd, ret;
    char *buffer;
 
    progname = argv[0];
    if (argc != 2) {
        usage();
        exit(0);
    }
    filename = argv[1];
    ret = posix_memalign(&amp;buffer, 512, PAGE_SIZE);
    if(ret) {
      printf("%s: %s", progname, strerror(ret));
      exit(-5);
    }
    printf("%s: Got aligned buffer %p\n", 
       progname, buffer);
    fd = open(filename, O_RDWR|O_CREAT|O_DIRECT, 
        S_IRWXU);
    if(-1 == fd) {
        perror(progname);
        exit(-1);
    }
    strcpy(buffer, "testing testing 1 2 3!");
    ret = write(fd, buffer, PAGE_SIZE);
 
    if(-1 == ret) {
      perror(progname);
      exit(-2);
    }
 
        printf("%s: Written: %s\n", progname, buffer);
 
    lseek(fd, SEEK_SET, 0);
    ret = read(fd, buffer, PAGE_SIZE);
    if(-1 == ret) {
       perror(progname);
       exit(-2);
     }
 
    printf("%s: Got %s\n", progname, buffer);
    return 0;
}