Escolar Documentos
Profissional Documentos
Cultura Documentos
ld wc
grep
nroff
Other apps
Unix System Kernel
• Three major tasks of kernel:
Process Management
Device Management
File Management
• Three additional Services for Kernel:
Virtual Memory
Networking
Network File Systems
• Experimental Kernel Features:
Multiprocessor support
Lightweight process (thread) support
Block Diagram of System Kernel
User Programs
Libraries
User Level
Kernel System Call Interface
Level
Inter-process
File Subsystem Process communication
control
Scheduler
subsystem
Device drivers
Memory
management
hardware control
hardware
Hardware Level
Booting
• When the computer is powered on or rebooted, a short
built-in program (maybe store in ROM) reads the first
block or two of the disk into memory. These blocks
contain a loader program, which was placed on the disk
when disk is formatted.
• The loader is started. The loader searches the root
directory for /unix or /root/unix and load the file into
memory
• The kernel starts to execute.
The first processes
• The kernel initializes its internal data structures:
it constructs linked list of free inodes, regions, page tabl
• The kernel creates u area and initializes slot 0 of process
table
• Process 0 is created
• Process 0 forks, invoking the fork algorithm directly
from the Kernel. Process 1 is created.
• In kernel mode, Process 1 creates user-level context
(regions) and copy code (/etc/init) to the new region.
• Process 1 calls exec (executes init).
init process
• The init process is a process dispatcher:spawning
processes, allow users to login.
• Init reads /etc/inittab and spawns getty
• when a user login successfully, getty goes through a login
procedure and execs a login shell.
• Init executes the wait system call, monitoring the death
of its child processes and the death of orphaned processe
by exiting parent.
System Call: open
• open: A process may open a existing file to read or write
• syntax:
fd = open(pathname, mode);
A. pathname is the filename to be opened
B. mode: read/write
• Example
#include <stdio.h> $ cc openEx1.c -o openEx1
#include <sys/types.h> $ openEx1
#include <fcntl.h> Before open ...
fd1=3 fd2=4 fd3=5
main() $
{
int fd1, fd2, fd3;
printf("Before open ...\n");
fd1 = open("/etc/passwd", O_RDONLY);
fd2 = open("./openEx1.c", O_WRONLY);
fd3 = open("/etc/passwd", O_RDONLY);
printf("fd1=%d fd2=%d fd3=%d \n", fd1, fd2, fd3);
}
System Call: read
• read: A process may read an opened file
• syntax:
fd = read(fd, buffer, count);
A. fd: file descriptor
B. buffer: data to be stored in
C. count: the number (count) of byte
• Example
#include <stdio.h>
#include <sys/types.h> $ cc openEx2.c -o openEx2
#include <fcntl.h> $ openEx2
=======
main() fd1=3 buf1=root:x:0:1:Super-Us
{ fd1=3 buf2=er:/:/sbin/sh
int fd1, fd2, fd3; daemo
char buf1[20], buf2[20]; =======
buf1[19]='\0'; $
buf2[19]='\0';
printf("=======\n");
fd1 = open("/etc/passwd", O_RDONLY);
read(fd1, buf1, 19);
printf("fd1=%d buf1=%s \n",fd1, buf1);
read(fd1, buf2, 19);
printf("fd1=%d buf2=%s \n",fd1, buf2);
printf("=======\n");
}
System Call: creat
• creat: A process may create a new file by creat system
call
• syntax:
fd = write(pathname, mode);
A. pathname: file name
B. mode: read/write
Example
System Call: close
• close: A process may close a file by close system
call
• syntax:
close(fd);
A. fd: file descriptor
Example
System Call: write
• write: A process may write data to an opened file
• syntax:
fd = write(fd, buffer, count);
A. fd: file descriptor
B. buffer: data to be stored in
C. count: the number (count) of byte
• Example
/* creatEx1.c */
#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>
main()
{
int fd1;
char *buf1="I am a string\n";
char *buf2="second line\n";
printf("======\n");
fd1 = creat("./testCreat.txt", O_WRONLY);
write(fd1, buf1, 20);
write(fd1, buf2, 30);
printf("fd1=%d buf1=%s \n",fd1, buf1);
close(fd1);
chmod("./testCreat.txt", 0666);
printf("======\n");
}
Introduction
What is File System?
The abstraction used by kernel to represent and
organize the storage resources.
UNIX File System in general
File system is organized in tree structure.
File tree can be arbitrarily deep.
File name must NOT LONGER than 256 chars.
Single path name must NOT LONGER than 1023 chars.
Creating File System
Mounting File System
File tree is composed of File System
Use mount command to map a directory within the
existing file tree (mount point) to the root of the new
file system.
mount /dev/hda2 /usr
Use umount command to detach the file system.
Detaching will fail if the file system is busy.
File System / (root)
tty lp
sh ls
passwd terminfo user1
user2
Organizing of The File System (cont.)
/ The root directory
/bin or /sbin Commands for basic
system operation
/dev Device entries
/etc Critical startup and
configuration files.
/lib Library for the C
compiler
/tmp Temporary files
/var/adm or /var/log Accounting file, log
files
Types of Files
Regular Files
binary
GIF, JPEG, Executable etc.
text
scripts, program source code, documentation
Supports sequential and random access
Types of Files (cont.)
Directory
Can contain ANY kind of files
what is “.” and “..”??
Device File
Allows programs to communicate with hardware.
Kernel modules handles device management.
Types of Files (cont.)
Device Files (cont.)
Character Device
Accepts a stream of characters, without regard to any block
structure.
It is not addressable, therefore no seek operation
Block Device
Information stored in fixed-sized block
It is addressable, therefore seek operation is possible.
Types of Files (cont.)
UNIX Domain Sockets (BSD)
sockets that are local to a particular host and are
referenced through a file system object rather than a
network port.
X windows
Named Pipe
Allow processes to communicate with each other.
Types of Files (cont.)
Hard links
Linking files by reference
System maintains a count of the number of links
Does not work across file systems.
Soft links
Linking files by name
No counter is maintained
Work across file system
File Permissions
The Setuid and Setgid bits
Setuid with octal value 4000
Setgid with octal value 2000
These bits allow programs to access files that processes
that would otherwise off limits to the user that runs
them.
Types of Files (cont.)
Sticky Bit
Not very popular in today’s system
If a directory has sticky bit set, then only the owner can
remove file from the directory.
/tmp is a good example.
Types of Files (cont.)
The Permission Bit
9 permission bits used to determine 3 types of accesses,
READ, WRITE, EXECUTE.
Permission can be set based on GROUP, OWNER,
ANYONE ELSE.
Use chmod command to change permission
Binary 001 for EXECUTE
Binary 010 for WRITE
Binary 100 for READ
Types of Files (cont.)
INODES
Kernel maintains file information in a structure called
inode.
Creation, modification time stamps
Ownership, file size etc.
Examples
cc p1.c
make command
Syntax
make [-f makefile] [-eiknpqrsStuwdDPBNMOg] [names]