Cogs and Levers A blog full of technical stuff

Unix IPC: Semaphores

Semaphores can simplify access to shared resources. The following snippet will show you how to create a semaphore set and destroy it.

key_t key;
int semid;

/* get an IPC key */
key = ftok("filename", 'E');
/* create the new semaphore set */
semid = semget(key, 10, 0666 | IPC_CREAT);

/* destroy the semaphore */
semctl(semid, 0, IPC_RMID);

Further reading

Unix IPC: Pipes and FIFOs

This snippet will show you two processes communicating between each other using a pipe.

int pfds[2];
char buf[30];

/* open the pipe */
pipe(pfds);

if (!fork()) {
	/* the child will write to the pipe */
	write(pfds[1], "test", 5);
	exit(0);
} else {
	/* the parent will read from the pipe */
	read(pfds[0], buf, 5);
	wait(NULL);
}

FIFOs are just pipes that have a specific name. In this snippet the name is a constant defined elsewhere called FIFO_NAME.

/* first program writes to the named pipe */
char buffer[20];
int num, fd;

/* create the named pipe */
mknod(FIFO_NAME, S_IFIFO | 0666, 0);
/* open for writing */
fd = open(FIFO_NAME, O_WRONLY);
/* write to the pipe */
write(fd, buffer, strlen(buffer));


/* second program reads from the named pipe */
char buffer[20];
int num, fd;
/* create the named pipe */
mknod(FIFO_NAME, S_IFIFO | 0666, 0);
/* open for reading */
fd = open(FIFO_NAME, O_RDONLY);
/* read from the pipe */
read(fd, s, 20);

Further reading

The mknod system call

Unix IPC: Message Queues

Message queues are pretty common structures for inter-process communication. A common queue is created by one of the processes, from there it can be connected to by any other process and have messages submitted to it.

This snippet shows the creation of a queue and sending a message. Another block below this will show receiving a message from the queue.

char buf[20];
int msqid;
key_t key;

/* --- message sender --- */

/* make a key */
key = ftok("first", 'B');
/* create the message queue */
msqid = msgget(key, 0644 | IPC_CREAT));

/* put a message onto the queue */
msgsnd(msqid, &buf, 20, 0);

/* destroy (remove) the message queue */
msgctl(msqid, IPC_RMID, NULL);


/* --- message receiver --- */

/* make a key */
key = ftok("second", 'B');
/* connect to the message queue */
msqid = msgget(key, 0644);
/* receive the message off the queue */
msgrcv(msqid, &buf, 20, 0, 0);

Further reading

Unix IPC: Memory Mapped Files

This snippet will show you how to open a file and map it into memory.

int fd, pagesize;
char *data;

/* open the file */
fd = open("somefile", O_RDONLY);

/* get the current page size */
pagesize = getpagesize();

/* map the 2nd page into memory */
data = mmap((caddr_t)0, pagesize, PROT_READ, MAP_SHARED, fd, pagesize);

/* start using the data pointer */

Further reading

The mmap system call

Unix IPC: Locking Files

Locking a file helps assure your program that no other processes can tamper with it or a region of it.

This snippet will show you how to lock and unlock a file.struct flock fl; int fd;

/* fill out the lock structure */
fl.l_type   = F_WRLCK;
fl.l_whence = SEEK_SET;
fl.l_start  = 0;
fl.l_len    = 0;
fl.l_pid    = getpid();

/* open the file */
fd = open("filename", O_WRONLY);

/* lock the file */
fcntl(fd, F_SETLKW, &fl);

/* --- complete any work here with the file locked --- */

/* unlock the file now */
fl.l_type   = F_UNLCK;
fcntl(fd, F_SETLK, &fl);

Further reading

The fcntl system call