环境变量

image.png
image.png
image.png

  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include<stdlib.h>
  4. //告诉编译器 这个全局变量在其他地方定义了,
  5. extern char **environ;
  6. //显示当前进程所有的环境变量
  7. void showall_env(){
  8. //变量当前环境变量
  9. for (char **ptr = environ; **ptr =!NULL; ++ptr){
  10. printf("%s \n",*ptr);
  11. }
  12. }
  13. int main()
  14. {
  15. //setenv("TEST","1234567890",1);
  16. //显示环境变量
  17. //showall_env();或者写成下面这样
  18. char* spath = getenv("PATH");
  19. printf("%s\n",spath);
  20. return 0;
  21. }

进程

什么是进程

image.png

内存布局

image.png

使用proc获取进程信息-进程先关命令

image.png
image.png
image.png

Linux proc文件系统介绍

image.png

常用proc目录

image.png

  1. #include <unistd.h>
  2. #include <stdio.h>
  3. int main()
  4. {
  5. //获取当前进程pid
  6. printf("curid = %d",getpid());
  7. getchar();//暂停的功能
  8. return 0;
  9. }

proc目录的使用

image.png

  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include<dirent.h>
  4. #include<string.h>
  5. #include<errno.h>
  6. void proc_show_module(int pid){
  7. char path[266];
  8. sprintf(path,"/proc/%d/maps",pid);
  9. FILE *fd = fopen(path,"r");
  10. if(fd == NULL){
  11. printf("无法打开文件 :%s\n",path);
  12. return;
  13. }
  14. //%llx %llx %4s %x %5s %d %s
  15. unsigned long long base = 0,end = 0;
  16. char pageatt[5];
  17. char offset;
  18. char time[6];
  19. unsigned int inode;
  20. while (7 == fscanf(fd,"%llx-%llx %4s %x %5s %d %s", &base, &end, pageatt, &offset, time, &inode, path))
  21. {
  22. if (offset == 0){
  23. printf("\t%llx-%llx %4s %x %5s %d %s\n", base, end, pageatt, offset, time, inode, path);
  24. }
  25. }
  26. //关闭文件
  27. fclose(fd);//注意fclosed后面不要d,fclosed报一大推错还找不到
  28. }
  29. void proc_show_name(int pid){
  30. //打开文件
  31. char path[266] ;
  32. sprintf(path,"/proc/%d/status",pid);
  33. FILE *fd = fopen(path, "r");
  34. if (fd == NULL ){
  35. printf("无法打开文件:%s\n", path);
  36. return ;
  37. }
  38. fscanf(fd, "Name: %s\n", path);
  39. printf("%s",path);
  40. //关闭文件
  41. fclose(fd);
  42. }
  43. int main(){
  44. //1.遍历/proc目录
  45. struct DIR *pdir = opendir("/proc");
  46. if (pdir == NULL){
  47. printf("无法打开/proc目录\n");
  48. return 0;
  49. };
  50. // 2.遍历目录下所有数字的目录
  51. int pid;
  52. char path[266] = {0};
  53. char proc_name[100];
  54. struct dirent *ent = NULL;
  55. while(ent = readdir(pdir)){
  56. //得到进程id,如果无法将字符串转换成数字,说明目录名不是进程id
  57. if (1 != sscanf(ent->d_name,"%d",&pid))
  58. continue;
  59. //进程信息(包括进程名)保存在status文件中中
  60. proc_show_name(pid);
  61. //进程pid
  62. printf("%6d \n",pid);
  63. proc_show_module(pid);
  64. }
  65. //关闭目录流
  66. closedir(pdir);
  67. return 0;
  68. }

进程的创建

image.png
image.png

  1. #include <unistd.h>
  2. #include <sys/types.h>
  3. #include <stdio.h>
  4. int main()
  5. {
  6. pid_t pid;
  7. //创建一个进程
  8. pid = fork();
  9. char* message;
  10. int n;
  11. //如果返回值是-1那么,创建失败
  12. if(pid<0)
  13. {
  14. message="fork falled\n";
  15. return 0;
  16. }
  17. //如果返回值是,那么当前处于子进程
  18. if(pid ==0)
  19. {
  20. message="this is the parent \n";
  21. n = 3;
  22. }
  23. //如果返回值是>0,那么当前处于主进程
  24. else
  25. {
  26. message="this is the child \n";
  27. n = 6;
  28. }
  29. for (int i = 0; i < n; i++)
  30. {
  31. printf("%s",message);
  32. }
  33. return 0;
  34. }

exec函数

image.png
image.png

  1. #include <unistd.h>
  2. #include <sys/types.h>
  3. #include <stdio.h>
  4. void CreateProcess(char* path)
  5. {
  6. pid_t pid;
  7. //创建一个进程
  8. pid = fork();
  9. //如果返回值是-1那么,创建失败
  10. if(pid<0)
  11. {
  12. printf("create process falled\n");
  13. return 0;
  14. }
  15. //如果返回值是,那么当前处于子进程
  16. if(pid ==0)
  17. {
  18. execl(path,NULL);
  19. }
  20. return;
  21. }
  22. int main(){
  23. CreateProcess("/usr/bin/firefox");
  24. printf("this is the parent\n");
  25. getchar();
  26. return 0;
  27. }

僵尸进程

image.png

等待函数

image.png

  1. #include <unistd.h>
  2. #include <sys/types.h>
  3. #include <stdio.h>
  4. #include <sys/wait.h>
  5. #include <stdlib.h>
  6. int main(){
  7. pid_t pid;
  8. pid= fork();
  9. if(pid<0){
  10. perror("fork failed\n");
  11. exit(0);
  12. }
  13. if(pid == 0){//子进程
  14. for(int i=0; i<5;i++){
  15. printf("this is child %d\n",getpid());
  16. sleep(1);//微秒usleep
  17. }
  18. }
  19. else//父进程
  20. {
  21. int stat_val;
  22. getchar();
  23. pid_t child_pid = wait(&stat_val);//阻塞当前进程,直到子进程结束
  24. //waitpid(&stat_val,0)
  25. printf("pid = %d exit",child_pid);
  26. if(WIFEXITED(stat_val)){//如果子进程正常值
  27. printf("child exite with code %d\n",WEXITSTATUS(stat_val));
  28. }
  29. }
  30. }

image.png
image.png