How to learn system programming (learn Linux programming with Linux)
1 Analysis Utility
/bin,/usr/bin,/usr/local/bin
Learn how to use tools, analysis tools, understanding features and principles
2 Learning system Calls
Functions and system calls are essentially functions. Different libraries, different header files
What system calls are required to be analyzed and how the system calls are used: parameters? return value?
3 Programming implementation
Using the above principles and a set of collaborative system calls, self-programming to implement the function of the program
or actively copy the code, learning those system calls are working together
C language learning can refer to this idea, the implementation of the standard library
The three questions that accompany us to study frequently:
What can I do?
How is it implemented?
How do you write yourself?
For example, Linux operating system
What can I do?
Login-Run Program-logoff
How do I log in? How do I get the program name? How do I run the program?
Directory Operations: Directory tree: Ls,cd,pwd,mkdir,rmdir
How is the directory tree organized? Where does the directory exist? What is the current directory?
File operations: Cat, MORE/LESS/PG, CP, MV, LPR
How is file data stored? How to copy, move, rename? Where does the file name exist?
File access control: Ugo
How do I set it?
From the OS point of view: Online games
Communication
Collaboration
Network access
BC/DC----> Network b/S
Standard I/O Learning Example: more
What can I do?
How to use
More filename
More < filename
Command | More
How is it implemented?
Pseudo code:
If the user does not enter a file name
Output keyboard Input Content
For the entire file
Show 24 rows
Prompt user to select space Enter Q
If the user chooses a space
Show next 24 lines
If the user chooses to enter
Show Next line
If the user chooses Q
Exit
Function call:
Fopen/fclose
stdin stdout stderr
Fgets/fputs
Getchar/putchar
printf ("\033[7m more? \033[m ")
Write yourself:
How can I tell if I read the end of a file?
Feof
The return value of Fgets is 0
Fseek and file read and write locations
Problem:
Select All to enter
Text and anti-white more roll up together
Percentage
Adaptation window
Redirect: Who | More, LS |more
/dev/tty (stream and file)
System call: How file I/O is programmed with the Linux learning system
How do multi-user systems know who is using the system? W.H.O.
What can the Who do? Use it, please.
Man who (info. Who, who--help)
How to achieve who?
Read Manpages
Man Mans
Search Manpages
Man-k
Man-k XXX | Grep-i YYY
Read the header file
See Also
Man
Man-k utmp
Mans 5 Utmp
Utmp.h
Grep-i Xxx-nr/usr/include
Utmp_file
struct UTMP
#define UT_NAME Ut_user
Ut_line
#define Ut_time Ut_tv.tv_sec
Pseudo code:
Open utmp File
For file
Read a record
Show Records
Close File
Which system calls?
Man-k File | Grep-i Read
See Also
Open/read/close
struct UTMP s;
Read (FD, &s, sizeof (s));
Mans
CF fopen Fread fclose
Write your own who:
Read:return value
/var/run/utmp
Var/run/unp
Problem: Blank record
Ut_type
Time Conversion
Man-k Time | Grep-i Transform (TRANS)
Asctime (3)
CTime (3)
LocalTime (3)
Man-k Time | Grep-i Convert
No
Find keywords: At least Chinese have ideas
Transformation
Time format
Man-k Time | Grep-i format
Strftime (3)
How do I copy files? Cp
What can CP do?
CP SRC DST
How to implement CP?
Open/close
Open (FFF, o_wronly| o_creat,0644)
Creat (FFF, 0644);
n = Read (FD, buf, BUFSIZE);//#define BUFSIZE 4096
Write (fd, BUF, N);
Pseudo code:
Open source file
Create a target file
For source files
Read the source file into the buffer
Writes buffer contents to the target file
Closing source and destination files
Write your own CP:
System invoke error Handling:
Error type: errno
Display error message: perror (3)
Error handling Encapsulation function
Fork ()
if ((PID = fork ()) < 0)
Unix_error ("fork Error");
return PID;
Those who learn more:
AC last cat head tail OD dd
Read/write can read the file contents, how to read the filename and file attributes? Ls
LS can do?
Ls
Ls-l
Ls-a
Ls-lu: Last Access time
Ls-s: File size in blocks
Ls-t: Sort by Time
Ls-f: Show File types
List file directories
displaying file information
How do I list file directories?
How do i show file properties?
How can I tell if a name is a file or a directory?
File tree
Files and directories are organized into a directory tree, where nodes are directories or files
A directory is a special file whose contents are the names of directories and files, similar to Utmp
Unlike files, directories are not empty
How to implement LS?
Man-k Direct
Man-k Direct | Grep-i Read
Man-k Direct | Grep-i entry
Readdir
See Also:opendir Closedir
Summary: Code patterns
fopen Fread/fwrite fclose
Open Read/write Close
Opendir Readdir Closedir
Pseudo code:
Open catalog File
for catalog files
Read directory entry
Show file name
Close File directory file
Write your own LS:
Incoming parameters, display any directory: ls/tmp; LS/; Ls/dev
Sub-column
. Opening implied file:-A
Sort by: man-k sort
Code Base: DRY
Xxx_uitl.h XXX_UTIL.C
Error handling, parameter handling, common structure ...
Xxx_datastru.h xxx_datastru.c
Linked list, doubly linked list
Tree, figure ...
XXX_ALGORITH.H XXX_ALGORTHM.C
What can ls-l do?
Show file information: mode (file type files (1), access control), number of links, file owner, group, size, last modified time, file name
How to implement Ls-l?
Man-k File | Grep-i Infomation (Status, property, attribute)
Stat (1) stat (2)
struct STAT
Last modified: St_mtime CTime
Mode: St_mode
Type UGS rwx rwx rwx
Octal mask
struct STAT info;
if ((Info.st_mode & 0170000) = = 0040000)
printf ("This is a directory \ n");
File type macros
#define S_ISDIR (M) (((m) &0170000)) = = (0040000))
struct STAT info;
if (S_isdir (Info.st_mode))
printf ("This is a directory \ n");
Access control properties? For example, can I write? How do I define a macro?
S_isreg (MoD) '-'
S_isdir (mode) ' d '
S_ISCHR (Mode) ' C '
S_ISBLK (mode)) ' B '
Uid/gid
/etc/passwd
Getpwuid (3)
struct passwd
/etc/group
Getgrgid (3)
struct GROUP
Write your own ls-l: ls2.c
Stat (2) struct STAT
Getpwuid (3) struct passwd
Getgrgid (3) struct group
Record Count
Sort by file name Qsort
File Property Modification and Open/creat
Open/creat umask (1) umask (2)
chmod (1) chmod (2)
Chown (1) chgrp (1) chown (2)
Touch (1) utime (2)
MV (1) Rename (2)
Learn to spare: Tree chmod file chown chgrp finger touch
Tree Ls-r
A file contains data, a directory is a list of files, and a directory forms a directory tree.
What does the file mean in the directory?
What does the user's home directory (home) mean?
File system (reference material for problem solving)
Path: Absolute path, relative path. Pwd
File systems in the eyes of the user
Directories and files: Composing a directory tree
Directory command:
. .. /
Mkdir
RmDir
-P
Mv
Cd
Pwd
...
Exercise: Building a directory tree
File command:
Cp
-R
Cat
Mv
Rm
-R
ln
Ls
...
Directory tree Commands
Tree
Ls-r
Chmod-r
Du
Find: Notice the difference from grep
...
The depth of the directory tree??
Thinking:
The file system forms a directory tree, and the operating system provides the appropriate operational commands to work together
What is a catalog?
How do I know the directory where the files are located?
What does it mean to switch directories (CDS)?
How does the PWD know your directory?
20135234 Ma Qiyang-—— Information Security system design basics Nineth Week study Summary