 Hi everyone, in this video we will learn how we can use the mf function to allocate memory in a C program. So I have written this mf.c program here, let's open this with visual code. So this is the mf.c program. I will go over the code and then we will compile and run this. So here is the main function. I have defined a signal handler, I will come to this later. So first we get the page size of the system using the getPageSize function. So the page size is generally 4kb which is 4096 bytes. Then we define the PID using the getPID function and this will return the PID of the process which is running this code. Then we use printf to print out the page size and the PID of this program. And this is where we are using the mf function to allocate memory. So here we have defined this caster region which is a pointer to the memory. And this mf function takes first argument as the address at which we want to allocate memory and the system will try to assign us memory at that particular address. But we have given null here. So it will just give us the memory at any available address. Then the second argument defines the size of the memory that we want to allocate. So here we have given 100 times the page size which means it will be around 400 kilobytes of memory. So we are allocating about 400 kilobytes of memory. Then this defines what are the permissions on that memory. So we have read and write permissions on that memory region. This indicates whether this memory is backed by some file or not. So we have used map anonymous which means that it is not backed by any file. It is just a chunk of memory. And it is private which means that it is not shared with other processes. And because this memory is not backed by any file. So we use minus 1 for file descriptor because there is no file with which we want to back this memory. And we use 0 as offset. So these two arguments are used when the memory region is actually backed by some file. Now we check if the return value is map failed then we print out an error could not map and return. Otherwise we print out that we have mapped 100 pages at this particular address. And then we have this while loop. But this while loop does is it iterates over 0 to 100 and accesses the first byte of every allocated page and adds it to some. So this is just to make sure that we are accessing every page. And then it sleeps for one second and it repeats it again and again. So the program will be stuck here so that we can analyze certain aspects of the program. But what if we want to exit the program then we can use ctrl plus c. And because we have mapped some memory and we need to unmap that before exiting the program. So we have defined this signal handler which handles the segment which is ctrl plus c. And what this handler does is it unmaps the memory region using m unmap call. And it takes the address of the region and the size of the memory. And if the unmap result is not equal to 0 then we print out the error that could not unmap. So we print out that successfully unmap and then we exit the program. So that's the complete mmap.c code. So let's compile it and execute it. So I'll open the terminal and compile it using gcc mmap.c and then I'll run the error out. So it shows that system page size is 4096 bytes, PID of this process is 9871 and then we have mapped some 100 pages at this particular virtual address. And we can use ctrl c to exit the program and it prints successfully unmapped. So that's the mmap.c program. Now let's try to analyze this program further. So what I'll do is I will comment out this mmap part so that it doesn't allocate any memory and I'll comment out this part as well. And this part as well. So now the program just prints out the page size and PID and just keeps running this while loop. And I will also comment out the signal handler. So now let's compile this program and run this. So it shows 9925 is the PID. Now I'll open another terminal and I have written this command here. So this is just the ps command and this hyphen is used to define the output. So I want PID, the virtual memory size, which is the VSS and RSS, which is the resident set size and the percentage of memory used in the output of this ps command. And I want all of this information for this particular PID. So I'll change this PID with 19925. So this shows me VSZ, which is the virtual memory size. This is the amount of memory which a program can access, which is 4512 kilobytes. And then when a program actually uses some memory, that is when it adds up in RSS, which is the resident set size. So RSS is 768 kilobytes, which means that program can access 4512 kilobytes of memory, but it is actually using just 768 kilobytes of the memory. Now let's stop this program and let's uncomment the MAP system call. So here I will uncomment these two and I will also uncomment the signal handler. So here it will map some 400 kilobytes of memory, but I'll keep this commented so that we are not actually using that allocated memory and it should just show up in the VSZ and not in RSS. So let's compile and let's run this command again with 20226s PID. So here we see that VSZ did increase by 400 kilobytes, which is the 400 kilobytes extra memory that we allocated using the MAP call. And RSS just increased by approximately 100, which is due to uncommenting the parts of the code and it does not include the 400 kilobytes of memory which was allocated using MAP. Now let's stop this as well and let's uncomment the remaining part. Now we are allocating memory and we are also accessing the memory in a while loop. So now let's compile and use this 20279 as the PID here. So now you can see that there is a significant increase in RSS because now it includes the 400 kilobytes of memory that is being accessed by the program. So we see a significant increase in both VSZ and RSS. So that's it for this video. Thanks and have a nice day!