01 Introduction
01 Introduction
朱金辉
[email protected]
华南理工大学软件学院
课程基本信息
◼ 学时:32学时
◼ 课程 QQ群
◼ 群名称: 2024高级操作系统
◼ 群 号: 832619022
2
课程目标与内容
◼ 熟悉Unix操作系统原理
◼ 熟悉Linux程序开发环境
◼ 学习使用Linux API
◼ 文件系统 / IO
◼ 进程 / 线程
◼ 进程间通信 / 网络通信
◼ 探讨Linux环境原生应用系统设计方法
◼ 了解专业领域操作系统
◼ 机器人、终端、云计算
3
课程考核
1. 平时成绩 40%
◼ 课程考勤
◼ 源代码阅读与分析报告
2. 期末考试 60%
◼ 开卷
◼ 闭卷
4
源代码阅读与分析
◼ 分组完成:一组4~5人
◼ 代码阅读:阅读和分析开源系统源代码
◼ 主题选择:系统级底层软件,见下一页
◼ 课堂报告:在课堂上展示PPT和讲解系统
◼ 报告人:1人或多人
◼ 时间安排:
◼ 第11、12、13周
◼ 每组15分钟左右
◼ 每周安排8组
5
报告主题选择
◼ 操作系统
◼ 华为鸿蒙,Redox OS等
◼ 网络中间件
◼ GRPC,ZMQ,Kafka,socket.io等
◼ 分布式系统框架
◼ K8S,KubeEdge等
◼ 浏览器及前端框架
◼ Chromium,Vue,React,Angular等
◼ 深度学习框架
◼ pytorch,tensorflow,ncnn,deepSpeed等
6
案例-grpc
网络模型
7
案例-grpc
线程模型
8
案例-grpc
9
Text Book
FreeBSD 8.0
Linux 3.2.0
Mac OS X 10.6.8
Solaris 10
http://www.apuebook.com/about3e.html
10
Reference books
11
Chapter 1.
UNIX System Overview
1. Introduction
13
2. Unix Architecture
14
API Example
⚫ execve
⚫ glibc
15
3. Logging In
◼ Login name
◼ Password
◼ /etc/passwd
◼ name,
◼ encrypted password or “x” (password is in /etc/shadow),
◼ numeric user ID,
◼ numeric group ID,
◼ real name,
◼ home directory,
◼ shell program
16
Logging In
17
Zsh
◼ vi ~/.zshrc
plugins=(
git
colored-man-pages
)
18
4. Files and Directories
◼ Filesystem: hierarchical arrangement of directories and
files
◼ Root directory: /
◼ File attributes: type, size, owner, permissions, last
modification time, …
◼ stat(), fstat(): return file attribute struct
19
Files and Directories
◼ Filename
◼ Chars not allowed: (/) and (NULL)
◼ Two filenames automatically created whenever a
new dir is created:
◼ . (dot) current directory
◼ .. (dot-dot) parent directory
◼ What is .. in root directory (/)?
20
Files and Directories
◼ Pathname
◼ A sequence of zero or more filenames, separated by
slashes (/), and optionally starting with a slash
◼ Absolute pathname
◼ Relative pathname
21
Program 1.3:
(bare bones implementation of ls command)
#include “apue.h"
#include <dirent.h>
if (argc != 2)
err_quit(“usage: ls directory_name");
23
5. Input and Output
◼ File Descriptors
◼ small nonnegative integers that kernel uses to identify
files being accessed by a process
◼ Standard Input
◼ Standard Output
◼ Standard Error
24
Input and Output
◼ ls
◼ stdin, stdout, stderr: terminal
◼ ls > myfile.abc
◼ Stdout: myfile.abc
◼ How to redirect stderr to a file?
◼ How to redirect stdin from a file?
◼ Unbuffered I/O
◼ open(), read(), write(), lseek(), close()
25
Program 1.4: stdin stdout
#include "apue.h"
#define BUFFSIZE 4096
int main(void) {
int n;
char buf[BUFFSIZE];
while ( (n = read(STDIN_FILENO, buf, BUFFSIZE)) > 0)
if (write(STDOUT_FILENO, buf, n) != n)
err_sys("write error");
if (n < 0)
err_sys("read error");
exit(0);
}
26
Standard I/O
◼ A buffered interface
◼ No need to worry about BUFFSIZE
◼ Deal with “lines of input”
◼ fgets() reads an entire line
◼ read() reads a specified # of bytes
◼ printf() (#include <stdio.h>)
27
Program 1.5:
stdin stdout using standard I/O
#include "apue.h"
int main(void) {
int c;
while ( (c = getc(stdin)) != EOF)
if (putc(c, stdout) == EOF)
err_sys("output error");
if (ferror(stdin))
err_sys("input error");
exit(0);
}
28
6. Programs and Processes
29
Program 1.6: process ID
#include "apue.h"
int main(void) {
printf("hello world from process ID
%d\n", getpid());
exit(0);
}
30
Process Control
◼ Three functions
◼ fork()
◼ exec(): 6 variants
◼ waitpid()
31
Program 1.7: exec stdin cmds
#include "apue.h"
#include <sys/wait.h>
int
main(void)
{
char buf[MAXLINE]; /* from apue.h */
pid_t pid;
int status;
/* parent */
if ((pid = waitpid(pid, &status, 0)) < 0)
err_sys("waitpid error");
printf("%% ");
}
exit(0);
}
Threads
33
7. Error Handling
34
Error Handling (contd)
◼ 2 functions for printing error messages:
#include <string.h>
char *strerror(int errnum);
#include <stdio.h>
void perror(const char *msg);
◼ strerror() returns a string
◼ perror() outputs "msg: <error_msg>"
35
Program 1.8: use of error func
#include <errno.h>
#include "apue.h"
errno = ENOENT;
perror(argv[0]);
exit(0);
}
36
Program 1.8: results
$ a.out
EACCES: Permission denied
a.out: No such file or directory
37
8. User Identification
◼ User ID: numeric identifier of a user
◼ Group ID: numeric identifier of a group
#include "apue.h"
int main(void) {
printf("uid = %d, gid = %d\n",
getuid(), getgid());
exit(0);
}
38
9. Signals
39
Signals Example: shell2.c
#include "apue.h"
#include <sys/wait.h>
40
Signals (contd)
else if (pid == 0) { /* child */
execlp(buf, buf, (char *) 0);
err_ret("couldn't execute: %s", buf);
exit(127);
}
/* parent */
if ( (pid = waitpid(pid, &status, 0)) < 0)
err_sys("waitpid error");
printf("%% ");
}
exit(0);
}
void sig_int(int signo) {
printf("interrupt\n%% ");
}
41
10. Time Values
42
Time Values (contd)
43
Time Values (contd)
44
11. System Calls & Library Functions
◼ System Calls:
Entry points into an OS kernel
◼ Cannot be changed by user
◼ A function of the same name in the standard C
library
◼ User just calls those C functions whenever
system calls are needed
45
System Calls & Library Functions
◼ Library Functions: not entry points into kernel, just
functions, but they may invoke one or more system calls
◼ E.g.: printf() invokes write() system call
◼ E.g.: strcpy(), atoi(): do not invoke any system call
◼ Implementor view: fundamental diff
◼ Programmer view: no critical difference
46
System Calls & Library Functions
application code
user process
C library functions
system calls
kernel
application code
sbrk()
system call kernel
48