跳转至

操作系统面试题(山月)

什么是守护进程

原文:https://q.shanyue.tech/base/os/50.html

Issue

欢迎在 Gtihub Issue 中回答此问题: Issue 50(opens new window)

Author

回答者: shfshanyue(opens new window)

守护进程,可以被认为是后台进程,当你把控制台关闭时,它仍然还会运行。

当然 - 这只是表面,从更深层次看呢?

什么是对象存储,与块存储和文件存储有什么区别

原文:https://q.shanyue.tech/base/os/54.html

Issue

欢迎在 Gtihub Issue 中回答此问题: Issue 54(opens new window)

SIGINT SIGTERM SIGKILL 有什么区别

原文:https://q.shanyue.tech/base/os/64.html

Issue

欢迎在 Gtihub Issue 中回答此问题: Issue 64(opens new window)

Author

回答者: shfshanyue(opens new window)

TODO

有没有使用过 oss,对象存储相比块存储和文件存储有什么优缺点

原文:https://q.shanyue.tech/base/os/66.html

Issue

欢迎在 Gtihub Issue 中回答此问题: Issue 66(opens new window)

Author

回答者: shfshanyue(opens new window)

OSS 兼具分布式与读取速度的平衡吧

什么是 IO 多路复用

原文:https://q.shanyue.tech/base/os/107.html

Issue

欢迎在 Gtihub Issue 中回答此问题: Issue 107(opens new window)

Author

回答者: shfshanyue(opens new window)

假设你的服务器要同时处理 100 个 socket 连接

  1. 开一个线程,挨个处理。这就没法处理并发了
  2. 开 100 个线程,分别处理。这么多的线程在内核态频频进进出出也很消耗性能
  3. 开一个线程,去检查 socket 是否就绪,如果就绪则再开一个线程去处理。这就是 IO 多路复用了

selectpollepoll 是三中典型的 IO 多路复用的系统调用,但实际上现在 epoll 使用的最多。当你使用 strace 查看 nodejs 某个服务的系统调用时,将会发现 nodejs 就是使用了 epoll

Author

回答者: zhangxiaokun(opens new window)

不会开新的线程去处理,轮训所有注册的 socket,就绪就去处理

文件系统中 mtime、ctime 和 atime 指什么,都有什么不同

原文:https://q.shanyue.tech/base/os/116.html

Issue

欢迎在 Gtihub Issue 中回答此问题: Issue 116(opens new window)

Author

回答者: shfshanyue(opens new window)

linux 中,

  • mtimemodified time 指文件内容改变的时间戳
  • ctimechange time 指文件属性改变的时间戳,属性包括 mtime。而在 windows 上,它表示的是 creation time

所以 ctime 会比 mtime 要大一些,使用 stat 查看文件属性如下

$ stat hello.txt
  File: ‘hello.txt’
  Size: 30              Blocks: 8          IO Block: 4096   regular file
Device: fd01h/64769d    Inode: 917526      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-12-10 16:15:55.253325208 +0800
Modify: 2019-12-10 16:15:52.740653330 +0800
Change: 2019-12-10 16:15:52.742653069 +0800
 Birth: - 

而 http 服务选择 Last_Modified 时一般会选择 mtime

如何监控文件的变动

原文:https://q.shanyue.tech/base/os/124.html

更多描述

当一个文件或者一个目录下的文件有所变动时能够监控到。

如一些前端和后端项目在开发环境下会根据项目目录变动而重启或者重新编译

Issue

欢迎在 Gtihub Issue 中回答此问题: Issue 124(opens new window)

Author

回答者: shfshanyue(opens new window)

  1. 在 node 中调用 API fs.watch
  2. 在 linux 中原理是 inotify,macos 中原理是 FSEvents,windows 中原理是 ReadDirectoryChangesW
  3. 由于内核对文件监控更加细粒度,更加敏感,当每次修改文件时可能触发内核多次调用,需要防抖
  4. 注意软链接,读写权限等文件系统属性

可参考文章 How to Watch for Files Changes in Node.js(opens new window)精读《如何利用 Nodejs 监听文件夹》(opens new window)

Author

回答者: everlose(opens new window)

我不太清楚问这个的目的,是 fs.watch 吧?若要问的深一点底层怎么做的监听,我就不会了。贴一下答案?

Author

回答者: shfshanyue(opens new window)

@everlose 简单的总结了以下,见楼上

一个守护进程的创建步骤是什么,如何用 C 语言创建

原文:https://q.shanyue.tech/base/os/139.html

更多描述

50

Issue

欢迎在 Gtihub Issue 中回答此问题: Issue 139(opens new window)

Node 中 cluster 的原理是什么

原文:https://q.shanyue.tech/base/os/141.html

Issue

欢迎在 Gtihub Issue 中回答此问题: Issue 141(opens new window)

Author

回答者: shfshanyue(opens new window)

  1. fork 子进程
  2. Load Balance
  3. 多进程共享端口

相关文章

什么是文件描述符 (file descriptor)

原文:https://q.shanyue.tech/base/os/171.html

Issue

欢迎在 Gtihub Issue 中回答此问题: Issue 171(opens new window)

Author

回答者: shfshanyue(opens new window)

fd 在内核中通常表示为非负整数,当进程新建或者打开文件时,内核将会返回一个文件描述符,使用它可以用来读写文件。

另外,新建 socket 时也会产生一个文件描述符。

另外,有三个特殊的文件描述符,用以表示标准输入,标准输出及标准错误

  • STDIN_FILENO: 0
  • STDOUT_FILENO: 1
  • STDERR_FILENO: 2

进程与线程的区别是什么

原文:https://q.shanyue.tech/base/os/256.html

Issue

欢迎在 Gtihub Issue 中回答此问题: Issue 256(opens new window)

如何创建一个进程

原文:https://q.shanyue.tech/base/os/300.html

Issue

欢迎在 Gtihub Issue 中回答此问题: Issue 300(opens new window)

Author

回答者: shfshanyue(opens new window)

  • exec
  • fork

什么是 exit code

原文:https://q.shanyue.tech/base/os/424.html

Issue

欢迎在 Gtihub Issue 中回答此问题: Issue 424(opens new window)

Author

回答者: shfshanyue(opens new window)

exit code 代表一个进程的返回码,通过系统调用 exit_group 来触发。在 POSIX 中,0 代表正常的返回码,1-255 代表异常返回码,一般主动抛出的错误码都是 1

这里有一张关于异常码的附表 Appendix E. Exit Codes With Special Meanings(opens new window)

异常码在操作系统中随处可见,以下是一个关于 cat 命令的异常以及它的 exit code,并使用 strace 追踪系统调用。

$ cat a
cat: a: No such file or directory

# 使用 strace 查看 cat 的系统调用
# -e 只显示 write 与 exit_group 的系统调用
$ strace -e write,exit_group cat a
write(2, "cat: ", 5cat: )                    = 5
write(2, "a", 1a)                        = 1
write(2, ": No such file or directory", 27: No such file or directory) = 27
write(2, "\n", 1
)                       = 1
exit_group(1)                           = ?
+++ exited with 1 +++ 

从系统调用的最后一行可以看出,该进行的 exit code 是 1,并把错误信息输出到 stderr (标准错误的 fd 为 2) 中

如何查看 exit code

strace 中可以来判断进程的 exit code,但是不够方便过于冗余,特别身处 shell 编程环境中。

有一种简单的方法,通过 echo $? 来确认返回码

$ cat a
cat: a: No such file or directory

$ echo $?
1 

什么是 coredump,如何配置与分析

原文:https://q.shanyue.tech/base/os/425.html

Issue

欢迎在 Gtihub Issue 中回答此问题: Issue 425(opens new window)

同一进程的线程共享那些资源

原文:https://q.shanyue.tech/base/os/459.html

Issue

欢迎在 Gtihub Issue 中回答此问题: Issue 459(opens new window)

Author

回答者: shfshanyue(opens new window)

  • 全局变量
  • 文件

什么是原码、补码与反码

原文:https://q.shanyue.tech/base/os/690.html

Issue

欢迎在 Gtihub Issue 中回答此问题: Issue 690(opens new window)

Author

回答者: shfshanyue(opens new window)

  • 原码:
  • 反码: 反码按位取反
  • 补码: 正数和 0 的补码就是该数字本身,负数的补码则是反码加一

补码

0 1 1 1 1 1 1 1 = 127
0 0 0 0 0 0 1 0 = 2
0 0 0 0 0 0 0 1 = 1
0 0 0 0 0 0 0 0 = 0
1 1 1 1 1 1 1 1 = −1
1 1 1 1 1 1 1 0 = −2
1 0 0 0 0 0 0 1 = −127
1 0 0 0 0 0 0 0 = −128

软链接和硬链接有何区别

原文:https://q.shanyue.tech/base/os/750.html

Issue

欢迎在 Gtihub Issue 中回答此问题: Issue 750(opens new window)

Author

回答者: Gentlemancj(opens new window)

https://www.jianshu.com/p/dde6a01c4094

这个解释满分

Author

回答者: nmsn(opens new window)

https://www.jianshu.com/p/dde6a01c4094

这个解释满分

链接挂了


我们一直在努力

apachecn/AiLearning

【布客】中文翻译组