Unit1-Unix File System Structure-Part2
Unit1-Unix File System Structure-Part2
Structure
Unix File System is a logical method of organizing and
storing large amounts of information in a way that makes it easy
to manage.
All data in Unix is organized into files. All files are organized into
directories. These directories are organized into a tree-like
structure called the file system.
At the very top of the file system is a directory called “root” which
is represented by a “/”. All other files are “descendants” of root.
The Unix file system uses a directory hierarchy that allows for easy
navigation and organization of files.
Directories can contain both files and other directories, and each
file or directory has a unique name.
Each file and directory has an owner and a group associated with
it, and permissions can be set to allow or restrict access to these
entities.
One of the most important features of the Unix file system is its
support for symbolic links, which are pointers to other files or
directories.
of files
Ordinary Files
An ordinary file is a file on the system that contains data, text, or
program instructions.
Used to store your information, such as some text you have
written or an image you have drawn. This is the type of file
that you usually work with.
Always located within/under a directory file.
Do not contain other files.
In long-format output of ls -l, this type of file is specified by
the “-” symbol.
Directories
Directories store both special and ordinary files. For users familiar
with Windows or Mac OS, UNIX directories are equivalent to folders.
A directory file contains an entry for every file and subdirectory
that it houses. If you have 10 files in a directory, there will be 10
entries in the directory. Each entry has two components. (1) The
Filename (2) A unique identification number for the file or directory
(called the inode number)
Branching points in the hierarchical tree.
Used to organize groups of files.
May contain ordinary files, special files or other directories.
Never contain “real” information which you would work
with (such as text). Basically, just used for organizing files.
All files are descendants of the root directory, ( named / )
located at the top of the tree.
In long-format output of ls –l , this type of file is specified by the “d”
symbol.
Special Files
Used to represent a real physical device such as a printer, tape
drive or terminal, used for Input/Output (I/O) operations. Device or
special files are used for device Input/Output(I/O) on UNIX and
Linux systems. They appear in a file system just like an ordinary file
or a directory. On UNIX systems there are two flavors of special
files for each device, character special files and block special files :
When a character special file is used for device
Input/Output(I/O), data is transferred one character at a
time. This type of access is called raw device access.
When a block special file is used for device
Input/Output(I/O), data is transferred in large fixed-size
blocks. This type of access is called block device access.
For terminal devices, it’s one character at a time. For disk devices
though, raw access means reading or writing in whole chunks of
data – blocks, which are native to your disk.
In long-format output of ls -l, character special files are
marked by the “c” symbol.
In long-format output of ls -l, block special files are marked
by the “b” symbol.
Pipes
UNIX allows you to link commands together using a pipe. The pipe
acts a temporary file which only exists to hold data from one
command until it is read by another.
A Unix pipe provides a one-way flow of data. The output or result of
the first command sequence is used as the input to the second
command sequence. To make a pipe, put a vertical bar (|) on the
command line between two commands.For example: who | wc -l In
long-format output of ls –l , named pipes are marked by the “p”
symbol.
Sockets
A Unix socket (or Inter-process communication socket) is a
special file which allows for advanced inter-process
communication.
system calls
Process Control :
This system calls perform the task of process creation, process
termination, etc.
The Linux System calls under this are fork() , exit() , exec().
fork()
A new process is created by the fork() system call.
A new process may be created with fork() without a
new program being run-the new sub-process simply
continues to execute exactly the same program that
the first (parent) process was running.
It is one of the most widely used system calls under
process management.
exit()
The exit() system call is used by a program to
terminate its execution.
The operating system reclaims resources that were
used by the process after the exit() system call.
exec()
A new program will start executing after a call to
exec()
Running a new program does not require that a new
process be created first: any process may call
exec() at any time. The currently running program is
immediately terminated, and the new program
starts executing in the context of the existing
process.
File Management :
File management system calls handle file manipulation jobs like
creating a file, reading, and writing, etc. The Linux System calls
under this are open(), read(), write(), close().
open():
It is the system call to open a file.
This system call just opens the file, to perform
operations such as read and write, we need to
execute different system call to perform the
operations.
read():
This system call opens the file in reading mode
We can not edit the files with this system call.
Multiple processes can execute the read() system
call on the same file simultaneously.
write():
This system call opens the file in writing mode
We can edit the files with this system call.
Multiple processes can not execute the write()
system call on the same file simultaneously.
close():
This system call closes the opened file.
Device Management :
Device management does the job of device manipulation like
reading from device buffers, writing into device buffers, etc. The
Linux System calls under this is ioctl().
ioctl():
ioctl() is referred to as Input and Output Control.
ioctl is a system call for device-specific input/output
operations and other operations which cannot be
expressed by regular system calls.
Information Maintenance:
It handles information and its transfer between the OS and the user
program. In addition, OS keeps the information about all its
processes and system calls are used to access this information. The
System calls under this are getpid(), alarm(), sleep().
getpid():
getpid stands for Get the Process ID.
The getpid() function shall return the process ID of
the calling process.
The getpid() function shall always be successful and
no return value is reserved to indicate an error.
alarm():
This system call sets an alarm clock for the delivery
of a signal that when it has to be reached.
It arranges for a signal to be delivered to the calling
process.
sleep():
This System call suspends the execution of the
currently running process for some interval of time
Meanwhile, during this interval, another process is
given chance to execute
Communication :
These types of system calls are specially used for inter-process
communications.
Two models are used for inter-process communication
1. Message Passing(processes exchange messages with one
another)
2. Shared memory(processes share memory region to
communicate)
The system calls under this are pipe() , shmget() ,mmap().
pipe():
The pipe() system call is used to communicate
between different Linux processes.
It is mainly used for inter-process communication.
The pipe() system function is used to open file
descriptors.
shmget():
shmget stands for shared memory segment.
It is mainly used for Shared memory
communication.
This system call is used to access the shared
memory and access the messages in order to
communicate with the process.
mmap():
This function call is used to map or unmap files or
devices into memory.
The mmap() system call is responsible for mapping
the content of the file to the virtual memory space
of the process.
#include<unistd.h>
#include<stdio.h>
#include<fcntl.h>
int main()
{
int fp;
char msg[30]="this is write operation";
char rd_fm_fl[35];
fp=open("test.txt", O_RDWR);
printf("fp = %d",fp);
if(fp != -1)
{
printf("write to file");
write(fp,msg,sizeof(msg));
lseek(fp,0,SEEK_SET);
read(fp,rd_fm_fl,sizeof(msg));
printf("\nThe message written to file is \n %s",rd_fm_fl);
close(fp);
}
return 0;}