分类: 嵌入式
2015-05-07 21:53:47
原文地址:Linux进程1-进程的概念 作者:一生有你llx
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,并设置错误码
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).