Tuesday, December 1, 2026

The Bad Feeling Caused by Excessive Scrolling

AI Disclaimer: The ideas and reflections in this post are my own. I used an AI language model to help me rephrase and structure the text so the ideas come across more clearly, but I had to refine it with my own words and expressions.


For a while, I've had a recurring bad feeling after using social media. I would open YouTube or Instagram for a short break and close it an hour later feeling empty, neither tired nor rested. My first reaction was to block the apps, but it never worked. Whenever I blocked one, I just moved to another, like Reddit, Twitter, or news sites. So I stopped trying to fight the feeling and tried to understand it.

Blocking never felt right to me anyway. I believe everything humans create has a natural reason behind it, and social media must have one too. Cutting it out would be like fighting my own nature, like trying not to sleep. So the real question was not how to block social media, but how to understand it and why it was making me feel bad.

To answer that, I used a trick I like for hard problems, which is to solve a simpler version first and then add complexity.

So imagine a person living completely alone. When he runs out of things to do, he gets bored, and the boredom pushes him to look for something new to do. In short, he finds something and does it. You probably have a memory of this from childhood. When you were left alone with nothing to do, you would invent a game, build something out of whatever was around, or go explore the backyard.

This simple case shows how I think humans work. We operate in two symbiotic modes, thinking and doing. In thinking mode, we look around, take in ideas and decide what to do next. In doing mode, we work on something and finish it. Each one feeds the other. Boredom pushes the person into thinking, thinking gives him something to do, and once it's done, he is back to thinking about what comes next. Neuroscience describes something similar as exploration and exploitation, where the brain switches between searching for new options and pursuing a known one (Aston-Jones & Cohen, 2005).

Then, I tried thinking about how this lone person would be with a phone. He would still do what he had to do, like finding food, but the moment he had nothing to do, he would pick up the phone and start scrolling, just like we all do. That's when I realized that the phone works like any form of entertainment. It sounds obvious, and I already knew that. But what I didn't realise is that all entertainment serves that same need for exploration that humans have. Deep down, scrolling has exactly the same feeling as chatting with a neighbor, going to the theater or walking the dog. And I had never really understood why entertainment exists until then.

That led to the real question. Lots of entertainment is not a new problem. A full-day trip is nothing but exploration and entertainment, and it feels great. So why does a full afternoon of scrolling feel bad? The difference is in how the exploring happens. On a trip or a long walk, you explore one thing for a long time. Your mind stays on the same line of thought, develops an idea, and eventually lands on something you want to do. Then you go and do it. That is exactly what the brain is looking for.

Scrolling breaks that process. Each video is a different exploration. And exploring is good in itself, because it develops ideas of what to do. But in this case, each video lasts from a few seconds to a minute, so no idea ever has time to develop. So the brain keeps exploring, and after hours it still hasn't found anything it wants to do, and that is where the bad feeling comes from. 

In the end, the only things you end up doing are the ones that other people or circumstances demand, like going to the market (so I don't starve), doing my job (so I don't get fired), going to the gym (because you are already used to).

Understanding this changed how I see my phone. Endless short exploration confuses the brain and leaves me feeling bad. Now I try to use it consciously, as a way to find something to do, and not keep scrolling on autopilot until the bad feeling shows up.












Monday, April 21, 2025

When Passion Fades: The Struggle Between Obligation and Desire

There’s a noticeable difference in how I feel when I’m doing something I genuinely want to do, versus when I’m just completing a task someone gave me. I think I first became aware of this during my third year of college. At that point, I had a mountain of assignments—some from class, others from research projects. They weren’t particularly difficult, but something felt off.

The problem wasn’t the workload. It was the lack of continuity. Once I completed an assignment, I didn’t feel motivated to keep going or explore further. It was like my brain flipped a switch: task done, now shut down. There was no internal drive to go beyond the bare minimum, and honestly, if it weren’t for the looming deadlines, I probably wouldn’t have done them at all.

And that word—deadline—has always rubbed me the wrong way. It felt cold, mechanical. To me, you should want to do something because you’re curious, excited, or passionate—not simply because someone told you to do it within a set timeframe.

This frustration with external expectations isn’t new. I’ve always compared the energy I put into "deadline tasks" with the flow I get when I’m doing things just because I feel like it. This blog post is part of a series where I’m trying to understand why some activities spark enthusiasm and creativity, while others—no matter how small—drain me completely.

At one point, I wondered if my attention span was wrecked by smartphone addiction. I tried forcing myself to cut back, but that didn’t solve anything. If something is truly engaging, it should easily compete with mindless scrolling or random cat videos. Yet somehow, it didn’t.

I’ve also steered clear of motivational videos. They might give you a quick jolt of inspiration, but it’s short-lived. They don't offer sustainable change, at least not for me.

Interestingly, I heard Magnus Carlsen talk about this on the Joe Rogan podcast. He said he loved playing chess for the sake of it, but lost interest when his coach started giving him arbitrary training assignments. It became a chore, and the joy disappeared. I related to that deeply. Not that I’m on Carlsen’s level, but I think the core issue is similar: when something doesn’t come from within, it doesn’t feel like mine. And maybe that’s the trick—maybe the key to doing something wholeheartedly is feeling like you chose it yourself.

It reminds me of the difference between walking and being pushed. If you're walking on your own path—even if it's uphill—you’re in control of the pace, the direction, and the purpose. But if someone’s behind you, pushing you forward, the same walk suddenly feels heavier, even if the terrain hasn’t changed. The weight isn’t in your legs—it’s in your mind.

This also shows up in creative projects. I remember a time when I spontaneously started learning to sketch. I was excited, watching tutorials, practicing lines, losing hours in that beautiful zone where time disappears. But the moment I signed up for a structured online course with weekly assignments and “feedback deadlines,” the excitement vanished. It felt like I’d turned my hobby into homework.

And maybe that’s where the deeper question lies: how do we protect the joy in what we do? How do we keep something from becoming an obligation, even if it needs structure? Perhaps the answer is less about rejecting discipline and more about owning it. Choosing your own deadlines. Setting your own goals. Making the work feel like an extension of your interests, not a disruption of them.

Sometimes I think the answer is in curiosity. When you’re curious, you're not chasing a goal—you’re just exploring. Like a child digging through sand, not trying to build a castle, just fascinated by what’s beneath. That energy is different. It’s sustainable. And it’s fulfilling in a way that “finishing tasks” never is.

So I’m still figuring it out—how to keep that spark alive. How to balance the things I must do with the things I want to do, and ideally make the two overlap more often. Until then, I’ll keep writing, reflecting, and trying to notice the difference between walking freely and being pushed.

Tuesday, February 11, 2025

Why ‘Liking’ Something Might Be an Illusion


I've had friends tell me they "really like programming," and that statement stuck with me. At times, I felt the same way myself.

After completing a project—like building my own version of Tetris or a simple To-Do app using a newly discovered graphics library—I would feel a strong sense of enjoyment for programming. But by the next day, that excitement would fade. This phenomenon never quite made sense to me. Isn’t "liking" something supposed to be a long-term feeling? At least, that's how I always thought it should work.

Recently, however, I had an insight that helped clarify this contradiction. What if it's impossible to "like" doing something in itself? If that were true, it would mean that even "liking to do good for the community" isn’t a real thing—which might sound strange, but let me explain.

I now believe that instead of liking an activity itself, what we actually enjoy is doing that activity well. This idea makes more sense when considering how humans, like all living beings, rely on feedback from their environment to gauge success and progress.

Think about the first time you try to understand how integrated circuits work. At first, the green tracks on the chip seem meaningless. But then you learn that they transfer electricity to different parts of the chip, making the whole system function. You go from knowing nothing to understanding something significant—your learning rate is practically infinite. Since you improved so quickly, it feels great, and you might conclude that you "really like electronics." But what you actually enjoy is learning electronics well. Later, when you dive into more complex topics like energy bands, the process is much slower and frustrating. Suddenly, you don’t "like" it as much anymore.

Here’s a more practical example: I tried surfing three times in my life. The first time, I actively focused on improvement, applying techniques I had seen in a YouTube video. The result? I did surprisingly well, and my brain told me, "I like surfing!" But on my second and third attempts, I felt more relaxed, assuming I had already grasped the basics. However, without muscle memory to reinforce my previous learning, my technique declined. I mispositioned my feet, mistimed the waves, and struggled more than before. My brain now told me the opposite: "I don't like surfing."

These examples suggest that people generally enjoy doing things well rather than just doing them. This perspective also explains a common behavioral pattern: people tend to overestimate their abilities and progress. Initially, I saw this as a flaw, but now I wonder if it might be beneficial. If we need to feel competent to enjoy an activity, then those who overestimate themselves may be more inclined to pursue and stick with new challenges simply because they believe they’re good at them.

This argument challenges the idea that humility is always a virtue. According to this hypothesis, excessive humility could be detrimental—it might prevent someone from recognizing their own competence, thereby reducing their motivation to continue. On the other hand, extreme overconfidence can lead to arrogance and social issues. The ideal balance lies somewhere in between: acknowledging progress without becoming overconfident.

One lingering question remains: Does "doing well" always require comparison? If so, that would explain why Chris Pratt’s character in Passengers became so bored over time—without anyone to compare himself to, he lost a sense of achievement. But even without direct comparison, I believe people can still enjoy activities if they see personal growth and improvement.

With all that said, the next time you hear someone say, "I like doing this," it might be worth asking: What exactly do they like? Is it the activity itself—or the feeling of doing it well?

Sunday, October 20, 2024

The Science of Cravings: Why the First Bite Always Wins


Have you ever finished a delicious meal and suddenly craved a piece of chocolate? That craving arises because the pleasure of eating has ended, and your brain, eager to maintain stimulation, searches for the next source of enjoyment.

Humans naturally seek activities that make them feel good. The brain measures "feeling good" by the amount of hormones—such as dopamine—circulating in it. To maintain a steady flow, it constantly directs us toward actions that maximize these hormones. In the case of chocolate, once the meal is over, the brain instinctively searches for something else that provides a similar or greater level of satisfaction. Chocolate often emerges as an appealing choice.

However, the first bite of chocolate is always more satisfying than the last. This is because the brain self-regulates, signaling, "That's enough, move on to something else." From an evolutionary perspective, this mechanism is crucial. If early humans fixated on only one pleasurable activity, such as building shelters, they wouldn’t have diversified their skills to include hunting, farming, and socializing. To promote variety, the brain gradually reduces the reward response to repetitive tasks over time.

This pattern also appears in everyday activities like studying or working. Initially, you might feel motivated to start your homework to get it off your mind. But as time passes, that motivation fades, and your brain begins seeking alternative sources of stimulation—perhaps calling a friend, gardening, or grabbing a snack. This is simply your brain’s way of ensuring you remain engaged and hormonally balanced to facilitate learning and productivity.

In today’s digital world, the brain has an even easier time finding quick sources of stimulation. The first time you watch a hilarious cat video on Instagram Reels, your brain immediately registers Instagram as a reliable dopamine source. This memory persists because the brain dislikes being unstimulated for long, so it prioritizes recalling past activities that provided easy satisfaction. This explains the constant urge to open entertainment apps—your brain perceives your current task as less rewarding than simply watching more cat videos.

Similarly, consider a programmer debugging code. After hours of effort, a stubborn bug refuses to be solved. Frustrated, the brain—unable to generate satisfaction from failure—shifts its focus. Instead of intensifying problem-solving efforts, it searches for an immediate dopamine fix. That’s when you unconsciously reach for your phone, drawn toward the very distractions that momentarily ease your frustration.

Over time, this cycle becomes more pronounced. You might even receive a screen time notification, realizing how much time you've wasted on entertainment apps. Attempting to fight this urge by forcing yourself to focus entirely on work may succeed for a day, but exhaustion soon sets in, making the temptation to scroll through social media even stronger. The more your brain associates an app with an easy dopamine hit, the harder it becomes to resist.

Trying to fight this instinct head-on is like chopping wood with the blunt side of an axe—it’s ineffective and exhausting. A better approach is understanding why the brain works this way and using that knowledge to your advantage. Instead of simply uninstalling Instagram, a more sustainable solution is convincing yourself that it isn’t a genuine source of happiness. However, rewiring your brain isn’t easy. Since your neural pathways are already conditioned to seek stimulation from these apps, true change requires strong personal conviction.

On a final note, I was inspired to write this after hearing someone say they "needed" to watch TikTok videos to recharge from a tiring day. This made me reflect on the rabbit hole entertainment apps create. What that person didn’t realize was that the very thing they turned to for relief—TikTok—was actually contributing to their exhaustion in the first place.

Wednesday, October 16, 2024

The Silent Cost of Convenience: How Technology is Stealing Our Thinking Time


At first glance, the idea that technology is making us dumber seems absurd. How could something that enabled humanity to reach the Moon possibly be bad for us? And no, I’m not going to push the tired argument that saving phone numbers in our smartphones has made us incapable of memorizing them. Stick with me for a moment—I want to show you something deeper.

As my high school sociology teacher used to say, technology is anything that helps a living being accomplish a task. Cars assist us with transportation, phones enhance communication, and computers boost productivity. However, every technological advancement comes with a hidden trade-off: it transforms slow, time-consuming processes into instant, effortless ones. While this efficiency allows us to achieve more complex feats, we rarely stop to ask—were we designed to operate at this speed?

Consider this: the first Homo sapiens appeared around 300,000 years ago, but the earliest evidence of human creativity—art, trade, ornaments, and burial rites—only dates back about 30,000 years. Before even the invention of agriculture, daily life was simple. People spent time guarding their communities, walking long distances for food, and gathering firewood. The same brain that once handled these basic activities is now tasked with processing abstract concepts like algorithms and marketing strategies. Yet, human evolution never accounted for this sudden shift. Our brains were shaped in an environment where we had ample time to think, plan, and reflect.

In the past, the necessity of performing repetitive, slow tasks naturally created idle time. This downtime was crucial for considering options, envisioning the future, and generating ideas. As technology removes these monotonous activities from our daily lives, it also strips away the unstructured time our minds need to wander and reflect. Today, boredom has become a rare experience. Many people unknowingly seek refuge in the stillness of religious gatherings—not necessarily for faith alone, but because it might be the only hour of boredom they experience all week. The rest of their time is consumed by video calls, social media, and entertainment, leaving little room for deep contemplation.

Somewhere along the way, we lost the understanding that doing nothing—or engaging in slow, repetitive tasks—can actually be beneficial. These moments allow us to process life on a broader scale, fostering a sense of control and clarity. Yet, in a world where we can be stimulated at all times, the only chance we get to reflect is perhaps during a Sunday mass or while washing dishes. But how does a modern human, conditioned to constant engagement, even recognize that they might need seven hours of stillness—just like their ancestors had while farming the land?

Without this mental breathing room, I believe we’re witnessing a rise in psychological issues, driven by a lack of confidence and the inability to engage in meaningful long-term thinking. We’ve known this since WALL-E warned us about it, yet few people actually take the time to unplug and let their minds wander. Instead, we remain trapped in an endless cycle of short-term decisions, never allowing ourselves the space to truly think things through.

Technology has given us incredible advantages, but it has also stolen something fundamental: the ability to simply be with our own thoughts. And if we don’t start reclaiming that time, we might wake up one day to realize that, despite all our progress, we’ve forgotten how to truly think.

Wednesday, September 25, 2024

The Dopamine Trap: How Our Brains Process Rewards and Distractions


Processors in computers operate with an internal clock frequency, synchronizing their operations to execute instructions. Every time a computer processes tasks, energy flows through its circuits, generating heat. To manage this, processors use Dynamic Frequency Scaling, adjusting speed to balance performance and heat dissipation. This mechanism allows the processor to slow down when fewer tasks are required, reducing energy consumption and preventing overheating.

Our brains, however, seem to function differently. Instead of an internal clock, their processing speed appears to fluctuate based on the presence of hormones. When adrenaline surges—say, when escaping a wild boar (I originally considered a bear, but Leonardo DiCaprio proved otherwise)—our cognitive functions accelerate. Conversely, when lying in bed, hormone production drops, and mental activity slows. However, prolonged hormone deficiency does not seem sustainable. From an evolutionary standpoint, maintaining a baseline level of cognitive engagement is crucial for survival and progress. Perhaps this explains why our minds wander in unexpected ways, such as generating a flood of ideas during a Sunday mass. This observation also hints at why prolonged engagement with social media can feel unnatural—despite its entertainment value, something within us resists being submerged in it indefinitely. Over time, I started questioning the addictive design of these platforms and whether it’s possible to escape their grip altogether.

At its core, the desire to engage with something—anything—is what fuels our attraction to entertainment apps like YouTube. These platforms continuously offer new content to explore, preventing boredom from setting in. When boredom does strike, the brain immediately seeks the next course of action. Typically, it follows one of two paths:

  1. The Straightforward Path: This consists of well-defined tasks requiring minimal decision-making. Examples include picking up a book you’ve already started, continuing a blog post, or tackling a cybersecurity challenge. This path is appealing because it offers clear next steps. However, it also includes effortless distractions like scrolling through Instagram—an option that requires no mental effort yet provides instant gratification.

  2. The Novel Path: This involves tackling something new—such as building a physics engine in C, setting up a private DNS server, or contemplating an original idea. These tasks require initial effort just to figure out how to begin. Unlike the Straightforward Path, where progress is immediate, the Novel Path often leads to hesitation. The challenge is avoiding paralysis by over-planning—spending too much time envisioning every step rather than diving in. When a project is simple, mapping out the entire process in advance may work. But for complex endeavors, experimentation is essential. Otherwise, one risks endlessly contemplating possibilities without ever taking action, much like walking on a treadmill and expecting the scenery to change.

The difference between these two paths is clear: the Straightforward Path provides immediate clarity, while the Novel Path requires navigating uncertainty. Yet, despite its challenges, the brain seems to reward the Novel Path more generously—following a "less effort, less reward" principle. This reward system likely evolved to encourage innovation. After all, a species that only repeats familiar actions stagnates, while one that constantly explores new possibilities thrives. This explains why completing a challenging project feels profoundly more fulfilling than an hour of mindless scrolling.

Unfortunately, modern technology exploits this system by flooding the Straightforward Path with effortless, high-reward distractions. Tasks that once required effort—such as reading a book or tidying up—now compete with activities that demand almost nothing: listening to music, watching videos, or endlessly swiping through TikTok. This shift distorts the effort-to-reward ratio, making it alarmingly easy to engage in low-effort behaviors while receiving a deceptive sense of accomplishment. My biggest concern is the possibility of an entire generation growing up without ever experiencing the deep satisfaction that comes from voluntarily pursuing and completing something challenging—because their attention has been hijacked by instant gratification.

This brings me to another key factor: boredom. Boredom often serves as the gateway to Novel Path exploration. Whether sitting in church or enduring a dull lecture, these moments frequently spark new ideas. However, in our hyper-connected world, boredom is rarely tolerated. Eating a meal without watching a video, finishing a project without immediately checking notifications, or simply staring out the window until inspiration strikes—these experiences are becoming rare. Instead, the Straightforward Path takes over almost instinctively, with smartphones offering the fastest escape. While this provides immediate mental engagement, it often represents a local maximum—a temporary reward that falls short of the deeper fulfillment found through true creative exploration.

Perhaps we should give ourselves more time before reflexively reaching for our phones. But what would be the clear incentive to do so?

Tuesday, September 24, 2024

The Brain’s Resistance to Change: Why We Struggle to Shift Focus

Imagine you own a car factory with a well-oiled production line that has been running smoothly for 20 years. Your entire setup is optimized to manufacture Nissan 350Zs, and although sales are still decent, they’ve declined over time. After conducting a market study, you discover that producing Porsche 911s would significantly boost sales. The catch? Transitioning your factory to build Porsches would require a complete overhaul of the production line. On top of that, a voice in your head whispers, “Why change something that’s still working?”—a sentiment that makes the decision even harder.

This dilemma isn’t limited to car manufacturing. This morning, over breakfast, I realized that I’ve experienced a similar internal struggle in an entirely different context. A while ago, I was experimenting with video editing techniques and truly enjoying the process. At some point, I told myself that I should stop and switch to another task. Surprisingly, this turned out to be the most challenging part of the day. It felt as if my brain was resisting the change with all its might. Even though I logically knew it was time to move on, my brain kept flooding itself with chemicals designed to keep me focused on what I was already doing.

Not being a neurologist, I started thinking of an analogy: the brain might function like the flow of a river. Picture a river—every water droplet is moving in the same direction, with a steady speed, which only changes gradually over time. If you suddenly try to redirect the flow, the entire mass of water resists, naturally trying to maintain its course. Similarly, when you're deeply engaged in an activity, your brain has directed its energy and resources to the neurons that best support that task. Changing direction requires a full-scale reconfiguration of hormones and neural pathways, which is why it feels so difficult.

This concept might also explain why social media and entertainment apps are so addictive. If you’re fully immersed in a difficult math problem, resisting distractions like TikTok is relatively easy—the ‘river’ is already flowing in the direction of deep concentration. However, if you open your phone first thing in the morning, when no strong cognitive direction is established, it’s easy to fall into a scrolling loop. Once engaged, your brain’s ‘Entertainment Neurons’ take control, and shifting focus to something demanding becomes a battle against inertia. Even if you force yourself to start a productive task—say, working on an assignment—your brain might still be drawn to the dopamine rush of social media, making concentration difficult.

What concerns me most is how our habits shape the brain over time. Repeated exposure to social media strengthens neural pathways associated with instant gratification, while reducing the volume of mental resources allocated to deep thinking and problem-solving. It’s frightening to consider that my fingers have memorized the exact motion required to open Instagram, as if on autopilot. While social media certainly has its benefits—improving communication and socialization—it’s alarming that something as mindless as clicking an app has become so reflexive.

Moreover, I’ve noticed that excessive use of entertainment apps diminishes my ability to ponder big questions—questions about life, society, and existence itself. Deep thinking requires an uninterrupted mental state, something that modern distractions constantly threaten. Yet, at the same time, completely avoiding social media might come at a cost—potentially hindering my ability to connect with others.

So, what’s the solution? Balancing focus, entertainment, and deep thinking feels like a million-dollar question—one that we all must grapple with in an era of endless digital distractions.

Saturday, March 30, 2024

How to create a Shell: Linux Pipes

 


After having a program that can execute new programs, the next step would be to add new features to the program. One such feature is the pipe, which should behave just like that:
$ ls -l
total 52
drwxrwxr-x 2 joao joao  4096 mar 11 19:09 include
-rw-rw-r-- 1 joao joao   137 mar 11 19:17 Makefile
-rwxrwxr-x 1 joao joao 28768 mar 30 08:49 shell
drwxrwxr-x 2 joao joao  4096 mar 29 18:19 src
drwxrwxr-x 2 joao joao  4096 mar 30 08:12 tests
$ ls -l | wc -l
6
$ ls -l | rev 
25 latot
edulcni 90:91 11 ram 6904  oaoj oaoj 2 x-rxwrxwrd
elifekaM 71:91 11 ram 731   oaoj oaoj 1 --r-wr-wr-
llehs 94:80 03 ram 86782 oaoj oaoj 1 x-rxwrxwr-
crs 91:81 92 ram 6904  oaoj oaoj 2 x-rxwrxwrd
stset 21:80 03 ram 6904  oaoj oaoj 2 x-rxwrxwrd
$ ls -l | rev | rev  
total 52
drwxrwxr-x 2 joao joao  4096 mar 11 19:09 include
-rw-rw-r-- 1 joao joao   137 mar 11 19:17 Makefile
-rwxrwxr-x 1 joao joao 28768 mar 30 08:49 shell
drwxrwxr-x 2 joao joao  4096 mar 29 18:19 src
drwxrwxr-x 2 joao joao  4096 mar 30 08:12 tests

In the first example using pipe, we can see that all the content from the ls -l command goes to the wc -l command. Therefore, the wc command can properly count the number of lines that were printed out by the ls command.


To do that, the shell has to create a pipe that can receive information at one end and push it to the other end so that another process can read information. To be more explicit, what really happens is something like this:


Fortunately, all the work that is associated with the information flow is handled by the operating system. The only thing the shell has to do is to create the pipe and define what is at each end.

But how can we create a pipe? Well, that is easy to create and a little hard to use. To create it, you can just call the syscall pipe(2), and use the provided example. However, if you want information about how it works, refer to the pipe(7) manual page.

One thing at a time. First, how do we call it?

#include <unistd.h>

int pipe(int pipefd[2]);

So, taking only the information from the beginning of the manual page, we could use it like this:

int main(int argc, char *argv[]) {
    int pipefd[2];

    pipe(pipefd);

    return 0;
}

It still doesn't do much, but first, let us understand what the array pipefd is. Remember back in the first post, File Descriptors are just integer numbers that we use to tell the operating system where to read from or write to. Well, the same thing can be used in pipes to tell the OS which end of the pipe we want to access. Each pipe has two ends:


So, the OS opens two different File Descriptors (stored at 
pipefd[0] and pipefd[1]), one for each end. We use pipefd[1] if we want to Write something to the pipe, and pipefd[0] if we want to Read something from the pipe.


First, here is an example using error handling and printing out each file descriptor returned by the pipe syscall:

int main(int argc, char *argv[]) {
    int pipefd[2];

    if (pipe(pipefd) == -1) {
        perror("pipe");
        return 1;
    }

    printf("pipefd[0]: %d\n", pipefd[0]);
    printf("pipefd[1]: %d\n", pipefd[1]);

    return 0;
}

Additionally, here is an easy example to understand how you can use the syscalls write and read using a pipe:

int main(int argc, char *argv[]) {
    char write_msg[] = "Hello, World!";
    char read_msg[sizeof(write_msg)];

    int pipefd[2];

    pipe(pipefd);

    write(pipefd[1], write_msg, sizeof(write_msg));    

    read(pipefd[0], read_msg, sizeof(read_msg));

    printf("read_msg: \"%s\"\n", read_msg);

    return 0;
}

* You can only write to pipefd[1] and read from pipefd[0].

Alternatively, you can think of a Pipe as an OS inner buffer that stores data and gives you two different file descriptors to access it. The special thing about pipes is that it is shared between processes, so if you adapt the fork example from the first post, you can read and write stuff between different processes:

int main(int argc, char *argv[]) {
    int pipefd[2];
    pipe(pipefd);

    if (!fork()) {
        char write_buffer[] = "Hello!";
        write(pipefd[1], write_buffer, sizeof(write_buffer));
        printf("Child wrote: \"%s\"\n", write_buffer);
    } else {
        char buffer[1024] = {0};
        read(pipefd[0], buffer, sizeof(buffer));
        printf("Parent read: \"%s\"\n", buffer);
        wait(NULL);
    }

    return 0;
}

That example should work, you can see that we used the Read End of the pipe in the Parent Process and the Write End of the pipe in the Child Process, so that both processes can communicate. 

Since the fork(2) syscall copies the process, each process has its own copy of pipefd[0] and pipefd[1], however, neither process is using both File Descriptors, so we should close the File Descriptors that are not being used:

int main(int argc, char *argv[]) {
    int pipefd[2];
    pipe(pipefd);

    if (!fork()) {
        char write_buffer[] = "Hello!";
        close(pipefd[0]);
        write(pipefd[1], write_buffer, sizeof(write_buffer));
        printf("Child wrote: \"%s\"\n", write_buffer);
    } else {
        char buffer[1024] = {0};
        close(pipefd[1]);
        read(pipefd[0], buffer, sizeof(buffer));
        printf("Parent read: \"%s\"\n", buffer);
        wait(NULL);
    }

    return 0;
}

It is always good practice to close unused file descriptors, especially when handling multiprocessing and pipes. That is because if you have multiple processes with pipefd[0] open, the OS will figure that there still might be something to be entered, so it will hang the program, causing errors that are difficult to track because of multiprocessing.

But taking a few more steps, what if you want to try to create a pipe operation just like the one we tried in the shell:

$ ls -l | wc -l

Well, to do that you would have to create one pipe, execute both programs ls and wc, and redirect everything that goes to ls stdout to the Write End of the pipe and use everything that is at the Read End of the pipe as stdin for the wc program.

int main(int argc, char *argv[]) {
    int pipefd[2];
    pipe(pipefd);

    if (!fork()) {
        close(pipefd[0]);

        dup2(pipefd[1], 1);

        execl("/bin/ls", "ls", "-l", NULL);
    } else {
        close(pipefd[1]);

        dup2(pipefd[0], 0);

        execl("/usr/bin/wc", "wc", "-l", NULL);
    }

    printf("This line should not execute!\n");

    return 0;
}

And if you don't close the unused File Descriptors, that is where pipes start to become a problem, take a moment to try the following code, and see that it does not finish:

int main(int argc, char *argv[]) {
    int pipefd[2];
    pipe(pipefd);

    if (!fork()) {
        dup2(pipefd[1], 1);

        execl("/bin/ls", "ls", "-l", NULL);
    } else {
        dup2(pipefd[0], 0);

        execl("/usr/bin/wc", "wc", "-l", NULL);
    }

    printf("This line should not execute!\n");

    return 0;
}

Since the parent process still has pipefd[0] opened, the wc program still thought that there might be some more input available, so it didn't finish.

However, even though our second-last program executes the pipe operation correctly, what should we do if wanted our program to continue execution after running ls -l | wc -l. Well, you would have to fork() one more time, just like this:

int main(int argc, char *argv[]) {

    if (!fork()) {
        int pipefd[2];
        pipe(pipefd);
        
        if (!fork()) {
            close(pipefd[0]);

            dup2(pipefd[1], 1);

            execl("/bin/ls", "ls", "-l", NULL);
        } else {
            close(pipefd[1]);

            dup2(pipefd[0], 0);

            execl("/usr/bin/wc", "wc", "-l", NULL);
        }
    } else {
        wait(NULL);
    }

    printf("This line should execute!\n");

    return 0;
}

The bad side of that solution is that it will create an unsustainable amount of if-else conditions. So if you wanted to create one more pipe, it would start becoming messy. To solve that, you can do the same thing as the last example, using the following:

int main(int argc, char *argv[]) {
    int pipefd[2];
    pipe(pipefd);

    if (!fork()) {
        close(pipefd[0]);

        dup2(pipefd[1], 1);

        execl("/bin/ls", "ls", "-l", NULL);

    }

    if (!fork()) {
        close(pipefd[1]);

        dup2(pipefd[0], 0);

        execl("/usr/bin/wc", "wc", "-l", NULL);
    }

    close(pipefd[0]);
    close(pipefd[1]);        

    wait(NULL);

    printf("This line should execute!\n");

    return 0;
}

So now, if you want to nest more pipes, such as:

ls -l | rev | wc -l

You would have to create two pipes, doing as follows:

int main(int argc, char *argv[]) {
    int pipefd1[2];
    pipe(pipefd1);

    if (!fork()) {
        close(pipefd1[0]);

        dup2(pipefd1[1], 1);

        execl("/bin/ls", "ls", "-l", NULL);

    }

    int pipefd2[2];
    pipe(pipefd2);

    if (!fork()) {
        close(pipefd1[1]);

        dup2(pipefd1[0], 0);
        dup2(pipefd2[1], 1);

        execl("/usr/bin/rev", "rev", NULL);
    }

    close(pipefd1[0]);
    close(pipefd1[1]);
    
    if (!fork()) {
        close(pipefd2[1]);

        dup2(pipefd2[0], 0);

        execl("/usr/bin/wc", "wc", "-l", NULL);
    }
       
    close(pipefd2[0]);
    close(pipefd2[1]);        

    wait(NULL);

    printf("This line should execute!\n");

    return 0;
}

And that should be enough information to increment your shell with the pipe feature.

References

1. Implementing a Job Control Shell. https://www.gnu.org/software/libc/manual/html_node/Implementing-a-Shell.html

Sunday, March 17, 2024

How to create a Shell: Linux process creation


Any Linux user has already used a shell to get something compiled or installed. But just some have already built a shell of their own. Therefore, in this post, I would like to explain how processes are created and how to manipulate process file descriptors. With that said, you can create your own shell that might work just like this:
cmd> /usr/bin/ls
1.in  Makefile  hey  hey.c  in.txt  include  out.txt  output.txt  shell  src
cmd> /usr/bin/ls -l
total 72
-rw-rw-r-- 1 joao joao    76 Mar 12 19:50 1.in
-rw-rw-r-- 1 joao joao   137 Mar 11 19:17 Makefile
-rwxrwxr-x 1 joao joao 16064 Mar 15 14:21 hey
-rw-rw-r-- 1 joao joao   280 Mar 15 14:21 hey.c
-rw-rw-r-- 1 joao joao     5 Mar 12 19:47 in.txt
drwxrwxr-x 2 joao joao  4096 Mar 11 19:09 include
-rw-rw-r-- 1 joao joao     8 Mar 12 19:50 out.txt
-rw-rw-r-- 1 joao joao    11 Mar 11 23:13 output.txt
-rwxrwxr-x 1 joao joao 23320 Mar 12 19:48 shell
drwxrwxr-x 2 joao joao  4096 Mar 11 19:09 src
cmd> ./hey   
Enter your name: Joao
Hello Joao!
cmd> ./hey Joao
Hello Joao
cmd> ./hey Joao > out.txt 
cmd> /usr/bin/cat out.txt 
Hello Joao

* Note that in that simple shell, the program path is used (e.g. /usr/bin/ls instead of the alias ls). That is done to simplify the shell, but a good exercise would be to use the PATH environment variable to allow users to use those aliases.

During some of the explanations, I find it easier to understand certain functions if we know how they are implemented. Thus, I will take examples from the very first version of Linux, v0.01 [1]. That is due to its simplicity, and even though the main algorithm might be the same nowadays, the code might not be as friendly.


What is a System Call


In this section, I want to take a second and explain what a system call is, and how a user might use it.

When you are writing code in C, you might have to call your own functions to solve specific tasks, such as a program that might calculate the power of a number:
int power(int a, int b) {
    int result = 1;
    for (int i = 0; i < b; i++) {
        result *= a;
    }
    return result;
}

int main() {
    int a = 2;
    int b = 3;
    printf("2 ^ 3 = %d\n", power(a, b));
    return 0;
}

However, there are more complex tasks that you may want to use in a program, such as telling the shell to display a letter on the screen. Particularly, that is not a complex task to think about, but it is complex system-wise because you need to make your program communicate the letter to the shell so that it can display it on the screen.

Fortunately, someone very smart had this problem way before us, because they created a fundamental function called write() that allows your program to communicate that letter correctly. But how would I use it in a C code?

First of all, you would have to look at the manual page for that function to figure out what parameter it accepts, and which header files you have to include in your program to use it. You would face something like this:

#include <unistd.h>

ssize_t write(int fd, const void *buf, size_t count);

It simply means that, to call that system call you would have to indicate what file should the operating system make this write to, what buffer contains the data that will be used, and how many bytes should be used. As an example, there is a very simple program that uses the write() system call.

#include <unistd.h>

int main() {
    char buffer[] = "Hello, World!\n";
    write(1, buffer, sizeof(buffer));
}

After the buffer was defined, we simply told the operating system to write it to the stdout, where the shell might be expecting something as input to show the user.

Besides that, there are many other system calls shown on the syscalls manual page. Another example that can be used is the read() which has a similar reason to be used as the write() system call, that is why I am not spending time with it. Instead, I will show how the nanosleep() system call can be used.

Repeating the process that was done before, and looking at the nanosleep() manual page, we can see the parameters expected by that function.


#include <time.h>

int nanosleep(const struct timespec *req,
              struct timespec * rem);

That function declaration means that the function must receive a struct containing the time that the process was requested to sleep for, and a struct that the OS will use in case there are any problems during execution but there is still some time remaining. Next, all that is left is knowing what is inside the struct timespec which is easily solved if you are using an IDE. Nevertheless, if you are not, you can check the time.h header file and figure out for yourself:


#ifndef _STRUCT_TIMESPEC
#define _STRUCT_TIMESPEC
struct timespec {
	__kernel_old_time_t	tv_sec;		/* seconds */
	long			tv_nsec;	/* nanoseconds */
};
#endif

Finally, with all variable types searched, using the syscall is as simple as that:

#include <time.h>

int main() {
    struct timespec req = {2, 0};
    struct timespec rem;
    nanosleep(&req, &rem);
}

All things considered, we can go back to the idea of creating a shell.

Process Creation


In the example shown in the beginning, the program /usr/bin/ls was used to display the files in the current working directory. In this section, I will explain how to create a brand-new process with the content that is stored at /usr/bin/ls. 

First of all, in the Linux operating system, the information about all processes is stored in a struct called task_struct:

struct task_struct {
/* these are hardcoded - don't touch */
	long state;	/* -1 unrunnable, 0 runnable, >0 stopped */
	long counter;
	long priority;
	long signal;
	fn_ptr sig_restorer;
	fn_ptr sig_fn[32];
/* various fields */
	int exit_code;
	unsigned long end_code,end_data,brk,start_stack;
	long pid,father,pgrp,session,leader;
	unsigned short uid,euid,suid;
	unsigned short gid,egid,sgid;
	long alarm;
	long utime,stime,cutime,cstime,start_time;
	unsigned short used_math;
/* file system info */
	int tty;		/* -1 if no tty, so it must be signed */
	unsigned short umask;
	struct m_inode * pwd;
	struct m_inode * root;
	unsigned long close_on_exec;
	struct file * filp[NR_OPEN];
/* ldt for this task 0 - zero 1 - cs 2 - ds&ss */
	struct desc_struct ldt[3];
/* tss for this task */
	struct tss_struct tss;
};  

Looking at that struct you can find out what are all the features that are stored by the OS for each process that is created, such as the state, priority, id. All that is needed to be known by the system to decide which process is executed at each time.

You don't really need to understand what all of those fields mean, I put it there just for you to understand deeply what each system call is doing. Additionally, as you would expect of a mu nniprogramming operating system, there is an array of task_struct, called task, to store all the created processes.

struct task_struct * task[NR_TASKS] = {&(init_task.task), };

Having said that, we should seek an operating system API (also known as system call) that adds a new process to that array. If you care to search for it, you will find the fork() system call, defined inside the Linux Kernel, which is the only routine that adds a newly allocated variable inside the array that stores all processes. For us to understand what it does, there is the assembly code that is executed when a user executes the fork() system call:

_sys_fork:
	call _find_empty_process
	testl %eax,%eax
	js 1f
	push %gs
	pushl %esi
	pushl %edi
	pushl %ebp
	pushl %eax
	call _copy_process
	addl $20,%esp
 	ret

Although there are many operations, we can focus just on the call operations that execute the functions _find_empty_process() and _copy_process(). Those function names are very interesting so we can find out what the syscall does by looking at them.

First, the OS looks for an empty place on the tasks array and then it defines a new task_struct to place it in there with exactly the same content that is inside the process that is called the fork() syscall. That behavior also gives the syscall its name, because it works like a "process mitosis".

Furthermore, it is possible to check even how the find_empty_process and copy_process functions work. However, besides some error-checking code, they work just like you would have thought they work:

int find_empty_process(void)
{
	int i;

	repeat:
		if ((++last_pid)<0) last_pid=1;
		for(i=0 ; i<NR_TASKS ; i++)
			if (task[i] && task[i]->pid == last_pid) goto repeat;
	for(i=1 ; i <NR_TASKS ; i++)
		if (!task[i])
			return i;
	return -EAGAIN;
}

/*
 *  Ok, this is the main fork-routine. It copies the system process
 * information (task[nr]) and sets up the necessary registers. It
 * also copies the data segment in it's entirety.
 */
int copy_process(int nr,long ebp,long edi,long esi,long gs,long none,
		long ebx,long ecx,long edx,
		long fs,long es,long ds,
		long eip,long cs,long eflags,long esp,long ss)
{
	struct task_struct *p;
	int i;
	struct file *f;

	p = (struct task_struct *) get_free_page();
	if (!p)
		return -EAGAIN;
	*p = *current;	/* NOTE! this doesn't copy the supervisor stack */
	p->state = TASK_RUNNING;
	p->pid = last_pid;
	p->father = current->pid;
	p->counter = p->priority;
	p->signal = 0;
	p->alarm = 0;
	p->leader = 0;		/* process leadership doesn't inherit */
	p->utime = p->stime = 0;
	p->cutime = p->cstime = 0;
	p->start_time = jiffies;
	p->tss.back_link = 0;
	p->tss.esp0 = PAGE_SIZE + (long) p;
	p->tss.ss0 = 0x10;
	p->tss.eip = eip;
	p->tss.eflags = eflags;
	p->tss.eax = 0;
	p->tss.ecx = ecx;
	p->tss.edx = edx;
	p->tss.ebx = ebx;
	p->tss.esp = esp;
	p->tss.ebp = ebp;
	p->tss.esi = esi;
	p->tss.edi = edi;
	p->tss.es = es & 0xffff;
	p->tss.cs = cs & 0xffff;
	p->tss.ss = ss & 0xffff;
	p->tss.ds = ds & 0xffff;
	p->tss.fs = fs & 0xffff;
	p->tss.gs = gs & 0xffff;
	p->tss.ldt = _LDT(nr);
	p->tss.trace_bitmap = 0x80000000;
	if (last_task_used_math == current)
		__asm__("fnsave %0"::"m" (p->tss.i387));
	if (copy_mem(nr,p)) {
		free_page((long) p);
		return -EAGAIN;
	}
	for (i=0; i < NR_OPEN;i++)
		if (f=p->filp[i])
			f->f_count++;
	if (current->pwd)
		current->pwd->i_count++;
	if (current->root)
		current->root->i_count++;
	set_tss_desc(gdt+(nr<<1)+FIRST_TSS_ENTRY,&(p->tss));
	set_ldt_desc(gdt+(nr<< 1)+FIRST_LDT_ENTRY,&(p->ldt));
	task[nr] = p;	/* do this last, just in case */
	return last_pid;
}

That might lead to a very interesting question: If all processes are forked, where does the first process come from? Well, if you look at your Linux Operating system to check which is the process with ID 1, you can see that it came from a program called init. 

$ ps aux | head -n 2
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root           1  0.1  0.0 167660 12848 ?        Ss   11:57   0:43 /sbin/init splash

Moreover, you can look inside the Linux source code and find what the init.c file really is, but it essentially initializes some code and runs the first shell of the system:

void main(void)		/* This really IS void, no error here. */
{			/* The startup routine assumes (well, ...) this */
/*
 * Interrupts are still disabled. Do necessary setups, then
 * enable them
 */
	time_init();
	tty_init();
	trap_init();
	sched_init();
	buffer_init();
	hd_init();
	sti();
	move_to_user_mode();
	if (!fork()) {		/* we count on this going ok */
		init();
	}
/*
 *   NOTE!!   For any other task 'pause()' would mean we have to get a
 * signal to awaken, but task0 is the sole exception (see 'schedule()')
 * as task 0 gets activated at every idle moment (when no other tasks
 * can run). For task0 'pause()' just means we go check if some other
 * task can run, and if not we return here.
 */
	for(;;) pause();
}

void init(void)
{
	int i,j;

	setup();
	if (!fork())
		_exit(("/bin/update",NULL,NULL));
	(void) open("/dev/tty0",O_RDWR,0);
	(void) dup(0);
	(void) dup(0);
	printf("%d buffers = %d bytes buffer space\n\r",NR_BUFFERS,
		NR_BUFFERS*BLOCK_SIZE);
	printf(" Ok.\n\r");
	if ((i=fork()) < 0)
		printf("Fork failed in init\r\n");
	else if (!i) {
		close(0);close(1);close(2);
		setsid();
		(void) open("/dev/tty0",O_RDWR,0);
		(void) dup(0);
		(void) dup(0);
		_exit(execve("/bin/sh",argv,envp));
	}
	j=wait(&i);
	printf("child %d died with code %04x\n",j,i);
	sync();
	_exit(0);	/* NOTE! _exit, not exit() */
}

That process is the father of all processes that are executed. In fact, the process that executes the fork() operation is called "father" and the process that is created is called "child". Furthermore, that relationship is so important for the OS, that it is stored inside the task_struct as the parameter "father".

Back to the track, how do you use the fork() system call? Repeating the important process of looking at the manual page we can find the following declaration:

#include <unistd.h>

pid_t fork(void);

Quite easy, it means that I program like that executes correctly the fork system call:

#include <sys/types.h>
#include <unistd.h>

int main() {

    fork();

    return 0;
}

Right, but after the child process is created, where is the next line that the father process executes? And what about the child process? If we try adding a print function just behind the fork call, we can find that out:

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>

int main() {

    fork();

    printf("Hey!\n");

    return 0;
}

As we can see in the result, the print function will be executed twice, once by the father-process and later by the child-process. Interestingly, it is not executed necessarily in that order. In other words, the child-process can be executed first if the OS decides so.

But if both processes continue the execution just below the fork() function, it does not help us much if we can't control what each process executes independently. That is why the return value of the fork() function is used. If we take a look at what is return, we can see that there are different return values on each execution, so that we can control what each execution is doing.

int main() {
    pid_t pid = fork();

    printf("Hey! %d\n", pid);

    return 0;
}

If we execute it many times we will see that one of the PIDs printed changes, but there is one that is always zero. Therefore, to understand which process receives each value, we can go back to the man page at the RETURN VALUE section from the manual page:

RETURN VALUE
       On success, the PID of the child process is returned in the
       parent, and 0 is returned in the child.  On failure, -1 is
       returned in the parent, no child process is created, and errno is
       set to indicate the error.

All that means that the process that receives 0 is the child-process, and the process that something else is the father-process. So to control what each process executes, we can do something like that:

int main() {
    pid_t pid = fork();

    if (pid == 0) {
        printf("Child-Process\n");
    } else {
        printf("Parent-Process\n");
    }

    return 0;
}

Also, if you look closely at the init function, you can see that this exact strategy was used to create a new shell. But now there is a tricky part, what happens when we want the child-process to ask for the user input? 

int main() {
    pid_t pid = fork();

    if (pid == 0) {
        printf("Child-Process\n");
        
        int number;
        printf("Enter a number: ");
        scanf("%d", &number);
        printf("You entered: %d\n", number);

    } else {
        printf("Parent-Process\n");
    }

    return 0;
}

You will figure out that the execution is canceled before you can try to type anything in the terminal. That is because both father and child processes are executed in parallel, but as soon as the father process is done, the shell prompt is executed again. Thus, to solve that we can execute another syscall in the father process for it to wait for the child to execute, such as the nanoseconds() syscall:

int main() {
    pid_t pid = fork();

    if (pid == 0) {
        printf("Child-Process\n");
        
        int number;
        printf("Enter a number: ");
        scanf("%d", &number);
        printf("You entered: %d\n", number);

    } else {
        printf("Parent-Process\n");

        struct timespec req = {10, 0};
        struct timespec rem;
        nanosleep(&req, &rem);
    }

    return 0;
}

Great, now you have 10 seconds to enter something inside the child process. However, the use case of having the father-process to wait for the child-process seems too regular for a solution like that. In fact, the solution to that process is to use another syscall called wait(), which was built exactly for this use case:

int main() {
    pid_t pid = fork();

    if (pid == 0) {
        printf("Child-Process\n");
        
        int number;
        printf("Enter a number: ");
        scanf("%d", &number);
        printf("You entered: %d\n", number);

    } else {
        printf("Parent-Process\n");

        wait(NULL);
    }

    return 0;
}

In that program, the father-process executes the print function and hangs until the child process is terminated. 

Although using fork() that way is useful for parallel processing, there is another usage of process creation which is to create a new process with the content of another binary file. Going back to the shell idea, the process being executed is the shell process, so if the user might want to execute a binary to list the files in the current working directory (e.g. /usr/bin/ls) or a binary to print out the content of a specific file (e.g. /usr/bin/cat), we have to create a new process using fork() with the instructions inside those binary files.

Lucky for us, there is a syscall called execve() that can do just that. Referencing the manual page again we can find out the function declaration:

#include <unistd.h>

int execve(const char *pathname, char *const argv[], char *const envp[]);

That function will replace all the process memory with the content of the binary that you reference using the pathname variable. Consider the following example that uses the execve() syscall:

int main() {
    char *const argv[] = {"/usr/bin/ls", "-l", NULL};
    
    execve("/usr/bin/ls", argv, NULL);

    printf("This line should not be printed\n");

    return 0;
}

That function receives the path from the binary that you want it to use, and then it allows the user to give arguments to the binary using the argv array, and environment variables using the envp array. Those variables are the same ones that you can access if you create a C program like that:

int main(int argc, char *argv[], char *envp[]) {
    
    /* Your Code Here */

    return 0;
}

* Note that both arrays argv and envp arrays must finish with a NULL pointer, because otherwise, the program that is being executed will not be able to know when the array ends. 

* Also, note that the process changes itself completely when the execve is executed, and even though the execve() example has a print function to execute after the execve, it will never be called.

As a result of that, we can improve the fork() example by making the child process execute another binary, just like that:

int main() {
    pid_t pid = fork();

    if (pid == 0) {
        
        char pathname[] = "/usr/bin/ls";
        char *argv[] = {pathname, "-l", NULL};
        char *envp[] = {NULL};

        execve(pathname, argv, envp);

    } else {
        wait(NULL);
    }

    return 0;
}

Manipulating File Descriptors


Next, the second feature of your Mini-Shell is being able to use the content of a file as input or redirect the output of the process to a certain file, just like in the example:

cmd> /bin/cat in.txt 
Joao
cmd> ./hey < in.txt
Enter your name: Hello Joao!
cmd> ./hey < in.txt > out.txt 
cmd> /bin/cat out.txt 
Enter your name: Hello Joao!

Every time you open a file to write or read it, you receive a File Descriptor representing that file that can be used just inside the process that called open(). That File Descriptor is an integer number that serves as an identifier of that opened file. With that ID, you can tell the OS which file it is supposed to "read from" or "write to". Take the following example:

int main() {
    int fd = open("in.txt", O_RDONLY);
    printf("fd: %d\n", fd);
    return 0;
}

If you run the example above you will see that the opened file is now identified as 3 to the process, and you will use that number on the read/write calls. Additionally, that value means that File Descriptors 0, 1, and 2 already exist. Indeed they do exist and they are:
  • 0: Standard Input.
  • 1: Standard Output.
  • 2: Standard Error.
Those are not exactly files with bytes stored in your disk but they are files to the OS. And you use them when:
  • 0: You want to read something from the user. So you can only read from it.
  • 1: You want to write something to the user. So you can only write to it.
  • 2: You want to write an error to the user. So you can only write to it.
There is a code to show an example of that usage:

int main() {
    char buffer[1024] = {0};
    
    ssize_t count = read(0, buffer, 1023);

    if (count == -1) {
        write(2, "An error occurred in the read.\n", 31);
    } else {
        write(1, buffer, count);
    }
}

In summary, the next opened File Descriptor was 3 because it was the next on the list of available identifiers.

Furthermore, you can go back to the Linux source code to check exactly what happens to the task_struct of the process that calls the syscall to open a file:

int sys_open(const char * filename,int flag,int mode)
{
	struct m_inode * inode;
	struct file * f;
	int i,fd;

	mode &= 0777 & ~current->umask;
	for(fd=0 ; fd<NR_OPEN ; fd++)
		if (!current->filp[fd])
			break;
	if (fd>=NR_OPEN)
		return -EINVAL;
	current->close_on_exec &= ~(1<<fd);
	f=0+file_table;
	for (i=0 ; i<NR_FILE ; i++,f++)
		if (!f->f_count) break;
	if (i>=NR_FILE)
		return -EINVAL;
	(current->filp[fd]=f)->f_count++;
	if ((i=open_namei(filename,flag,mode,&inode))<0) {
		current->filp[fd]=NULL;
		f->f_count=0;
		return i;
	}
/* ttys are somewhat special (ttyxx major==4, tty major==5) */
	if (S_ISCHR(inode->i_mode))
		if (MAJOR(inode->i_zone[0])==4) {
			if (current->leader && current->tty<0) {
				current->tty = MINOR(inode->i_zone[0]);
				tty_table[current->tty].pgrp = current->pgrp;
			}
		} else if (MAJOR(inode->i_zone[0])==5)
			if (current->tty<0) {
				iput(inode);
				current->filp[fd]=NULL;
				f->f_count=0;
				return -EPERM;
			}
	f->f_mode = inode->i_mode;
	f->f_flags = flag;
	f->f_count = 1;
	f->f_inode = inode;
	f->f_pos = 0;
	return (fd);
}

It is pretty simple, inside the task_struct of the process, there is a variable called filp, which is an array that will store information about each file that is opened by that process. So the filp[0] will store information about the Standard Input File, the filp[1] will store information about the Standard Output File, and so on. And that is what the File Descriptor is, just an index in the filp array that is returned to the user.

When you call the open system call, that function looks for the next available spot in the filp array and creates a struct file with important information about the file that you requested to open. In the future, the OS will look at this information to access the correct memory addresses on your disk.

However, a certain file on your disk can be opened more than once by the same process, so that it has the same struct file in two different spots of the filp array:

int main() {
   int fd = open("in.txt", O_RDONLY);

   printf("fd: %d\n", fd);

   int fd2 = open("in.txt", O_RDONLY);

   printf("fd2: %d\n", fd2);

   return 0;
}

That same process is done by the dup() system call, in the following way:

int main() {
    int fd = open("in.txt", O_RDONLY);

    printf("fd: %d\n", fd);

    int fd2 = dup(fd);

    printf("fd2: %d\n", fd2);

    return 0;
}

The previous code will get the next free index at the array filp and duplicate the same struct file in there. Furthermore, there is a dup2() system call, that is an extension of dup() because it allows the user to choose which File Descriptor to duplicate it to. Consider the following code:

int main() {
    int fd = open("in.txt", O_RDONLY);

    printf("fd: %d\n", fd);

    int fd2 = dup2(fd, 5);

    printf("fd2: %d\n", fd2);

    return 0;
}

In that code, we said that we want index 5 to store the struct from the file that we opened before. That is very serious, what might happen if we say that we want to duplicate it to the index 0?

int main() {
    int fd = open("in.txt", O_RDONLY);

    printf("fd: %d\n", fd);

    int fd2 = dup2(fd, 0);

    printf("fd2: %d\n", fd2);

    return 0;
}

Remember that inside the index 0 of the filp array, there is information about the Standard Input File? It means that every time you use a function that tries to ask for information from the user (e.g. scanf()), it will use the File Descriptor 0 for it. Therefore, if you duplicate the File Descriptor of an ordinary file to that specific index 0, you will modify where those functions get the information from. For example:

int main() {
    char buffer[1024] = {0};
    
    int fd = open("in.txt", O_RDONLY);

    int fd2 = dup2(fd, 0);

    scanf("%[^\n]", buffer);

    printf("Buffer: %s\n", buffer);

    return 0;
}

In that example, you won't have to write anything to the console, because the scanf() will get the information it needs from the "in.txt" file. 

The same result can be done both to the Standard Output FIle and the Standard Error File. Using the same logic, the following code example stores all the information printed to the "out.txt" file:

int main() {
    int fd = open("out.txt", O_WRONLY | O_CREAT, 0644);
    
    dup2(fd, 1);

    printf("Hello, world!\n");

    return 0;
}

Conclusion

That sums it all up, with all that information and some string parsing you will be able to create something that looks just like that simple shell I showed you in the beginning.

References