进程基础-进程的创建、管理与调度、终止
2015-12-23 15:00
211 查看
进程有三种状态:就绪、执行、阻塞。
一、进程操作
1、进程的创建
程序可通过getpid()函数获取自身所运行的进程的ID,也可以通过getppid()获取父进程ID。
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
//printf("test\n");
printf("getpid:%d\n",(int)getpid());
printf("getppid:%d\n",(int)getppid());
return 0;
}
进程的创建有system()和fork()函数。
函数原型:pid_t fork()
一个进程通过调用fork会创建一个被称为子进程的副本进程,这个子进程和父进程不同的地方只有进程的ID,其他都是一样的,因此要保存这个子进程的ID。子进程创建后,父子进程一起从调用fork()函数的地方继续执行,竞争系统资源。
若创建失败,则fork返回-1;若子进程创建成功,则fork函数在子进程中返回0,在父进程中返回子进程ID.
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main()
{
pid_t child_pid;
child_pid = fork();
if(child_pid > 0)
printf("father id:%d,child id:%d\n",getpid(),child_pid);
else if(child_pid == 0)
printf("child id :%d\n", getpid());
else
printf("create process failed\n");
return 0;
}
运行结果:
father id:2899,child id:2900
child id :2900
2、进程的管理与调度
linux内核有一个进程控制表(Process Table),此表中的每一项都是一个task_struct结构,即代表一个进程。task_struct结构中有对应进程的优先级字段,进度调度器根据优先级的值,选择最迫切需要运行的就行进程来执行。
管理最好的办法是命令:(1)ps -aux
3、进程的终止
子进程一经创建,即与父进程一起被系统调度。子进程运行完毕后,并不立即释放所占用的进程控制表项,而是作为前面提到的僵进程存在与系统中。直至父进程正常退出或调用wait函数,子进程才彻底终止。
pid_t wait(int *status)
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main()
{
pid_t child_pid, pid;
child_pid = fork();
int status = 0;
if(child_pid > 0){
printf("father id:%d,child id:%d\n",getpid(),child_pid);
pid = wait(&status);
printf("child process finished pid:%d\n",child_pid);
}
else if(child_pid == 0)
printf("child id :%d\n", getpid());
else
printf("create process failed\n");
return 0;
}
运行结果:
father id:3486,child id:3487
child id :3487
child process finished pid:3487
调用了wait函数的父进程会马上阻塞自己,由wait函数自动分析是否当前进程的某个子进程已经退出,如果找到了这样一个已经变成僵进程的子进程,wait函数就会搜集这个子进程的信息,将它彻底终止并返回子进程结束状态值;如果没有找到这样一个子进程,wait函数就会一直阻塞在这里,直到出现一个变成僵进程的子进程为止。
类似的还有 pid_t waitpid(pid_t pid, int *status, int options);
可以通过命令kill终止。
一、进程操作
1、进程的创建
程序可通过getpid()函数获取自身所运行的进程的ID,也可以通过getppid()获取父进程ID。
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
//printf("test\n");
printf("getpid:%d\n",(int)getpid());
printf("getppid:%d\n",(int)getppid());
return 0;
}
进程的创建有system()和fork()函数。
函数原型:pid_t fork()
一个进程通过调用fork会创建一个被称为子进程的副本进程,这个子进程和父进程不同的地方只有进程的ID,其他都是一样的,因此要保存这个子进程的ID。子进程创建后,父子进程一起从调用fork()函数的地方继续执行,竞争系统资源。
若创建失败,则fork返回-1;若子进程创建成功,则fork函数在子进程中返回0,在父进程中返回子进程ID.
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main()
{
pid_t child_pid;
child_pid = fork();
if(child_pid > 0)
printf("father id:%d,child id:%d\n",getpid(),child_pid);
else if(child_pid == 0)
printf("child id :%d\n", getpid());
else
printf("create process failed\n");
return 0;
}
运行结果:
father id:2899,child id:2900
child id :2900
2、进程的管理与调度
linux内核有一个进程控制表(Process Table),此表中的每一项都是一个task_struct结构,即代表一个进程。task_struct结构中有对应进程的优先级字段,进度调度器根据优先级的值,选择最迫切需要运行的就行进程来执行。
管理最好的办法是命令:(1)ps -aux
3、进程的终止
子进程一经创建,即与父进程一起被系统调度。子进程运行完毕后,并不立即释放所占用的进程控制表项,而是作为前面提到的僵进程存在与系统中。直至父进程正常退出或调用wait函数,子进程才彻底终止。
pid_t wait(int *status)
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main()
{
pid_t child_pid, pid;
child_pid = fork();
int status = 0;
if(child_pid > 0){
printf("father id:%d,child id:%d\n",getpid(),child_pid);
pid = wait(&status);
printf("child process finished pid:%d\n",child_pid);
}
else if(child_pid == 0)
printf("child id :%d\n", getpid());
else
printf("create process failed\n");
return 0;
}
运行结果:
father id:3486,child id:3487
child id :3487
child process finished pid:3487
调用了wait函数的父进程会马上阻塞自己,由wait函数自动分析是否当前进程的某个子进程已经退出,如果找到了这样一个已经变成僵进程的子进程,wait函数就会搜集这个子进程的信息,将它彻底终止并返回子进程结束状态值;如果没有找到这样一个子进程,wait函数就会一直阻塞在这里,直到出现一个变成僵进程的子进程为止。
类似的还有 pid_t waitpid(pid_t pid, int *status, int options);
可以通过命令kill终止。
相关文章推荐
- Java Web 的servlet 之过滤器 filter
- iOS应用程序生命周期(前后台切换,应用的各种状态)详解
- 证书文件(pfx)读取时报 “指定的网络密码不正确”
- 语法、函数、开发工具详细释义查询网站
- rsync 排除指定目录或文件进行同步
- 第三章
- 设计模式C++实现--外观模式、组合模式
- android tourguide 引导
- 断点续传的demo
- jquery判断滚动条是否到底部
- 已经加载了类库,还是报错启动报错:java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderList
- C语言头文件不定义全局变量、函数等
- 关联过滤 - 报表系统
- Java格式化日期
- setCompoundDrawables 不能显示Drawable出来的问题。
- Android Proguard介绍
- YARN的内存和CPU配置
- SharePoint&Infopath——添加Products.xml到表单作为数据连接
- android小问题:keystore问题-微信支付-Android studio
- 数据库的一些有关