xv6, Deep Dive — Part2

Balemarthy Vamsi Krishna
4 min readMar 22, 2023

--

Picture from Canva Pro

Imagine a room full of people attending a marriage. Every attendee is all by himself or herself. He or she might be busy, resting, eating food, engaged in conversation, waiting for someone and many more. Suffice to say that few are busy, few are active, few are ready to get busy, and few are resting and many more. Be in whatever state they are no one wants literally others to “step on their feet” and I mean literally “no step on their feet”.

Somehow this picture always strikes my mind when I think about “Processes”. This picture important for me because it gave a shift from the idea of thinking a single while(1) loop kind of programming to something that is different, where there can be many “while(1)-s”, tasks or processes.

Many processes that exists do not come from thin area. They are to be created. The creation starts with the first process ever created and in the UNIX kind of operating systems it is called the “init” process.

It is this process that later can give birth to many other processes. In this post, let me unravel how the init process actually looks like in xv6. I believe this is mostly the way that many other UNIX based OS-es follow.

For the impatient who wants to look at the code, here is the code and what follows below the code link is the detailed explanation of the code.

GitHub — mit-pdos/xv6-public: xv6 OS

Explanation of the file with inline code:

No explanation needed for the below lines. Include the needed headers.

#include “types.h”
#include “stat.h”
#include “user.h”
#include “fcntl.h”

List of arguments that will be passed later to the “exec” function called in the main function

char *argv[] = { “sh”, 0 };

When the main function starts it opens that console enabling the user to communicate with the computer or device. This is the basic and primitive way of user interaction and I believe it just fall from the earlier way of coding and allowing use to talk with the code running in the device. Perfectly no change here.

int main(void)
{
int pid, wpid;

if(open(“console”, O_RDWR) < 0)
{
mknod(“console”, 1, 1);
open(“console”, O_RDWR);
}

Next comes the creation of various standard file descriptors : stdin, stdout and stderr. The system call that is used is “dup” and for sure I will cover it when I comes to the implementation of the minimal list of system calls implemented as part of xv6. Suffice to say that copies of stdin are made and we name as stdout and stderr.

dup(0); // stdout
dup(0); // stderr

This is my favourite infinite loop (could be while(1) or for(;;)). I somehow felt I was on the right track with respect to my understanding after seeing this.

What to do, when you are on your own initially to understand something, self-doubt is the biggest threat that can kill the progress.

The most important this that I want to divert your attention are the lines that are bold

pid = fork() is the call that creates a child process.
The error checks are self-evident.

It is when the Pid is 0, that we go forward and actually create so call “shell” the first human interaction that can be done programmatically and that will interact with the kernel proper.

“sh” is the name of the program present in the memory and it is put to execution using “exec” call with the parameter defined at the top of the function.
I did a change to the code here, by comparing the return value of the “exec” code. I think that is mising in the code.

The last lines are written to take of zombie and we will discuss about it in upcoming articles.

for(;;)
{
printf(1, “init: starting sh\n”);
pid = fork();

if(pid < 0){
printf(1, “init: fork failed\n”);
exit();
}

if(pid == 0)
{
if (-1 == exec(“sh”, argv))
{
printf(1, “init: exec sh failed\n”);
exit();
}
}

while((wpid=wait()) >= 0 && wpid != pid)
printf(1, “zombie!\n”);
}
}

That is all readers. I hope this small code piece would have made you appreciated how simple, logical and beautiful is the design of UNIX operating systems.

Grab the link above and start you own journey.

You are interested in contribution, reach out to me and let us talk.

Thank you very much and I wish you a happy learning.

--

--

Balemarthy Vamsi Krishna

Early career embedded software mentor. Talks and writes about interviews, resume building, technical branding, growth and hike