1# procfs<a name="EN-US_TOPIC_0000001123696719"></a> 2 3- [Basic Concepts](#section146801917174017) 4- [Working Principles](#section479762916408) 5- [Development Guidelines](#section1221174524014) 6 - [Development Example](#section52016575401) 7 8 9## Basic Concepts<a name="section146801917174017"></a> 10 11The proc filesystem \(procfs\) is a virtual file system that displays process or other system information in a file-like structure. It is more convenient to obtain system information in file operation mode compared with API calling mode. 12 13## Working Principles<a name="section479762916408"></a> 14 15In the OpenHarmony kernel, procfs is automatically mounted to the **/proc** directory during startup. Only the kernel module can create file nodes to provide the query service. 16 17## Development Guidelines<a name="section1221174524014"></a> 18 19To create a procfs file, you need to use **ProcMkdir** to create a directory and use **CreateProcEntry** to create a file. The development of the file node function is to hook the read and write functions to the file created by **CreateProcEntry**. When the procfs file is read or written, the hooked functions will be called to implement customized functions. 20 21### Development Example<a name="section52016575401"></a> 22 23The following describes how to create the **/proc/hello/world** file to implement the following functions: 24 251. Create a file in **/proc/hello/world**. 26 272. Read the file. When the file is read, "HelloWorld!" is returned. 28 293. Write the file and print the data written in the file. 30 31``` 32#include "proc_fs.h" 33 34static int TestRead(struct SeqBuf *buf, void *arg) 35{ 36 LosBufPrintf(buf, "Hello World! \n"); /* Print "Hello World!" to the buffer. The data in the buffer will be returned to the read result. */ 37 return 0; 38} 39 40static int TestWrite(struct ProcFile *pf, const char *buffer, size_t buflen, loff_t *ppos) 41{ 42 if ((buffer == NULL) || (buflen <= 0)) { 43 return -EINVAL; 44 } 45 46 PRINTK("your input is: %s\n", buffer); /* Different from the read API, the write API prints the data only to the console. */ 47 return buflen; 48} 49static const struct ProcFileOperations HELLO_WORLD_OPS = { 50 .read = TestRead, 51 .write = TestWrite, 52}; 53 54void HelloWorldInit(void) 55{ 56 /* Create the hello directory.*/ 57 struct ProcDirEntry *dir = ProcMkdir("hello", NULL); 58 if (dir == NULL) { 59 PRINT_ERR("create dir failed!\n"); 60 return; 61 } 62 63 /*Create the world file. */ 64 struct ProcDirEntry *entry = CreateProcEntry("world", 0, dir); 65 if (entry == NULL) { 66 PRINT_ERR("create entry failed!\n"); 67 return; 68 } 69 70 /* Hook the customized read and write APIs to the file. */ 71 entry->procFileOps = &HELLO_WORLD_OPS; 72} 73``` 74 75**Verification** 76 77After the OS startup, run the following command in the shell: 78 79``` 80OHOS # cat /proc/hello/world 81OHOS # Hello World! 82OHOS # echo "yo" > /proc/hello/world 83OHOS # your input is: yo 84``` 85 86