现在的位置: 首页 > 综合 > 正文

[Linux学习笔记]进程概念及控制

2013年03月19日 ⁄ 综合 ⁄ 共 1100字 ⁄ 字号 评论关闭

内容:进程的标识、运行、终止、进程间竞争和进程操作

1.进程的运行和终止

每个进程都有唯一的ID,ID为0 的系统进程swapper称作交换进程,是一个系统调度进程;ID为1的进程init是一个用户进程,负责读与系统有关的初始化文件;ID为2的进程是pagedaemon页精灵进程,是系统进程。这些进程是特殊的进程,是不能被中止的。

关于进程的运行和终止的一些函数:fork、vfork、exec、exit。

fork函数是Linux系统创建新进程的惟一方法。fork函数可以创建除交换进程、init进程和页精灵进程之外的用户进程。

vfork函数用来创建一个新进程,进程建立以后马上执行exec一执行一个新程序。

exec本身不能建立新进程,因为执行exec前后原父进程和子进程的ID并没有改变。

exit函数调用由atexit注册的清除的函数,并关闭所有的标准I/O流。进程的退出状态很重要,一般以0值表示正常退出。

函数的原型为:

#include <sys/types.h>
#include <unistd.h>
pid_t fork(void);
pid_t vfork(void);

//
#include <unistd.h>
int execl(const char *pathname, const char arg0,...);
int execv(const char *pathname, char *const argv[]);
int execle(const char *pathname, const char *arg0,...);
int execve(const char *pathname, char *const argv[], char *const envp[]);
int excelp(const *filename, const char *arg0,...);
int execvp(const char *filename, char *const argv[]);

//
#inlcude <stdlib.h>
void exit(int status);

#inlcude <unistd.h>
void _exit(int status);

2.wait操作

当调用wait或者waitpid系统调用,进程可能出现3种状态:阻塞(当其所有子进程还在运行)、子进程处于终止状态(立即返回)和出错立即返回(没有子进程)。

wait函数的格式:

#include <sys/types.h>
#include <sys/wait.h>
pid_t wait(int *statloc);
pid_t waitpid(pid_t pid, int statloc, int options);

//成功返回进程ID,失败返回-1

抱歉!评论已关闭.