ch01.4 - System Programing
ch01.4 - System Programing
4: System Programing
Outline
Introduction
System Programs
System Programming
Operating System Concepts – 10th Edition 2a.2 Silberschatz, Galvin and Gagne ©2018
1
Introduction
Operation System
Operating System Concepts – 10th Edition 2a.3 Silberschatz, Galvin and Gagne ©2018
Introduction
Operating System Concepts – 10th Edition 2a.4 Silberschatz, Galvin and Gagne ©2018
2
System Programs
System Programs
• Help Program Development and Execution can be done
conveniently
• Some of the System Programs are simply user interfaces,
others are complex.
• It traditionally lies between the user interface and system calls.
• the user can only view up-to-the System Programs he can’t
see System Calls._người dùng chỉ có thể xem các chương trình
hệ thống tối đa mà anh ta không thể xem cuộc gọi hệ thống
Operating System Concepts – 10th Edition 2a.5 Silberschatz, Galvin and Gagne ©2018
3
System Programming
Unix
Windows
C++
Operating System Concepts – 10th Edition 2a.7 Silberschatz, Galvin and Gagne ©2018
1. Concept
2. Basic commands working with folder
3. System Programming Languages: Assembly, C, Python
Operating System Concepts – 10th Edition 2a.8 Silberschatz, Galvin and Gagne ©2018
4
9
Command
Operating System Concepts – 10th Edition 2a.9 Silberschatz, Galvin and Gagne ©2018
10
Path
echo $PATH: the paths that are set before
# echo $PATH
/usr/kerberos/sbin:/usr/kerberos/bin...
Absolute path: independence with the current
directory of the user and start with /
The relative path: depends on the current
directory of the user and does not start with /
Operating System Concepts – 10th Edition 2a.10 Silberschatz, Galvin and Gagne ©2018
5
11
Time to execute the command
Operating System Concepts – 10th Edition 2a.11 Silberschatz, Galvin and Gagne ©2018
12
Operating System Concepts – 10th Edition 2a.12 Silberschatz, Galvin and Gagne ©2018
6
13
Basic commands working with normal files
1. Create file: touch, cat
2. Copy, rename: cp
3. Move, rename: mv
4. list: ls
5. Remove: rm
6. Find file: find
7. Link: ln
8. Compare: cmp (compare), comm, diff , diff3
9. Edit: cat, head, tail, pg, more
10. Display with text/binary: od [tùy chọn]
11. Count: wc
12. size: sum
13. Compress and decompress: pack và gzip
14. Divide: Split
15. cut
16. sort
17. awk
Operating System Concepts – 10th Edition 2a.13 Silberschatz, Galvin and Gagne ©2018
14
Data Flow Management
Operating System Concepts – 10th Edition 2a.14 Silberschatz, Galvin and Gagne ©2018
7
15
UNIX standard I/O
Operating System Concepts – 10th Edition 2a.15 Silberschatz, Galvin and Gagne ©2018
16
Redirecting data flows
Redirecting to new file
- Use ">"
- Ex : ls > file1
Redirecting to old file
- Use " >>"
- Ex: cat file_2 >> file_1
Redirecting from a file
- Use "< "
- Ex: tee < file1
Operating System Concepts – 10th Edition 2a.16 Silberschatz, Galvin and Gagne ©2018
8
17
pipe
- A shell technique used to concatenate the data streams of several
processes
- We can communicate with each other using pipes, for
example : command_1 | command_2 | command_3
Where:
command_1 need has
output, command_2 is filter
command_3 need has
input.
PIPE: each pipe connects two data streams of two adjacent instructions
through an intermediate pipe, represented by the BUFER.
Operation: command_i ==> BUFER ==> command_j
Ex:
Operating System Concepts – 10th Edition 2a.17 Silberschatz, Galvin and Gagne ©2018
18
Filtering
1. tr (replace)
tr string_1 string_2
2. fgrep
3. grep
4. egrep
grep [option] PATTERN FILE
“string" PATTERN is regular expression
Operating System Concepts – 10th Edition 2a.18 Silberschatz, Galvin and Gagne ©2018
9
19
Regular expression
Operating System Concepts – 10th Edition 2a.19 Silberschatz, Galvin and Gagne ©2018
20
Wildcards
• .: any ASCII character, except <RETURN>:
grep ‘ .*’ file => all rows in “file", including blank lines.
• [ ]: an ASCII character in square brackets, but needs to be
enclosed between quotes like: [^xyzt]
• -: two ASCII characters inside square brackets, for example
[b-y], represents a character in the range, but also needs to
be enclosed in quotes like '[b-y]'.
• \: remove the special meaning of the character following it
and return the original meaning.
• ^: exception
Ex: an expression like [^xyzt] represents a character other than x,
y, z, t
Operating System Concepts – 10th Edition 2a.20 Silberschatz, Galvin and Gagne ©2018
10
Shell programming
Variable: $0, $1, $2….; $#, $*, $@
Function try_func() {
echo <gtri> #return stdout
return <gtri>
}
# Call function
x=$(try_func)
Strutures
• If
• For
• While, until
• Case
File System programming
• Directory
• File
Operating System Concepts – 10th Edition 2a.21 Silberschatz, Galvin and Gagne ©2018
Structures
if condition while codition
then do
statements statements
else done
statements
fi until codition
do
if condition1 statements
then done
statements
condition2; then case mau in
elif statements mau1)
statements;;
else
mau2)
statements
statements;;
fi *)
for variable in values statements ;;
do esac
statements
done
Operating System Concepts – 10th Edition 2a.22 Silberschatz, Galvin and Gagne ©2018
11
Ex, run script
$ for file in * Cat > vidu1.sh
> do #! /bin/sh
> if grep -l ‘Hello’ $file for file in *
> then do
> more $file if grep -l ‘Hello’ $file
> fi then
> done more $file
fi
done
exit 0
Run script:
chmod +x <filename>
./<filename>
Operating System Concepts – 10th Edition 2a.23 Silberschatz, Galvin and Gagne ©2018
Ex
Define Max_2num(), then find Max of N intergers
max_2num() #Main
{ max=$1
if [ $1 –gt $2 ] for i in $*
then do
m=$1 max=$(max_2num $max $i)
else done
m=$2 echo “Max $# so: $max”
fi exit 0
echo $m
return $m
}
Operating System Concepts – 10th Edition 2a.24 Silberschatz, Galvin and Gagne ©2018
12
Lab No.4. Simple shell programs
No.4.
• number is even or odd
• year is leap year or not
• find the factorial of a number
• swap the two integers
Others:
• Print the prime numbers in any sequence of numbers passed in from the
command line (using a function check 1 number is prime or not)
• Write a function to find the greatest common divisor of two numbers,
then use the function you just wrote to find the UCLN of an array.
• Input an array and sort the array ascending.
• Check increment, decrement, symmetric arrays.
• Input 1 array. Remove the odd elements in the array, Then print the
remaining array
Operating System Concepts – 10th Edition 2a.25 Silberschatz, Galvin and Gagne ©2018
13
Linux file system calls
Directory
• Note that DIR structure is an internal structure used by
readdir, closedir to maintain information about the
directory being read.
• The dirent structure contains the inode number and the
name. This information is collected into a file called
Function Description Returns
mkdir(const char * pathname, Create a 0 if OK, -1 on error
mode) dirctory
rmdir(const char * pathname) Delete a 0 if OK, -1 on error
directory Pointer of DIR if OK, NULL on
opendir(const char * pathname) Open a
error
directory
Pointer of dirent if OK, NULL
readdir(DIR * dp) Read a
at the end of directory or
directory
closedir(DIR * dp) Close a 0 if OK, -1 on error
Ex, simulates command ls to list all the file in the current directory.
Operating System Concepts – 10th Edition 2a.27 Silberschatz, Galvin and Gagne ©2018
C File I/O: There are types and functions in the library stdio.h
that are used for file I/O. Reading from or writing to a file in
C requires 3 basic steps:
• Open the file.
• Do all the reading or writing.
• Close the file
For files you want to read or write, you need a file pointer: FILE *fp;
• FILE is some kind of structure that contains all the
information necessary to control the file
Open File -- fopen(const char filename, const char mode)
• will initialize an object of the type FILE
Close File -- fclose(FILE *fp )
• When done with a file, it must be closed
Operating System Concepts – 10th Edition 2a.28 Silberschatz, Galvin and Gagne ©2018
14
Text I/O Functions
Mode Description
feof detects end-of-file marker in a
file
fscanf reads formatted input from a
file
fprintf prints formatted output to a
file
fgets reads a string from a file
fputs prints a string to a file
fgetc reads a character from a file
fputc prints a character to a file
Ex: fgetc_cp.c use fputc(int char, FILE *stream) writes a
character (an unsigned char) specified by the argument char
to the specified stream and advances the position indicator
for the stream.
• Similarly, fgetc(FILE *stream) reads a character from the
specified stread and advances the position indicator for
the stream
Operating System Concepts – 10th Edition 2a.29 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition 2a.30 Silberschatz, Galvin and Gagne ©2018
15
Practice
Operating System Concepts – 10th Edition 2a.31 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
16