hocky

Operating Systems 2021-1 (OS211)

Written by Hocky Yudhiono in Pontianak

Top 10 List of Week 04

Hello, this is my top 10 list of Week 04! Minggu-minggu sebelumnya ialah minggu yang tangguh, kita membahas tentang File Systems dan bagaimana struktur File secara umum. Materi minggu ini ialah Main Memory, kalau menurut buku Silberschatz. Namun tertulis Topic pada slide ialah Addresing, Shared Lib, dan Pointer. Nah, berikut ialah daftar dari beberapa link yang bagus menurut saya untuk minggu ini. 😁

1. Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc() (Article)

Array is like the most primitive and the “Proto” of all data structures, it stores basically list of any elements. Well, C Dynamic Memory Allocation can be defined as a procedure in which the size of a data structure (like Array) is changed during the runtime. C provides some functions to achieve these tasks. There are 4 library functions provided by C defined under <stdlib.h> header file to facilitate dynamic memory allocation in C programming. They are malloc() , calloc() , free() , realloc(). I really recommend you to at least understand only this one article out of others C array stuffs out there! It’s really interesting and rewarding if you understand how C works, moreover on this dynamic memory allocation topic!!! 🍧🍧

2. Cheat Engine (App + Article) a.k.a Read/Write Other Process’ Memory

OK. Who never uses cheat engine? It’s a legend app every late 90’s and 2000’s kids use to cheat on many singleplayer games. First things first, what is interesting about it, it’s open source, check the Github Repository here. Second, it works by reading process memory and writing process memory. There is a dynamically linked library, called the kernel32.dll in Windows that is used by most applications that change memory of other applications or communicate between two processes. More on it is in the blog post here. What things being more could about is, Cheat Engine actually works based on a debugging API 🐜🐛! But of course it’s a bit different story for Linux-Based Distribution and Mac! So yeah, check the blog post as well!

3. Compiling, assembling, and linking (Video + Forum Discussion)

So, beyond an assembler turning a source into an object binary file, there’s actually a linker that makes ca program executable. So, you might ask, What do linkers do? To understand it, let’s say you have a hello world program, consists of a line printf("Hello Venti!\n"); when it is compiled, it simply puts only a reference to the printf function, and then the linker will resolve this reference. It “copies” all the necessary stuffs and references to create a single executable. It’s interesting, knowing that programs is actually not that simple and turns out they actually being heavily optimized by the compiler and operating systems 🤡!

4. Why are &array and array pointing to the same address? (Forum Discussion)

Okay, so I tested out the codes in the demos and this one 15-uts181.c really bugs me. The behavior is correct, but the case is different when you make a pointer than the pointer refers to a new array. It turns out that C’s pointer behavior is actually consistent. It just me that overcomplicates it 😁! When an array is declared, it will make this kind of behavior.

+----------+----------+----------+----------+----------+
| array[0] | array[1] | array[2] | array[3] | array[4] |
+----------+----------+----------+----------+----------+
^
|
&array[0]
|
&array

This forum is really interesting as it will give you a really good insight on how C pointer works!

5. What is Linux swap? (Video + Forum Discussion)

Swap space is basically the way out if your RAM run out of memory. Basically the Linux Kernel will take some information from the RAM and will take the information to the swap space to free some of it and doesn’t crash due to lack of memory. It’s just like an additional memory for your operating systems 📝. For the forum, you can read it here. Well, the golden rule is the Swap Area is usually twice the size of your RAM, but it also depends on the ability for your computer to hibernate. Also, it depends on the speed of your hard drive, some say that it’s not recommended to have large swap space if you use SSD as it will decrease it write cycle and life span. So, Yeah! You should read some of those articles about swap space 😁😁!

6. Using 4KB Page Size for Virtual Memory is Obsolete (Research Paper)

Ok, that link will lead to a research paper, which might be really boring to read. But, if a page of an operating system is too big, the program won’t be optimal, but if it’s too small, it will be too slow either 🐢. In Finding Optimal Page Size, see that bigger page table size cause an extra overhead because we have to divide that table into the pages and then store that into the main memory. So, to count the optimal page size, you must consider the best overhead over the best performance. Next, on how is page size determined in virtual address space? In my osp, when i run the command too see my page size, it’s 4kB!

hockyy@osp:~$ getconf PAGESIZE
4096

Almost all architectures support a 4kB page size. Some architectures support larger pages (and a few also support smaller pages), but 4kB is a very widespread default! It’s really interesting if you understand about these page sizes stuffs!

7. Make File (Colored Documentations + Forum)

So, make file is essentially a script that lets you make file, usually it consists of some several commands to let you compile something or just arrange some things into a/some file(s). what is the difference between a makefile and a shell script? The answer is: It’s easier, and makefile is dedicated for it. There are even topological features which lets you save a progress if it’s already been done, or lets you run some different flags when making those files 📁. It’s interesting because. Well, now you can understand what makefile(s) files are in our Demos Mr. RMS provided 😊😊.

8. The difference between char* and char[] (Forum)

This is by far the most duplicated questions I personally have seen on stack overflow, the table provided itself basically explains how this thing works. So basically when you declare a string with the first way notation, it will place the string in a read only parts of the memory. It is immutable, and will return a pointer to that. While the other way around, you can also store it as an array of characters! C is truly amazing, that’s why I think you should read it and it’s interesting 😮😮! Reference 1, Reference 2, Reference 3.

Example:                       Allocation Type:     Read/Write:    Storage Location:   Memory Used (Bytes):
===========================================================================================================
const char* str = "Stack";     Static               Read-only      Code segment        6 (5 chars plus '\0')
char* str = "Stack";           Static               Read-only      Code segment        6 (5 chars plus '\0')
char* str = malloc(...);       Dynamic              Read-write     Heap                Amount passed to malloc
char str[] = "Stack";          Static               Read-write     Stack               6 (5 chars plus '\0')
char strGlobal[10] = "Global"; Static               Read-write     Data Segment (R/W)  10

9. Essentials: Pointer Power! - Computerphile (Video)

Pointers are fundamental in programming! Even Java uses it, you can pass objects just by only it address, not the whole object! That’s what makes it different from primitives. The video will explain to you the basic about pointer, about how it works and how addresses and pointer passing works in C 👍🏻👍🏻👍🏻! This will definitely help us to struggle through the weird C Demos codess 💻!!!

10. C Corner Cases (and Funny Things) (Blog)

also Top 20 C pointer mistakes and how to fix them and Strange Corners of C, are one of the bests blog posts about people who are trying to learn C and discovered some C weird(?) But actually makes sense! It’s interesting because as you read the post, you tried it for yourself and voila! Now you understand it as well. I’m so sorry my half of Top 10 is filled with C stuff 😭😭😭. Not that I’m not creative. I code in C++, but I almost never touched the pointer stuffs as the latest libraries of C++14 already fulfill my needs. The funny thing is, sometimes C’s cache is way better if we use plain array instead of those newer things! Also pointer is a cool concept that plays with memory!

That’s all Week 4 from me. I’ll start doing Week 5 as soon as possible!

Peace out!


🌵 © Hocky Yudhiono, 2021 - 2021