The document discusses Linux low-level I/O routines including system calls for file manipulation such as open(), read(), write(), close(), and ioctl(). It describes how files are represented in UNIX as sequences of bytes and different file types. It also covers the standard C I/O library functions, file descriptors, blocking vs non-blocking I/O, and other system calls related to file I/O like ftruncate(), lseek(), dup2(), and fstat(). Examples of code using these system calls are provided.
- Linux originated as a clone of the UNIX operating system. Key developers included Linus Torvalds and developers from the GNU project.
- Linux is open source, multi-user, and can run on a variety of hardware. It includes components like the Linux kernel, shell, terminal emulator, and desktop environments.
- The document provides information on common Linux commands, files, users/groups, permissions, and startup scripts. It describes the Linux file system and compression/archiving utilities.
The document provides an overview of the Linux filesystem, including its hierarchical tree structure with common subdirectories like /bin, /home, and /usr. It discusses useful commands for navigating the filesystem like cd, pwd, and running privileged commands with sudo. The document also compares the Linux and Windows filesystem structures and file types. It introduces package management with apt-get and the power of pipes in Linux.
The document provides an overview of the Linux file system structure and common Linux commands. It describes that in Linux, everything is treated as a file, including devices, pipes and directories. It explains the different types of files and partitions in Linux, and provides examples of common file manipulation and system monitoring commands.
The document provides information about an upcoming UNIX and Shell Scripting workshop, including contact information for the workshop instructor R. Chockalingam, and covers topics that will be discussed such as the architecture and components of the UNIX operating system, basic UNIX commands, text editors, the file system structure, flags and arguments, and more.
The structure of Linux - Introduction to Linux for bioinformaticsBITS
This 3th slide deck of the training 'Introduction to linux for bioinformatics' gives a broad overview of the file system structure of linux. We very gently introducte the command line in this presentation.
This document provides a summary of the Unix and GNU/Linux command line. It begins with an overview of files and file systems in Unix, including that everything is treated as a file. It then discusses command line interpreters (shells), and commands for handling files and directories like ls, cd, cp, and rm. It also covers redirecting standard input/output, pipes, and controlling processes. The document is intended as training material and provides a detailed outline of its contents.
The document summarizes the contents of a training on the Unix and GNU/Linux command line. It covers shells and command line interpreters, the filesystem structure, file handling commands like ls, cd, cp, and file permissions. It also discusses standard input/output redirection, pipes, process control and environment variables. The training contents are organized into 5 sections covering these topics at an introductory level.
The document summarizes the contents of a training presentation on the Unix and GNU/Linux command line. It covers shells and command line interpreters, the filesystem structure including common directories, file handling commands like ls, cd, cp, and an introduction to pipes and I/O redirection. Special files and directories like symlinks, devices, and ~ (home directory) are explained. File permissions and ownership are also mentioned.
File is a container for storing information. It can be treated as a sequence of characters. Unlike the old DOS files, a UNIX file doesn‟t contain eof (end- of –file) mark. A file‟s size is not stored in the file, nor even its name. All files attributes are kept in separate area of hard disk, not accessible to humans, but only to the kernel.ThesisScientist.com
This document provides an overview of common Linux software and how to install additional software. It discusses the major desktop environments GNOME and KDE and default applications like Firefox, Thunderbird, and OpenOffice. It describes the file structure with directories like home, bin, etc. It also outlines several methods for installing software, including via package managers, downloading binaries or source code. The key difference between Linux and Windows is that Linux has a different file structure and installation process which can cause culture shock for new users.
This document discusses various concepts related to file input/output (I/O) in Linux system programming. It covers opening, reading from, writing to, closing, seeking within, and truncating files using system calls like open(), read(), write(), close(), lseek(), ftruncate(). It also discusses related topics like file descriptors, blocking vs non-blocking I/O, synchronized I/O, direct I/O, and positional reads/writes.
This document provides an overview of the Linux operating system and how to use basic Linux commands. It explains that Linux is a free version of UNIX that is operated through a command line terminal rather than a graphical user interface. It also describes how to access the course Linux server using SSH and SFTP, navigate and manipulate files and directories using commands like ls, cd, cp, and rm, view file contents with cat and more, and get help with commands like man. Finally, it provides a list of common Linux shell commands and how to run and edit programs.
Lesson 2 Understanding Linux File SystemSadia Bashir
The document provides an overview of Linux file systems and file types. It discusses:
1) The main types of files in Linux including directories, special files, links, sockets and pipes.
2) The standard Linux directory structure and the purpose of directories like /bin, /sbin, /etc, and /usr.
3) Common Linux file extensions and hidden files that begin with a dot.
4) Environment variables and how they can be used to customize a system.
5) Symbolic links and how they create references to files without copying the actual file.
The document provides an overview of Linux operating system concepts including:
- Linux is an open source operating system that interacts with hardware and allocates resources.
- It supports multi-tasking and multi-user environments. Common types include Debian, Ubuntu, and Redhat.
- Key components include the kernel, shell programs, file management commands, text editors, browsers, and programming tools.
This document provides an overview and introduction to the hardware, software, and file structure of the EduBook device. It discusses the hardware components, how to open the case and access internal parts. It then summarizes the available operating systems, describes the Linux file structure and key directories. The document outlines software options like browsers and office applications that are preinstalled. It concludes with some tips on software issues, advanced options for running Windows programs in Wine, and contact information.
The document discusses the components of a file system in a UNIX system including the boot block, super block, inode list, and data blocks. It describes the information stored in the super block and inodes. Inodes contain metadata about files such as ownership, permissions, size, and location of data blocks. The document also covers various file-related system calls like open, read, write, close, and lseek. It provides examples of using these calls to perform operations like opening, reading, writing to files and modifying the file offset. File locking mechanisms using flock struct and fcntl calls are also explained.
This document contains an assignment submission for a 4th semester networking course at the University of Engineering and Technology in Taxila, Pakistan. The assignment was submitted by M. Ubaid Ashraf with registration number 20-CP-09 to professor Sir Adnan Mustafa. The assignment contains 18 tasks related to Linux commands like cd, ls, touch, cat etc. and includes writing a simple shell script to calculate interest and explaining the purpose of commands like kill, grep, tail etc.
The document provides information about Linux including its history, components, and usage. It can be summarized as follows:
Linux originated in 1991 when Linus Torvalds created the Linux kernel. Since then, thousands of programmers have enhanced Linux, which is now used on millions of computers worldwide. Linux is a fully-networked, multi-user, multi-tasking operating system that provides both command line and graphical interfaces. It includes core utilities like editors, compilers, and networking tools. Common distributions include Red Hat Enterprise Linux and Fedora, which provide commercial support and the latest open source technologies, respectively.
The document discusses the UNIX operating system. It describes UNIX as a stable, multi-user, multi-tasking system used for servers, desktops and laptops. It also discusses the different components that make up the UNIX system, including the kernel, shell, and programs. It explains the directory structure and file hierarchy with the root directory at the top. It provides examples of different types of files and concludes by describing some basic date and time commands in UNIX.
This document provides an overview of file administration in Linux. It describes the three types of files in Linux - ordinary disk files which contain user data, special files which represent devices, and directory files which contain other files and directories. It outlines guidelines for naming files and directories, explaining which characters to avoid. It also introduces the file command for determining a file's type and describes the basic Linux directory structure with files and directories organized in a tree format.
This document provides an introduction and overview of the UNIX operating system. It discusses that UNIX is an operating system, like Windows, that manages communication between software and hardware. Popular versions of UNIX include Linux and Android. The document outlines reasons for using UNIX such as reliability, low/no cost, and open source access. It describes the core components of UNIX including the kernel, bootloader, daemons, and shells. Key features like being portable, multi-user, and providing security are highlighted. Common UNIX commands and utilities are listed along with file permissions, redirection, pipes, and filters.
The document provides an overview of the contents of a training on the Unix and GNU/Linux command line. It covers topics such as shells and filesystem structure, file handling commands, standard input/output redirection, task control, text editing and system administration basics. The training aims to teach users full control of tasks and how to get help and find resources on the command line.
The document discusses various topics related to Unix system administration including device files, block and character devices, major and minor numbers, the Unix file system structure, partitions, file systems, inodes, mounting and unmounting file systems, disk space usage commands, compression tools, printing files, file system checking, user management commands, the role of init in startup and shutdown, run levels, and backup tools. Some key points include:
1. Device files allow all physical devices like tapes, printers, disks to be accessed as normal files.
2. Block and character devices are distinguished based on whether access is via blocks (block devices) or directly (character devices).
3. Major and minor numbers help the
This document discusses embedded Linux programming. It covers topics such as what Linux is, the layers in a Linux system including the kernel and user programs, how Linux differs from legacy real-time operating systems, and an agenda for a course on embedded Linux driver development that covers the Linux kernel, memory management, interrupts, and networking. It also provides information on basic Linux command line tools and file permissions.
This document provides a beginner's guide to learning Linux. It covers topics such as what Linux is, understanding files and folders, users and permissions, the root user, opening a terminal, basic commands like ls, cd, pwd, and tar for archiving files. The guide explains important Linux concepts and provides examples of common commands to get started using the Linux command line.
The document summarizes the contents of a training on the Unix and GNU/Linux command line. It covers shells and command line interpreters, the filesystem structure, file handling commands like ls, cd, cp, and file permissions. It also discusses standard input/output redirection, pipes, process control and environment variables. The training contents are organized into 5 sections covering these topics at an introductory level.
The document summarizes the contents of a training presentation on the Unix and GNU/Linux command line. It covers shells and command line interpreters, the filesystem structure including common directories, file handling commands like ls, cd, cp, and an introduction to pipes and I/O redirection. Special files and directories like symlinks, devices, and ~ (home directory) are explained. File permissions and ownership are also mentioned.
File is a container for storing information. It can be treated as a sequence of characters. Unlike the old DOS files, a UNIX file doesn‟t contain eof (end- of –file) mark. A file‟s size is not stored in the file, nor even its name. All files attributes are kept in separate area of hard disk, not accessible to humans, but only to the kernel.ThesisScientist.com
This document provides an overview of common Linux software and how to install additional software. It discusses the major desktop environments GNOME and KDE and default applications like Firefox, Thunderbird, and OpenOffice. It describes the file structure with directories like home, bin, etc. It also outlines several methods for installing software, including via package managers, downloading binaries or source code. The key difference between Linux and Windows is that Linux has a different file structure and installation process which can cause culture shock for new users.
This document discusses various concepts related to file input/output (I/O) in Linux system programming. It covers opening, reading from, writing to, closing, seeking within, and truncating files using system calls like open(), read(), write(), close(), lseek(), ftruncate(). It also discusses related topics like file descriptors, blocking vs non-blocking I/O, synchronized I/O, direct I/O, and positional reads/writes.
This document provides an overview of the Linux operating system and how to use basic Linux commands. It explains that Linux is a free version of UNIX that is operated through a command line terminal rather than a graphical user interface. It also describes how to access the course Linux server using SSH and SFTP, navigate and manipulate files and directories using commands like ls, cd, cp, and rm, view file contents with cat and more, and get help with commands like man. Finally, it provides a list of common Linux shell commands and how to run and edit programs.
Lesson 2 Understanding Linux File SystemSadia Bashir
The document provides an overview of Linux file systems and file types. It discusses:
1) The main types of files in Linux including directories, special files, links, sockets and pipes.
2) The standard Linux directory structure and the purpose of directories like /bin, /sbin, /etc, and /usr.
3) Common Linux file extensions and hidden files that begin with a dot.
4) Environment variables and how they can be used to customize a system.
5) Symbolic links and how they create references to files without copying the actual file.
The document provides an overview of Linux operating system concepts including:
- Linux is an open source operating system that interacts with hardware and allocates resources.
- It supports multi-tasking and multi-user environments. Common types include Debian, Ubuntu, and Redhat.
- Key components include the kernel, shell programs, file management commands, text editors, browsers, and programming tools.
This document provides an overview and introduction to the hardware, software, and file structure of the EduBook device. It discusses the hardware components, how to open the case and access internal parts. It then summarizes the available operating systems, describes the Linux file structure and key directories. The document outlines software options like browsers and office applications that are preinstalled. It concludes with some tips on software issues, advanced options for running Windows programs in Wine, and contact information.
The document discusses the components of a file system in a UNIX system including the boot block, super block, inode list, and data blocks. It describes the information stored in the super block and inodes. Inodes contain metadata about files such as ownership, permissions, size, and location of data blocks. The document also covers various file-related system calls like open, read, write, close, and lseek. It provides examples of using these calls to perform operations like opening, reading, writing to files and modifying the file offset. File locking mechanisms using flock struct and fcntl calls are also explained.
This document contains an assignment submission for a 4th semester networking course at the University of Engineering and Technology in Taxila, Pakistan. The assignment was submitted by M. Ubaid Ashraf with registration number 20-CP-09 to professor Sir Adnan Mustafa. The assignment contains 18 tasks related to Linux commands like cd, ls, touch, cat etc. and includes writing a simple shell script to calculate interest and explaining the purpose of commands like kill, grep, tail etc.
The document provides information about Linux including its history, components, and usage. It can be summarized as follows:
Linux originated in 1991 when Linus Torvalds created the Linux kernel. Since then, thousands of programmers have enhanced Linux, which is now used on millions of computers worldwide. Linux is a fully-networked, multi-user, multi-tasking operating system that provides both command line and graphical interfaces. It includes core utilities like editors, compilers, and networking tools. Common distributions include Red Hat Enterprise Linux and Fedora, which provide commercial support and the latest open source technologies, respectively.
The document discusses the UNIX operating system. It describes UNIX as a stable, multi-user, multi-tasking system used for servers, desktops and laptops. It also discusses the different components that make up the UNIX system, including the kernel, shell, and programs. It explains the directory structure and file hierarchy with the root directory at the top. It provides examples of different types of files and concludes by describing some basic date and time commands in UNIX.
This document provides an overview of file administration in Linux. It describes the three types of files in Linux - ordinary disk files which contain user data, special files which represent devices, and directory files which contain other files and directories. It outlines guidelines for naming files and directories, explaining which characters to avoid. It also introduces the file command for determining a file's type and describes the basic Linux directory structure with files and directories organized in a tree format.
This document provides an introduction and overview of the UNIX operating system. It discusses that UNIX is an operating system, like Windows, that manages communication between software and hardware. Popular versions of UNIX include Linux and Android. The document outlines reasons for using UNIX such as reliability, low/no cost, and open source access. It describes the core components of UNIX including the kernel, bootloader, daemons, and shells. Key features like being portable, multi-user, and providing security are highlighted. Common UNIX commands and utilities are listed along with file permissions, redirection, pipes, and filters.
The document provides an overview of the contents of a training on the Unix and GNU/Linux command line. It covers topics such as shells and filesystem structure, file handling commands, standard input/output redirection, task control, text editing and system administration basics. The training aims to teach users full control of tasks and how to get help and find resources on the command line.
The document discusses various topics related to Unix system administration including device files, block and character devices, major and minor numbers, the Unix file system structure, partitions, file systems, inodes, mounting and unmounting file systems, disk space usage commands, compression tools, printing files, file system checking, user management commands, the role of init in startup and shutdown, run levels, and backup tools. Some key points include:
1. Device files allow all physical devices like tapes, printers, disks to be accessed as normal files.
2. Block and character devices are distinguished based on whether access is via blocks (block devices) or directly (character devices).
3. Major and minor numbers help the
This document discusses embedded Linux programming. It covers topics such as what Linux is, the layers in a Linux system including the kernel and user programs, how Linux differs from legacy real-time operating systems, and an agenda for a course on embedded Linux driver development that covers the Linux kernel, memory management, interrupts, and networking. It also provides information on basic Linux command line tools and file permissions.
This document provides a beginner's guide to learning Linux. It covers topics such as what Linux is, understanding files and folders, users and permissions, the root user, opening a terminal, basic commands like ls, cd, pwd, and tar for archiving files. The guide explains important Linux concepts and provides examples of common commands to get started using the Linux command line.
Sorting Order and Stability in Sorting.
Concept of Internal and External Sorting.
Bubble Sort,
Insertion Sort,
Selection Sort,
Quick Sort and
Merge Sort,
Radix Sort, and
Shell Sort,
External Sorting, Time complexity analysis of Sorting Algorithms.
Raish Khanji GTU 8th sem Internship Report.pdfRaishKhanji
This report details the practical experiences gained during an internship at Indo German Tool
Room, Ahmedabad. The internship provided hands-on training in various manufacturing technologies, encompassing both conventional and advanced techniques. Significant emphasis was placed on machining processes, including operation and fundamental
understanding of lathe and milling machines. Furthermore, the internship incorporated
modern welding technology, notably through the application of an Augmented Reality (AR)
simulator, offering a safe and effective environment for skill development. Exposure to
industrial automation was achieved through practical exercises in Programmable Logic Controllers (PLCs) using Siemens TIA software and direct operation of industrial robots
utilizing teach pendants. The principles and practical aspects of Computer Numerical Control
(CNC) technology were also explored. Complementing these manufacturing processes, the
internship included extensive application of SolidWorks software for design and modeling tasks. This comprehensive practical training has provided a foundational understanding of
key aspects of modern manufacturing and design, enhancing the technical proficiency and readiness for future engineering endeavors.
☁️ GDG Cloud Munich: Build With AI Workshop - Introduction to Vertex AI! ☁️
Join us for an exciting #BuildWithAi workshop on the 28th of April, 2025 at the Google Office in Munich!
Dive into the world of AI with our "Introduction to Vertex AI" session, presented by Google Cloud expert Randy Gupta.
its all about Artificial Intelligence(Ai) and Machine Learning and not on advanced level you can study before the exam or can check for some information on Ai for project
Analysis of reinforced concrete deep beam is based on simplified approximate method due to the complexity of the exact analysis. The complexity is due to a number of parameters affecting its response. To evaluate some of this parameters, finite element study of the structural behavior of the reinforced self-compacting concrete deep beam was carried out using Abaqus finite element modeling tool. The model was validated against experimental data from the literature. The parametric effects of varied concrete compressive strength, vertical web reinforcement ratio and horizontal web reinforcement ratio on the beam were tested on eight (8) different specimens under four points loads. The results of the validation work showed good agreement with the experimental studies. The parametric study revealed that the concrete compressive strength most significantly influenced the specimens’ response with the average of 41.1% and 49 % increment in the diagonal cracking and ultimate load respectively due to doubling of concrete compressive strength. Although the increase in horizontal web reinforcement ratio from 0.31 % to 0.63 % lead to average of 6.24 % increment on the diagonal cracking load, it does not influence the ultimate strength and the load-deflection response of the beams. Similar variation in vertical web reinforcement ratio leads to an average of 2.4 % and 15 % increment in cracking and ultimate load respectively with no appreciable effect on the load-deflection response.
Passenger car unit (PCU) of a vehicle type depends on vehicular characteristics, stream characteristics, roadway characteristics, environmental factors, climate conditions and control conditions. Keeping in view various factors affecting PCU, a model was developed taking a volume to capacity ratio and percentage share of particular vehicle type as independent parameters. A microscopic traffic simulation model VISSIM has been used in present study for generating traffic flow data which some time very difficult to obtain from field survey. A comparison study was carried out with the purpose of verifying when the adaptive neuro-fuzzy inference system (ANFIS), artificial neural network (ANN) and multiple linear regression (MLR) models are appropriate for prediction of PCUs of different vehicle types. From the results observed that ANFIS model estimates were closer to the corresponding simulated PCU values compared to MLR and ANN models. It is concluded that the ANFIS model showed greater potential in predicting PCUs from v/c ratio and proportional share for all type of vehicles whereas MLR and ANN models did not perform well.
Value Stream Mapping Worskshops for Intelligent Continuous SecurityMarc Hornbeek
This presentation provides detailed guidance and tools for conducting Current State and Future State Value Stream Mapping workshops for Intelligent Continuous Security.
2. In Linux system, almost every thing is treated like a file.
All input and output are done by reading or writing files, because all
peripheral devices, even keyboard and screen are files in the file system.
This means that a single homogeneous interface handles all communication
between a program and peripheral devices.
so we can use five basic functions: open, close, read, write, and ioctl for disk
files, serial ports, sockets, printers, etc.
Directories are also a special sort of files.
Every Directory or a file has a name and some properties like creation date,
user permissions, size of the file, and its location on the disk.
All these properties are stored against the file’s inode number (this no. is
unique only with in a disk partition).
Linux File Structure
3. A system uses a file’s inode number, while the directory structure names that
file for our benefit.
A directory is a file that holds the inode numbers and names of other files.
Removing the filename also removes the inode number from the directory.
To see the inode number type this command $ls -i
Using ln -s hello.c soft.c command you can make soft link to the same
file/directory in different directories.
Using ln hello.c hard.c you can make hard links to the same file in different
directories on the same partition.
Hard link his not allowed for a directory.
Hard links have the same inode number as the original file.
Linux File Structure
4. Symbolic (Soft) links (they are like short cuts in Windows) have their own
unique inode number.
You can’t create a hard link to a file on a different partition but you can create
a soft link on a different partition.
ls -l command also indicates the type of file,
The first hyphen (-) symbol indicates a regular file
l indicates a Soft Link, d indicates a Directory
p Indicates a FIFO or a Pipe
s Indicates a Unix Domain Socket.
c Indicates a Character Device (such as a terminal),
b Indicates a Block Device (such as a hard disk). Try this ls -l /dev | less
Linux File Structure
5. To mount IDE cdrom type following command
# mount -t iso9660 /dev/cdrom /mnt/cdrom
# cd /home/cdrom
To mount .iso file type following command
# mount -t iso9660 –o loop /home/file.iso /mnt/cdrom1
# cd /home/cdrom1
To mount a FAT32 partition
# mount -t vfat /dev/hda1 /mnt/winfiles
To mount a NTFS partition
# mount -t ntfs /dev/hda4 /mnt/myntfs
To unmount use # umount /mnt/cdrom
Files and Devices
To open cdrom # eject –v
To close cdrom # eject -t
6. Three important File Devices in Linux are
/dev/console: This device represents the system console (screen). Error
messages and diagnostics are often sent to this device.
/dev/tty: This device is used as virtual terminal for the user.
There’s only one /dev/console device, there are many different terminals like
tty1(first virtual terminal), tty2 etc.
/dev/null
This file is the null device. All output written to this device is discarded e.g.
$ echo do not want to see this > /dev/null
$ cp /dev/null empty_file
$ echo do not want to see this >> msg.txt
File Devices in Linux
7. Linux System
A hierarchy view of Unix system, where
various file functions exist in user space and
kernel space in the different levels, and
complete different jobs.
8. System calls: The Linux system provides its service through a set of functions
called as system calls. open, read, write, close, ioctl are the examples of
system calls.
Device drivers: The Kernel uses device drivers to interact with the hardware.
Device drivers are placed at /dev location.
Library functions: To provide a higher level interface to devices and disk files,
Linux provides a number of standard libraries, e.g.
< stdio.h >; < time.h >; < stdlib.h >; < math.h >, etc.
Using Library functions, we can avoid the penalty in making system calls.
Linux has to switch from running user code to executing the kernel code and
back again, so the system calls are more expensive than function calls.
Linux System
9. What is file descriptor?
If you read and write a file, the system will check:
1. Does this file exit?
2. Do you have permission to access it?
If all is well, it will return to the program a small non-negative integer, and this
is a file descriptor.
When a program starts, it usually has three of these descriptors already
opened. These are:
0: Standard input
1: Standard output
2: Standard error
Linux System
10. You can associate other file descriptors with files and devices by using the
open system call.
write
#include <unistd.h>
size_t write(int fildes, const void *buf, size_t nbytes);
The write system call arranges for the first nbytes bytes from buf to be written
to the file associated with the file descriptor fildes.
It returns the number of bytes actually written. If the function returns 0, it
means no data was written.
if it returns –1, there has been an error in the write call, and the error will
be specified in the errno global variable.
Low Level File Access
11. Now write your first program
This program simply prints a message to the standard output. When a
program exits, all open file descriptors are automatically closed, so you don’t
need to close them explicitly.
This won’t be the case, however, when you’re dealing with buffered output.
Now replace the code != with == and run the program again
Low Level File Access
#include <unistd.h>
#include <stdlib.h>
int main(){
if ((write(1, "Here is some datan", 18)) != 18)
write(2, "A write error has occurred on file descriptor 1 n",46);
exit(0);
}
simple_write.c
12. read
#include <unistd.h>
size_t read(int fildes, void *buf, size_t nbytes);
The read system call reads up to nbytes bytes of data from the file associated
with the file descriptor fildes and places them in the data area buf.
It returns the number of data bytes actually read, which may be less than the
number requested.
If a read call returns 0, it had nothing to read; it reached the end of the file.
Again, an error on the call will cause it to return –1.
The Next program, simple_read.c, copies the first 128 bytes of the standard
input to the standard output. It copies all of the even input if there are fewer
than 128 bytes.
Low Level File Access
13. Create some input for the program using echo, which is piped to your
program. $ echo hello there | ./simple_read
Redirect input from a file. Now first part of the file draft1.txt is appearing on
the standard output. $ ./simple_read < draft1.txt
Low Level File Access
#include <unistd.h>
#include <stdlib.h>
int main(){
char buffer[128]; int nread;
nread = read(0, buffer, 128);
if (nread == -1)
write(2, "A read error has occurredn", 26);
if ((write(1,buffer,nread)) != nread)
write(2, "A write error has occurredn",27);
exit(0);
}
simple_read.c
14. open
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
int open(const char *path, int oflags);
int open(const char *path, int oflags, mode_t mode);
To create a new file descriptor, you need to use the open system call. In simple
terms, open establishes an access path to a file or device. If successful, it
returns a file descriptor that can be used in read, write, and other system calls.
The name of the file or device to be opened is passed as a parameter, path;
the oflags parameter is used to specify actions to be taken on opening the file.
The open call must specify one of the file access modes shown in the following
table:
Low Level File Access
15. To The call may also include a combination
(using a bitwise OR) of the following optional
modes in the oflags parameter:
O_APPEND: Place written data at the end of the file.
O_TRUNC: Set the length of the file to zero, discarding existing contents.
O_CREAT: Creates the file, if necessary, with permissions given in mode.
O_EXCL: Used with O_CREAT, ensures that the caller creates the file. This
protects against two programs creating the file at the same time.
If the file already exists, open will fail.
Low Level File Access
Mode Description
O_RDONLY Open for read-only
O_WRONLY Open for write-only
O_RDWR Open for reading and writing
16. Initial Permissions
When you create a file using the O_CREAT flag with open, you must use the
three-parameter form. mode, the third parameter, is made from a bitwise OR
of the flags defined in the header file sys/stat.h. These are:
S_IRUSR: Read permission, owner
S_IWUSR: Write permission, owner
S_IXUSR: Execute permission, owner
S_IRGRP: Read permission, group
S_IWGRP: Write permission, group
S_IXGRP: Execute permission, group
S_IROTH: Read permission, others
S_IWOTH: Write permission, others
S_IXOTH: Execute permission, others
Low Level File Access
17. Permission Bits and Their Values
Bit Octal value Text value Corresponding permission
8 4 0 0 - r-- --- --- Owner may read
7 2 0 0 - -w- --- --- Owner may write
6 1 0 0 - --x --- --- Owner may execute
5 0 4 0 - --- r-- --- Group may read
4 0 2 0 - --- -w- --- Group may write
3 0 1 0 - --- --x --- Group may execute
2 0 0 4 - --- --- r-- Everyone else may read
1 0 0 2 - --- --- -w- Everyone else may write
0 0 0 1 - --- --- --x Everyone else may execute
18. Difference between File and Directory Permissions
Directory Permissions
read We can list the files and subdirectories contained in that
directory.
write We can create and remove files and subdirectories within it.
execute We can enter the directory using the cd command
File Permissions
read We can read and copy a file but can not modify or execute
write We can modify that file
execute We can execute but can’t read or modify
19. For example open ("myfile", O_CREAT, S_IRUSR|S_IXOTH);
has the effect of creating a file called myfile, with read permission for the
owner and execute permission for others, and only those permissions.
$ ls -ls myfile
0 -r-------x 1 neil software 0 Sep 22 08:11 myfile*
The umask is a system variable that encodes a mask for file permissions to be
used when a file is created.
You can change the variable by executing the umask command to supply a
new value. The value is a three-digit octal value.
Each digit is the result of ORing values from 1, 2, or 4; the meanings are
shown in the following table. The separate digits refer to “user,” “group,” and
“other” permissions, respectively.
Low Level File Access
20. Low Level File Access
Digit Value Meaning
1 0 No user (owner) permissions are to be disallowed.
4 User (owner) read permission is disallowed.
2 User (owner) write permission is disallowed.
1 User (owner) execute permission is disallowed.
2 0 No group permissions are to be disallowed.
4 Group read permission is disallowed.
2 Group write permission is disallowed.
1 Group execute permission is disallowed.
3 0 No other permissions are to be disallowed.
4 Other read permission is disallowed.
2 Other write permission is disallowed.
1 Other execute permission is disallowed.
21. For example, to block “group” write and execute, and “other” write, the
umask would be
Values for each digit are ORed together; so the second digit will need to be 2
| 1, giving 3. The resulting umask is 032.
You can also use chmod command to set the permissions of a file or a folder.
# chmod 755 a.out
You can use chown command to set the ownership of a file or a folder.
# chown nobody:nobody widgets
Low Level File Access
Digit Value Description
1 0 Owner can read = 4, write = 2 and execute = 1
2 2
1
Group can read = 4 only
3 2 Others can read = 4 and execute = 1
22. close #include <unistd.h>
int close(int fildes);
You use close to terminate the association between a file descriptor, fildes, and its file. The
file descriptor becomes available for reuse. It returns 0 if successful and –1 on error.
Ioctl #include <unistd.h>
int ioctl(int fildes, int cmd, ...);
It provides an interface for controlling the behavior of devices and their descriptors and
configuring underlying services.
ioctl performs the function indicated by cmd on the object referenced by the descriptor
fields. It may take an optional third argument, depending on the functions supported by a
particular device.
For example, the following call to ioctl on Linux turns on the keyboard LEDs:
ioctl(tty_fd, KDSETLED, LED_NUM|LED_CAP|LED_SCR);
Low Level File Access
23. Try It Out A File Copy Program
Following program, copies one file to another, character by character.
First Make a test input file, say 1Mb in size, and name it file.in then compile
copy_system.c
Low Level File Access
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
int main(){
char c; int in, out;
in = open("file.in", O_RDONLY);
out = open("file.out", O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR);
while(read(in,&c,1) == 1)
write(out,&c,1);
exit(0);
}
copy_system.c
24. Running the program will give something like the following
TIMEFORMAT="" time ./copy_system ls -ls file.in file.out
A Second File Copy Program (copy_block.c)
$ rm file.out $ TIMEFORMAT="" time ./copy_block
Low Level File Access
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
int main(){
char block[1024]; int in, out, nread;
in = open("file.in", O_RDONLY);
out = open("file.out", O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR);
while((nread = read(in,block,sizeof(block))) > 0)
write(out,block,nread);
exit(0);
}
copy_block.c
25. A Third File Copy Program (copy_stdio.c)
$ TIMEFORMAT="" time ./copy_stdio
Low Level File Access
#include <stdio.h>
#include <stdlib.h>
int main(){
int c;
FILE *in, *out;
in = fopen("file.in", "r");
out = fopen("file.out", "w");
while((c = fgetc(in)) != EOF)
fputc(c,out);
exit(0);
}
copy_stdio.c
26. Linux provides a special file system, procfs, that is usually made available as the directory
/proc.
It contains many special files that allow higher-level access to driver and kernel information.
Type this command $ ls /proc
$ cat /proc/cpuinfo // gives details of the processors available:
$ cat /proc/meminfo // gives information about memory usage
$ cat /proc/version // gives information about kernel version
$ cat /proc/net/sockstat // gives network socket usage statistics
$ cat /proc/sys/fs/file-max // tells total no of files that all running programs can open at the
same time this can be changed directly using
# echo 80000 >/proc/sys/fs/file-max
$ cat /proc/sys/fs/file-max
$ cat /proc/sys/net/ipv4/ip_forward // should it work as a router
/proc File System
27. Each process in Linux has a unique identifier: a number between 1 and about 32,000.
The ps command provides a list of currently running processes. ps -aux
PID TTY TIME CMD
9118 pts/1 00:00:00 ftp
9230 pts/1 00:00:00 ps
10689 pts/1 00:00:01 bash
The process identifier for ftp here is given as 9118, so you need to look in /proc/9118 for
details about it:
$ ls -l /proc/9118
You can tell that the program /usr/bin/pftp is running and that its current working directory
is /home/neil/BLP4e/chapter03.
$ od -c /proc/9118/cmdline
Here, you can see that ftp was started with the command line ftp 192.168.0.12
$ ls /proc/9118/fd // you can see that
ftp has open descriptors 0, 1, 2, and 3, as we might expect. These are the standard input,
output, and error descriptors plus a connection to the remote server.
/proc File System