--- tags: OS --- # OS Homework 2: Listing Task 105042015 沈冠妤 外語20 ## :memo: Code Explain ### Iterate linearly: ![](https://i.imgur.com/C8V2fMz.png) 1. **struct task_struct** is defined in **<linux/sched.h>** 1. Iterate through all tasks with **for_each_process()** 1. Print the information for each iteration ### Iterate in DFS: ![](https://i.imgur.com/5R5wPCD.png) DFS (Depth first search) means to iterate in preorder-traversal manner. 1. Start with a node (head) 1. Iterate through all of its children 1. Retrieve the child node's sibling 1. Print the information of the sibling node 1. Repeat from the first step, and the head node becomes the sibling node. An image example below (Shown from layer 2): ![](https://i.imgur.com/Pkq9xjT.png) 1. Start with a node (0) 1. Iterate through all of its children (1, 2), take (2) for example 1. Retrieve (2)'s sibling, which is (1) 1. Print the information of (1) Repeat again: 1. Start with a node (1) 1. Iterate through all of its children (3, 4), take (4) 1. Retrieve (4)'s sibling, which is (3) 1. Print the information of (3) So on so forth... ## :memo: Output Screenshot ### Linearly: ![](https://i.imgur.com/AkD3Yja.png) <p style="text-align: center;">(above: dmesg)</p> ![](https://i.imgur.com/tqG1d1C.png) <p style="text-align: center;">(above: ps -el)</p> --- ### DFS: ![](https://i.imgur.com/TRMfQvG.png) <p style="text-align: center;">(above: dmesg)</p> ![](https://i.imgur.com/8CvH3iX.png) <p style="text-align: center;">(above: ps -el)</p> * In dmesg, 241~662's parent node is 1 * 662's next node printed in dmesg, 844, is the child node of 662 ## :memo: Reference * [演算法筆記 - Binary Tree](http://www.csie.ntnu.edu.tw/~u91029/BinaryTree.html) * [<linux/sched.h> documentation](https://github.com/torvalds/linux/blob/master/include/linux/sched.h)