Linux System Programming 学习笔记(五) 进程管理
1. 进程是unix系统中两个最重要的基础抽象之一(另一个是文件)
2. pid
3. 运行新进程
pid_t pid = fork();
if (pid == -) {
fprintf(stderr, "fork error\n");
} /* the child */
if (pid == ) {
const char* args[] = {"grep", NULL};
int ret = execv("bin/grep", args);
if (ret == -) {
fprintf(stderr, "execv error\n");
exit(EXIT_FAILURE);
}
}
4. copy-on-write
5. exit
6. wait
#include <sys/types.h>
#include <sys/wait.h> /* wait() returns the pid of a terminated child or −1 on error. */
pid_t wait (int *status); pid_t waitpid (pid_t pid, int *status, int options);
7. system
#define _XOPEN_SOURCE /* if we want WEXITSTATUS, etc. */
#include <stdlib.h>
int system (const char *command);
/* my_system - sync spwans and waits for the command
* /bin/sh -c <cmd> .
*
* Return -1 on error of any sort, or the exit code from
* the launched process. Does not block or ignore any signals.
*/ int my_system(const char* cmd)
{
int status;
pid_t pid = fork();
if (pid == -) {
fprintf(stderr, "fork error\n");
return -;
} else if (pid == ) {
const char* argv[];
argv[] = "sh";
argv[] = "-c";
argv[] = "cmd";
argv[] = NULL;
execv("bin/sh", argv);
exit(-);
}
if (waitpid(pid, &status, ) == -) {
return -;
} else if (WIFEXITED(status)) {
return WEXITSTATUS(status);
}
return -;
}
8. 僵尸进程
9. Users and Groups
#include <sys/types.h>
#include <unistd.h> int setuid (uid_t uid);
int setgid (gid_t gid); uid_t getuid (void);
gid_t getgid (void);
/* one process group containing three processes */
$ cat ship-inventory.txt | grep booty | sort
10. 守护进程
Linux System Programming 学习笔记(五) 进程管理的更多相关文章
- Linux System Programming 学习笔记(九) 内存管理
1. 进程地址空间 Linux中,进程并不是直接操作物理内存地址,而是每个进程关联一个虚拟地址空间 内存页是memory management unit (MMU) 可以管理的最小地址单元 机器的体系 ...
- Linux System Programming 学习笔记(八) 文件和目录管理
1. 文件和元数据 每个文件都是通过inode引用,每个inode索引节点都具有文件系统中唯一的inode number 一个inode索引节点是存储在Linux文件系统的磁盘介质上的物理对象,也是L ...
- Linux System Programming 学习笔记(一) 介绍
1. Linux系统编程的三大基石:系统调用.C语言库.C编译器 系统调用:内核向用户级程序提供服务的唯一接口.在i386中,用户级程序执行软件中断指令 INT n 之后切换至内核空间 用户程序通过寄 ...
- Linux System Programming 学习笔记(十) 信号
1. 信号是软中断,提供处理异步事件的机制 异步事件可以是来源于系统外部(例如用户输入Ctrl-C)也可以来源于系统内(例如除0) 内核使用以下三种方法之一来处理信号: (1) 忽略该信号.SIG ...
- Linux System Programming 学习笔记(七) 线程
1. Threading is the creation and management of multiple units of execution within a single process 二 ...
- Linux System Programming 学习笔记(六) 进程调度
1. 进程调度 the process scheduler is the component of a kernel that selects which process to run next. 进 ...
- Linux System Programming 学习笔记(四) 高级I/O
1. Scatter/Gather I/O a single system call to read or write data between single data stream and mu ...
- Linux System Programming 学习笔记(二) 文件I/O
1.每个Linux进程都有一个最大打开文件数,默认情况下,最大值是1024 文件描述符不仅可以引用普通文件,也可以引用套接字socket,目录,管道(everything is a file) 默认情 ...
- Linux System Programming 学习笔记(十一) 时间
1. 内核提供三种不同的方式来记录时间 Wall time (or real time):actual time and date in the real world Process time:the ...
随机推荐
- PCA检测人脸的简单示例_matlab实现
PCA检测人脸的简单示例,matlab R2009b上实现训练:训练用的20副人脸: %训练%Lx=X'*Xclear;clc;train_path='..\Data\TrainingSet\';ph ...
- centos启动流程
centos6启动流程 1.主板,post加电自检,检查硬件环境 2.主板选择一个硬盘进行引导,执行mbr446 grub stage1 3.grub stage1.5 加载/boot分区文件系统驱动 ...
- 浅谈 MySQL 中优化 SQL 语句查询常用的 30 种方法
1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中使用!=或<>操作符,否则将引擎放弃使用索 ...
- jenkins+maven+svn 自动化部署
背景: 公司的web平台使用JAVA写的,但是不是用Tomcat部署的,代码内部自带了Web服务器,所以只需要有JAVA环境,将代码打包上传,启动脚本就可以. 项目是根据pom.xml打包成的是.zi ...
- GoogleTest 之路1-Generic Build Instructions编译指导总方案
准备工作 为了在你的测试中使用GoogleTest, 你必须让你的编译系统 知道到哪里去寻找GoogleTest 的头文件和源文件. 具体的方法只能依赖于你具体使用的哪种编译系统了,一般来讲这个非常容 ...
- CUB reduce errorinvalid configuration argument
解决CUB reduce errorinvalid configuration argument问题 在写TensorFlow代码时遇到报错 CUB reduce errorinvalid confi ...
- python 中 * 与**的使用
1.参数中使用如果是函数定义中参数前的*表示的是将调用时的多个参数放入元组中,**则表示将调用函数时的关键字参数放入一个字典中 如定义以下函数 def func(*args): print(args) ...
- apicloud入门学习笔记1:简单介绍
官网地址:https://www.apicloud.com/ 新手开发指南:https://docs.apicloud.com/APICloud/junior-develop-guide 开发语言:H ...
- STM32F407VET6之IAR之ewarm7.80.4工程建立(基于官方固件库1.6版本) 的工程文件目录
最后整理结构如下所示,├─cmsis│ startup_stm32f401xx.s│ startup_stm32f40xx.s│ startup_stm32f40_41xxx.s│ startup_s ...
- zoj 4054
#define ll long long ; int t; ll ans,tmp; char s[N]; int main() { scanf("%d",&t); whil ...