Study of Linux Introduction To Linux
Study of Linux Introduction To Linux
INTRODUCTION TO LINUX
Linux is a generic term referring to Unix-like computer operating systems based on the Linux
kernel. Their development is one of the most prominent examples of free and open source
software collaboration; typically all the underlying source code can be used, freely modified,
and redistributed by anyone.
The name "Linux" comes from the Linux kernel, originally written in 1991 by Linus Torvalds.
The rest of the system usually comprises components such as the Apache HTTP Server, the X
Window System, the K Desktop Environment, and utilities and libraries from the GNU
operating system (announced in 1983 by Richard Stallman).
Many quantitative studies of free / open source software focus on topics including market share
and reliability, with numerous studies specifically examining Linux. The Linux market is
growing rapidly, and the revenue of servers, desktops, and packaged software running Linux
was expected to exceed $35.7 billion by 2008.
LINUX FILE SYSTEM
A file system is the methods and data structures that an operating system uses to keep track of
files on a disk or partition; that is, the way the files are organized on the disk. The word is also
used to refer to a partition or disk that is used to store the files or the type of the file system.
The difference between a disk or partition and the file system it contains is important. A few
programs (including, reasonably enough, programs that create file systems) operate directly on
the raw sectors of a disk or partition; if there is an existing file system there it will be destroyed
or seriously corrupted. Most programs operate on a file system, and therefore won't work on a
partition that doesn't contain one (or that contains one of the wrong type). Before a partition or
disk can be used as a file system, it needs to be initialized, and the bookkeeping data structures
need to be written to the disk. This process is called making a file system.
Most UNIX file system types have a similar general structure, although the exact details vary
quite a bit. The central concepts are superblock, inode, data block, directory block, and
indirection block. The superblock contains information about the file system as a whole, such
as its size (the exact information here depends on the file system). An inode contains all
information about a file, except its name. The name is stored in the directory, together with the
number of the inode. A directory entry consists of a filename and the number of the inode
which represents the file. The inode contains the numbers of several data blocks, which are
used to store the data in the file. There is space only for a few data block numbers in the inode,
however, and if more are needed, more space for pointers to the data blocks is allocated
dynamically. These dynamically allocated blocks are indirect blocks; the name indicates that
in order to find the data block, one has to find its number in the indirect block first.
Like UNIX, Linux chooses to have a single hierarchical directory structure. Everything starts
from the root directory, represented by /, and then expands into sub-directories instead of
having so-called 'drives'. In the Windows environment, one may put one's files almost
anywhere: on C drive, D drive, E drive etc. Such a file system is called a hierarchical structure
and is managed by the programs themselves (program directories), not by the operating system.
On the other hand, Linux sorts directories descending from the root directory / according to
their importance to the boot process.
Linux, like Unix also chooses to be case sensitive. What this means is that the case, whether in
capitals or not, of the characters becomes very important. This feature accounts for a fairly
large proportion of problems for new users especially during file transfer operations whether it
may be via removable disk media such as floppy disk or over the wire by way of FTP.
The following bin/ dev/ home/ lost+found/ proc/ sbin/ usr/ boot/ etc/ lib/ mnt/ root/ tmp/ var/
are explained in detail.
/sbin - This directory contains all the binaries that are essential to the working of the system.
These include system administration as well as maintenance and hardware configuration
programs.
/bin - In contrast to /sbin, the bin directory contains several useful commands that are used by
both the system administrator as well as nonprivileged users.
/boot - This directory contains the system.map file as well as the Linux kernel. Lilo places the
boot sector backups in this directory.
/dev - This is a very interesting directory that highlights one important characteristic of the
Linux filesystem - everything is a file or a directory. Look through this directory and you should
see hda1, hda2 etc, which represent the various partitions on the first master drive of the system.
/dev/cdrom and /dev/fd0 represent your CDROM drive and your floppy drive.
/etc - This directory contains all the configuration files for your system. Your lilo.conf file lies
in this directory as does hosts, resolv.conf and fstab. /home –These are the user home
directories, which can be found under /home/username.
/lib - This contains all the shared libraries that are required by system programs. Windows
equivalent to a shared library would be a DLL file.
/lost+found - Linux should always go through a proper shutdown. Sometimes your system
might crash or a power failure might take the machine down. Either way, at the next boot, a
lengthy filesystem check using fsck will be done. Fsck will go through the system and try to
recover any corrupt files that it finds. The result of this recovery operation will be placed in
this directory.
/mnt - This directory usually contains mount points or sub-directories where you mount your
floppy and your CD.
/opt - This directory contains all the software and add-on packages that are not part of the
default installation.
/proc - This is a special directory on your system.
/root - We talked about user home directories earlier and well this one is the home directory of
the user root.
/tmp - This directory contains mostly files that are required temporarily.
/usr - This is one of the most important directories in the system as it contains all the user
binaries.
/usr/src/linux contains the source code for the Linux kernel.
/var - This directory contains spooling data like mail and also the output from the printer
daemon. The above content briefs about Linux and the file system of Linux
Fork-exec is a commonly used technique in Unix whereby an executing process spawns a new
program. fork() is the name of the system call that the parent process uses to "divide" itself
("fork") into two identical processes. After calling fork(), the created child process is actually
an exact copy of the parent - which would probably be of limited use - so it replaces itself with
another process using the system call exec().
The parent process can either continue execution or wait for the child process to complete. The
child, after discovering that it is the child, replaces itself completely with another program, so
that the code and address space of the original program are lost.
If the parent chooses to wait for the child to die, then the parent will receive the exit code of
the program that the child executed. Otherwise, the parent can ignore the child process and
continue executing as it normally would; to prevent the child becoming a zombie it should wait
on children at intervals or on SIGCHLD.
When the child process calls exec(), all data in the original program is lost, and replaced with
a running copy of the new program. This is known as overlaying. Although all data is replaced,
the file descriptors that were open in the parent are closed only if the program has explicitly
marked them close-on-exec. This allows for the common practice of the parent creating a pipe
prior to calling fork() and using it to communicate with the executed program.
Example:
/* using execvp to execute the contents of argv */
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
execvp(argv[1], &argv[1]);
perror("exec failure");
exit(1);
}
Example of wait():
#include <sys/types.h>
#include <sys/wait.h>
Void main()
{
int status;
pid_t pid;
pid = fork();
if(pid == -1)
printf(“\nERROR child not created “);
else if (pid == 0) /* child process */
{
printf("\n I'm the child!");
exit(0);
}
else /* parent process */
{
wait(&status);
printf("\n I'm the parent!")
printf("\n Child returned: %d\n", status)
}
}
#include <unistd.h>
int close(int fd);
DESCRIPTION
close() closes a file descriptor, so that it no longer refers to any file and may be reused. If fd is
the last copy of a particular file descriptor the resources associated with it are freed;
RETURN VALUE
close() returns zero on success. On error, -1 is returned, and errno is set appropriately.
#include <sys/types.h>
#include <dirent.h>
struct dirent *readdir(DIR *dir);
DESCRIPTION
The readdir() function returns a pointer to a dirent structure representing the next directory
entry in the directory stream pointed to by dir. It returns NULL on reaching the end-of-file or
if an error occurred.
On Linux, the dirent structure is defined as follows:
struct dirent {
ino_t d_ino; /* inode number */
off_t d_off; /* offset to the next dirent */
unsigned short d_reclen; /* length of this record */
unsigned char d_type; /* type of file */
char d_name[256]; /* filename */
};
The data returned by readdir() may be overwritten by subsequent calls to readdir() for the same
directory stream.
RETURN VALUE
The readdir() function returns a pointer to a dirent structure, or NULL if an error occurs or end-
of-file is reached. On error, errno is set appropriately.
opendir() System Call
NAME
Opendir() – To open a directory
#include <sys/types.h>
#include <dirent.h>
DIR *opendir(const char *name);
DESCRIPTION
The opendir() function opens a directory stream corresponding to the directory name, and
returns a pointer to the directory stream. The stream is positioned at the first entry in the
directory.
RETURN VALUE
The opendir() function returns a pointer to the directory stream. On error, NULL is returned,
and errno is set appropriately.
ERRORS
EACCES Permission denied.
EMFILE Too many file descriptors in use by process.
ENFILE Too many files are currently open in the system.
ENOENT Directory does not exist, or name is an empty string.
ENOMEM Insufficient memory to complete the operation.
ENOTDIR name is not a directory.
The file descriptor is returned by a "creat()" or "open()" system call. The "buffer" is a pointer
to a variable or an array that contains the data; and the "buffer length" gives the number of
bytes to be written into the file. While different data types may have different byte lengths on
different systems, the "sizeof()" statement can be used to provide the proper buffer length in
bytes. A "write()" call could be specified as follows:
float array[10];
write( fd, array, sizeof( array ) );
The "write()" function returns the number of bytes it actually writes. It will return -1 on an
error.
Example:
#include<stdio.h>
#include<fcntl.h>
#include<stdlib.h>
main(int argc,char *argv[])
{
int fd,i;
char ch[1];
if (argc<2)
{ printf("Usage: mycat filename\n");
exit(0);
}
fd=open(argv[1],O_RDONLY);
if(fd==-1)
printf("%s is not exist",argv[1]);
else
{
printf("Contents of the file %s is : \n",argv[1]);
while(read(fd,ch,1)>0)
printf("%c",ch[0]);
close(fd);
}