进程概念(2)

张开发
2026/4/21 6:47:30 15 分钟阅读

分享文章

进程概念(2)
目录1.理解系统调用2.进程1. 基本概念与基本操作进程内核数据结构对象自己的代码数据2.task_struct补充两个小知识cwd ----current work dir 当前程序所在的路径exe进程对应的可执行文件1.理解系统调用操作系统要向上提供对应的服务操作系统不能相信任何用户或者人---系统调用银行就是典型的这种为我们提供服务但是不相信不允许我们进入银行内部进行访问提供了一个个窗口。所以同理操作系统要把自己封装起来但是也要给一个叫系统调用的东西所以之后获取操作系统数据等都是通过系统调用进行的银行除了内部的工作人员银行行长系统还有外面的大堂经理同理操作系统除了要把自己内部的工作做好还要给用户提供服务。比如给开发者提供各种库给操作者使用者提供外壳、命令行指令Linux/windows/macos-C语言-c函数---输出参数和返回值用户和操作系统之间进行某种数据交互• 在开发角度操作系统对外会表现为⼀个整体但是会暴露自己的部分接口供上层开发使用这部分由操作系统提供的接口叫做系统调用。• 系统调用在使用上功能比较基础对用户的要求相对也比较高所以有心的开发者可以对部分系统调用进行适度封装从而形成库有了库就很有利于更上层用户或者开发者进行二次开发库函数和系统调用属于上下层的关系不清楚库函数到底有没有进行系统调用调用库函数时有没有调用硬件只要访问了硬件那么必定访问系统调用操作系统是怎么管理进行进程管理的呢很简单先把进程描述起来再把进程组织起来2.进程1. 基本概念与基本操作•课本概念程序的⼀个执⾏实例正在执⾏的程序等•内核观点担当分配系统资源CPU时间内存的实体。•当前进程内核数据结构(task_struct)⾃⼰的程序代码和数据当我们自己编译好了一个可执行文件根据冯诺依曼程序加载运行到内存中所以说把一个可执行文件从磁盘加载到内存就是进程吗同一时刻操作系统内加载了很多可执行程序还有一款软件从一开始就加载进来了----操作系统这些程序在内存什么位置加载这些代码和数据有没有已经被cpu执行完了系统中有这么多程序要申请内存释放内存操作系统必然要对多个记载内存中的程序进行管理---先描述再组织struct xxx PCB{代码地址数据地址id优先级 进程控制块状态........struct xxx *next}到时候所有属性都可以在节点中找到我们在操作系统内就找到一个操作系统列表---进程列表进程内核数据结构对象自己的代码数据PCB是在操作系统中的统一叫法描述进程的内存中的结构体---PCB---进程控制块在linux下这个PCB具体是个结构体struct task_struct进程的所有属性都可以直接或间接通过tsak_struct找到进程PCB(task_struct)自己的代码和数据任何一个进程加载进来的时候除了把代码和数据加载到内存里操作系统还要在自己的内部为该代码和数据创建对应的task_struct结构体然后这个结构体可以找到代码和数据。所有的task_struct在操作系统内可以通过链表一样的PCB管理起来所以对进程的管理就变成了对链表的增删查改PCB相当于简历CPU相当于面试官一旦一个可执行程序加载到内存中这个可执行程序是最不重要的重要的是操作系统创建对应的PCB来描述它。那么具体的task_struct怎么样的2.task_struct内容分类•标示符:描述本进程的唯⼀标示符⽤来区别其他进程。•状态:任务状态退出代码退出信号等。•优先级:相对于其他进程的优先级。•程序计数器:程序中即将被执⾏的下⼀条指令的地址。•内存指针:包括程序代码和进程相关数据的指针还有和其他进程共享的内存块的指针•上下⽂数据:进程执⾏时处理器的寄存器中的数据[休学例⼦要加图CPU寄存器]。•I∕O状态信息:包括显⽰的I/O请求,分配给进程的I∕O设备和被进程使⽤的⽂件列表。•记账信息:可能包括处理器时间总和使⽤的时钟数总和时间限制记账号等。• 其他信息task_struct部分源代码组织进程可以在内核源代码里找到它。所有运行在系统里的进程都以 task_struct 双链表的形式存在内核里。[user1iZ5waahoxw3q2bZ 26-4-20]$ ll total 20 -rw-rw-r-- 1 user1 user1 73 Apr 20 22:21 Makefile -rwxrwxr-x 1 user1 user1 8496 Apr 20 22:23 myprocess -rw-rw-r-- 1 user1 user1 137 Apr 20 22:23 myprocess.c [user1iZ5waahoxw3q2bZ 26-4-20]$ cat Makefile myprocess:myprocess.c gcc -o $ $^ .PHONY:clean clean: rm -f myprocess [user1iZ5waahoxw3q2bZ 26-4-20]$ cat myprocess.c #includestdio.h #includeunistd.h int main() { while(1) { sleep(1); printf(我是一个进程!\n); } }我们历史上执行的所有指令、工具、自己的程序运行起来全部都是进程getpid 获取进程ID 系统调用NAME getpid, getppid - get process identification SYNOPSIS #include sys/types.h #include unistd.h pid_t getpid(void); pid_t getppid(void);[user1iZ5waahoxw3q2bZ 26-4-20]$ cat myprocess.c #includestdio.h #includeunistd.h #includesys/types.h int main() { while(1) { sleep(1); printf(我是一个进程,我的pid:%d\n,getpid()); } } [user1iZ5waahoxw3q2bZ 26-4-20]$ ./myprocess 我是一个进程,我的pid:17507 我是一个进程,我的pid:17507 我是一个进程,我的pid:17507 我是一个进程,我的pid:17507 我是一个进程,我的pid:17507ps axj查看进程[user1iZ5waahoxw3q2bZ ~]$ ps axj | head -1 ; ps axj |grep myprocess PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND 17419 17507 17507 17419 pts/0 17507 S 1001 0:00 ./myprocess 17513 17537 17536 17513 pts/1 17536 S 1001 0:00 grep --colorauto myprocessps axj | head -1 ; ps axj |grep myprocess | grep -v grep除去grep进程的进程信息[user1iZ5waahoxw3q2bZ ~]$ ps axj | head -1 ; ps axj |grep myprocess | grep -v grep PPID PID PGID SID TTY TPGID STAT UID TIME COMMAND 17419 17507 17507 17419 pts/0 17507 S 1001 0:00 ./myprocessctrlc是杀掉进程的kill -9pid也能杀掉进程[user1iZ5waahoxw3q2bZ ~]$ kill -9 17507我是一个进程,我的pid:17507 Killed补充两个小知识ls /proc查看进程[user1iZ5waahoxw3q2bZ ~]$ ls /proc 1 16 17551 262 379 527 840 diskstats kcore mounts swaps 10 17280 18 264 38 530 9 dma keys mtrr sys 11 17407 1856 27 39 539 96 driver key-users net sysrq-trigger 1149 17416 19 28 425 545 acpi execdomains kmsg pagetypeinfo sysvipc 1180 17418 2 288 47 546 buddyinfo fb kpagecount partitions timer_list 12 17419 20 289 49 550 bus filesystems kpageflags sched_debug timer_stats 1265 17454 21 29 5 6 cgroups fs loadavg schedstat tty 13 17456 22 290 50 65 cmdline interrupts locks scsi uptime 1357 17510 23 3 503 7 consoles iomem mdstat self version 1379 17512 24 358 51 774 cpuinfo ioports meminfo slabinfo vmallocinfo 13871 17513 25 36 52 8 crypto irq misc softirqs vmstat 14 17550 26 37 522 835 devices kallsyms modules stat zoneinfo当该进程退出proc会自动给其移除[user1iZ5waahoxw3q2bZ ~]$ ls /proc/17550 -dl dr-xr-xr-x 9 user1 user1 0 Apr 20 22:35 /proc/17550目前我们只需要了解其中两个cwd ----current work dir 当前程序所在的路径fopen会记录在自己当前的进程如果不存在就新建[user1iZ5waahoxw3q2bZ 26-4-20]$ make gcc -o myprocess myprocess.c [user1iZ5waahoxw3q2bZ 26-4-20]$ ./myprocess 我是一个进程,我的pid:17570 我是一个进程,我的pid:17570 我是一个进程,我的pid:17570 ^C [user1iZ5waahoxw3q2bZ 26-4-20]$ ll total 20 -rw-rw-r-- 1 user1 user1 0 Apr 20 22:46 hello txt -rw-rw-r-- 1 user1 user1 73 Apr 20 22:21 Makefile -rwxrwxr-x 1 user1 user1 8600 Apr 20 22:46 myprocess -rw-rw-r-- 1 user1 user1 211 Apr 20 22:45 myprocess.c [user1iZ5waahoxw3q2bZ 26-4-20]$ cat myprocess.c #includestdio.h #includeunistd.h #includesys/types.h int main() { fopen(hello txt,a); while(1) { sleep(1); printf(我是一个进程,我的pid:%d\n,getpid()); } }chdir更改进程路径[user1iZ5waahoxw3q2bZ 26-4-20]$ ll total 20 -rw-rw-r-- 1 user1 user1 73 Apr 20 22:21 Makefile -rwxrwxr-x 1 user1 user1 8656 Apr 20 22:48 myprocess -rw-rw-r-- 1 user1 user1 258 Apr 20 22:48 myprocess.c [user1iZ5waahoxw3q2bZ 26-4-20]$ cat myprocess.c #includestdio.h #includeunistd.h #includesys/types.h int main() { chdir(/home/user1/linux-learning/linux); fopen(hello txt,a); while(1) { sleep(1); printf(我是一个进程,我的pid:%d\n,getpid()); } } [user1iZ5waahoxw3q2bZ 26-4-20]$ cd .. [user1iZ5waahoxw3q2bZ linux]$ ll total 16 drwxrwxr-x 2 user1 user1 4096 Apr 15 16:52 26-4-15 drwxrwxr-x 3 user1 user1 4096 Apr 15 22:15 26-4-15.2 drwxrwxr-x 2 user1 user1 4096 Apr 16 18:37 26-4-16 drwxrwxr-x 2 user1 user1 4096 Apr 20 22:48 26-4-20 -rw-rw-r-- 1 user1 user1 0 Apr 20 22:48 hello txtexe进程对应的可执行文件感觉你的观看期待我们的下次再见

更多文章