Table of Contents

<< Back to Writing Codezero Applications

Applications Using Codezero POSIX Services

1.) Writing a POSIX Program Using libposix

Applications may be written with no special treatment, using the POSIX API system calls that are currently supported by libposix.

In order to create a build environment with libposix support, please refer to the test0 application SConstruct file as an example. Normally, including a reference to the libposix path and include directory should suffice.

Below is a code snippet from test0 application leveraging some of the POSIX API functions supported by libposix:

/*
 * Test mmap/munmap posix calls.
 *
 * Copyright (C) 2007, 2008 Bahadir Balban
 */
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <tests.h>
#include <errno.h>
 
#define PAGE_SIZE		0x1000
 
int mmaptest(void)
{
	int fd;
	void *base;
	int x = 0x1000;
 
	if ((fd = open("./mmapfile.txt", O_CREAT | O_TRUNC | O_RDWR, S_IRWXU)) < 0)
		goto out_err;
 
	/* Extend the file */
	if ((int)lseek(fd, PAGE_SIZE*16, SEEK_SET) < 0)
		goto out_err;
 
	if (write(fd, &x, sizeof(x)) < 0)
		goto out_err;
 
	if ((int)(base = mmap(0, PAGE_SIZE*16, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) < 0)
		goto out_err;
 
	*(unsigned int *)(base + PAGE_SIZE*2) = 0x1000;
	if (msync(base + PAGE_SIZE*2, PAGE_SIZE, MS_SYNC) < 0)
		goto out_err;
 
	if (munmap(base + PAGE_SIZE*2, PAGE_SIZE) < 0)
		goto out_err;
 
	*(unsigned int *)(base + PAGE_SIZE*3) = 0x1000;
	*(unsigned int *)(base + PAGE_SIZE*1) = 0x1000;
 
	printf("MMAP TEST           -- PASSED --\n");
	return 0;
 
out_err:
	printf("MMAP TEST           -- FAILED --\n");
	return 0;
}
 
int main(int argc, char *argv[])
{
        mmaptest();
}

The POSIX calls currently supported by libposix are: open, close, read, write, lseek, creat, stat, readdir, mkdir, chdir, mmap, munmap, shmget/at/dt, fork, execve, clone, exit, getpid, and gettimeofday.

Even though many more API calls are needed to support complete POSIX applications, the current set of system calls sufficiently allow creation of well-organized programs with solid memory, file and process management capabilities.

<< Back to Writing Codezero Applications