🦄 refactor(C13, C14, C115): 更新了linux系统开发部分的结构
This commit is contained in:
298
C15-进程/C15-进程.md
Normal file
298
C15-进程/C15-进程.md
Normal file
@@ -0,0 +1,298 @@
|
||||
# 目录
|
||||
- [目录](#目录)
|
||||
- [进程基本知识](#进程基本知识)
|
||||
- [进程标识符`pid`](#进程标识符pid)
|
||||
- [进程的产生](#进程的产生)
|
||||
- [进程的消亡及释放资源](#进程的消亡及释放资源)
|
||||
- [`exec`函数族](#exec函数族)
|
||||
- [用户权限及组权限](#用户权限及组权限)
|
||||
- [观摩课:解释器文件](#观摩课解释器文件)
|
||||
- [`system()`函数](#system函数)
|
||||
- [进程会计](#进程会计)
|
||||
- [进程时间](#进程时间)
|
||||
- [守护进程](#守护进程)
|
||||
- [系统日志](#系统日志)
|
||||
|
||||
|
||||
# 进程基本知识
|
||||
|
||||
已经进入**多进程**阶段
|
||||
|
||||
## 进程标识符`pid`
|
||||
|
||||
类型`pid_t`,传统意义上是一个16位有符号整型数。
|
||||
|
||||
命令`ps`
|
||||
|
||||
常用命令:`ps axf`,`ps aux`,`ps axm`,`ps ax -L`
|
||||
|
||||
进程号是顺次向下使用
|
||||
|
||||
```c
|
||||
// 返回当前进程号
|
||||
pid_t getpid(void);
|
||||
|
||||
// 返回父进程的进程号
|
||||
pid_t getppid(void);
|
||||
```
|
||||
|
||||
|
||||
|
||||
## 进程的产生
|
||||
|
||||
`pid_t fork();`
|
||||
|
||||
- 以**复制(duplicating)**当前进程的方式创建一个新进程
|
||||
- 和`setjmp`一样,执行一次,返回两次
|
||||
- 在`fork`处复制,不会从头运行
|
||||
|
||||
`fork`后父子进程的不同之处:
|
||||
|
||||
1. `fork`的返回值不一样
|
||||
2. `pid`不同
|
||||
3. `ppid`也不同
|
||||
4. 未决信号和文件锁不继承
|
||||
5. 资源利用量清0
|
||||
|
||||
`init`进程:**1号**,是所有进程的祖先进程
|
||||
|
||||
调度器的调度策略来决定哪个进程先执行
|
||||
|
||||
`fflush()`的重要性
|
||||
|
||||
```c
|
||||
/*
|
||||
* vfork创建的子进程只能做exec或者exit
|
||||
* ! 基本废弃
|
||||
*/
|
||||
pid_t vfork(void);
|
||||
```
|
||||
|
||||
|
||||
|
||||
## 进程的消亡及释放资源
|
||||
|
||||
```c
|
||||
// 等待进程状态发生变化
|
||||
pid_t wait(int *status); // 阻塞
|
||||
|
||||
pid_t waitpid(pid_t pid, int *status, int options);
|
||||
|
||||
int waitid(idtype_t idtype, id_t id, siginfo_t *infop, int options);
|
||||
|
||||
|
||||
wait3();
|
||||
wait4();
|
||||
```
|
||||
|
||||
分配法和交叉分配法,90%优先选择交叉分配法。
|
||||
|
||||
池类算法:
|
||||
上游往池子里放任务,下游三个线程从池子里取任务。
|
||||
|
||||
|
||||
|
||||
## `exec`函数族
|
||||
|
||||
eg. `bash`进程创建`primer`进程
|
||||
|
||||
```c
|
||||
// exec函数族:替换当前进程的映像
|
||||
|
||||
extern char **environ;
|
||||
|
||||
int execl(const char *path, const char *arg, ...);
|
||||
|
||||
int execlp(const char *file, const char *arg, ...);
|
||||
|
||||
int execle(const char *path, const char *arg, ..., char * const envp[]);
|
||||
|
||||
int execv(const char *path, char *const argv[]);
|
||||
|
||||
int execvp(const char *file, char *const argv[]);
|
||||
|
||||
```
|
||||
|
||||
## 用户权限及组权限
|
||||
|
||||
`u+s`:如果文件是可执行的,则执行文件时,是以文件的拥有者的权限执行的。
|
||||
|
||||
```bash
|
||||
-rwsr-xr-x 1 root root 68248 Mar 23 2023 /usr/bin/passwd
|
||||
```
|
||||
|
||||
所以普通用户执行`passwd`时,是以`root`的权限执行的。
|
||||
|
||||
`g+s`:如果文件是可执行的,则执行文件时,是以文件的所在组的权限执行的。
|
||||
|
||||
`uid`和`gid`都有三种类型:
|
||||
|
||||
1. `real uid`:进程的实际所有者
|
||||
2. `effective uid`:进程的有效所有者
|
||||
3. `saved uid`:进程的保存的有效所有者
|
||||
|
||||
```
|
||||
shell获取身份的流程
|
||||
fork exec fork
|
||||
init -->--> getty -->--> login -->--> shell
|
||||
exec exec
|
||||
root root root user
|
||||
```
|
||||
|
||||
```c
|
||||
// 获取当前用户的real uid
|
||||
uid_t getuid(void);
|
||||
|
||||
// 获取当前用户的effective uid
|
||||
uid_t geteuid(void);
|
||||
|
||||
// 获取当前进程的real gid
|
||||
pid_t getegid(void);
|
||||
|
||||
// 获取当前进程的effective gid
|
||||
pid_t getgid(void);
|
||||
|
||||
// 设置当前进程的real uid
|
||||
int setuid(uid_t uid);
|
||||
|
||||
// 设置当前进程的effective uid
|
||||
int seteuid(uid_t uid);
|
||||
|
||||
// 设置当前进程的real gid
|
||||
int setgid(gid_t gid);
|
||||
|
||||
// 设置当前进程的effective gid
|
||||
int setegid(gid_t gid);
|
||||
|
||||
// 交换uid和gid (原子操作)
|
||||
int setreuid(uid_t ruid, uid_t euid);
|
||||
|
||||
// 交换gid和egid (原子操作)
|
||||
int setregid(gid_t rgid, gid_t egid);
|
||||
|
||||
```
|
||||
|
||||
|
||||
|
||||
## 观摩课:解释器文件
|
||||
|
||||
> unix讲究机制而非策略
|
||||
|
||||
脚本,后缀名是什么都可以,一般用`sh`, `exec`
|
||||
|
||||
```bash
|
||||
#!/bin/cat
|
||||
|
||||
# some shell
|
||||
```
|
||||
|
||||
`#!`是一种约定俗成的标记,告诉系统这个脚本应该用什么解释器来执行。
|
||||
|
||||
|
||||
|
||||
## `system()`函数
|
||||
|
||||
```c
|
||||
/*
|
||||
* 运行一个shell命令
|
||||
* 调用/bin/sh
|
||||
*/
|
||||
int system(const char *command);
|
||||
```
|
||||
|
||||
相当于`fork+exec+wait`的封装
|
||||
|
||||
|
||||
|
||||
## 进程会计
|
||||
|
||||
```c
|
||||
//! freeBSD系统的方言
|
||||
int acct(const char *filename);
|
||||
```
|
||||
|
||||
|
||||
|
||||
## 进程时间
|
||||
|
||||
```c
|
||||
clock_t times(struct tms *buf);
|
||||
|
||||
// clock_t 滴答数
|
||||
|
||||
struct tms{
|
||||
clock_t tms_utime; /* user time */
|
||||
clock_t tms_stime; /* system time */
|
||||
clock_t tms_cutime; /* user time of children */
|
||||
clock_t tms_cstime; /* system time of children */
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
|
||||
|
||||
## 守护进程
|
||||
|
||||
1. 守护进程`PPID`为1
|
||||
2. 守护进程没有控制终端,`TTY`为?
|
||||
3. `PID, PGID, SID`相同
|
||||
|
||||
```c
|
||||
pid_t setpgid(pid_t pid, pid_t pgid);
|
||||
pid_t getpgid(pid_t pid);
|
||||
|
||||
pid_t getpgrp(void); //! 方言
|
||||
pid_t getpgrp(psid_t pid); //! 方言
|
||||
```
|
||||
|
||||
- 会话(session):一个或多个进程组的集合,以`sid`为标识
|
||||
`pid_t setsid(void);`
|
||||
`setsid`必须由非`leader`进程调用,从而创建一个新的会话。
|
||||
- 前台进程组:正在与终端交互的进程组
|
||||
- 后台进程组:正在运行,但不与终端交互的进程组
|
||||
|
||||
- 终端:
|
||||
我们接触的都是虚拟终端
|
||||
|
||||
|
||||
**单实例守护进程**:锁文件`/var/run/name.pid`
|
||||
|
||||
启动脚本文件:`/etc/rc*...`
|
||||
|
||||
|
||||
|
||||
## 系统日志
|
||||
|
||||
`syslogd`服务
|
||||
|
||||
```c
|
||||
#include <syslog.h>
|
||||
|
||||
/**
|
||||
* 打开系统日志
|
||||
*
|
||||
* @prarm: ident 标识符
|
||||
* @prarm: option 选项 LOG_CONS, LOG_NDELAY, LOG_NOWAIT, LOG_PERROR ...
|
||||
* @prarm: facility 来源 LOG_USER, LOG_DAEMON, LOG_KERN, LOG_LOCAL0~7 ...
|
||||
*/
|
||||
void openlog(const char *ident, int option, int facility);
|
||||
|
||||
/**
|
||||
* 记录系统日志
|
||||
*
|
||||
* @prarm: priority 优先级 以 ERR 与 WARNING 为分界点
|
||||
* @prarm: format 格式化字符串
|
||||
* @prarm: ... 格式化参数
|
||||
*/
|
||||
void syslog(int priority, const char *format, ...);
|
||||
|
||||
/**
|
||||
* 关闭系统日志
|
||||
*/
|
||||
void closelog(void);
|
||||
```
|
||||
|
||||
```bash
|
||||
sudo tail /var/log/messages # 老师
|
||||
journalctl -r # 我的debian
|
||||
```
|
||||
28
C15-进程/ex.c
Normal file
28
C15-进程/ex.c
Normal file
@@ -0,0 +1,28 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/**
|
||||
* @brief data +%s
|
||||
* @details
|
||||
*
|
||||
* @param argc
|
||||
* @param argv
|
||||
* @return int
|
||||
*/
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
puts("Begin!");
|
||||
fflush(NULL); // !!!
|
||||
|
||||
execl("/bin/date", "date", "+%s", NULL);
|
||||
// NULL 作为参数列表的结束标志
|
||||
|
||||
// !! 如果不出错,不会到这里
|
||||
perror("execl()");
|
||||
exit(1);
|
||||
|
||||
puts("End!");
|
||||
|
||||
exit(0);
|
||||
}
|
||||
42
C15-进程/few.c
Normal file
42
C15-进程/few.c
Normal file
@@ -0,0 +1,42 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/**
|
||||
* @brief few: folk, execl, wait
|
||||
* @details
|
||||
*
|
||||
* @param argc
|
||||
* @param argv
|
||||
* @return int
|
||||
*/
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
pid_t pid;
|
||||
|
||||
puts("Begin!");
|
||||
|
||||
fflush(NULL);
|
||||
|
||||
pid = fork();
|
||||
if (pid < 0)
|
||||
{
|
||||
perror("fork()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (0 == pid) // child
|
||||
{
|
||||
execl("/bin/date", "date", "+%s", NULL);
|
||||
perror("execl()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
wait(NULL);
|
||||
|
||||
puts("End!");
|
||||
|
||||
exit(0);
|
||||
}
|
||||
40
C15-进程/fork1.c
Normal file
40
C15-进程/fork1.c
Normal file
@@ -0,0 +1,40 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
pid_t pid;
|
||||
|
||||
/*
|
||||
* 如果 ./fork1 > /tmp/out
|
||||
* begin会由父进程输出两次
|
||||
* 缓冲问题
|
||||
* 文件时全缓冲
|
||||
*/
|
||||
printf("[%d]Begin!\n", getpid());
|
||||
|
||||
fflush(NULL); // !!! 在fork之前刷新该刷新的流
|
||||
|
||||
pid = fork();
|
||||
if (pid < 0)
|
||||
{
|
||||
perror("fork()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (pid == 0) // child
|
||||
{
|
||||
printf("[%d]Child is working!\n", getpid());
|
||||
}
|
||||
else // parent
|
||||
{
|
||||
printf("[%d]Parent is working!\n", getpid());
|
||||
}
|
||||
|
||||
printf("[%d]End!\n", getpid());
|
||||
|
||||
// getchar();
|
||||
|
||||
exit(0);
|
||||
}
|
||||
83
C15-进程/mydaemon.c
Normal file
83
C15-进程/mydaemon.c
Normal file
@@ -0,0 +1,83 @@
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/syslog.h>
|
||||
#include <syslog.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define FNAME "/tmp/out"
|
||||
|
||||
static int deamonize(void)
|
||||
{
|
||||
pid_t pid;
|
||||
int fd;
|
||||
|
||||
pid = fork();
|
||||
if (pid < 0)
|
||||
return -1;
|
||||
|
||||
if (pid > 0) // parent process
|
||||
exit(0);
|
||||
|
||||
fd = open("/dev/null", O_RDWR);
|
||||
if (fd < 0)
|
||||
return -1;
|
||||
|
||||
dup2(fd, 0);
|
||||
dup2(fd, 1);
|
||||
dup2(fd, 2);
|
||||
if (fd > 2)
|
||||
close(fd);
|
||||
|
||||
setsid();
|
||||
|
||||
chdir("/"); // 防止一直占用某设备
|
||||
// umask(0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
FILE *fp;
|
||||
int i;
|
||||
|
||||
openlog("mydaemon", LOG_PID, LOG_DAEMON);
|
||||
|
||||
if (deamonize())
|
||||
{
|
||||
syslog(LOG_ERR, "daemonize() failed!");
|
||||
exit(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
syslog(LOG_INFO, "daemonize() success!");
|
||||
}
|
||||
|
||||
fp = fopen(FNAME, "w");
|
||||
if (fp == NULL)
|
||||
{
|
||||
syslog(LOG_ERR, "fopen() failed!: %s", strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
syslog(LOG_INFO, "%s was opened successfully!", FNAME);
|
||||
|
||||
for (i = 0;; i++)
|
||||
{
|
||||
fprintf(fp, "%d\n", i);
|
||||
fflush(fp); //! 刷新缓冲区
|
||||
syslog(LOG_DEBUG, "%d is printed", i);
|
||||
sleep(1);
|
||||
}
|
||||
|
||||
// 目前守护进程只能异常终止,下面其实无法执行到
|
||||
fclose(fp);
|
||||
closelog();
|
||||
|
||||
exit(0);
|
||||
}
|
||||
98
C15-进程/myshell.c
Normal file
98
C15-进程/myshell.c
Normal file
@@ -0,0 +1,98 @@
|
||||
#include <glob.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define DELIMS " \t\n"
|
||||
|
||||
struct cmd_st
|
||||
{
|
||||
glob_t globres;
|
||||
};
|
||||
|
||||
|
||||
static void prompt(void)
|
||||
{
|
||||
printf("mysh-0.1$ ");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief 解析命令行
|
||||
* @details
|
||||
* line 不加 const 是考虑以后可能加 alias 功能
|
||||
*
|
||||
* @param line
|
||||
* @param res
|
||||
*/
|
||||
static void parse(char *line, struct cmd_st *res)
|
||||
{
|
||||
char *tok;
|
||||
int i = 0;
|
||||
|
||||
//* 根据分隔符分割小串
|
||||
// strtok();
|
||||
while (1)
|
||||
{
|
||||
tok = strsep(&line, DELIMS);
|
||||
if (NULL == tok)
|
||||
break;
|
||||
|
||||
if ('\0' == tok[0]) // 判断是否为空串
|
||||
continue;
|
||||
|
||||
// 用glob nocheck 来解析
|
||||
// 用 i 来让第一次不追加
|
||||
glob(tok, GLOB_NOCHECK | GLOB_APPEND * i, NULL, &res->globres);
|
||||
i = 1;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
char *linebuf = NULL;
|
||||
size_t linebuf_size = 0;
|
||||
struct cmd_st cmd;
|
||||
pid_t pid;
|
||||
|
||||
while (1)
|
||||
{
|
||||
prompt();
|
||||
|
||||
if (getline(&linebuf, &linebuf_size, stdin) < 0)
|
||||
break;
|
||||
|
||||
parse(linebuf, &cmd);
|
||||
|
||||
if (0)
|
||||
{
|
||||
/* dosomething */
|
||||
}
|
||||
else // 外部命令
|
||||
{
|
||||
pid = fork();
|
||||
if (pid < 0)
|
||||
{
|
||||
perror("fork");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (0 == pid) // child
|
||||
{
|
||||
execvp(cmd.globres.gl_pathv[0], cmd.globres.gl_pathv);
|
||||
perror("execvp");
|
||||
exit(1);
|
||||
}
|
||||
else // parent
|
||||
{
|
||||
wait(NULL);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
exit(0);
|
||||
}
|
||||
54
C15-进程/mysu.c
Normal file
54
C15-进程/mysu.c
Normal file
@@ -0,0 +1,54 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/**
|
||||
* @brief 实现一个简单的su命令
|
||||
* @details
|
||||
* Usage: mysu uid
|
||||
*
|
||||
* @param argc
|
||||
* @param argv
|
||||
* @return int
|
||||
*/
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
if (argc < 3)
|
||||
{
|
||||
fprintf(stderr, "Usage: mysu uid");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
pid_t pid;
|
||||
|
||||
pid = fork();
|
||||
if (pid < 0)
|
||||
{
|
||||
perror("fork");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (0 == pid)
|
||||
{
|
||||
// child process
|
||||
setuid(atoi(argv[1]));
|
||||
execvp(argv[2], argv + 2);
|
||||
perror("execvp");
|
||||
exit(1);
|
||||
}
|
||||
else // parent process
|
||||
wait(NULL);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* !!!
|
||||
* 此时,执行./mysu 0 cat /etc/shadow 依旧没有权限
|
||||
* su - 切换到 root 用户
|
||||
* 通过
|
||||
* chown root mysu && chmod u+s mysu 即可
|
||||
* 再回到普通用户,执行./mysu 0 cat /etc/shadow 即可查看 shadow 文件
|
||||
*/
|
||||
37
C15-进程/primer0.c
Normal file
37
C15-进程/primer0.c
Normal file
@@ -0,0 +1,37 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define LEFT 30000000
|
||||
#define RIGHT 30000200
|
||||
|
||||
/**
|
||||
* @brief 求质数(单机版)
|
||||
* @details
|
||||
*
|
||||
* @param argc
|
||||
* @param argv
|
||||
* @return int
|
||||
*/
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int i, j, mark;
|
||||
|
||||
for (i = LEFT; i <= RIGHT; i++)
|
||||
{
|
||||
mark = 1;
|
||||
for (j = 2; j < i / 2; j++)
|
||||
{
|
||||
if (i % j == 0)
|
||||
{
|
||||
mark = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (mark)
|
||||
printf("%d is a primer\n", i);
|
||||
}
|
||||
|
||||
exit(0);
|
||||
}
|
||||
53
C15-进程/primer1.c
Normal file
53
C15-进程/primer1.c
Normal file
@@ -0,0 +1,53 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define LEFT 30000000
|
||||
#define RIGHT 30000200
|
||||
|
||||
/**
|
||||
* @brief 求质数(多进程版)
|
||||
* @details
|
||||
*
|
||||
* @param argc
|
||||
* @param argv
|
||||
* @return int
|
||||
*/
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int i, j, mark;
|
||||
pid_t pid;
|
||||
|
||||
for (i = LEFT; i <= RIGHT; i++)
|
||||
{
|
||||
|
||||
pid = fork();
|
||||
if (pid < 0)
|
||||
{
|
||||
perror("fork()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (pid == 0) // child
|
||||
{
|
||||
mark = 1;
|
||||
for (j = 2; j < i / 2; j++)
|
||||
{
|
||||
if (i % j == 0)
|
||||
{
|
||||
mark = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (mark)
|
||||
printf("%d is a primer\n", i);
|
||||
|
||||
sleep(1000); // * 让父进程先结束
|
||||
exit(0); // ! 退出子进程
|
||||
// ! init 在孤儿进程 exit 后才收尸
|
||||
}
|
||||
}
|
||||
|
||||
// sleep(1000); // * 让子进程先结束,子进程会变成僵尸态
|
||||
exit(0);
|
||||
}
|
||||
56
C15-进程/primer2.c
Normal file
56
C15-进程/primer2.c
Normal file
@@ -0,0 +1,56 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define LEFT 30000000
|
||||
#define RIGHT 30000200
|
||||
|
||||
/**
|
||||
* @brief 求质数(多进程版)
|
||||
* @details
|
||||
*
|
||||
* @param argc
|
||||
* @param argv
|
||||
* @return int
|
||||
*/
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int i, j, mark;
|
||||
pid_t pid;
|
||||
|
||||
for (i = LEFT; i <= RIGHT; i++)
|
||||
{
|
||||
|
||||
pid = fork();
|
||||
if (pid < 0)
|
||||
{
|
||||
perror("fork()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (pid == 0) // child
|
||||
{
|
||||
mark = 1;
|
||||
for (j = 2; j < i / 2; j++)
|
||||
{
|
||||
if (i % j == 0)
|
||||
{
|
||||
mark = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (mark)
|
||||
printf("%d is a primer\n", i);
|
||||
|
||||
exit(0); // ! 退出子进程
|
||||
}
|
||||
}
|
||||
|
||||
// int st;
|
||||
for (i = LEFT; i <= RIGHT; i++)
|
||||
// wait(&st);
|
||||
wait(NULL);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
59
C15-进程/primerN.c
Normal file
59
C15-进程/primerN.c
Normal file
@@ -0,0 +1,59 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define LEFT 30000000
|
||||
#define RIGHT 30000200
|
||||
#define N 3
|
||||
|
||||
|
||||
/**
|
||||
* @brief 求质数(多进程版)
|
||||
* @details
|
||||
*
|
||||
* @param argc
|
||||
* @param argv
|
||||
* @return int
|
||||
*/
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int i, j, n, mark;
|
||||
pid_t pid;
|
||||
|
||||
for (n = 0; n < N; n++)
|
||||
{
|
||||
pid = fork();
|
||||
if (pid < 0)
|
||||
{
|
||||
perror("fork()");
|
||||
exit(1); // ! 加入在第三次fork失败时,退出,前两次没有收尸
|
||||
}
|
||||
|
||||
if (pid == 0)
|
||||
{
|
||||
for (i = LEFT + n; i <= RIGHT; i += N)
|
||||
{
|
||||
|
||||
mark = 1;
|
||||
for (j = 2; j < i / 2; j++)
|
||||
{
|
||||
if (i % j == 0)
|
||||
{
|
||||
mark = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (mark)
|
||||
printf("[%d]%d is a primer\n", n, i);
|
||||
}
|
||||
|
||||
exit(0); // ! 退出子进程
|
||||
}
|
||||
}
|
||||
|
||||
for (n = 0; n < N; n++)
|
||||
wait(NULL);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
45
C15-进程/sleep.c
Normal file
45
C15-进程/sleep.c
Normal file
@@ -0,0 +1,45 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/**
|
||||
* @brief few: folk, execl, wait
|
||||
* @details
|
||||
*
|
||||
* @param argc
|
||||
* @param argv
|
||||
* @return int
|
||||
*/
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
pid_t pid;
|
||||
|
||||
puts("Begin!");
|
||||
|
||||
fflush(NULL);
|
||||
|
||||
pid = fork();
|
||||
if (pid < 0)
|
||||
{
|
||||
perror("fork()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (0 == pid) // child
|
||||
{
|
||||
// execl("/bin/sleep", "sleep", "100", NULL);
|
||||
execl("/bin/sleep", "httpd", "100", NULL);
|
||||
// ! 低级木马:进程树里看到的是httpd 100
|
||||
|
||||
perror("execl()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
wait(NULL);
|
||||
|
||||
puts("End!");
|
||||
|
||||
exit(0);
|
||||
}
|
||||
9
C15-进程/system.c
Normal file
9
C15-进程/system.c
Normal file
@@ -0,0 +1,9 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
system("date +%s > /tmp/out");
|
||||
|
||||
exit(0);
|
||||
}
|
||||
38
C15-进程/system1.c
Normal file
38
C15-进程/system1.c
Normal file
@@ -0,0 +1,38 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/**
|
||||
* @brief few: folk, execl, wait
|
||||
* @details
|
||||
*
|
||||
* @param argc
|
||||
* @param argv
|
||||
* @return int
|
||||
*/
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
pid_t pid;
|
||||
|
||||
fflush(NULL);
|
||||
|
||||
pid = fork();
|
||||
if (pid < 0)
|
||||
{
|
||||
perror("fork()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (0 == pid) // child
|
||||
{
|
||||
execl("/bin/sh", "sh", "-c", "date +%s", NULL);
|
||||
perror("execl()");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
wait(NULL);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
Reference in New Issue
Block a user