3/1 2 week(s) - 國立中正大學資訊工程學系暨研究所

系統程式
System Programming
http://ecourse.elearning.ccu.edu.tw/
羅習五
國立中正大學資訊工程學系
[email protected] Class: EA-001
(05)2720411 ext. 33116 Office: EA-517
本份投影片及本學期投影片,大量參考自
熊博安教授所製作的「系統程式」教學投
影片
Text and Reference Book
• Advanced Programming in the UNIX
Environment, 2nd Edition, W. Richard Stevens,
Stephen A. Rago, Addison Wesley, 2005, 開發代
理 (927 pages), “APUE” in short
• http://www.apuebook.com/
1st Edition
2nd Edition
3rd Edition
About Rich Stevens
• On Wikipedia:
▫ http://en.wikipedia.org/wiki/W._Richard_Stevens
“Stevens died in 1999, at the age of 48. In 2000, he
was posthumously awarded the USENIX Lifetime
Achievement Award.”
About Rich Stevens
Stevens先生不幸逝于1999年9月1日,至于死因家
人不便透露,不过有三种说法:
▫ 攀岩
▫ 滑翔意外
▫ 滑雪
感謝Stevens帶給我們這麼好的一本課本!
Text and Reference Book
• Linux System Programming, Robert Love,
O'Reilly Media, 2007
Syllabus(暫定)
Topic
Chapter
Dates
• Overview
1
2/20 ~ 3/1 2 week(s)
• Files, Dirs, Stdio, Sys Info
36
3/6 ~ 4/12
• Mid-Term Exam
6 week(s)
4/15~4/19
• Process Environment
7
4/24 ~ 5/3 2 week(s)
• Process Control
8
5/8 ~ 5/10 1 week(s)
• Signals
10
5/15 ~ 5/17 1 week(s)
• IPC
15
5/22 ~ 5/31 2 week(s)
• ?(threads)
?(11,12) 6/5~6/14
• Final Exam
6/17 ~ 6/21
2 week(s)
課程形式
• 課堂上課(EA001)
• 數位教材(ecourse, cyberCCU)
Program Assignments (暫定)
• Total 4 program assignments announced on:
1.
2.
3.
4.
?Shell:
???:
???:
???:
3/06 ~ 3/20
3/20 ~ 4/03
4/24 ~ 5/01
5/22 ~ 6/05
Grading(暫定)
• Mid-Term Exam
30%
• Final Exam
30%
• Program Assignments
40%
• Bonus (Q/A, quiz, attendance, …)
Dates(暫定)
• Midterm Exam: 4/10 13:30~15:30
• Final Exam: 6/19 13:30~15:30
• Program Assignments (total 4)
▫ To be done individually
▫ 2 to 4 weeks for each program
▫ Backgrounds:
 Some data structures,
 Some UNIX/Linux operations
Rules
• NO COPYING of assignments/projects (a
single case of copying 
BOTH parties will get ZERO point for ALL
assignments and projects)
• NO CHEATING in exams
(BOTH parties will get ZERO points for that
exam)
Rules (cont’d)
• Class Quiz:
▫ Correct Answer  Bonus Points
▫ Wrong or No Answer  Deduction Points
• Class “Attendance”:
▫ 1 absence  Deduct 5%
▫ 2 absences  Deduct 10%
▫ n absences  Deduct 5n%
• “Attendance”:帶上講義或課本或筆記
System Programming
ENJOY THE COURSE!!!
Unix programming envirunments
本份投影片大量參考自Ashok Srinivasan
的投影片
UNIX programming environment
•
•
•
•
Editors
C compilers
Debugger
Makefiles
▫ make
Editors
• vi, pico, emacs, etc
• What is good about emacs?
▫
▫
▫
▫
emacs is more than just an editor
You can compile and edit within emacs
If you know lisp, you can expand its functionality
Some useful commands to get started:




C-h t get tutorial
C-g
cancel command
C-x C-c quit emacs
C-h
help
C compilers
• gcc, cc, icc, llvm
• Using ANSI C, the code must pass with flags
–Wall –ansi –pedantic
with no warning messages
• Some examples
▫ gcc –g –Wall –ansi –pedantic example1.c
▫ gcc –g –c –Wall –ansi –pedantic example1.c
Debugger
• ddd, xxgdb, gdb
• The code must be compiled with –g option.
• The power of a debugger:
▫ Finding the line that causes coredump.
▫ See example:
 Break point, show value, change value, step, next, continue,
print
▫ Very efficient in debugging sequential code
▫ Not very effective in debugging concurrent code
(multiple threads, multiple processes)
• Good software development practice: You must have
seen each line of your code execute in the debugger,
at least once
Make
• make [-f makefile][option] target
▫ A tool to update files derived from other files
▫ The default files for make
are ./makefile, ./Makefile, ./s.makefile
▫ Use the –f option to specify some other file
 make –f makefile1
▫ The makefile has three components
 Macros: define constants
 Target rules: Specify how targets are made
 Inference rules: Specify how targets can be made, implicitly.
make will first check if a target rule applies, before using
inference rules.
Header files
• Usually define interfaces between separately
compiled modules
• May contain macro definitions, preprocessor
directives, declarations of types, and function
prototypes
• Should not contain variable definitions or
executable code
Conditional Code in Headers
• Preprocessor directives are used to prevent the
body of a header file from being used multiple
times.
#ifndef MYHEADER
#define MYHEADER
/* the body of the header file */
#endif
Macros with and without Parameters
• #define MAX_LENGTH 256
▫ ... for (i = 0; i < MAX_LENGTH; i++) ...
• Macros can have parameters
▫ #define max(a,b) (a > b) ? a : b
• What is wrong with the following?
▫ #define sum(a, b) a + b
▫ #define product(a, b) a*b
Some Unix System Calls
#include <stdlib.h>
int system(const char *string);
▫ Works as if string is typed into the shell at a
terminal
▫ Returns the exit status (see man page for waitpid)
▫ Usually -1 is returned if there is an error
Portability
• Standards
▫ Source code portability: ANSI/ISO C
▫ UNIX standards: POSIX, open group
▫ Internet engineering task force (IETF)
• 32 bit vs 64 bit
• Byte order
▫ Little endian vs big endian
Source Code Portability
• Standard programming language
▫ Example: ANSI/ISO C
 c89, c90, c99, c11
• Standard libraries
• Standard API to operating system
▫ Example: POSIX.1
• Auto-configuration mechanisms
• Programmer discipline
Unix Standards
• POSIX (IEEE STDS 1003.x and ISO/IEC 9945)
▫
▫
▫
▫
POSIX.1: System API for C language
POSIX.2: Shell and utilities
POSIX.5: System API for Ada language
POSIX.9: System API for Fortran language
• See also http://www.pasc.org and
http://www.standards.ieee.org
IETF
• Internet Engineering Task Force (IETF)
▫ Network designers, operators, vendors,
researchers
▫ Deals with the Internet
▫ Issues RFCs
• See also http://www.ietf.org
64-bit vs. 32-bit architecture
• Pointers cannot be stored as
int
• size_t cannot be stored as
int
• long may not be long
enough for size_t and
offset_t
Datatype 32bit
64bit
char
8
8
short
16
16
int
32
32
long
32
64
pointer
32
64
(long long) 64
64
Byte order
• Little-Endian
▫ Low-order byte is
stored at lowest
address
• Big-Endian
▫ High-order byte is
stored at lowest
address