SlideShare une entreprise Scribd logo
1  sur  79
*
Unix System Kernel
by shehrevar davierwalaby shehrevar davierwala
*
Unix: Introduction
● Operating System: a system that manages the
resources
of a computer.
● Resources: CPUs, Memory, I/O devices, Network
● Kernel: the memory resident portion of Unix system
● File system and process control system are two major
components of Unix Kernel.
by shehrevar davierwalaby shehrevar davierwala
*
Architecture of Unix System
hardwar
e
kernel
sh who
date
ed
wc
grep
as
nroff
ld
cc
cpp
emacs
Other apps
● OS interacts directly with
the hardware
● Such OS is called
system kernel
by shehrevar davierwalaby shehrevar davierwala
*
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
by shehrevar davierwalaby shehrevar davierwala
*
Block Diagram of System Kernel
System Call Interface
File Subsystem
Inter-process
communication
Scheduler
Memory
management
Process
control
subsystem
Device drivers
hardware control
hardware
Libraries
User Programs
User Level
Kernel
Level
Hardware Level
by shehrevar davierwalaby shehrevar davierwala
*
Process Control Subsystem
● Process Synchronization
● Interprocess communication
● Memory management:
● Scheduler: process scheduling
(allocate CPU to Processes)
by shehrevar davierwalaby shehrevar davierwala
*
File subsystem
● A file system is a collection of files and directories on
a disk or tape in standard UNIX file system format.
● Kernel’s file sybsystem regulates data flow between
the kernel and secondary storage devices.
by shehrevar davierwalaby shehrevar davierwala
*
Hardware Control
● Hardware control is responsible for handling
interrupts
and for communicating with the machine.
● Devices such as disks or terminals may interrupt the
CPU while a process is executing.
● The kernel may resume execution of the interrupted
process after servicing the interrupt.
by shehrevar davierwalaby shehrevar davierwala
*
Processes
● A program is an executable file.
● A process is an instance of the program in execution.
● For example: create two active processes
$ emacs &
$ emacs &
$ ps
PID TTY TIME CMD
12893 pts/4 0:00 tcsh
12581 pts/4 0:01
emacs
12582 pts/4 0:01by shehrevar davierwalaby shehrevar davierwala
*
Processes
● A process has
● text: machine instructions
●(may be shared by other processes)
● data
● stack
● Process may execute either in user mode and in
kernel
mode.
● Process information are stored in two places:
● Process table
● User table by shehrevar davierwalaby shehrevar davierwala
*
User mode and Kernel mode
● At any given instant a computer running the Unix system
is either executing a process or the kernel itself is
running
● The computer is in user mode when it is executing
instructions in a user process and it is in kernel mode
when it is executing instructions in the kernel.
● Executing System call ==> User mode to Kernel mode
perform I/O operations
system clock interrupt
by shehrevar davierwalaby shehrevar davierwala
*
Process Table
● Process table: an entry in process table has the following
information:
● process state:
●A. running in user mode or kernel mode
●B. Ready in memory or Ready but swapped
●C. Sleep in memory or sleep and swapped
● PID: process id
● UID: user id
● scheduling information
● signals that is sent to the process but not yet handled
● a pointer to per-process-region table
● There is a single process table for the entire system
by shehrevar davierwalaby shehrevar davierwala
*
User Table (u area)
● Each process has only one private user table.
● User table contains information that must be accessible
while the process is in execution.
● A pointer to the process table slot
● parameters of the current system call, return values
error codes
● file descriptors for all open files
● current directory and current root
● process and file size limits.
● User table is an extension of the process table.
by shehrevar davierwalaby shehrevar davierwala
*
u area
Active process
resident
swappable
data
stack
text
Process
table
Per-process
region table
Region
table
Kernel
address
space
user
address
space
by shehrevar davierwalaby shehrevar davierwala
*
Shared Program Text and
Software Libraries
● Many programs, such as shell, are often being
executed by several users simultaneously.
● The text (program) part can be shared.
● In order to be shared, a program must be compiled using
a special option that arranges the process image so that
the variable part(data and stack) and the fixed part
(text)
are cleanly separated.
● An extension to the idea of sharing text is sharing
libraries.
● Without shared libraries, all the executing programsby shehrevar davierwalaby shehrevar davierwala
*
Active process
data
stack
text
Process
table
Per-process
region table
Region
table
data
stack
text
Reference
count = 2
by shehrevar davierwalaby shehrevar davierwala
*
System Call
● A process accesses system resources through system call.
● System call for
● Process Control:
fork: create a new process
wait: allow a parent process to synchronize its
execution with the exit of a child process.
exec: invoke a new program.
exit: terminate process execution
● File system:
File: open, read, write, lseek, close
inode: chdir, chown chmod, stat fstat
others: pipe dup, mount, unmount, link, unlink
by shehrevar davierwalaby shehrevar davierwala
*
System call: fork()
● fork: the only way for a user to create a process in Unix
operating system.
● The process that invokes fork is called parent process
and the newly created process is called child process.
● The syntax of fork system call:
newpid = fork();
● On return from fork system call, the two processes have
identical copies of their user-level context except for the
return value pid.
● In parent process, newpid = child process id
● In child process, newpid = 0;by shehrevar davierwalaby shehrevar davierwala
*
/* forkEx1.c */
#include <stdio.h>
main()
{
int fpid;
printf("Before forking ...n");
fpid = fork();
if (fpid == 0) {
printf("Child Process fpid=%dn", fpid);
} else {
printf("Parent Process fpid=%dn", fpid);
}
printf("After forking fpid=%dn", fpid);
}
$ cc forkEx1.c -o forkEx1
$ forkEx1
Before forking ...
Child Process fpid=0
After forking fpid=0
Parent Process fpid=14707
After forking fpid=14707
$
by shehrevar davierwalaby shehrevar davierwala
*
/* forkEx2.c */
#include <stdio.h>
main()
{
int fpid;
printf("Before forking ...n");
system("ps");
fpid = fork();
system("ps");
printf("After forking
fpid=%dn", fpid);
}
$ forkEx2
Before forking ...
PID TTY TIME CMD
14759 pts/9 0:00 tcsh
14778 pts/9 0:00 sh
14777 pts/9 0:00 forkEx2
PID TTY TIME CMD
14781 pts/9 0:00 sh
14759 pts/9 0:00 tcsh
14782 pts/9 0:00 sh
14780 pts/9 0:00 forkEx2
14777 pts/9 0:00 forkEx2
After forking fpid=14780
$ PID TTY TIME CMD
14781 pts/9 0:00 sh
14759 pts/9 0:00 tcsh
14780 pts/9 0:00 forkEx2
After forking fpid=0
$ ps
PID TTY TIME CMD
14759 pts/9 0:00 tcsh
$
by shehrevar davierwalaby shehrevar davierwala
*
System Call: getpid() getppid()
● Each process has a unique process id (PID).
● PID is an integer, typically in the range 0 through
30000.
● Kernel assigns the PID when a new process is created.
● Processes can obtain their PID by calling getpid().
● Each process has a parent process and a corresponding
parent process ID.
● Processes can obtain their parent’s PID by calling
getppid().
by shehrevar davierwalaby shehrevar davierwala
*
/* pid.c */
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
main()
{
printf("pid=%d ppid=%dn",getpid(), getppid());
}
$ cc pid.c -o pid
$ pid
pid=14935 ppid=14759
$
by shehrevar davierwalaby shehrevar davierwala
*
/* forkEx3.c */
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
main()
{
int fpid;
printf("Before forking ...n");
fpid = fork();
if (fpid == 0) {
printf("Child Process fpid=%d pid=%d ppid=%dn",
fpid, getpid(), getppid());
} else {
printf("Parent Process fpid=%d pid=%d ppid=%dn",
fpid, getpid(), getppid());
}
printf("After forking fpid=%d pid=%d ppid=%dn",
fpid, getpid(), getppid());
}
by shehrevar davierwalaby shehrevar davierwala
*
$ cc forkEx3.c -o forkEx3
$ forkEx3
Before forking ...
Parent Process fpid=14942 pid=14941 ppid=14759
After forking fpid=14942 pid=14941 ppid=14759
$ Child Process fpid=0 pid=14942 ppid=1
After forking fpid=0 pid=14942 ppid=1
$ ps
PID TTY TIME CMD
14759 pts/9 0:00 tcsh
by shehrevar davierwalaby shehrevar davierwala
*
System Call: wait()
● wait system call allows a parent process to
wait
for the demise of a child process.
● See forkEx4.c
by shehrevar davierwalaby shehrevar davierwala
*
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
main()
{
int fpid, status;
printf("Before forking ...n");
fpid = fork();
if (fpid == 0) {
printf("Child Process fpid=%d pid=%d ppid=%dn",
fpid, getpid(), getppid());
} else {
printf("Parent Process fpid=%d pid=%d ppid=%dn",
fpid, getpid(), getppid());
}
wait(&status);
printf("After forking fpid=%d pid=%d ppid=%dn",
fpid, getpid(), getppid());
}
by shehrevar davierwalaby shehrevar davierwala
*
$ cc forkEx4.c -o forkEx4
$ forkEx4
Before forking ...
Parent Process fpid=14980 pid=14979 ppid=14759
Child Process fpid=0 pid=14980 ppid=14979
After forking fpid=0 pid=14980 ppid=14979
After forking fpid=14980 pid=14979 ppid=14759
$
by shehrevar davierwalaby shehrevar davierwala
*
System Call: exec()
● exec() system call invokes another program by replacing
the current process
● No new process table entry is created for exec()
program.
Thus, the total number of processes in the system isn’t
changed.
● Six different exec functions:
execlp, execvp, execl, execv, execle, execve,
(see man page for more detail.)
● exec system call allows a process to choose its successor.
by shehrevar davierwalaby shehrevar davierwala
*
/* execEx1.c */
#include <stdio.h>
#include <unistd.h>
main()
{
printf("Before execing ...n");
execl("/bin/date", "date", 0);
printf("After execn");
}
$ execEx1
Before execing ...
Sun May 9 16:39:17 CST 1999
$ by shehrevar davierwalaby shehrevar davierwala
*
/* execEx2.c */
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
main()
{
int fpid;
printf("Before execing ...n");
fpid = fork();
if (fpid == 0) {
execl("/bin/date", "date", 0);
}
printf("After exec and fpid=%dn",fpid);
}
$ execEx2
Before execing ...
After exec and fpid=14903
$ Sun May 9 16:47:08 CST 1999
$
by shehrevar davierwalaby shehrevar davierwala
*
Handling Signal
● A signal is a message from one process to another.
● Signal are sometime called “software interrupt”
● Signals usually occur asynchronously.
● Signals can be sent
A. by one process to anther (or to itself)
B. by the kernel to a process.
● Unix signals are content-free. That is the only thing that
can be said about a signal is “it has arrived or not”
by shehrevar davierwalaby shehrevar davierwala
*
Handling Signal
● Most signals have predefined meanings:
A. sighup (HangUp): when a terminal is closed, the
hangup signal is sent to every process in control
terminal.
B. sigint (interrupt): ask politely a process to terminate.
C. sigquit (quit): ask a process to terminate and produce a
codedump.
D. sigkill (kill): force a process to terminate.
● See signEx1.c
by shehrevar davierwalaby shehrevar davierwala
*
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
main() {
int fpid, *status;
printf("Before forking ...n");
fpid = fork();
if (fpid == 0) {
printf("Child Process fpid=%d pid=%d ppid=%dn",
fpid, getpid(), getppid());
for(;;); /* loop forever */
} else {
printf("Parent Process fpid=%d pid=%d ppid=%dn",
fpid, getpid(), getppid());
}
wait(status); /* wait for child process */
printf("After forking fpid=%d pid=%d ppid=%dn",
fpid, getpid(), getppid());
}
by shehrevar davierwalaby shehrevar davierwala
*
$ cc sigEx1.c -o sigEx1
$ sigEx1 &
Before forking ...
Parent Process fpid=14989 pid=14988 ppid=14759
Child Process fpid=0 pid=14989 ppid=14988
$ ps
PID TTY TIME CMD
14988 pts/9 0:00 sigEx1
14759 pts/9 0:01 tcsh
14989 pts/9 0:09 sigEx1
$ kill -9 14989
$ ps
...
by shehrevar davierwalaby shehrevar davierwala
*
Scheduling Processes
● On a time sharing system, the kernel allocates the CPU to
a process for a period of time (time slice or time quantum)
preempts the process and schedules another one when
time slice expired, and reschedules the process to continue
execution at a later time.
● The scheduler use round-robin with multilevel feedback
algorithm to choose which process to be executed:
A. Kernel allocates the CPU to a process for a time slice.
B. preempts a process that exceeds its time slice.
C. feeds it back into one of the several priority queues.
by shehrevar davierwalaby shehrevar davierwala
*
Process Priority
swapper
wait for Disk IO
wait for buffer
wait for inode
...
wait for child
exit
User level 0
User level 1
User level n
...
Kernel Mode
User Mode
ProcessesPriority Levels
by shehrevar davierwalaby shehrevar davierwala
*
Process Scheduling
(Unix System V)
● There are 3 processes A, B, C under the following
assumptions:
A. they are created simultaneously with initial priority 60.
B. the clock interrupt the system 60 times per second.
C. these processes make no system call.
D. No other process are ready to run
E. CPU usage calculation: CPU = decay(CPU) = CPU/2
F. Process priority calculation: priority = CPU/2 + 60.
G. Rescheduling Calculation is done once per second.
by shehrevar davierwalaby shehrevar davierwala
*
Process A
Priority CPU count
Process B
Priority CPU count
Process C
Priority CPU count
60 0
…
60
75 30
67 15
63 7
…
67
76 33
60 0
60 0
…
60
75 30
67 15
63 7
...
60 0
60 0
60 0
…
60
75 30
67 15
1
2
3
4
0
by shehrevar davierwalaby shehrevar davierwala
*
Unix System Kernel
Instructors:
Fu-Chiung Cheng
( 鄭福炯 )
Associate Professor
Computer Science & Engineering
Tatung Institute of Technology
by shehrevar davierwalaby shehrevar davierwala
*
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.
by shehrevar davierwalaby shehrevar davierwala
*
The first processes
● The kernel initializes its internal data structures:
it constructs linked list of free inodes, regions, page
table
● 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).by shehrevar davierwalaby shehrevar davierwala
*
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
processes
by exiting parent.
by shehrevar davierwalaby shehrevar davierwala
*
Init fork/exec
a getty progrma
to manage the line
Getty prints
“login:” message
and
waits for someone
to login
The login process
prints the
password message,
read the password
then check the
password
The shell runs
programs for the
user unitl the
user logs off
When the shell
dies, init wakes up
and fork/exec a
getty for the line
by shehrevar davierwalaby shehrevar davierwala
*
File Subsystem
● A file system is a collection of files and directories on
a disk or tape in standard UNIX file system format.
● Each UNIX file system contains four major parts:
A. boot block:
B. superblock:
C. i-node table:
D. data block: file storage
by shehrevar davierwalaby shehrevar davierwala
*
File System Layout
Block 0: bootstrap
Block 1: superblock
Block 2
Block n
...
Block n+1
The last Block
...
Block 2 - n:i-nodes
Block n+1 - last:Files
by shehrevar davierwalaby shehrevar davierwala
*
Boot Block
● A boot block may contains several physical blocks.
● Note that a physical block contains 512 bytes
(or 1K or 2KB)
● A boot block contains a short loader program for
booting
● It is blank on other file systems.
by shehrevar davierwalaby shehrevar davierwala
*
Superblock
● Superblock contains key information about a file
system
● Superblock information:
A. Size of a file system and status:
label: name of this file system
size: the number of logic blocks
date: the last modification date of super block.
B. information of i-nodes
the number of i-nodes
the number of free i-nodes
C. information of data block: free data blocks.by shehrevar davierwalaby shehrevar davierwala
*
I-nodes
● i-node: index node (information
node)
● i-list: the list of i-nodes
● i-number: the index of i-list.
● The size of an i-node: 64 bytes.
● i-node 0 is reserved.
● i-node 1 is the root directory.
● i-node structure: next page
by shehrevar davierwalaby shehrevar davierwala
*
I-node structure
mode
owner
timestamp
Size
Block count
Direct blocks
0-9
Double indirect
Triple indirect
Single indirect
Data block
Data block
Data block
Indirect block
...
Data block
Data block
Data block
...
Indirect block
Indirect block
Indirect block
...
Reference count
by shehrevar davierwalaby shehrevar davierwala
*
I-node structure
● mode: A. type: file, directory, pipe, symbolic link
B. Access: read/write/execute (owner, group,)
● owner: who own this I-node (file, directory, ...)
● timestamp: creation, modification, access time
● size: the number of bytes
● block count: the number of data blocks
● direct blocks: pointers to the data
● single indirect: pointer to a data block which
pointers to the data blocks (128 data blocks).
● Double indirect: (128*128=16384 data blocks)
● Triple indirect: (128*128*128 data blocks)by shehrevar davierwalaby shehrevar davierwala
*
Data Block
● A data block has 512 bytes.
A. Some FS has 1K or 2k bytes per blocks.
B. See blocks size effect (next page)
● A data block may contains data of files or data of
a directory.
● File: a stream of bytes.
● Directory format:
i-# Next size File name pad
by shehrevar davierwalaby shehrevar davierwala
*
Report.txt
home
john
bin
find
alex jenny
notes
grep
i-# Next 10 Report.txt pad i-# Next 3
bin pad i-# Next 5 notes pad 0 Nextby shehrevar davierwalaby shehrevar davierwala
*
Boot Block
SuperBlock
i-node
i-node
i-node
i-node
...
...
...
Current Dir
Report.txt
source
notes
...
...
...
...
i-nodes
Data
Blocks
Report.txt
home
kc
source
find
alex
notes
grep
Device
driver
&
Hardware
control
Current
director
y
inode
u area
i-node
i-node
i-node
...
...
In-core
inodes
by shehrevar davierwalaby shehrevar davierwala
*
In-core inode table
● UNIX system keeps regular files and directories on
block
devices such as disk or tape,
● Such disk space are called physical device address space.
● The kernel deals on a logical level with file system
(logical device address space) rather than with disks.
● Disk driver can transfer logical addresses into physical
device addresses.
● In-core (memory resident) inode table stores the
inode information in kernel space.
by shehrevar davierwalaby shehrevar davierwala
*
In-core inode table
● An in-core inode contains
A. all the information of inode in disks.
B. status of in-core inode
inode is locked,
inode data changed
file data changed.
C. the logic device number of the file system.
D. inode number
E. reference count
by shehrevar davierwalaby shehrevar davierwala
*
File table
● The kernel have a global data structure, called file table,
to store information of file access.
● Each entry in file table contains:
A. a pointer to in-core inode table
B. the offset of next read or write in the file
C. access rights (r/w) allowed to the opening process.
D. reference count.
by shehrevar davierwalaby shehrevar davierwala
*
User File Descriptor table
● Each process has a user file descriptor table to identify
all opened files.
● An entry in user file descriptor table pointer to an
entry
of kernel’s global file table.
● Entry 0: standard input
● Entry 1: standard output
● Entry 2: error output
by shehrevar davierwalaby shehrevar davierwala
*
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
by shehrevar davierwalaby shehrevar davierwala
*
#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>
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);
}
$ cc openEx1.c -o openEx1
$ openEx1
Before open ...
fd1=3 fd2=4 fd3=5
$
by shehrevar davierwalaby shehrevar davierwala
*
…
CNT=2
/etc/passwd
CNT=1
./openEx2.c
in-core
inodes
Pointer
to
Descript
or table
U area
User file
descriptor
table
0
1
2
3
4
5
6
7
.
.
.
...
...
CNT=1 R
CNT=1 W
...
CNT=1 R
file table
...
...
...
by shehrevar davierwalaby shehrevar davierwala
*
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
by shehrevar davierwalaby shehrevar davierwala
*
#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>
main()
{
int fd1, fd2, fd3;
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");
}
$ cc openEx2.c -o openEx2
$ openEx2
=======
fd1=3 buf1=root:x:0:1:Super-Us
fd1=3 buf2=er:/:/sbin/sh
daemo
=======
$
by shehrevar davierwalaby shehrevar davierwala
*
#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>
main()
{
int fd1, fd2, fd3;
char buf1[20], buf2[20];
buf1[19]='0';
buf2[19]='0';
printf("======n");
fd1 = open("/etc/passwd", O_RDONLY);
fd2 = open("/etc/passwd", O_RDONLY);
read(fd1, buf1, 19);
printf("fd1=%d buf1=%s n",fd1, buf1);
read(fd2, buf2, 19);
printf("fd2=%d buf2=%s n",fd2, buf2);
printf("======n");
}
$ cc openEx3.c -o openEx3
$ openEx3
======
fd1=3 buf1=root:x:0:1:Super-Us
fd2=4 buf2=root:x:0:1:Super-Us
======
$
by shehrevar davierwalaby shehrevar davierwala
*
…
CNT=2
/etc/passwd
...
in-core
inodes
Descript
or
table
U area
User file
descriptor
table
0
1
2
3
4
5
6
7
.
.
.
...
...
CNT=1 R
...
...
CNT=1 R
file table
...
...
...
by shehrevar davierwalaby shehrevar davierwala
*
System Call: dup
● dup: copy a file descriptor into the first free slot of the
user file descriptor table.
● syntax:
newfd = dup(fd);
A. fd: file descriptor
Example
by shehrevar davierwalaby shehrevar davierwala
*
#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>
main()
{
int fd1, fd2, fd3;
char buf1[20], buf2[20];
buf1[19]='0';
buf2[19]='0';
printf("======n");
fd1 = open("/etc/passwd", O_RDONLY);
fd2 = dup(fd1);
read(fd1, buf1, 19);
printf("fd1=%d buf1=%s n",fd1, buf1);
read(fd2, buf2, 19);
printf("fd2=%d buf2=%s n",fd2, buf2);
printf("======n"); char buf1[20], buf2[20];
}
$ cc openEx4.c -o openEx4
$ openEx4
======
fd1=3 buf1=root:x:0:1:Super-Us
fd2=4 buf2=er:/:/sbin/sh
daemo
======
$
by shehrevar davierwalaby shehrevar davierwala
*
…
CNT=1
/etc/passwd
...
in-core
inodes
Descript
or
table
U area
User file
descriptor
table
0
1
2
3
4
5
6
7
.
.
.
...
...
CNT=2 R
...
...
...
file table
...
...
...
by shehrevar davierwalaby shehrevar davierwala
*
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
by shehrevar davierwalaby shehrevar davierwala
*
System Call: close
● close: A process may close a file by close system
call
● syntax:
close(fd);
A. fd: file descriptor
Example
by shehrevar davierwalaby shehrevar davierwala
*
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
by shehrevar davierwalaby shehrevar davierwala
*
/* creatEx1.c */
#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>
main()
{
int fd1;
char *buf1="I am a stringn";
char *buf2="second linen";
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");
}
by shehrevar davierwalaby shehrevar davierwala
*
$ cc creatEx1.c -o creatEx1
$ creatEx1
======
fd1=3 buf1=I am a string
======
$ ls -l testCreat.txt
-rw-rw-rw- 1 cheng staff 50 May 10 20:37 testCreat.txt
$ more testCreat.txt
...
by shehrevar davierwalaby shehrevar davierwala
*
System Call: stat/fstat
● stat/fstat: A process may query the status of a file (locked)
file type, file owner, access permission. file size, number
of links, inode number, access time.
● syntax:
stat(pathname, statbuffer); fstat(fd, statbuffer);
A. pathname: file name
B. statbuffer: read in data
C. fd: file descriptor
Example
by shehrevar davierwalaby shehrevar davierwala
*
/* statEx1.c */
#include <sys/stat.h>
main()
{
int fd1, fd2, fd3;
struct stat bufStat1, bufStat2;
char buf1[20], buf2[20];
printf("======n");
fd1 = open("/etc/passwd", O_RDONLY);
fd2 = open("./statEx1", O_RDONLY);
fstat(fd1, &bufStat1); fstat(fd2, &bufStat2);
printf("fd1=%d inode no=%d block size=%d blocks=%dn",
fd1, bufStat1.st_ino,bufStat1.st_blksize, bufStat1.st_blocks);
printf("fd2=%d inode no=%d block size=%d blocks=%dn",
fd2, bufStat2.st_ino,bufStat2.st_blksize, bufStat2.st_blocks);
printf("======n");
}
by shehrevar davierwalaby shehrevar davierwala
*
$ cc statEx1.c -o statEx1
$ statEx1
======
fd1=3 inode no=21954 block size=8192 blocks=6
fd2=4 inode no=190611 block size=8192 blocks=
======
...
by shehrevar davierwalaby shehrevar davierwala
*
System Call: link/unlink
● link: hardlink a file to another
● syntax:
link(sourceFile, targetFile); unlink(file)
A. sourceFile targetFile, file: file name
Example:
Lab exercise: write a c program which use link/unlink
system call. Use ls -l to see the reference count.
by shehrevar davierwalaby shehrevar davierwala
*
System Call: chdir
● chdir: A process may change the current directory
of a processl
● syntax:
chdir(pathname);
A. pathname: file name
Example
by shehrevar davierwalaby shehrevar davierwala
*
#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>
main()
{
chdir("/usr/bin");
system("ls -l");
}
$ ls -l /usr/bin
$
by shehrevar davierwalaby shehrevar davierwala
*
End of
System Kernel
Lecture
by shehrevar davierwalaby shehrevar davierwala

Contenu connexe

Tendances

Unix operating system basics
Unix operating system basicsUnix operating system basics
Unix operating system basicsSankar Suriya
 
Linux advanced concepts - Part 1
Linux advanced concepts - Part 1Linux advanced concepts - Part 1
Linux advanced concepts - Part 1NAILBITER
 
Linux Internals - Kernel/Core
Linux Internals - Kernel/CoreLinux Internals - Kernel/Core
Linux Internals - Kernel/CoreShay Cohen
 
Unix operating system
Unix operating systemUnix operating system
Unix operating systemABhay Panchal
 
Linux kernel Architecture and Properties
Linux kernel Architecture and PropertiesLinux kernel Architecture and Properties
Linux kernel Architecture and PropertiesSaadi Rahman
 
Introduction to char device driver
Introduction to char device driverIntroduction to char device driver
Introduction to char device driverVandana Salve
 
Kernel module programming
Kernel module programmingKernel module programming
Kernel module programmingVandana Salve
 
Unix Internals OS Architecture
Unix Internals OS ArchitectureUnix Internals OS Architecture
Unix Internals OS ArchitectureKhader Shaik
 
The Linux Kernel Implementation of Pipes and FIFOs
The Linux Kernel Implementation of Pipes and FIFOsThe Linux Kernel Implementation of Pipes and FIFOs
The Linux Kernel Implementation of Pipes and FIFOsDivye Kapoor
 
Introduction to Unix operating system Chapter 1-PPT Mrs.Sowmya Jyothi
Introduction to Unix operating system Chapter 1-PPT Mrs.Sowmya JyothiIntroduction to Unix operating system Chapter 1-PPT Mrs.Sowmya Jyothi
Introduction to Unix operating system Chapter 1-PPT Mrs.Sowmya JyothiSowmya Jyothi
 
The structure of process
The structure of processThe structure of process
The structure of processAbhaysinh Surve
 
Linux Char Device Driver
Linux Char Device DriverLinux Char Device Driver
Linux Char Device DriverGary Yeh
 
Architecture Of The Linux Kernel
Architecture Of The Linux KernelArchitecture Of The Linux Kernel
Architecture Of The Linux Kernelguest547d74
 

Tendances (20)

Introduction to UNIX
Introduction to UNIXIntroduction to UNIX
Introduction to UNIX
 
Linux kernel architecture
Linux kernel architectureLinux kernel architecture
Linux kernel architecture
 
Unix operating system basics
Unix operating system basicsUnix operating system basics
Unix operating system basics
 
Linux advanced concepts - Part 1
Linux advanced concepts - Part 1Linux advanced concepts - Part 1
Linux advanced concepts - Part 1
 
Linux introduction
Linux introductionLinux introduction
Linux introduction
 
Linux Internals - Kernel/Core
Linux Internals - Kernel/CoreLinux Internals - Kernel/Core
Linux Internals - Kernel/Core
 
Unix operating system
Unix operating systemUnix operating system
Unix operating system
 
Linux kernel Architecture and Properties
Linux kernel Architecture and PropertiesLinux kernel Architecture and Properties
Linux kernel Architecture and Properties
 
Introduction to char device driver
Introduction to char device driverIntroduction to char device driver
Introduction to char device driver
 
Linux architecture
Linux architectureLinux architecture
Linux architecture
 
Process management
Process managementProcess management
Process management
 
Basic Linux Internals
Basic Linux InternalsBasic Linux Internals
Basic Linux Internals
 
Kernel module programming
Kernel module programmingKernel module programming
Kernel module programming
 
Unix Internals OS Architecture
Unix Internals OS ArchitectureUnix Internals OS Architecture
Unix Internals OS Architecture
 
LINUX Device Drivers
LINUX Device DriversLINUX Device Drivers
LINUX Device Drivers
 
The Linux Kernel Implementation of Pipes and FIFOs
The Linux Kernel Implementation of Pipes and FIFOsThe Linux Kernel Implementation of Pipes and FIFOs
The Linux Kernel Implementation of Pipes and FIFOs
 
Introduction to Unix operating system Chapter 1-PPT Mrs.Sowmya Jyothi
Introduction to Unix operating system Chapter 1-PPT Mrs.Sowmya JyothiIntroduction to Unix operating system Chapter 1-PPT Mrs.Sowmya Jyothi
Introduction to Unix operating system Chapter 1-PPT Mrs.Sowmya Jyothi
 
The structure of process
The structure of processThe structure of process
The structure of process
 
Linux Char Device Driver
Linux Char Device DriverLinux Char Device Driver
Linux Char Device Driver
 
Architecture Of The Linux Kernel
Architecture Of The Linux KernelArchitecture Of The Linux Kernel
Architecture Of The Linux Kernel
 

En vedette

Unix processes
Unix processesUnix processes
Unix processesSunil Rm
 
Unit 8
Unit 8Unit 8
Unit 8siddr
 
AOS Lab 2: Hello, xv6!
AOS Lab 2: Hello, xv6!AOS Lab 2: Hello, xv6!
AOS Lab 2: Hello, xv6!Zubair Nabi
 
Unit 1
Unit 1Unit 1
Unit 1siddr
 
Unit 3
Unit  3Unit  3
Unit 3siddr
 
What is a smartphone-R1
What is a smartphone-R1What is a smartphone-R1
What is a smartphone-R1Yong Heui Cho
 
Android power management
Android power managementAndroid power management
Android power managementJerrin George
 
Smartphone Hardware Architecture
Smartphone Hardware ArchitectureSmartphone Hardware Architecture
Smartphone Hardware ArchitectureYong Heui Cho
 
Introduction to Operating System (Important Notes)
Introduction to Operating System (Important Notes)Introduction to Operating System (Important Notes)
Introduction to Operating System (Important Notes)Gaurav Kakade
 
Unix operating system
Unix operating systemUnix operating system
Unix operating systemmidhunjose4u
 
Processes Control Block (Operating System)
Processes Control Block (Operating System)Processes Control Block (Operating System)
Processes Control Block (Operating System)Imdad Ullah
 
android architecture
android architectureandroid architecture
android architectureAashita Gupta
 
Operating system concepts (notes)
Operating system concepts (notes)Operating system concepts (notes)
Operating system concepts (notes)Sohaib Danish
 
Unix Operating System
Unix Operating SystemUnix Operating System
Unix Operating Systemsubhsikha
 
Creating HTML Pages
Creating HTML PagesCreating HTML Pages
Creating HTML PagesMike Crabb
 
What is a Kernel? : Introduction And Architecture
What is a Kernel? : Introduction And ArchitectureWhat is a Kernel? : Introduction And Architecture
What is a Kernel? : Introduction And Architecturepec2013
 

En vedette (20)

Unix processes
Unix processesUnix processes
Unix processes
 
Unit 8
Unit 8Unit 8
Unit 8
 
NTFS and Inode
NTFS and InodeNTFS and Inode
NTFS and Inode
 
AOS Lab 2: Hello, xv6!
AOS Lab 2: Hello, xv6!AOS Lab 2: Hello, xv6!
AOS Lab 2: Hello, xv6!
 
Unit 1
Unit 1Unit 1
Unit 1
 
How inodes Work
How inodes WorkHow inodes Work
How inodes Work
 
Unit 3
Unit  3Unit  3
Unit 3
 
What is a smartphone-R1
What is a smartphone-R1What is a smartphone-R1
What is a smartphone-R1
 
Android power management
Android power managementAndroid power management
Android power management
 
Smartphone Hardware Architecture
Smartphone Hardware ArchitectureSmartphone Hardware Architecture
Smartphone Hardware Architecture
 
Introduction to Operating System (Important Notes)
Introduction to Operating System (Important Notes)Introduction to Operating System (Important Notes)
Introduction to Operating System (Important Notes)
 
Unix operating system
Unix operating systemUnix operating system
Unix operating system
 
Operating system notes pdf
Operating system notes pdfOperating system notes pdf
Operating system notes pdf
 
Processes Control Block (Operating System)
Processes Control Block (Operating System)Processes Control Block (Operating System)
Processes Control Block (Operating System)
 
android architecture
android architectureandroid architecture
android architecture
 
Operating system concepts (notes)
Operating system concepts (notes)Operating system concepts (notes)
Operating system concepts (notes)
 
Unix Operating System
Unix Operating SystemUnix Operating System
Unix Operating System
 
Creating HTML Pages
Creating HTML PagesCreating HTML Pages
Creating HTML Pages
 
What is a Kernel? : Introduction And Architecture
What is a Kernel? : Introduction And ArchitectureWhat is a Kernel? : Introduction And Architecture
What is a Kernel? : Introduction And Architecture
 
How We Caffeinate
How We CaffeinateHow We Caffeinate
How We Caffeinate
 

Similaire à Unix System Kernel Functions and Components

OSTEP Chapter2 Introduction
OSTEP Chapter2 IntroductionOSTEP Chapter2 Introduction
OSTEP Chapter2 IntroductionShuya Osaki
 
Tuning parallelcodeonsolaris005
Tuning parallelcodeonsolaris005Tuning parallelcodeonsolaris005
Tuning parallelcodeonsolaris005dflexer
 
Lecture2 process structure and programming
Lecture2   process structure and programmingLecture2   process structure and programming
Lecture2 process structure and programmingMohammed Farrag
 
Performance Analysis Tools for Linux Kernel
Performance Analysis Tools for Linux KernelPerformance Analysis Tools for Linux Kernel
Performance Analysis Tools for Linux Kernellcplcp1
 
Systems Programming Assignment Help - Processes
Systems Programming Assignment Help - ProcessesSystems Programming Assignment Help - Processes
Systems Programming Assignment Help - ProcessesHelpWithAssignment.com
 
System Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbs
System Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbsSystem Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbs
System Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbsashukiller7
 
Lessons Learned: Running InfluxDB Cloud and Other Cloud Services at Scale | T...
Lessons Learned: Running InfluxDB Cloud and Other Cloud Services at Scale | T...Lessons Learned: Running InfluxDB Cloud and Other Cloud Services at Scale | T...
Lessons Learned: Running InfluxDB Cloud and Other Cloud Services at Scale | T...InfluxData
 
Linux kernel tracing superpowers in the cloud
Linux kernel tracing superpowers in the cloudLinux kernel tracing superpowers in the cloud
Linux kernel tracing superpowers in the cloudAndrea Righi
 
Perf stat windows
Perf stat windowsPerf stat windows
Perf stat windowsAccenture
 
Advanced Operating Systems......Process Management
Advanced Operating Systems......Process ManagementAdvanced Operating Systems......Process Management
Advanced Operating Systems......Process ManagementVeejeya Kumbhar
 
Lecture_Slide_4.pptx
Lecture_Slide_4.pptxLecture_Slide_4.pptx
Lecture_Slide_4.pptxDiptoRoy21
 
PHP & Performance
PHP & PerformancePHP & Performance
PHP & Performance毅 吕
 

Similaire à Unix System Kernel Functions and Components (20)

Os lab final
Os lab finalOs lab final
Os lab final
 
OSTEP Chapter2 Introduction
OSTEP Chapter2 IntroductionOSTEP Chapter2 Introduction
OSTEP Chapter2 Introduction
 
Linux Internals - Part II
Linux Internals - Part IILinux Internals - Part II
Linux Internals - Part II
 
LP-Unit3.docx
LP-Unit3.docxLP-Unit3.docx
LP-Unit3.docx
 
Tuning parallelcodeonsolaris005
Tuning parallelcodeonsolaris005Tuning parallelcodeonsolaris005
Tuning parallelcodeonsolaris005
 
Docker.io
Docker.ioDocker.io
Docker.io
 
Lecture2 process structure and programming
Lecture2   process structure and programmingLecture2   process structure and programming
Lecture2 process structure and programming
 
Performance Analysis Tools for Linux Kernel
Performance Analysis Tools for Linux KernelPerformance Analysis Tools for Linux Kernel
Performance Analysis Tools for Linux Kernel
 
Systems Programming Assignment Help - Processes
Systems Programming Assignment Help - ProcessesSystems Programming Assignment Help - Processes
Systems Programming Assignment Help - Processes
 
System Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbs
System Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbsSystem Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbs
System Calls.pptxnsjsnssbhsbbebdbdbshshsbshsbbs
 
A22 Introduction to DTrace by Kyle Hailey
A22 Introduction to DTrace by Kyle HaileyA22 Introduction to DTrace by Kyle Hailey
A22 Introduction to DTrace by Kyle Hailey
 
Lecture 5 process concept
Lecture 5   process conceptLecture 5   process concept
Lecture 5 process concept
 
Lessons Learned: Running InfluxDB Cloud and Other Cloud Services at Scale | T...
Lessons Learned: Running InfluxDB Cloud and Other Cloud Services at Scale | T...Lessons Learned: Running InfluxDB Cloud and Other Cloud Services at Scale | T...
Lessons Learned: Running InfluxDB Cloud and Other Cloud Services at Scale | T...
 
Linux kernel tracing superpowers in the cloud
Linux kernel tracing superpowers in the cloudLinux kernel tracing superpowers in the cloud
Linux kernel tracing superpowers in the cloud
 
Perf stat windows
Perf stat windowsPerf stat windows
Perf stat windows
 
Advanced Operating Systems......Process Management
Advanced Operating Systems......Process ManagementAdvanced Operating Systems......Process Management
Advanced Operating Systems......Process Management
 
linux installation.pdf
linux installation.pdflinux installation.pdf
linux installation.pdf
 
An Introduction To Linux
An Introduction To LinuxAn Introduction To Linux
An Introduction To Linux
 
Lecture_Slide_4.pptx
Lecture_Slide_4.pptxLecture_Slide_4.pptx
Lecture_Slide_4.pptx
 
PHP & Performance
PHP & PerformancePHP & Performance
PHP & Performance
 

Plus de Shehrevar Davierwala

Plus de Shehrevar Davierwala (20)

Introduction_Swift
Introduction_SwiftIntroduction_Swift
Introduction_Swift
 
PsudoCode.pptx
PsudoCode.pptxPsudoCode.pptx
PsudoCode.pptx
 
Number System.pptx
Number System.pptxNumber System.pptx
Number System.pptx
 
Java Script (Module 1).pptx
Java Script (Module 1).pptxJava Script (Module 1).pptx
Java Script (Module 1).pptx
 
Website in Clicks Day 2
Website in Clicks Day 2Website in Clicks Day 2
Website in Clicks Day 2
 
Develop Website in Clicks
Develop Website in ClicksDevelop Website in Clicks
Develop Website in Clicks
 
Build Virtual Assistant Using AI
Build Virtual Assistant Using AI Build Virtual Assistant Using AI
Build Virtual Assistant Using AI
 
Build brand reputation using facebook
Build brand reputation using facebookBuild brand reputation using facebook
Build brand reputation using facebook
 
Digital Marketing Session 2
Digital Marketing Session 2Digital Marketing Session 2
Digital Marketing Session 2
 
Learn Digital Marketing : 0 to Hero Day 1
Learn Digital Marketing :  0 to Hero Day 1 Learn Digital Marketing :  0 to Hero Day 1
Learn Digital Marketing : 0 to Hero Day 1
 
Standard template
Standard templateStandard template
Standard template
 
Digital Marketing for Sustainable Business - Afghan Perspective
Digital Marketing for Sustainable Business - Afghan Perspective  Digital Marketing for Sustainable Business - Afghan Perspective
Digital Marketing for Sustainable Business - Afghan Perspective
 
Developing stunning website in clicks - 2
Developing stunning website in clicks - 2Developing stunning website in clicks - 2
Developing stunning website in clicks - 2
 
Developing stunning website in clicks
Developing stunning website in clicksDeveloping stunning website in clicks
Developing stunning website in clicks
 
Google forms for data analysis
Google forms for data analysisGoogle forms for data analysis
Google forms for data analysis
 
Webdesign session1
Webdesign session1Webdesign session1
Webdesign session1
 
Tech talk webtech
Tech talk webtechTech talk webtech
Tech talk webtech
 
Tech talk php_cms
Tech talk php_cmsTech talk php_cms
Tech talk php_cms
 
Ph pbasics
Ph pbasicsPh pbasics
Ph pbasics
 
Php mysql
Php mysqlPhp mysql
Php mysql
 

Dernier

Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
FILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipinoFILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipinojohnmickonozaleda
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 

Dernier (20)

Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
FILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipinoFILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipino
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 

Unix System Kernel Functions and Components

  • 1. * Unix System Kernel by shehrevar davierwalaby shehrevar davierwala
  • 2. * Unix: Introduction ● Operating System: a system that manages the resources of a computer. ● Resources: CPUs, Memory, I/O devices, Network ● Kernel: the memory resident portion of Unix system ● File system and process control system are two major components of Unix Kernel. by shehrevar davierwalaby shehrevar davierwala
  • 3. * Architecture of Unix System hardwar e kernel sh who date ed wc grep as nroff ld cc cpp emacs Other apps ● OS interacts directly with the hardware ● Such OS is called system kernel by shehrevar davierwalaby shehrevar davierwala
  • 4. * 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 by shehrevar davierwalaby shehrevar davierwala
  • 5. * Block Diagram of System Kernel System Call Interface File Subsystem Inter-process communication Scheduler Memory management Process control subsystem Device drivers hardware control hardware Libraries User Programs User Level Kernel Level Hardware Level by shehrevar davierwalaby shehrevar davierwala
  • 6. * Process Control Subsystem ● Process Synchronization ● Interprocess communication ● Memory management: ● Scheduler: process scheduling (allocate CPU to Processes) by shehrevar davierwalaby shehrevar davierwala
  • 7. * File subsystem ● A file system is a collection of files and directories on a disk or tape in standard UNIX file system format. ● Kernel’s file sybsystem regulates data flow between the kernel and secondary storage devices. by shehrevar davierwalaby shehrevar davierwala
  • 8. * Hardware Control ● Hardware control is responsible for handling interrupts and for communicating with the machine. ● Devices such as disks or terminals may interrupt the CPU while a process is executing. ● The kernel may resume execution of the interrupted process after servicing the interrupt. by shehrevar davierwalaby shehrevar davierwala
  • 9. * Processes ● A program is an executable file. ● A process is an instance of the program in execution. ● For example: create two active processes $ emacs & $ emacs & $ ps PID TTY TIME CMD 12893 pts/4 0:00 tcsh 12581 pts/4 0:01 emacs 12582 pts/4 0:01by shehrevar davierwalaby shehrevar davierwala
  • 10. * Processes ● A process has ● text: machine instructions ●(may be shared by other processes) ● data ● stack ● Process may execute either in user mode and in kernel mode. ● Process information are stored in two places: ● Process table ● User table by shehrevar davierwalaby shehrevar davierwala
  • 11. * User mode and Kernel mode ● At any given instant a computer running the Unix system is either executing a process or the kernel itself is running ● The computer is in user mode when it is executing instructions in a user process and it is in kernel mode when it is executing instructions in the kernel. ● Executing System call ==> User mode to Kernel mode perform I/O operations system clock interrupt by shehrevar davierwalaby shehrevar davierwala
  • 12. * Process Table ● Process table: an entry in process table has the following information: ● process state: ●A. running in user mode or kernel mode ●B. Ready in memory or Ready but swapped ●C. Sleep in memory or sleep and swapped ● PID: process id ● UID: user id ● scheduling information ● signals that is sent to the process but not yet handled ● a pointer to per-process-region table ● There is a single process table for the entire system by shehrevar davierwalaby shehrevar davierwala
  • 13. * User Table (u area) ● Each process has only one private user table. ● User table contains information that must be accessible while the process is in execution. ● A pointer to the process table slot ● parameters of the current system call, return values error codes ● file descriptors for all open files ● current directory and current root ● process and file size limits. ● User table is an extension of the process table. by shehrevar davierwalaby shehrevar davierwala
  • 14. * u area Active process resident swappable data stack text Process table Per-process region table Region table Kernel address space user address space by shehrevar davierwalaby shehrevar davierwala
  • 15. * Shared Program Text and Software Libraries ● Many programs, such as shell, are often being executed by several users simultaneously. ● The text (program) part can be shared. ● In order to be shared, a program must be compiled using a special option that arranges the process image so that the variable part(data and stack) and the fixed part (text) are cleanly separated. ● An extension to the idea of sharing text is sharing libraries. ● Without shared libraries, all the executing programsby shehrevar davierwalaby shehrevar davierwala
  • 17. * System Call ● A process accesses system resources through system call. ● System call for ● Process Control: fork: create a new process wait: allow a parent process to synchronize its execution with the exit of a child process. exec: invoke a new program. exit: terminate process execution ● File system: File: open, read, write, lseek, close inode: chdir, chown chmod, stat fstat others: pipe dup, mount, unmount, link, unlink by shehrevar davierwalaby shehrevar davierwala
  • 18. * System call: fork() ● fork: the only way for a user to create a process in Unix operating system. ● The process that invokes fork is called parent process and the newly created process is called child process. ● The syntax of fork system call: newpid = fork(); ● On return from fork system call, the two processes have identical copies of their user-level context except for the return value pid. ● In parent process, newpid = child process id ● In child process, newpid = 0;by shehrevar davierwalaby shehrevar davierwala
  • 19. * /* forkEx1.c */ #include <stdio.h> main() { int fpid; printf("Before forking ...n"); fpid = fork(); if (fpid == 0) { printf("Child Process fpid=%dn", fpid); } else { printf("Parent Process fpid=%dn", fpid); } printf("After forking fpid=%dn", fpid); } $ cc forkEx1.c -o forkEx1 $ forkEx1 Before forking ... Child Process fpid=0 After forking fpid=0 Parent Process fpid=14707 After forking fpid=14707 $ by shehrevar davierwalaby shehrevar davierwala
  • 20. * /* forkEx2.c */ #include <stdio.h> main() { int fpid; printf("Before forking ...n"); system("ps"); fpid = fork(); system("ps"); printf("After forking fpid=%dn", fpid); } $ forkEx2 Before forking ... PID TTY TIME CMD 14759 pts/9 0:00 tcsh 14778 pts/9 0:00 sh 14777 pts/9 0:00 forkEx2 PID TTY TIME CMD 14781 pts/9 0:00 sh 14759 pts/9 0:00 tcsh 14782 pts/9 0:00 sh 14780 pts/9 0:00 forkEx2 14777 pts/9 0:00 forkEx2 After forking fpid=14780 $ PID TTY TIME CMD 14781 pts/9 0:00 sh 14759 pts/9 0:00 tcsh 14780 pts/9 0:00 forkEx2 After forking fpid=0 $ ps PID TTY TIME CMD 14759 pts/9 0:00 tcsh $ by shehrevar davierwalaby shehrevar davierwala
  • 21. * System Call: getpid() getppid() ● Each process has a unique process id (PID). ● PID is an integer, typically in the range 0 through 30000. ● Kernel assigns the PID when a new process is created. ● Processes can obtain their PID by calling getpid(). ● Each process has a parent process and a corresponding parent process ID. ● Processes can obtain their parent’s PID by calling getppid(). by shehrevar davierwalaby shehrevar davierwala
  • 22. * /* pid.c */ #include <stdio.h> #include <sys/types.h> #include <unistd.h> main() { printf("pid=%d ppid=%dn",getpid(), getppid()); } $ cc pid.c -o pid $ pid pid=14935 ppid=14759 $ by shehrevar davierwalaby shehrevar davierwala
  • 23. * /* forkEx3.c */ #include <stdio.h> #include <sys/types.h> #include <unistd.h> main() { int fpid; printf("Before forking ...n"); fpid = fork(); if (fpid == 0) { printf("Child Process fpid=%d pid=%d ppid=%dn", fpid, getpid(), getppid()); } else { printf("Parent Process fpid=%d pid=%d ppid=%dn", fpid, getpid(), getppid()); } printf("After forking fpid=%d pid=%d ppid=%dn", fpid, getpid(), getppid()); } by shehrevar davierwalaby shehrevar davierwala
  • 24. * $ cc forkEx3.c -o forkEx3 $ forkEx3 Before forking ... Parent Process fpid=14942 pid=14941 ppid=14759 After forking fpid=14942 pid=14941 ppid=14759 $ Child Process fpid=0 pid=14942 ppid=1 After forking fpid=0 pid=14942 ppid=1 $ ps PID TTY TIME CMD 14759 pts/9 0:00 tcsh by shehrevar davierwalaby shehrevar davierwala
  • 25. * System Call: wait() ● wait system call allows a parent process to wait for the demise of a child process. ● See forkEx4.c by shehrevar davierwalaby shehrevar davierwala
  • 26. * #include <stdio.h> #include <sys/types.h> #include <unistd.h> main() { int fpid, status; printf("Before forking ...n"); fpid = fork(); if (fpid == 0) { printf("Child Process fpid=%d pid=%d ppid=%dn", fpid, getpid(), getppid()); } else { printf("Parent Process fpid=%d pid=%d ppid=%dn", fpid, getpid(), getppid()); } wait(&status); printf("After forking fpid=%d pid=%d ppid=%dn", fpid, getpid(), getppid()); } by shehrevar davierwalaby shehrevar davierwala
  • 27. * $ cc forkEx4.c -o forkEx4 $ forkEx4 Before forking ... Parent Process fpid=14980 pid=14979 ppid=14759 Child Process fpid=0 pid=14980 ppid=14979 After forking fpid=0 pid=14980 ppid=14979 After forking fpid=14980 pid=14979 ppid=14759 $ by shehrevar davierwalaby shehrevar davierwala
  • 28. * System Call: exec() ● exec() system call invokes another program by replacing the current process ● No new process table entry is created for exec() program. Thus, the total number of processes in the system isn’t changed. ● Six different exec functions: execlp, execvp, execl, execv, execle, execve, (see man page for more detail.) ● exec system call allows a process to choose its successor. by shehrevar davierwalaby shehrevar davierwala
  • 29. * /* execEx1.c */ #include <stdio.h> #include <unistd.h> main() { printf("Before execing ...n"); execl("/bin/date", "date", 0); printf("After execn"); } $ execEx1 Before execing ... Sun May 9 16:39:17 CST 1999 $ by shehrevar davierwalaby shehrevar davierwala
  • 30. * /* execEx2.c */ #include <sys/types.h> #include <unistd.h> #include <stdio.h> main() { int fpid; printf("Before execing ...n"); fpid = fork(); if (fpid == 0) { execl("/bin/date", "date", 0); } printf("After exec and fpid=%dn",fpid); } $ execEx2 Before execing ... After exec and fpid=14903 $ Sun May 9 16:47:08 CST 1999 $ by shehrevar davierwalaby shehrevar davierwala
  • 31. * Handling Signal ● A signal is a message from one process to another. ● Signal are sometime called “software interrupt” ● Signals usually occur asynchronously. ● Signals can be sent A. by one process to anther (or to itself) B. by the kernel to a process. ● Unix signals are content-free. That is the only thing that can be said about a signal is “it has arrived or not” by shehrevar davierwalaby shehrevar davierwala
  • 32. * Handling Signal ● Most signals have predefined meanings: A. sighup (HangUp): when a terminal is closed, the hangup signal is sent to every process in control terminal. B. sigint (interrupt): ask politely a process to terminate. C. sigquit (quit): ask a process to terminate and produce a codedump. D. sigkill (kill): force a process to terminate. ● See signEx1.c by shehrevar davierwalaby shehrevar davierwala
  • 33. * #include <stdio.h> #include <sys/types.h> #include <unistd.h> main() { int fpid, *status; printf("Before forking ...n"); fpid = fork(); if (fpid == 0) { printf("Child Process fpid=%d pid=%d ppid=%dn", fpid, getpid(), getppid()); for(;;); /* loop forever */ } else { printf("Parent Process fpid=%d pid=%d ppid=%dn", fpid, getpid(), getppid()); } wait(status); /* wait for child process */ printf("After forking fpid=%d pid=%d ppid=%dn", fpid, getpid(), getppid()); } by shehrevar davierwalaby shehrevar davierwala
  • 34. * $ cc sigEx1.c -o sigEx1 $ sigEx1 & Before forking ... Parent Process fpid=14989 pid=14988 ppid=14759 Child Process fpid=0 pid=14989 ppid=14988 $ ps PID TTY TIME CMD 14988 pts/9 0:00 sigEx1 14759 pts/9 0:01 tcsh 14989 pts/9 0:09 sigEx1 $ kill -9 14989 $ ps ... by shehrevar davierwalaby shehrevar davierwala
  • 35. * Scheduling Processes ● On a time sharing system, the kernel allocates the CPU to a process for a period of time (time slice or time quantum) preempts the process and schedules another one when time slice expired, and reschedules the process to continue execution at a later time. ● The scheduler use round-robin with multilevel feedback algorithm to choose which process to be executed: A. Kernel allocates the CPU to a process for a time slice. B. preempts a process that exceeds its time slice. C. feeds it back into one of the several priority queues. by shehrevar davierwalaby shehrevar davierwala
  • 36. * Process Priority swapper wait for Disk IO wait for buffer wait for inode ... wait for child exit User level 0 User level 1 User level n ... Kernel Mode User Mode ProcessesPriority Levels by shehrevar davierwalaby shehrevar davierwala
  • 37. * Process Scheduling (Unix System V) ● There are 3 processes A, B, C under the following assumptions: A. they are created simultaneously with initial priority 60. B. the clock interrupt the system 60 times per second. C. these processes make no system call. D. No other process are ready to run E. CPU usage calculation: CPU = decay(CPU) = CPU/2 F. Process priority calculation: priority = CPU/2 + 60. G. Rescheduling Calculation is done once per second. by shehrevar davierwalaby shehrevar davierwala
  • 38. * Process A Priority CPU count Process B Priority CPU count Process C Priority CPU count 60 0 … 60 75 30 67 15 63 7 … 67 76 33 60 0 60 0 … 60 75 30 67 15 63 7 ... 60 0 60 0 60 0 … 60 75 30 67 15 1 2 3 4 0 by shehrevar davierwalaby shehrevar davierwala
  • 39. * Unix System Kernel Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology by shehrevar davierwalaby shehrevar davierwala
  • 40. * 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. by shehrevar davierwalaby shehrevar davierwala
  • 41. * The first processes ● The kernel initializes its internal data structures: it constructs linked list of free inodes, regions, page table ● 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).by shehrevar davierwalaby shehrevar davierwala
  • 42. * 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 processes by exiting parent. by shehrevar davierwalaby shehrevar davierwala
  • 43. * Init fork/exec a getty progrma to manage the line Getty prints “login:” message and waits for someone to login The login process prints the password message, read the password then check the password The shell runs programs for the user unitl the user logs off When the shell dies, init wakes up and fork/exec a getty for the line by shehrevar davierwalaby shehrevar davierwala
  • 44. * File Subsystem ● A file system is a collection of files and directories on a disk or tape in standard UNIX file system format. ● Each UNIX file system contains four major parts: A. boot block: B. superblock: C. i-node table: D. data block: file storage by shehrevar davierwalaby shehrevar davierwala
  • 45. * File System Layout Block 0: bootstrap Block 1: superblock Block 2 Block n ... Block n+1 The last Block ... Block 2 - n:i-nodes Block n+1 - last:Files by shehrevar davierwalaby shehrevar davierwala
  • 46. * Boot Block ● A boot block may contains several physical blocks. ● Note that a physical block contains 512 bytes (or 1K or 2KB) ● A boot block contains a short loader program for booting ● It is blank on other file systems. by shehrevar davierwalaby shehrevar davierwala
  • 47. * Superblock ● Superblock contains key information about a file system ● Superblock information: A. Size of a file system and status: label: name of this file system size: the number of logic blocks date: the last modification date of super block. B. information of i-nodes the number of i-nodes the number of free i-nodes C. information of data block: free data blocks.by shehrevar davierwalaby shehrevar davierwala
  • 48. * I-nodes ● i-node: index node (information node) ● i-list: the list of i-nodes ● i-number: the index of i-list. ● The size of an i-node: 64 bytes. ● i-node 0 is reserved. ● i-node 1 is the root directory. ● i-node structure: next page by shehrevar davierwalaby shehrevar davierwala
  • 49. * I-node structure mode owner timestamp Size Block count Direct blocks 0-9 Double indirect Triple indirect Single indirect Data block Data block Data block Indirect block ... Data block Data block Data block ... Indirect block Indirect block Indirect block ... Reference count by shehrevar davierwalaby shehrevar davierwala
  • 50. * I-node structure ● mode: A. type: file, directory, pipe, symbolic link B. Access: read/write/execute (owner, group,) ● owner: who own this I-node (file, directory, ...) ● timestamp: creation, modification, access time ● size: the number of bytes ● block count: the number of data blocks ● direct blocks: pointers to the data ● single indirect: pointer to a data block which pointers to the data blocks (128 data blocks). ● Double indirect: (128*128=16384 data blocks) ● Triple indirect: (128*128*128 data blocks)by shehrevar davierwalaby shehrevar davierwala
  • 51. * Data Block ● A data block has 512 bytes. A. Some FS has 1K or 2k bytes per blocks. B. See blocks size effect (next page) ● A data block may contains data of files or data of a directory. ● File: a stream of bytes. ● Directory format: i-# Next size File name pad by shehrevar davierwalaby shehrevar davierwala
  • 52. * Report.txt home john bin find alex jenny notes grep i-# Next 10 Report.txt pad i-# Next 3 bin pad i-# Next 5 notes pad 0 Nextby shehrevar davierwalaby shehrevar davierwala
  • 54. * In-core inode table ● UNIX system keeps regular files and directories on block devices such as disk or tape, ● Such disk space are called physical device address space. ● The kernel deals on a logical level with file system (logical device address space) rather than with disks. ● Disk driver can transfer logical addresses into physical device addresses. ● In-core (memory resident) inode table stores the inode information in kernel space. by shehrevar davierwalaby shehrevar davierwala
  • 55. * In-core inode table ● An in-core inode contains A. all the information of inode in disks. B. status of in-core inode inode is locked, inode data changed file data changed. C. the logic device number of the file system. D. inode number E. reference count by shehrevar davierwalaby shehrevar davierwala
  • 56. * File table ● The kernel have a global data structure, called file table, to store information of file access. ● Each entry in file table contains: A. a pointer to in-core inode table B. the offset of next read or write in the file C. access rights (r/w) allowed to the opening process. D. reference count. by shehrevar davierwalaby shehrevar davierwala
  • 57. * User File Descriptor table ● Each process has a user file descriptor table to identify all opened files. ● An entry in user file descriptor table pointer to an entry of kernel’s global file table. ● Entry 0: standard input ● Entry 1: standard output ● Entry 2: error output by shehrevar davierwalaby shehrevar davierwala
  • 58. * 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 by shehrevar davierwalaby shehrevar davierwala
  • 59. * #include <stdio.h> #include <sys/types.h> #include <fcntl.h> 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); } $ cc openEx1.c -o openEx1 $ openEx1 Before open ... fd1=3 fd2=4 fd3=5 $ by shehrevar davierwalaby shehrevar davierwala
  • 60. * … CNT=2 /etc/passwd CNT=1 ./openEx2.c in-core inodes Pointer to Descript or table U area User file descriptor table 0 1 2 3 4 5 6 7 . . . ... ... CNT=1 R CNT=1 W ... CNT=1 R file table ... ... ... by shehrevar davierwalaby shehrevar davierwala
  • 61. * 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 by shehrevar davierwalaby shehrevar davierwala
  • 62. * #include <stdio.h> #include <sys/types.h> #include <fcntl.h> main() { int fd1, fd2, fd3; 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"); } $ cc openEx2.c -o openEx2 $ openEx2 ======= fd1=3 buf1=root:x:0:1:Super-Us fd1=3 buf2=er:/:/sbin/sh daemo ======= $ by shehrevar davierwalaby shehrevar davierwala
  • 63. * #include <stdio.h> #include <sys/types.h> #include <fcntl.h> main() { int fd1, fd2, fd3; char buf1[20], buf2[20]; buf1[19]='0'; buf2[19]='0'; printf("======n"); fd1 = open("/etc/passwd", O_RDONLY); fd2 = open("/etc/passwd", O_RDONLY); read(fd1, buf1, 19); printf("fd1=%d buf1=%s n",fd1, buf1); read(fd2, buf2, 19); printf("fd2=%d buf2=%s n",fd2, buf2); printf("======n"); } $ cc openEx3.c -o openEx3 $ openEx3 ====== fd1=3 buf1=root:x:0:1:Super-Us fd2=4 buf2=root:x:0:1:Super-Us ====== $ by shehrevar davierwalaby shehrevar davierwala
  • 64. * … CNT=2 /etc/passwd ... in-core inodes Descript or table U area User file descriptor table 0 1 2 3 4 5 6 7 . . . ... ... CNT=1 R ... ... CNT=1 R file table ... ... ... by shehrevar davierwalaby shehrevar davierwala
  • 65. * System Call: dup ● dup: copy a file descriptor into the first free slot of the user file descriptor table. ● syntax: newfd = dup(fd); A. fd: file descriptor Example by shehrevar davierwalaby shehrevar davierwala
  • 66. * #include <stdio.h> #include <sys/types.h> #include <fcntl.h> main() { int fd1, fd2, fd3; char buf1[20], buf2[20]; buf1[19]='0'; buf2[19]='0'; printf("======n"); fd1 = open("/etc/passwd", O_RDONLY); fd2 = dup(fd1); read(fd1, buf1, 19); printf("fd1=%d buf1=%s n",fd1, buf1); read(fd2, buf2, 19); printf("fd2=%d buf2=%s n",fd2, buf2); printf("======n"); char buf1[20], buf2[20]; } $ cc openEx4.c -o openEx4 $ openEx4 ====== fd1=3 buf1=root:x:0:1:Super-Us fd2=4 buf2=er:/:/sbin/sh daemo ====== $ by shehrevar davierwalaby shehrevar davierwala
  • 67. * … CNT=1 /etc/passwd ... in-core inodes Descript or table U area User file descriptor table 0 1 2 3 4 5 6 7 . . . ... ... CNT=2 R ... ... ... file table ... ... ... by shehrevar davierwalaby shehrevar davierwala
  • 68. * 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 by shehrevar davierwalaby shehrevar davierwala
  • 69. * System Call: close ● close: A process may close a file by close system call ● syntax: close(fd); A. fd: file descriptor Example by shehrevar davierwalaby shehrevar davierwala
  • 70. * 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 by shehrevar davierwalaby shehrevar davierwala
  • 71. * /* creatEx1.c */ #include <stdio.h> #include <sys/types.h> #include <fcntl.h> main() { int fd1; char *buf1="I am a stringn"; char *buf2="second linen"; 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"); } by shehrevar davierwalaby shehrevar davierwala
  • 72. * $ cc creatEx1.c -o creatEx1 $ creatEx1 ====== fd1=3 buf1=I am a string ====== $ ls -l testCreat.txt -rw-rw-rw- 1 cheng staff 50 May 10 20:37 testCreat.txt $ more testCreat.txt ... by shehrevar davierwalaby shehrevar davierwala
  • 73. * System Call: stat/fstat ● stat/fstat: A process may query the status of a file (locked) file type, file owner, access permission. file size, number of links, inode number, access time. ● syntax: stat(pathname, statbuffer); fstat(fd, statbuffer); A. pathname: file name B. statbuffer: read in data C. fd: file descriptor Example by shehrevar davierwalaby shehrevar davierwala
  • 74. * /* statEx1.c */ #include <sys/stat.h> main() { int fd1, fd2, fd3; struct stat bufStat1, bufStat2; char buf1[20], buf2[20]; printf("======n"); fd1 = open("/etc/passwd", O_RDONLY); fd2 = open("./statEx1", O_RDONLY); fstat(fd1, &bufStat1); fstat(fd2, &bufStat2); printf("fd1=%d inode no=%d block size=%d blocks=%dn", fd1, bufStat1.st_ino,bufStat1.st_blksize, bufStat1.st_blocks); printf("fd2=%d inode no=%d block size=%d blocks=%dn", fd2, bufStat2.st_ino,bufStat2.st_blksize, bufStat2.st_blocks); printf("======n"); } by shehrevar davierwalaby shehrevar davierwala
  • 75. * $ cc statEx1.c -o statEx1 $ statEx1 ====== fd1=3 inode no=21954 block size=8192 blocks=6 fd2=4 inode no=190611 block size=8192 blocks= ====== ... by shehrevar davierwalaby shehrevar davierwala
  • 76. * System Call: link/unlink ● link: hardlink a file to another ● syntax: link(sourceFile, targetFile); unlink(file) A. sourceFile targetFile, file: file name Example: Lab exercise: write a c program which use link/unlink system call. Use ls -l to see the reference count. by shehrevar davierwalaby shehrevar davierwala
  • 77. * System Call: chdir ● chdir: A process may change the current directory of a processl ● syntax: chdir(pathname); A. pathname: file name Example by shehrevar davierwalaby shehrevar davierwala
  • 78. * #include <stdio.h> #include <sys/types.h> #include <fcntl.h> main() { chdir("/usr/bin"); system("ls -l"); } $ ls -l /usr/bin $ by shehrevar davierwalaby shehrevar davierwala
  • 79. * End of System Kernel Lecture by shehrevar davierwalaby shehrevar davierwala