进程的消亡及释放资源
This commit is contained in:
28
C13-Linux系统编程/process_basic/ex.c
Normal file
28
C13-Linux系统编程/process_basic/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
C13-Linux系统编程/process_basic/few.c
Normal file
42
C13-Linux系统编程/process_basic/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
C13-Linux系统编程/process_basic/fork1.c
Normal file
40
C13-Linux系统编程/process_basic/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);
|
||||
}
|
||||
41
C13-Linux系统编程/process_basic/myshell.c
Normal file
41
C13-Linux系统编程/process_basic/myshell.c
Normal file
@@ -0,0 +1,41 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
prompt();
|
||||
|
||||
getline();
|
||||
|
||||
parse();
|
||||
|
||||
if (内部命令)
|
||||
{
|
||||
;
|
||||
}
|
||||
else // 外部命令
|
||||
{
|
||||
fork();
|
||||
if (< 0)
|
||||
{
|
||||
;
|
||||
}
|
||||
|
||||
if (0 ==) // child
|
||||
{
|
||||
execXX;
|
||||
perror();
|
||||
exit(1);
|
||||
}
|
||||
else // parent
|
||||
{
|
||||
wait();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
exit(0);
|
||||
}
|
||||
37
C13-Linux系统编程/process_basic/primer0.c
Normal file
37
C13-Linux系统编程/process_basic/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
C13-Linux系统编程/process_basic/primer1.c
Normal file
53
C13-Linux系统编程/process_basic/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
C13-Linux系统编程/process_basic/primer2.c
Normal file
56
C13-Linux系统编程/process_basic/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
C13-Linux系统编程/process_basic/primerN.c
Normal file
59
C13-Linux系统编程/process_basic/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
C13-Linux系统编程/process_basic/sleep.c
Normal file
45
C13-Linux系统编程/process_basic/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);
|
||||
}
|
||||
Reference in New Issue
Block a user