0% found this document useful (0 votes)
5 views48 pages

01 Introduction

The document outlines a course on Advanced Operating Systems, focusing on Unix and Linux principles, programming environments, and system design methods. It includes course objectives, assessment criteria, and details on group projects involving source code analysis. Additionally, it provides information on textbooks, reference materials, and key topics covered in the curriculum.

Uploaded by

Hu Da
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views48 pages

01 Introduction

The document outlines a course on Advanced Operating Systems, focusing on Unix and Linux principles, programming environments, and system design methods. It includes course objectives, assessment criteria, and details on group projects involving source code analysis. Additionally, it provides information on textbooks, reference materials, and key topics covered in the curriculum.

Uploaded by

Hu Da
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

高级操作系统

朱金辉
[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

◼ Advanced Programming in the UNIX


Environment
◼ W. Richard Stevens, Stephen A. Rago 3
Edition, 2013

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

◼ Tour of UNIX from a programmer’s perspective


◼ Brief descriptions and examples of terms and
concepts
◼ Overview of services provided by UNIX

13
2. Unix Architecture

Figure 1.1 Architecture of the UNIX operating system

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

• Shells: a command line interpreter that


reads user input and executes commands

17
Zsh

◼ sudo apt install zsh


◼ Oh-My-Zsh: Zsh plugin https://ohmyz.sh/

◼ 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>

int main(int argc, char *argv[]){


DIR *dp;
struct dirent *dirp;

if (argc != 2)
err_quit(“usage: ls directory_name");

if ( (dp = opendir(argv[1])) == NULL)


err_sys("can't open %s", argv[1]);
while ( (dirp = readdir(dp)) != NULL)
printf("%s\n", dirp->d_name);
closedir(dp);
exit(0);
}
$ man opendir
22
Program 1.3
◼ Edit and save in myls.c
cc myls.c (output: a.out)
./a.out /dev (output: . .. ………)
$ ./a.out /etc/ssl/private
can’t open /etc/ssl/private: Permission denied
a.out /dev/tty
can’t open /dev/tty: Not a directory

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

◼ Program: an executable file in disk


◼ Process: an executing instance of a program
◼ Process also called “task” by some OS
◼ Unique nonnegative integer identifier for each
process (pid)

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;

printf("%% "); /* print prompt (printf requires %% to print %) */


while (fgets(buf, MAXLINE, stdin) != NULL) {
if (buf[strlen(buf) - 1] == '\n')
buf[strlen(buf) - 1] = 0; /* replace newline with null */

if ((pid = fork()) < 0) {


err_sys("fork error");
} 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);
}
Threads

◼ All threads within a process share the same


address space, file descriptors, stacks, and
process related attributes.
◼ Each thread executes on its own stack.
◼ Threads are identified by IDs.

33
7. Error Handling

◼ Negative return value when error occurs


◼ #include <errno.h>
◼ errno variable
◼ never cleared if error does not occur
◼ never set to 0 by any function

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"

int main(int argc, char *argv[]) {


fprintf(stderr, "EACCES: %s\n", strerror(EACCES));

errno = ENOENT;
perror(argv[0]);

exit(0);
}

36
Program 1.8: results
$ a.out
EACCES: Permission denied
a.out: No such file or directory

◼ (argv[0] passed as arg to perror())

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

◼ A technique to notify a process that some


condition has occurred
◼ E.g.: divide by zero SIGFPE
◼ Process response to a signal
◼ Ignore the signal, OR
◼ Let the default action occur, OR
◼ Provide a function to handle the signal.

39
Signals Example: shell2.c
#include "apue.h"
#include <sys/wait.h>

static void sig_int(int); /* our signal-catching function */


int main(void) {
char buf[MAXLINE];
pid_t pid;
int status;
if (signal(SIGINT, sig_int) == SIG_ERR)
err_sys("signal error");
printf("%% "); /* print prompt (printf requires %% to print %) */
while (fgets(buf, MAXLINE, stdin) != NULL) {
buf[strlen(buf) - 1] = 0;/* replace newline with null*/
if ( (pid = fork()) < 0)
err_sys("fork error");

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

◼ Two different time values


◼ Calendar time: #seconds since the Epoch, which
is 00:00:00 Jan 1, 1970, Coordinated Universal
Time (UTC).
◼ Process time: measures CPU resources used by a
process, in clock ticks, which is 50, 60, or 100
ticks per second.

42
Time Values (contd)

◼ Execution time of a process has 3 values:


◼ clock time: total amount of time from process
start to finish
◼ user CPU time: CPU time due to user
instructions in a process
◼ system CPU time: CPU time due to kernel
activities on behalf of the process

43
Time Values (contd)

◼ To measure process execution time, use


the “time” command as follows:
time ls > /dev/null
real 0m19.81s
user 0m0.43s
sys 0m4.53s

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

ldd - print shared object dependencies


47
System Calls & Library Functions Example

application code

memory allocation user process


function malloc()

sbrk()
system call kernel

48

You might also like