Process

program

program==code+data;

一个进程可以对应多个程序,一个程序也可以变成多个进程。程序可以作为一种软件资源长期保存,以文件的形式存放在硬盘

process:

  1. 相应的程序+CPU上下文+一组系统资源
  2. 动态性, 独立性, 并发性
  3. 一个CPU,任何时刻最多只能有一个进程去使用它

process VS thread

  • 进程的资源分配的基本单位,是分配资源的抽象:进程把一组相关资源组合起来,构成了一个资源平台,或者说资源环境,包括运行上下文,内存地址空间,打开的文件等
  • 线程是系统调度的基本单位,是执行流程的抽象:进程上的一条执行流程,就是线程
  • 早期不区分这两者,一个进程,既指它的资源平台,又指它的执行流程,后来把二者分开,所以进程=线程+资源平台
  • 在嵌入式系统中,不使用process或thread,而是使用task,具体这个task是什么得看情况,VxWorks,ucosII,Jbed里是thread; linux里是process

Linux Process Types

A process is an instance of a computer that is currently being executed. Associated with a process is a variety of attributes (ownership, nice value, and SELinux context , to name a few) that extend or limit its ability to access resources on the computer. In Linux, there are 3 types processes: User Process, Daemon Process, Kernel Process

User Process

A user process is one that is initiately by a regular user account and runs in user space. Unless it is run in a way that gives the process special permissions, an ordinary user process has no special access to the processor or to files on the system that don’t belong to the user who launched the process.

Daemon Process

A daemon process is an application that is designed to run in the background, typically managing some kind of ongoing service. They run in user space

System often start daemons at boot time and have them run continuously until the system is shut down. Daemon can also be started or stopped on demand, set to run at particular system run levels, and in some cases signaled to reload configuration information on the fly.

Although daemon processes are typically managed as services by the root user,daemon processes often run as non-root users by a user account that is dedicated to the services.

Kernel Process

A Kernel process executes only in kernel space. They are similar to daemon processes. The primary different is that kernel processes have full access to kernel data structures, which makes them more powerful than daemon processes, which run in user space. Kernel processes are not as flexible as daemon processes. You can change the behavior of a daemon process by changing configuration files and reloading the service, however, changing kernel processes may require recompiling the kernel.

Linux Process States

When a process is created, the system assigns it a state. The state field of the process descriptor describes the current state of the process. The value of the state field is usually set with a simple assignment:

8 primary states of a process in Linux:

  1. TASK_RUNNING(ready/waiting)
  2. TASK_RUNNING(running)
  3. TASK_INTERRUPTIBLE(interruptible sleep/blocked)
  4. TASK_UNINTERRUPTIBLE(uninterruptible sleep/blocked)
  5. TASK_STOPED(stoped)
  6. TASK_ZOMBIE(zombie)
  7. TASK_DEAD(dead)
  8. TASK_TRACED(traced)

3 additional states of a process in Linux:

Two additional states are available for processes in systems that support virtual memory. In both of these states, processes are "stored" on secondary memory (typically a hard disk)

  1. Swapped out and waiting (Also called suspended and waiting.)

    A process may be swapped out, that is, removed from main memory and placed on external storage by the scheduler. From here the process may be swapped back into the waiting state.
  2. Swapped out and (interruptible/uninterruptible)blocked (Also called suspended and blocked.)

    Processes that are blocked may also be swapped out. In this event the process is both swapped out and blocked, and may be swapped back in again under the same circumstances as a swapped out and waiting process.

Q: Stopped state VS Terminated state

A: Stopped means the process has received one of SIGSTOP / SIGTSTP / SIGTTIN / SIGTTOU, and won't do anything much until it receives a SIGCONT.

Zombie means the underlying program is no longer executing, but the process remains in the process table as a zombie process until its parent process calls the wait system call to read its exit status, at which point the process is removed from the process table, finally ending the process's lifetime. If the parent fails to call wait, this continues to consume the process table entry (concretely the process identifier or PID), and causes a resource leak.

Q: Interruptible Sleep State VS Uninterruptible Sleep state

A:Interruptible Sleep means the process is waiting wither for a particular time slot or a particular event to occur. Once one of those things occurs, the process will come out of Interruptible Sleep.

An Uninterruptible Sleep state is one that won’t handle a signal right away. It will wake only as a result of a waited-upon resource becoming available or after a time-out occurs duing that wait(if the time – out is specified when the process is put to sleep). It is mostly used by device drivers waiting for disk or network I/O. When the process is sleeping uninterruptibly, signals accumulated during the sleep are noticed when the process returns from system call or trap.

User Thread

  • Belongs to a user process
  • Managed by itself
  • Shares address space, open files, user credentials etc . in the same process
  • Privatize program counter, stack, and register context etc.
  • Unindependently scheduled
  • Extremely inexpensive, consume no kernel resource.
  • Useful to realize concurrence inside a user-process
  • pthread_create()

LWP

Light Weight Process--Kernel-supported user thread

  • Belongs to a user process
  • Managed by kernel
  • Shares address space, open files, user credentials etc . in the same process
  • In addition to program counter, stack, and register context etc. needs to maintain some user state
  • Independently scheduled
  • Expensive to block
  • Useful for independent tasks with little interaction with other lightweight processes
  • clone()

Kernel Thread

  • Need not be associated with a user process
  • Managed by kernel
  • Shares kernel text, global data etc.
  • Privatize program counter , kernel stack register context etc.
  • Independently scheduled
  • Inexpensive
    • Require space only for kernel stack and register context
    • Fast context switching as no memory mappings are to be flushed
  • Useful for operations such as asynchronous I/O
  • Request can be synchronously handled by the kernel thread
  • fork()/vfork()

Linux Process VS Thread VS LWP的更多相关文章

  1. Linux process vs thread

    Linux process vs thread Question I have a query related to the implementation of threads in Linux. L ...

  2. process vs thread

    process vs thread http://blog.csdn.net/mishifangxiangdefeng/article/details/7588727 6.进程与线程的区别:系统调度是 ...

  3. Activity, Service,Task, Process and Thread之间的关系

    Activity, Service,Task, Process and Thread之间到底是什么关系呢? 首先我们来看下Task的定义,Google是这样定义Task的:a task is what ...

  4. kafka.common.KafkaException: Failed to acquire lock on file .lock in /tmp/kafka-logs. A Kafka instance in another process or thread is using this directory.

    1.刚才未启动zookeeper集群的时候,直接启动kafka脚本程序,kafka报错了,但是进程号启动起来来,再次启动出现如下所示的问题,这里先将进程号杀死,再启动脚本程序. [hadoop@sla ...

  5. Performance Tuning Using Linux Process Management Commands

    [root@bigdata-server-02 /]# ps --help all Usage: ps [options] Basic options: -A, -e all processes -a ...

  6. Shell script for logging cpu and memory usage of a Linux process

    Shell script for logging cpu and memory usage of a Linux process http://www.unix.com/shell-programmi ...

  7. Failed to acquire lock on file .lock in /tmp/kafka-logs. A Kafka instance in another process or thread is using this directory.

    1. 问题现象 启动 kafka 时报错:Failed to acquire lock on file .lock in /tmp/kafka-logs. A Kafka instance in an ...

  8. yum安装提示错误Thread/process failed: Thread died in Berkeley DB library

    问题描述: yum 安装更新提示 rpmdb: Thread/process failed: Thread died in Berkeley DB library 问题解决: 01.删除yum临时库文 ...

  9. Difference between Process and thread?

    What are the differences between a process and a thread? How are they similar? How can 2 threads com ...

随机推荐

  1. 【JUC】JDK1.8源码分析之CopyOnWriteArrayList(六)

    一.前言 由于Deque与Queue有很大的相似性,Deque为双端队列,队列头部和尾部都可以进行入队列和出队列的操作,所以不再介绍Deque,感兴趣的读者可以自行阅读源码,相信偶了Queue源码的分 ...

  2. Ajax跨域访问wcf服务中所遇到的问题总结。

    工具说明:vs2012,sql server 2008R2 1.首先,通过vs2012建立一个wcf服务项目,建立好之后.再新开一个vs2012 建立web项目,通过jQuery的ajax方法访问服务 ...

  3. Ionic2学习笔记(9):访问本地设备

    作者:Grey 原文地址: http://www.cnblogs.com/greyzeng/p/5559927.html               Ionic2提供了访问本地设备的方法,但是需要安装 ...

  4. EntityFramework查询--联合查询(Join,GroupJoin)

    首先我们先看一下Join public static IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult>(this ...

  5. [.NET逆向] .net IL 指令速查(net破解必备)

    .net的破解比较特殊,很多人看见IL就头疼,最近在研究的时候发现了这个东东 相信对广大学习net破解的人一定有帮助 .对上指令表一查,跟读原代码没什么区别了, 名称 说明 Add 将两个值相加并将结 ...

  6. jQuery手机菜单

      效果展示 http://hovertree.com/texiao/nav/4/ 手机扫描二维码查看效果: 源码下载 http://hovertree.com/h/bjaf/kroft6c7.htm ...

  7. Visual Studio 2015在.NET Core RC2项目中的一个错误。

    更新了.NET Core RC2 之后,VS的Web Tools更新为“Preview 1”了. 这个版本有一个问题,害我折腾了一个下午. 就是在项目界面的“依赖项 - NPM”上面错误地显示了不必要 ...

  8. Unity3D 5.x 简单实例 - 发射炮弹

    1,下载.安装: http://unity3d.com/cn/get-unity/download/archive 建议直接借助 UnityDownloadAssistant 进行安装,根据需要勾选需 ...

  9. 第一个Java程序HelloWorld

    代码如下: // 一个文件中只能有一个共有的类,并且与文件名称一致,大小写注意 public class HelloWorld{// 程序的入口public static void main(Stri ...

  10. JavaScript基本语法(二)

    上篇博文写到JavaScript的数据类型.JavaScript包括了字符串(String).数字(Number).布尔(Boolean).数组(Array).对象(Object).空(Null).未 ...