week9-进程和线程关系

##线程进程大白话######

进程:是一个程序的各种资源的集合,进程包括内存页资源 文件描述符  打开的文件 认证信息 谁启动的这个进程等,进程要操作cpu 必须先创建一个线程,进程本身不会执行

线程:是操作系统最小的调度单位,是一个指令的集合

学校比作一个os 教室是一个进程,教室里面的老师和学生等就是线程 ,所有在同一个进程里的线程 是共享同一块内存空间的

进程刚启动时候会默认创建一个主线程,主线程可以创建子线程,子线程和主线程是平等关系

启动进程快还是启动线程快? 当然是启动线程快,进程是好多资源的集合 线程就是指令集合

cpu如果有 4核就可以同时运行4个任务,2.7GHz 大概可以运行xx亿次运算 运算是指二进制的运算,时钟频率是指cpu每秒可中断的次数

cpu寄存器是存上下文关系的,上一次执行到哪里等等

线程和进程的区别?

同一个进程下面的线程共享内存空间,进程的内存是独立的

创建子进程是克隆的父进程,子进程之间内存是独立的 #如果父进程里面有一个1  2个子进程里面也会有个1,但是子进程里面的1是不相等的

同一个进程的线程之间可以之间交流,2个进程想通信 必须通过一个中间代理来实现。

创建新线程很简单,创建新进程需要对其父进行一次克隆

一个线程可以控制和操作同一进程里的其他线程,但是进程只能操作子进程

对主线程的修改会影响到其他线程,但是对父进程修改不会有影响

#########线程########

线程是操作系统能够进行运算调度的最小单位。它被包含在进程之中,是进程中的实际运作单位。一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行不同的任务

A thread is an execution context, which is all the information a CPU needs to execute a stream of instructions.

Suppose you're reading a book, and you want to take a break right now, but you want to be able to come back and resume reading from the exact point where you stopped. One way to achieve that is by jotting down the page number, line number, and word number. So your execution context for reading a book is these 3 numbers.

If you have a roommate, and she's using the same technique, she can take the book while you're not using it, and resume reading from where she stopped. Then you can take it back, and resume it from where you were.

Threads work in the same way. A CPU is giving you the illusion that it's doing multiple computations at the same time. It does that by spending a bit of time on each computation. It can do that because it has an execution context for each computation. Just like you can share a book with your friend, many tasks can share a CPU.

On a more technical level, an execution context (therefore a thread) consists of the values of the CPU's registers.

Last: threads are different from processes. A thread is a context of execution, while a process is a bunch of resources associated with a computation. A process can have one or many threads.

Clarification: the resources associated with a process include memory pages (all the threads in a process have the same view of the memory), file descriptors (e.g., open sockets), and security credentials (e.g., the ID of the user who started the process).

进程与线程的区别?

  1. Threads share the address space of the process that created it; processes have their own address space.
  2. Threads have direct access to the data segment of its process; processes have their own copy of the data segment of the parent process.
  3. Threads can directly communicate with other threads of its process; processes must use interprocess communication to communicate with sibling processes.
  4. New threads are easily created; new processes require duplication of the parent process.
  5. Threads can exercise considerable control over threads of the same process; processes can only exercise control over child processes.
  6. Changes to the main thread (cancellation, priority change, etc.) may affect the behavior of the other threads of the process; changes to the parent process does not affect child processes.
posted @ 2017-09-01 08:07  warren1236  阅读(92)  评论(0)    收藏  举报