Linux进程1-进程的概念

简介: 1、 什么是进程 一个正在运行的程序就是一个进程 2、 并发、并行、异步、同步、临界区 并发:针对一个处理器,看起来同时进行。一个处理器在同一时刻只能执行一个程序,但是CPU在多个进程之间快速切换,所以看起是同时进行的。

1、 什么是进程

一个正在运行的程序就是一个进程

2、 并发、并行、异步、同步、临界区

并发:针对一个处理器,看起来同时进行。一个处理器在同一时刻只能执行一个程序,但是CPU在多个进程之间快速切换,所以看起是同时进行的。这是一种假象 

并行:针对多个处理器,一台机器上有多个处理器,那么进程就有可能同时执行。

异步:是两个完全没有关系的操作。

同步:如果两个操作不应该同时发生,就需要有一种机制确保他们一定不会同时发生。(多个程序同时修改同时变量,这种操作是危险的,所以就需要同步)

临界区:多个进程同时访问的代码,临界区要求同一时刻只能有一个程序访问,需要同步机制

3、调度,cpu如何去执行程序

时间片轮,将CPU的时间平均分配

FIFO(first input first output),先进先出,先进来的先执行

短进程优先,时间短的先执行

高优先级调度,级别高的进程先执行

 

4、 死锁

多个进程相互竞争同一个资源

Sleep()睡眠会使其他程序先执行,这样就最简单的消除死锁,虽然不是很正确。

5、进程标识符ID

      pid_t  类型,它是一个无符号的整数unsigned int   %u

6、 创建一个进程fork()函数

   fork()创建一个子进程,有2个返回值。调用fork函数的进程叫父进程,新产生的进程叫子进程。子进程会复制父进程的数据。如果返回非0值,那么这个返回值是子进程的ID,但是这个ID是穿给父进程的。如果返回值是0,那代表当前是子进程。

   进程ID是进程唯一的标志,关于进程所有的操作都要通过ID

   获取当前进程ID getpid()

   获取父进程的ID getppid()

7、手册

FORK(2)                    Linux Programmer’s Manual                   FORK(2)

NAME
       fork - create a child process
        //创造一个子进程

SYNOPSIS
       #include
        //包含头文件

       pid_t fork(void);
        //不用传参,返回值是pid_t类型,其实一个无符号整数

DESCRIPTION
       fork() creates a new process by duplicating the calling process.  The new process, referred to as the child, is an exact
       duplicate of the calling process, referred to as the parent, except for the following points:
        //fork通过复制调用它的进程,来创造一个新的进程。

       *  The child has its own unique process ID, and  this  PID  does  not  match  the  ID  of  any  existing  process  group
          (setpgid(2)).
        //子进程有自己的ID,这个ID不和现有的进程组ID匹配

       *  The child’s parent process ID is the same as the parent’s process ID.

       *  The child does not inherit its parent’s memory locks (mlock(2), mlockall(2)).
        //子进程不会继承父进程的锁

       *  Process resource utilizations (getrusage(2)) and CPU time counters (times(2)) are reset to zero in the child.
        

       *  The child’s set of pending signals is initially empty (sigpending(2)).
        //子进程清空信号集

       *  The child does not inherit semaphore adjustments from its parent (semop(2)).
        //子进程没有继承父进程的信号

       *  The child does not inherit record locks from its parent (fcntl(2)).
          //子进程没有继承父进程的记录锁

       *  The child does not inherit timers from its parent (setitimer(2), alarm(2), timer_create(2)).
          //子进程没有继承父进程的时间

       *  The  child  does not inherit outstanding asynchronous I/O operations from its parent (aio_read(3), aio_write(3)), nor
          does it inherit any asynchronous I/O contexts from its parent (seeio_setup(2)).
          //子进程没有继承父进程的异步IO操作

       The process attributes in the preceding list are all specified in POSIX.1-2001.  The parent and child also  differ  with
       respect to the following Linux-specific process attributes:
          //子进程和父进程在进程属性方面也不一样

       *  The  child does not inherit directory change notifications (dnotify) from its parent (see the description of F_NOTIFY
          in fcntl(2)).
       *  The prctl(2) PR_SET_PDEATHSIG setting is reset so that the child does not receive a signal  when  its  parent  termi-
          nates.
          // PR_SET_PDEATHSIG被复位,确保在父进程结束的时候,子进程不会接收到信号

       *  Memory mappings that have been marked with the madvise(2) MADV_DONTFORK flag are not inherited across a fork().
          //内存映射被标记为MADV_DONTFORK,而不是从fork继承

       *  The termination signal of the child is always SIGCHLD (see clone(2)).
          //子进程结束的信号是SIGCHLD

       Note the following further points:

       *  The  child process is created with a single thread — the one that called fork().  The entire virtual address space of
          the parent is replicated in the child, including the states of  mutexes,  condition  variables,  and  other  pthreads
          objects; the use of pthread_atfork(3) may be helpful for dealing with problems that this can cause.


       *  The  child inherits copies of the parent’s set of open file descriptors.  Each file descriptor in the child refers to
          the same open file description (see open(2)) as the corresponding file descriptor in the parent.  This means that the
          two descriptors share open file status flags, current file offset, and signal-driven I/O attributes (see the descrip-
          tion of F_SETOWN and F_SETSIG in fcntl(2)).


       *  The child inherits copies of the parent’s set of open message queue descriptors (see mq_overview(7)).  Each  descrip-
          tor  in  the  child  refers to the same open message queue description as the corresponding descriptor in the parent.
          This means that the two descriptors share the same flags (mq_flags).


       *  The child inherits copies of the parent’s set of open directory streams (see opendir(3)).  POSIX.1-2001 says that the
          corresponding  directory  streams  in the parent and child may share the directory stream positioning; on Linux/glibc
          they do not.


RETURN VALUE
       On success, the PID of the child process is returned in the parent, and 0 is returned in the child.  On failure,  -1  is
       returned in the parent, no child process is created, and errno is set appropriately.
        //如果成功,在父进程中返回子进程的ID,在子进程中返回0.如果失败返回-1,并设置错误码

ERRORS
       EAGAIN
        fork()  cannot  allocate sufficient memory to copy the parent’s page tables and allocate a task structure for the
              child.
        //没有足够的内存

       EAGAIN
        It was not possible to create a new process because the caller’s RLIMIT_NPROC resource limit was encountered.  To exceed this limit,
        the process must have either the CAP_SYS_ADMIN or the CAP_SYS_RESOURCE capability.


       ENOMEM fork() failed to allocate the necessary kernel structures because memory is tight.
          //没有申请到足够的内核结构,由于内存满了

CONFORMING TO
       SVr4, 4.3BSD, POSIX.1-2001.

NOTES
       Under  Linux, fork() is implemented using copy-on-write pages, so the only penalty that it incurs is the time and memory
       required to duplicate the parent’s page tables, and to create a unique task structure for the child.


       Since version 2.3.3, rather than invoking the kernel’s fork() system call, the glibc fork() wrapper that is provided  as
       part  of  the  NPTL threading implementation invokes clone(2) with flags that provide the same effect as the traditional
       system call.  The glibc wrapper invokes any fork handlers that have been established using pthread_atfork(3).


相关文章
|
11月前
|
并行计算 Linux
Linux内核中的线程和进程实现详解
了解进程和线程如何工作,可以帮助我们更好地编写程序,充分利用多核CPU,实现并行计算,提高系统的响应速度和计算效能。记住,适当平衡进程和线程的使用,既要拥有独立空间的'兄弟',也需要在'家庭'中分享和并行的成员。对于这个世界,现在,你应该有一个全新的认识。
373 67
|
10月前
|
NoSQL Linux 编译器
GDB符号表概念和在Linux下获取符号表的方法
通过掌握这些关于GDB符号表的知识,你可以更好地管理和理解你的程序,希望这些知识可以帮助你更有效地进行调试工作。
426 16
|
10月前
|
Web App开发 Linux 程序员
获取和理解Linux进程以及其PID的基础知识。
总的来说,理解Linux进程及其PID需要我们明白,进程就如同汽车,负责执行任务,而PID则是独特的车牌号,为我们提供了管理的便利。知道这个,我们就可以更好地理解和操作Linux系统,甚至通过对进程的有效管理,让系统运行得更加顺畅。
284 16
|
10月前
|
Unix Linux
对于Linux的进程概念以及进程状态的理解和解析
现在,我们已经了解了Linux进程的基础知识和进程状态的理解了。这就像我们理解了城市中行人的行走和行为模式!希望这个形象的例子能帮助我们更好地理解这个重要的概念,并在实际应用中发挥作用。
201 20
|
9月前
|
监控 Shell Linux
Linux进程控制(详细讲解)
进程等待是系统通过调用特定的接口(如waitwaitpid)来实现的。来进行对子进程状态检测与回收的功能。
214 0
|
9月前
|
存储 负载均衡 算法
Linux2.6内核进程调度队列
本篇文章是Linux进程系列中的最后一篇文章,本来是想放在上一篇文章的结尾的,但是想了想还是单独写一篇文章吧,虽然说这部分内容是比较难的,所有一般来说是简单的提及带过的,但是为了让大家对进程有更深的理解与认识,还是看了一些别人的文章,然后学习了学习,然后对此做了总结,尽可能详细的介绍明白。最后推荐一篇文章Linux的进程优先级 NI 和 PR - 简书。
285 0
|
9月前
|
存储 Linux Shell
Linux进程概念-详细版(二)
在Linux进程概念-详细版(一)中我们解释了什么是进程,以及进程的各种状态,已经对进程有了一定的认识,那么这篇文章将会继续补全上篇文章剩余没有说到的,进程优先级,环境变量,程序地址空间,进程地址空间,以及调度队列。
168 0
|
9月前
|
Linux 调度 C语言
Linux进程概念-详细版(一)
子进程与父进程代码共享,其子进程直接用父进程的代码,其自己本身无代码,所以子进程无法改动代码,平时所说的修改是修改的数据。为什么要创建子进程:为了让其父子进程执行不同的代码块。子进程的数据相对于父进程是会进行写时拷贝(COW)。
230 0
|
12月前
|
存储 Linux 调度
【Linux】进程概念和进程状态
本文详细介绍了Linux系统中进程的核心概念与管理机制。从进程的定义出发,阐述了其作为操作系统资源管理的基本单位的重要性,并深入解析了task_struct结构体的内容及其在进程管理中的作用。同时,文章讲解了进程的基本操作(如获取PID、查看进程信息等)、父进程与子进程的关系(重点分析fork函数)、以及进程的三种主要状态(运行、阻塞、挂起)。此外,还探讨了Linux特有的进程状态表示和孤儿进程的处理方式。通过学习这些内容,读者可以更好地理解Linux进程的运行原理并优化系统性能。
449 4
|
12月前
|
Linux 数据库 Perl
【YashanDB 知识库】如何避免 yasdb 进程被 Linux OOM Killer 杀掉
本文来自YashanDB官网,探讨Linux系统中OOM Killer对数据库服务器的影响及解决方法。当内存接近耗尽时,OOM Killer会杀死占用最多内存的进程,这可能导致数据库主进程被误杀。为避免此问题,可采取两种方法:一是在OS层面关闭OOM Killer,通过修改`/etc/sysctl.conf`文件并重启生效;二是豁免数据库进程,由数据库实例用户借助`sudo`权限调整`oom_score_adj`值。这些措施有助于保护数据库进程免受系统内存管理机制的影响。