forked from wolverinn/Waking-Up
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# 操作系统 | ||
|
||
### 进程和线程有什么区别? | ||
- 进程(Process)是系统进行资源分配和调度的基本单位,线程(Thread)是程序执行的最小单位; | ||
- 线程依赖于进程而存在,一个进程至少有一个线程; | ||
- 进程有自己的独立地址空间,线程不拥有资源,而是共享所属进程的资源;因此CPU创建和切换线程的开销比进程小得多; | ||
- 线程之间的通信更方便,同一进程下的线程共享全局变量等数据,而进程之间的通信需要以进程间通信(IPC)的方式进行; | ||
- 多线程程序只要有一个线程崩溃,整个程序就崩溃了,但多进程程序中一个进程崩溃并不会对其它进程造成影响,因为进程有自己的独立地址空间,因此多进程更加健壮 | ||
|
||
<details> | ||
<summary>进程创建举例</summary> | ||
|
||
Python代码: | ||
|
||
``` | ||
import os | ||
print('当前进程:%s 启动中 ....' % os.getpid()) | ||
pid = os.fork() | ||
if pid == 0: | ||
print('子进程:%s,父进程是:%s' % (os.getpid(), os.getppid())) | ||
else: | ||
print('进程:%s 创建了子进程:%s' % (os.getpid(),pid )) | ||
``` | ||
输出结果: | ||
|
||
``` | ||
当前进程:27223 启动中 .... | ||
进程:27223 创建了子进程:27224 | ||
子进程:27224,父进程是:27223 | ||
``` | ||
|
||
分析:fork函数分别在父进程和子进程中返回,在子进程返回的值pid永远是0,在父进程返回的是子进程的进程id(标识符)。 | ||
</details> | ||
|
||
### 进程间通信有哪些方式? | ||
1. 管道(Pipe) | ||
<details> | ||
<summary>展开</summary> | ||
|
||
- 管道是半双工的,数据只能向一个方向流动;需要双方通信时,需要建立起两个管道; | ||
- 一个进程向管道中写的内容被管道另一端的进程读出。写入的内容每次都添加在管道缓冲区的末尾,并且每次都是从缓冲区的头部读出数据; | ||
- 只能用于父子进程或者兄弟进程之间(具有亲缘关系的进程) | ||
</details> | ||
|
||
2. 命名管道(FIFO) | ||
3. 消息队列 | ||
4. 信号(Signal) | ||
5. 共享内存 | ||
6. 信号量(Semaphore) | ||
7. 套接字(Socket) | ||
|
||
### 参考 | ||
- [进程间通信IPC -- 简书](https://www.jianshu.com/p/c1015f5ffa74) | ||
- [面试/笔试第二弹 —— 操作系统面试问题集锦 - CSDN博客](https://blog.csdn.net/justloveyou_/article/details/78304294) |