[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
BeginnersGuideLinuxProgramming

A Beginners Guide To Linux Programming

---------------------------------------------------------------

C under Linux

The C programming language is still very popular among system programmers worldwide and among Linux hackers in particular. The reason for this is simple: back in the late 70s, the authors of the well-known C bible, Kerninghan and Ritchie, worked at the Berkeley University on the Unix system. At this time, the complete Unix source code was written in assembly language.

Then, Kerninghan and their collegues at Berkeley decided to rewrite all the Unix source code in C, which was prior created by Dave Kerninghan. This was the birth day of the C programming language and since then, the connection between Unix and C lasts till today.

A Note on Kernel Versions

The programs presented have been tested under Kernel 2.4 and 2.6 both on SuSE Linux and Knoppix. The programs should work on other distros as well, untill you have installed a standard version. I could not test them under Kernel 2.2. Therefore i would recommend you to check out the man pages of your distro to see if it supports the functions i discuss here.

Working with Devices

The device concept of Linux is different to that of other operating systems. In Linux, virtually anything is regarded as a device. For example, the first harddrive, /dev/hda1 is a device; the CD-ROM is a device: /dev/multimedia/cdrom ; the soundcard is a device etc. For each device exists an entry in the filesystem. These entries in the filesystem are called Inodes .

The advantag of integrating devices in the filesystem is obvious. Instead of coding special code for each particular device, you can use the functions and libraries of the filesystem to access and manipulate devices.

For example you can use the standard open() and close() functions to open and close any of the above mentioned devices.

int open(char* pathname, int flags);
int close(int fd);


Accessing devices with ioctl()

One thing that is important when programming device drivers is, that, under Linux you first need to get permission to access any device. Another thing is that you need to chmod or sudo su to become root user. This is of overall importance and before start writing code to manipulate devices you may first change your user status.

To demonsrate the use of ioctl() i will write a simple program that opens and closes the CD-ROM device.

/* Opening and closing CD-ROM device */
  1. include <stdio.h>
  2. include <unistd.h>
  3. include <sys / ioctl.h>
  4. include <fcntl.h>
  5. include <linux/cdrom.h>
int main() { int fd; /* check if /dev/cdrom is accessible */ if((fd = fopen(" /dev/cdrom ", O_RDONLY | O_NONBLOCK == -1 )) { perror("eject: Cannot open /dev/cdrom"); return (1); } /* check for extended ioctl() access */ if((ioctl(fd,CDROMEJECT) == -1) { perror("eject: ioctl() failed"); return(1); } close(fd); return(0); }


This program is rather simple. It first needs to check if /dev/cdrom is available. If so, then it checks for extended access via ioctl() . If all tests are successfull, the program should eject the CD-ROM device and close it again.

Note: This program does not work with a Live Linux CD like Knoppix, because the operating system blocks the eject function of the CD-ROM .

Working with Processes

Linux is a multiprocessural operating system. Hence, its not bad to have a basic knowledge of how processes are created and to deal with them.

There are 2 ways of creating processes. fork and [/b] exec [/b].

(Comment: exec does not really create a new process, it merely replaces the running program in the current process by another program. fork however creates a new process running the same program as the current process. To start a program as a new process, first fork and then exec the program in the new process.)

last edited (June 9, 2004) by md2perpe, Number of views: 12484, Current Rev: 19 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.