Você está na página 1de 54

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.
Architecture of Unix System
emacs
• OS interacts directly with
sh who the hardware
• Such OS is called
kernel date system kernel
cpp
ed
cc as hardware

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)

home bin dev etc tmp lib

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.

Commonly used INODE information can be found by


using ls command
Group information and be modified by using chgrp
command.
Basic UNIX Commands
Handling
ls Files
: list files and Directories
cp : copy files
mv : move files
rm : remove files
mkdir : make directories
cd : change directories
rmdir : remove directories
pwd : print working directory
chmod : change permission mode
umask : set file-creation mode mask
lscommand
Syntax
ls [-Options] [name ...]
Description
Lists contents of directory.
Frequently Used Options
-a List all entries, including . and ..
-d Do not list contents of directories
-l Long listing
-F Mark directories with a '/', etc.
Examples
ls -alF
cp command
Syntax
cp [-Options] file1 [file2 ...] target
Description
File1 is copied to target.
Frequently Used Options
-f Force remove existing file
-i Ask before removing existing file
-r Copy directory trees
Examples
cp p1.c p2.c
cp p1.c p2.c mydir
mv command
Syntax
mv [-Options] file1 [file2 ...] target
Description
File1 is moved to target.
Frequently Used Options
-f Removes existing files without prompting the user
-i Asks before removing existing file
Examples
mv p*.c mydir
rmcommand
Syntax
rm [-f] [-i] file . . .
rm -r [-f] [-i] dirname . . . [file . . .]
Description
Removes files or directories.
Frequently Used Options
-f Removal of files without prompting the user
-i Interactive removal
-r Recursive removal
Examples
rm -f p*.o
rm -r mydir
mkdir
Syntax command
mkdir [-m mode] [-p] dirname . . .
Description
Creates the specified directories.
Options
-m Specifies the mode to be used
-p Create missing intermediate directories
Examples
mkdir -m 700 letter
mkdir abc
mkdir -p ./abc/def/ghi
cd command
Syntax
cd [directory]
Description
Change working directory.
If directory is not
specified, the value of shell
parameter $HOME is used as the new working
directory.
Examples
cd
cd ./abc/def/ghi
cd ..
rmdir
Syntax command
rmdir [-p] [-s] dirname . . .
Description
Removes directories.
Options
-p Remove the directory dirname and its parent
directories which become empty.
-s Suppress the message when –p is in effect
Examples
rmdir letter
chmod
Syntax command
chmod [-R] mode file ...
chmod [-R] [ugoa]{+|-|=}[rwxXstl] file ...
Description
Changes the permissions mode of a file or directory.
Examples
chmod 444 file1
chmod ugo+rw p*.c
chmod 700 mydir
umask
Syntax command – I
umask [ooo]
Description
Sets file-creation mode mask to ooo. The three octal
digits refer to read/write/execute permissions for
owner, group, and others, respectively.
The value of each specified digit is subtracted from
the corresponding ‘digit’ specified by the system for
the creation of a file.
If ooo is omitted, the current value of the mask is
printed.
umask command – II
Examples
umask 022 removes group and others write
permission (files normally created with mode 777
become mode 755; files created with mode 666
become mode 644).
grep command - II
Examples
grep -i unix p1.c
grep -n UNIX *.c *.h
ps –ef | grep mary
manSyntax
 command
 man [-Options] [-M path] [-T macropackage] [ -s section] name ...
 man [-M path] -k keyword ...
 Description
 On-line reference manuals
 Frequently Used Sections
1 User commands and application programs
2 System calls
3 Library functions
 Examples
 man -s 1 mkdir
 man mkdir
 man -k pipe
wc command
Syntax
wc [ -c|-m ] [ -lw ] [ file . . . ]
Description
Counts lines, words, and characters
Options
-c Count the number of bytes
-m Count the number of characters
-l Count the number of newline characters
-w Count the number of words
Examples
wc -l *.h *.c
gzip command
Syntax
gzip [ -acdfhlLnNrtvV19 ] [-S suffix] [ name . . . ]
Description
Compresses and expands data
Suffix .gz
Frequently Used Options
-c Write output on standard output
-d Uncompress
-h Help
-r Recursive
-v Verbose
gunzip
Syntax command
gunzip [ -acfhlLnNrtvV ] [-S suffix] [ name ... ]
Description
Uncompresses files (Same as gzip –d )
Automatically detects input format
Frequently Used Options
-c Write output on standard output
-h Help
-r Recursive
-v Verbose
Handling Processes
ps : Prints information about active processes
kill : Sends a signal to a process
ps command
Syntax
ps [-Options]
Description
Prints information about active processes.
Frequently Used Options
-A Every process now running
-e Same as -A
-f Full listing
-l Long listing
Examples
ps -ef
kill command
Syntax
kill [-signal] pid . . .
kill –l (the letter ‘L’ in lowercase)
Description
Sends a signal to the specified processes.
The value of signal may be numeric or symbolic.
Signal 15 is the default signal.
kill –l lists the defined signals.
Examples
kill 389
kill –3 401 402
kill -HUP 99999
Text Editors
vi
- Text editor based on an underlying line editor ex
emacs
- Powerful and extensible
- Hard to learn
Compiling and Linking
cc
- C compiler
- Default behavior is ANSI/ISO C
make
- Allows programmer to maintain, update, and
regenerate groups of computer programs.
cc command
Syntax –I
cc [-Options] ... file ...
Description
*.c are assumed to be C source programs.
*.o are compiled object files.
a.out is the default output program name.
Frequently Used Options
-c Produce an object file
-O Invoke optimizer
cc command – IIoutput file out.
-o out Name the final
-Dname Define the name to the C macro processor
-Idir Seek dir for include files

Examples
cc p1.c
make command
Syntax
 make [-f makefile] [-eiknpqrsStuwdDPBNMOg] [names]

Frequently Used Options


-f makefile Description file is makefile
-n Print commands, but do not execute them.
-u Build all targets regardless of whether they are
up-to-date or not.
Examples
make
make –f Project1.mak

Você também pode gostar