进程创建

普通函数调用完成后,最多返回(return)一次,但fork/vfork会返回二次,一次返回给父进程,一次返回给子进程

父进程的返回值为子进程的进程ID,子进程的返回值为0

1.pid_t fork(void)

父子进程共享代码段,fork之后子进程获得父进程数据空间、堆和栈的副本,然后各自独立操作自己的数据,但他们共享文件描述符

2.pid_t vfork(void)

父子进程共享资源不分家;子进程比父进程先运行,子进程结束后再运行父进程,像普通的函数调用一样

进程同步

1.pid_t wait(int *statloc)

由父进程调用,阻塞式等待任一子进程结束

2.pid_t waitpid(pid_t pid, int *statloc, int options)

可以选择等待指定子进程结束,可设置成非阻塞式等待

调用外部程序

1.exec函数家庭

exec函数族不创建新进程,只是用磁盘上的一个新程序替换了当前进程的正文段、数据段、堆和栈段

特别注意的是exec执行成功时不返回(就是不再执行exec下面的语句),在载入的程序执行完后就退出了

2.int system(char *cmdstring) 系统调用

system适合拿来调用系统内置的命令或shell脚本

进程优先级

1.int nice(int incr)

主动降低使用cpu的频率

2.int getpriority(int which, id_t who)

获取nice值

3.int setpriority(int which, id_t who, int value);

可以为进程、进程组和特定用户的所有进程设置优先级

进程终止

1.正常终止

  • 从main返回:return 0,关闭io流等资源文件
  • exit:同return 0
  • _exit:仅将自身设置成不可运行,由父进程调用wait/waitpid进行资源回收

2.异常终止

  • abort:因特殊情况主动终止
  • 由一个信号终止:被其它程序kill或运行期间产生错误(越界内存访问/除零等)被系统中止

例子

1.wait/waitpid/fork基本用法

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h> int main(){
pid_t pid;
if((pid=fork()) <0){
perror("fork error");
return -1;
}else if(pid==0){
if(system("ls -l")<0){
puts("system error");
_exit(-1);
}
_exit(0);
} if(waitpid(pid,NULL,0) != pid)
puts("wait error"); if((pid=fork()) <0){
perror("fork error");
return -1;
}else if(pid==0){
execlp("date","date",(char *)0);
puts("if execlp goes wrong,you will see me!");
_exit(-1);
} if(wait(NULL)<0)
puts("wait error"); return 0;
}

2.退出状态

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/wait.h> void pr_exit(int status){
if(WIFEXITED(status))
printf("normal exit, exit status=%d\n",WEXITSTATUS(status));
else if(WIFSIGNALED(status))
printf("abnormal exit, signal number=%d\n",WTERMSIG(status));
else if(WIFSTOPPED(status))
printf("child stoped, signal number=%d\n",WSTOPSIG(status));
else
printf("unknown exit\n");
} int main(){
pid_t pid;
int status;
//_exit
if((pid=fork())<0){
perror("fork error");
return -1;
}else if(pid==0)
_exit(7); if(wait(&status) != pid){
perror("wait error");
return -1;
}
pr_exit(status);
//abort
if((pid=fork())<0){
perror("fork error");
return -1;
}else if(pid==0)
abort(); if(wait(&status) != pid){
perror("wait error");
return -1;
}
pr_exit(status);
//divide by 0
if((pid=fork())<0){
perror("fork error");
return -1;
}else if(pid==0)
status /=0; if(wait(&status) != pid){
perror("wait error");
return -1;
}
pr_exit(status); return 0;
}

linux 进程控制笔记的更多相关文章

  1. linux进程学习笔记

    学习了linux下的进程,觉得应该整理一下,忘得差不多了,顺便回顾一下. 学而时习之,不亦说乎~~ 进程笔记 ,什么是进程? The Single UNIX Specification, Versio ...

  2. Linux进程控制(二)

    1. 进程的创建 Linux下有四类创建子进程的函数:system(),fork(),exec*(),popen() 1.1. system函数 原型: #include <stdlib.h&g ...

  3. Linux进程控制(一)

    1. Linux进程概述 进程是一个程序一次执行的过程,它和程序有本质区别.程序是静态的,它是一些保存在磁盘上的指令的有序集合:而进程是一个动态的概念,它是一个运行着的程序,包含了进程的动态创建.调度 ...

  4. Linux 进程管理 笔记

    https://www.ibm.com/developerworks/cn/linux/l-linux-process-management/index.htmlLinux 进程管理剖析 进程可以是短 ...

  5. Linux - 进程控制 代码(C)

    进程控制 代码(C) 本文地址:http://blog.csdn.net/caroline_wendy 输出进程ID.getpid(). 代码: /*By C.L.Wang * Eclipse CDT ...

  6. linux 进程学习笔记-运行新进程

    我们知道,当用fork启动一个新进程以后,新进程会复制父进程的大部份内存空间并接着运行父进程中的代码,如果我们使新进程不运行原父进程的代码,转而运行另外一个程序集中的代码,这就相当于启动了一个新程序. ...

  7. Linux 进程控制

    分享知乎上看到的一句话,共勉: 学习周期分为学习,思考,实践,校正四个阶段,周期越短,学习效率越高. 前面讲的都是操作系统如何管理进程,接下来,看看用户如何进行进程控制. 1.进程创建 先介绍一下函数 ...

  8. Linux进程控制(三)

    1. 进程间打开文件的继承 1.1. 用fork继承打开的文件 fork以后的子进程自动继承了父进程的打开的文件,继承以后,父进程关闭打开的文件不会对子进程造成影响. 示例: #include < ...

  9. Linux进程控制——exec函数族

    原文:http://www.cnblogs.com/hnrainll/archive/2011/07/23/2114854.html 1.简介 在Linux中,并不存在exec()函数,exec指的是 ...

随机推荐

  1. No saved view state could be found for the view identifier

    解决方法: javax.faces.application.ViewExpiredException:No saved view state could be found for the view i ...

  2. MySQL优化之COUNT(*)效率

    MySQL优化之COUNT(*)效率 刚给一个朋友解决他写的Discuz!插件的问题,说到MySQL的COUNT(*)的效率,发现越说越说不清楚,干脆写下来,分享给大家. COUNT(*)与COUNT ...

  3. 《UNIX环境高级编程》学习心得 二

    窝萌来看我们看到这本书里的第一个程序 #include "apue.h" #include <dirent.h> int main(int argc, char *ar ...

  4. BZOJ 2879: [Noi2012]美食节 最小费用流 动态添边

    2879: [Noi2012]美食节 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 324  Solved: 179[Submit][Status] ...

  5. PBS常用指令合集

    以下以任务名 job.pbs对应任务ID 12341234为代表,提交者用户名为user. 1.基本指令-最常用 提交作业 qsub job.pbs 查询全部作业 qstat 查询个人作业 qstat ...

  6. iOS UIView简单缩放动画

    @interface ViewController () { UIView *animationView; UIButton *button; CGPoint animationPoint; } @e ...

  7. Swiper之滑块4

    最炫3D走一波: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" ...

  8. Cocos2d-JS中的cc.LabelAtlas

    cc.LabelAtlas是图片集标签,其中的Atlas本意是“地图集”.“图片集”,这种标签显示的文字是从一个图片集中取出的,因此使用cc.LabelAtlas需要额外加载图片集文件.cc.Labe ...

  9. iOS中关于.pch的新建与配置问题

    以前版本的Xcode新建一个项目都会自动生成.pch,这个文件的好处是,里面添加的东西会自动添加到每个类中,也就是说我们可以把要用的宏定义,和多个头文件等放到.pch中,这样我们就不需要重复的在每个类 ...

  10. unicode 汉字编码表

    啊:21834 阿:38463 埃:22467 挨:25384 哎:21710 唉:21769 哀:21696 皑:30353 癌:30284 蔼:34108矮:30702 艾:33406 碍:308 ...