Welcome to Linux Knowledge Base and Tutorial
"The place where you learn linux"
No Starch Press

 Create an AccountHome | Submit News | Your Account  

Tutorial Menu
Linux Tutorial Home
Table of Contents

· Introduction to Operating Systems
· Linux Basics
· Working with the System
· Shells and Utilities
· Editing Files
· Basic Administration
· The Operating System
· The X Windowing System
· The Computer Itself
· Networking
· System Monitoring
· Solving Problems
· Security
· Installing and Upgrading
· Linux and Windows

Glossary
MoreInfo
Man Pages
Linux Topics
Test Your Knowledge

Site Menu
Site Map
FAQ
Copyright Info
Terms of Use
Privacy Info
Disclaimer
WorkBoard
Thanks
Donations
Advertising
Masthead / Impressum
Your Account

Communication
Feedback
Forums
Private Messages
Surveys

Features
HOWTOs
News Archive
Submit News
Topics
User Articles
Web Links

Google
Google


The Web
linux-tutorial.info

Who's Online
There are currently, 294 guest(s) and 7 member(s) that are online.

You are an Anonymous user. You can register for free by clicking here

  

HOWTO Home

Current HOWTO: RTLinux HOWTO


RTLinux HOWTO: Inter-Process Communication Next Previous Contents

6. Inter-Process Communication

The example program that we have seen above is what is known as a realtime process. Not every part of a application program need be written in realtime. It is found that only that part of a program which requires precise time restrictions should be written as a realtime process. Others can be written and executed in user space. User spaces processes are often easier to write, execute and debug than realtime threads. But then, there should be a way to communicate between user space Linux processes and realtime thread.

There are several ways for inter-process communication. We will discuss the most important and common way of communication - the realtime FIFO.

6.1 Realtime FIFO

Realtime FIFOs are unidirectional queues (First In First Out). So at one end a process writes data into the FIFO, and from the other end of the FIFO, information is read by another process. Usually, one of these processes is the realtime thread and the other is a user space process.

The Realtime FIFOs are actually character devices (/dev/rtf*) with a major number of 150. Realtime threads uses integers to refer to each FIFO (for example - 2 for /dev/rtf2). There is a limit to the number of FIFOs. There are functions such as rtf_create(), rtf_destroy(), rtf_get(), rtf_put() etc for handling the FIFOs.

On the other hand, the Linux user process sees the realtime FIFOs as normal character devices. Therefore the functions such as open(), close(), read() and write() can be used on these devices.

6.2 Application Using FIFO

First, let us consider a simple C program (filename pcaudio.c) to play music (of just two tones) through the PC speaker. For the time being, let us assume that for playing the note, we need only write to the character device /dev/rtf3. (Later, we will see a realtime time process that reads from this FIFO (/dev/rtf3) and sends to the PC speaker)

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#define DELAY 30000

void make_tone1(int fd)
{
        static char buf = 0;
        write (fd, &buf, 1);
}

void make_tone2(int fd)
{
        static char buf = 0xff;
        write (fd, &buf, 1);
}

main()
{
        int i, fd = open ("/dev/rtf3", O_WRONLY);
        while (1)
        {
                for (i=0;i<DELAY;i++);
                make_tone1(fd);
                for (i=0;i<DELAY;i++);
                make_tone2(fd);
        }
}

Now, if the above shown program (pcaudio.c) is compiled and run, it should create regular sound patters corresponding to a square wave. But before that we need a module for reading from '/dev/rtf3' and sending the corresponding data to the PC speaker. This realtime program can be found at the rtlinux source tree (/usr/src/rtlinux/examples/sound/) . Insert the module sound.o using the command 'insmod'.

Since we have inserted a module for reading from the device, we can now execute our program (compile using 'gcc' and then execute the corresponding 'a.out'. So the process produces somewhat regular tones, when there is no other (time consuming) process in the system. But, when the X server is started in another console, there comes more prolonged silence in the tone. Finally, when a 'find' command (for a file in /usr directory) is executed, the sound pattern is completely distorted. The reason behind this is that, we are writing the data onto the FIFO in non-realtime.

We will, now, see how to run this process in realtime, so that the sound is produced without any kind of disturbance. First, we will convert the above program into a realtime program. (Filename rtaudio.c)

#include <rtl.h>
#include <pthread.h>
#include <rtl_fifo.h>
#include <time.h>

#define FIFO_NO 3
#define DELAY 30000
pthread_t thread;

void * sound_thread(int fd)
{
        int i;
        static char buf = 0;
        while (1)
        {
                for(i=0; i<DELAY; i++);
                buf = 0xff;
                rtf_put(FIFO_NO, &buf, 1);

                for(i=0;i<DELAY;i++);
                buf = 0x0;
                rtf_put(FIFO_NO, &buf, 1);
        }
        return 0;
}

int init_module(void)
{
        return pthread_create(&thread, NULL, sound_thread, NULL);
}

void cleanup_module(void)
{
        pthread_delete_np(thread);
}

If not already done, 'plug in' the module sound.o into the kernel. Compile the above program by writing a Makefile for it (as said earlier), thus producing the module 'rtaudio.o'. Before inserting this module, one more thing. Note that the above program runs into infinite loop. Since, we have not included code for the thread to sleep or stop, the thread will not cease its execution. In short, your PC speaker will go on producing the tone, and you will have to restart your computer for doing anything else.

So, let us change the code a little bit (only in the function sound_thread()) by asking the thread itself to make the delay between tones.

void * sound_thread(int fd)
{
        static char buf = 0;
        pthread_make_periodic_np (pthread_self(), gethrtime(), 500000000);

        while (1)
        {
                pthread_wait_np();
                buf = (int)buf^0xff;
                rtf_put(FIFO_NO, &buf, 1);
        }
        return 0;

}

This time we can stop the process by just removing the module by using the 'rmmod' command.

Here, we have seen how realtime FIFOs can be used for inter-process communication. Also the real need for RTLinux can be understood from the above example.


Next Previous Contents

The Linux Tutorial completely respects the rights of authors and artists to decide for themselves if and how their works can be used, independent of any existing licenses. This means if you are the author of any document presented on this site and do no wish it to be displayed as it is on this site or do not wish it to be displayed at all, please contact us and we will do our very best to accommodate you. If we are unable to accommodate you, we will, at your request, remove your document as quickly as possible.

If you are the author of any document presented on this site and would like a share of the advertising revenue, please contact us using the standard Feedback Form.


  

The Linux Tutorial is always looking for new contributors.


Login
Nickname

Password

Security Code
Security Code
Type Security Code


Don't have an account yet? You can create one. As a registered user you have some advantages like theme manager, comments configuration and post comments with your name.

Help if you can!


Amazon Wish List

Did You Know?
You can get all the latest Site and Linux news by checking out our news page.


Friends



Tell a Friend About Us

Bookmark and Share



Web site powered by PHP-Nuke

Is this information useful? At the very least you can help by spreading the word to your favorite newsgroups, mailing lists and forums.
All logos and trademarks in this site are property of their respective owner. The comments are property of their posters. Articles are the property of their respective owners. Unless otherwise stated in the body of the article, article content (C) 1994-2013 by James Mohr. All rights reserved. The stylized page/paper, as well as the terms "The Linux Tutorial", "The Linux Server Tutorial", "The Linux Knowledge Base and Tutorial" and "The place where you learn Linux" are service marks of James Mohr. All rights reserved.
The Linux Knowledge Base and Tutorial may contain links to sites on the Internet, which are owned and operated by third parties. The Linux Tutorial is not responsible for the content of any such third-party site. By viewing/utilizing this web site, you have agreed to our disclaimer, terms of use and privacy policy. Use of automated download software ("harvesters") such as wget, httrack, etc. causes the site to quickly exceed its bandwidth limitation and are therefore expressly prohibited. For more details on this, take a look here

PHP-Nuke Copyright © 2004 by Francisco Burzi. This is free software, and you may redistribute it under the GPL. PHP-Nuke comes with absolutely no warranty, for details, see the license.
Page Generation: 0.25 Seconds