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 ...
随机推荐
- 爬虫遇到HTTP Error 403的问题
# coding=gbk from bs4 import BeautifulSoup import requests import urllib x = 1 y = 1 def crawl(url): ...
- QT5:介绍
一.简介 QT是一个跨平台的C++开发库,主要用来开发图形用户界面(Graphical User Interface,GUI) QT除了可以绘制漂亮的界面(包括控件/布局/交互),还可以多线程/访问数 ...
- 01_4_Struts路径问题
01_4_Struts路径问题 1. Struts路径问题说明 struts2中的路径问题是根据action的路径而不是jsp路径来确定,所有尽量不要使用相对路径. 虽然可以使用redirect方式解 ...
- 使用max函数计算EXCEL个税公式
1.Max()函数是求括号内的数的最大值.2.其中,第一和第二个大括号{}内的数,相信作为财务的应该很清楚,就是个人所得税的缴税比例,以及速算个人应缴所得税的相关数据.3.在EXCEL中,使用{}表示 ...
- 前端开发面试题之JavaScript(转自公众号)(1)
js基本数据类型:Undefine Number Null Boolean String; js内置对象:数据封装类对象:object.Array.Boolean.String: 其他:Functio ...
- k8s的资源限制及资源请求
容器的资源需求及限制: 需求:requests ##定义容器运行时至少需要资源 限制:limits ##定义容器运行时最多能分配的资源 requests:pod.spec.con ...
- 如何用纯 CSS 创作一个行驶中的火车 loader
效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/RBLWzJ 可交互视频 ...
- 转载:将画布(canvas)图像保存成本地图片的方法
之前我曾介绍过如何将HTML5画布(canvas)内容转变成图片形式,方法十分简单.但后来我发现只将canvas内容转变成图片输出还不够,如何能将转变后的图片保存到本地呢? 其实,这个方法也是非常简单 ...
- Python PyAudio 安装使用
Python PyAudio安装: Python3.7 无法安装pyaudio pip install pyaudio提示error: Microsoft Visual C++ 14.0 is req ...
- selenium2等待元素加载
1.硬性等待 Thread.sleep(8000); 所谓的硬性等待就是,执行完相应操作就等待我设置的8s.无论网速快与慢,网速快的话,也许5s就打开网页了,可是程序必须接着等待剩下的3秒. 网速慢的 ...