<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: SinghDevHub</title>
    <description>The latest articles on Forem by SinghDevHub (@singhdevhub).</description>
    <link>https://forem.com/singhdevhub</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F981125%2Ff21f11cc-ad63-4ae0-a57d-745459c4aee0.jpeg</url>
      <title>Forem: SinghDevHub</title>
      <link>https://forem.com/singhdevhub</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/singhdevhub"/>
    <language>en</language>
    <item>
      <title>how we prevent ai agent's drift &amp; code slop generation</title>
      <dc:creator>SinghDevHub</dc:creator>
      <pubDate>Thu, 22 Jan 2026 18:17:55 +0000</pubDate>
      <link>https://forem.com/singhdevhub/how-we-prevent-ai-agents-drift-code-slop-generation-2eb7</link>
      <guid>https://forem.com/singhdevhub/how-we-prevent-ai-agents-drift-code-slop-generation-2eb7</guid>
      <description>&lt;p&gt;At &lt;a href="https://traycer.ai/" rel="noopener noreferrer"&gt;TraycerAI&lt;/a&gt; we reduced hallucinations, code slop generation and solved agent's drift. Finally you can ship complex features, end to end products with world's first AI product planner. &lt;/p&gt;

&lt;p&gt;Traycer is built on the core philosophy of Spec driven development and is way ahead than anyone due to its approach.&lt;/p&gt;

&lt;h3&gt;
  
  
  Lets discuss, how we've achieved this
&lt;/h3&gt;

&lt;p&gt;AI agents that write code have a dirty little secret, they drift, hallucinate, and generate confident garbage. After building AI agents that have executed large scale agentic workloads, here's what actually works to stop them.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Core Problem
&lt;/h3&gt;

&lt;p&gt;AI coding agents aren't stateless chatbots. They explore codebases, make decisions, loop through tool calls, and build context over time. Without proper guardrails, they drift from the original task, get stuck in infinite exploration cycles, or generate plausible-looking code that's fundamentally wrong.&lt;/p&gt;

&lt;p&gt;The solution isn't one clever trick, it's a system of interlocking safeguards.&lt;/p&gt;

&lt;p&gt;Circuit Breakers: Hard &amp;amp; Soft Thresholds&lt;/p&gt;

&lt;p&gt;Every agentic loop needs circuit breakers. We implement a dual-threshold system:&lt;/p&gt;

&lt;p&gt;At the warning threshold, the agent gets a nudge: "You've explored the codebase enough. Time to deliver your response." This redirects without forcing termination.&lt;/p&gt;

&lt;p&gt;At the hard threshold, we force completion. The agent must use whatever context it has gathered and produce output, no more exploration allowed. Without this, agents will happily research forever, burning tokens while producing nothing.&lt;/p&gt;

&lt;h3&gt;
  
  
  Structured Output Contracts
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Never trust raw LLM output. Every response passes through strict validation:&lt;/li&gt;
&lt;li&gt;JSON/XML schema validation&lt;/li&gt;
&lt;li&gt;Type checking against expected structures&lt;/li&gt;
&lt;li&gt;Completeness verification (did we get all required fields?)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If output is malformed, we retry with explicit correction instructions. If it's still broken after retries, we fail gracefully with actionable error messages, not silent corruption.&lt;/p&gt;

&lt;h3&gt;
  
  
  Explicit Termination Tools
&lt;/h3&gt;

&lt;p&gt;Here's a subtle but critical insight: don't let agents decide when they're "done" through natural language. Give them explicit termination tools they must invoke, complete_review, submit_plan, finish_verification.&lt;/p&gt;

&lt;p&gt;This creates auditable completion points. We know exactly when and why an agent decided to terminate, and we can validate that the termination was legitimate.&lt;/p&gt;

&lt;h3&gt;
  
  
  Persistent State Management
&lt;/h3&gt;

&lt;p&gt;Agent state is precious.&lt;br&gt;
Connection tracking ensures state survives interruptions. LRU caching keeps frequently-accessed objects in memory for performance. If the agent crashes mid-task or the user closes their laptop, we can resume from the last checkpoint, not restart from scratch.&lt;/p&gt;

&lt;p&gt;This isn't just about reliability. It's about building user trust. Nothing kills confidence faster than losing 10 minutes of agent work to a network hiccup.&lt;/p&gt;

&lt;h3&gt;
  
  
  AI Reviewing AI
&lt;/h3&gt;

&lt;p&gt;This sounds counterintuitive, but it works: after code generation, a separate verification agent reviews the output with fresh eyes.&lt;/p&gt;

&lt;p&gt;Does the change align with user intent?&lt;/p&gt;

&lt;p&gt;Are there unintended side effects?&lt;/p&gt;

&lt;p&gt;Did it miss edge cases the original agent overlooked?&lt;br&gt;
Different model, different prompt, different perspective. The verification agent has no investment in defending the original output, its only job is finding problems.&lt;/p&gt;

&lt;h3&gt;
  
  
  Provider Redundancy
&lt;/h3&gt;

&lt;p&gt;Single LLM provider equals single point of failure. Our routing layer maintains multiple providers per request type with automatic failover. If one provider hits rate limits or returns errors, we transparently switch to the next.&lt;/p&gt;

&lt;p&gt;The agent doesn't even know which provider responded. This abstraction keeps the core logic clean while giving us resilience against the inevitable API hiccups.&lt;/p&gt;

&lt;h3&gt;
  
  
  Scope Boundaries
&lt;/h3&gt;

&lt;p&gt;Vague scope produces vague output. We explicitly constrain agents: stay focused on the codebase, don't venture into production deployment, don't assume access to external systems you can't verify.&lt;/p&gt;

&lt;p&gt;When agents know their boundaries, they make better decisions within them.&lt;/p&gt;

&lt;h3&gt;
  
  
  What Doesn't Work
&lt;/h3&gt;

&lt;p&gt;A few anti-patterns we've learned to avoid:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;"Just tell the agent to be careful" :- Prompting for carefulness doesn't prevent systematic drift&lt;/li&gt;
&lt;li&gt;Single retry on failure :- Real-world errors need exponential backoff and provider failover&lt;/li&gt;
&lt;li&gt;Implicit completion detection :- "The agent stopped talking, so it must be done" is a recipe for incomplete work&lt;/li&gt;
&lt;li&gt;Trusting token counts for progress :- More tokens doesn't mean more useful work&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The System, Not The Trick
&lt;/h3&gt;

&lt;p&gt;Preventing AI agent drift isn't about one clever technique. It's about building a system where every layer catches what the others miss:&lt;/p&gt;

&lt;p&gt;Thresholds prevent infinite loops. Validation catches malformed output. Persistent state enables recovery. Verification catches logical errors. Redundancy handles infrastructure failures.&lt;br&gt;
Each safeguard is simple. Together, they create reliable agents.&lt;br&gt;
Build the system. Don't hope for the best.&lt;/p&gt;

&lt;p&gt;PS: These are patterns we’ve found effective across multiple agent systems.&lt;/p&gt;

&lt;p&gt;What's your biggest challenge with AI agent reliability?&lt;/p&gt;

</description>
      <category>genai</category>
      <category>ai</category>
      <category>llm</category>
      <category>mcp</category>
    </item>
    <item>
      <title>finally sharing what we built with our dev community</title>
      <dc:creator>SinghDevHub</dc:creator>
      <pubDate>Fri, 16 Jan 2026 09:53:14 +0000</pubDate>
      <link>https://forem.com/singhdevhub/finally-sharing-what-we-built-with-our-dev-community-531h</link>
      <guid>https://forem.com/singhdevhub/finally-sharing-what-we-built-with-our-dev-community-531h</guid>
      <description>&lt;div class="ltag__link"&gt;
  &lt;a href="/singhdevhub" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F981125%2Ff21f11cc-ad63-4ae0-a57d-745459c4aee0.jpeg" alt="singhdevhub"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="https://dev.to/singhdevhub/how-we-built-the-most-advanced-ai-product-planner-31lc" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;how we built the most advanced ai product planner&lt;/h2&gt;
      &lt;h3&gt;SinghDevHub ・ Jan 16&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#ai&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#rag&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#programming&lt;/span&gt;
        &lt;span class="ltag__link__tag"&gt;#product&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


</description>
      <category>ai</category>
      <category>rag</category>
      <category>programming</category>
      <category>product</category>
    </item>
    <item>
      <title>how we built the most advanced ai product planner</title>
      <dc:creator>SinghDevHub</dc:creator>
      <pubDate>Fri, 16 Jan 2026 09:43:02 +0000</pubDate>
      <link>https://forem.com/singhdevhub/how-we-built-the-most-advanced-ai-product-planner-31lc</link>
      <guid>https://forem.com/singhdevhub/how-we-built-the-most-advanced-ai-product-planner-31lc</guid>
      <description>&lt;h2&gt;
  
  
  intro
&lt;/h2&gt;

&lt;p&gt;𝐒𝐨𝐟𝐭𝐰𝐚𝐫𝐞 𝐈𝐬𝐧’𝐭 𝐚 𝐒𝐢𝐝𝐞𝐰𝐚𝐥𝐤 – 𝐈𝐭’𝐬 𝐚 𝐂𝐢𝐭𝐲&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;unplanned codebases are like unplanned cities: brittle, inconsistent, and costly to extend. &lt;/li&gt;
&lt;li&gt;scalable software, like civil infrastructure, needs clear specs, deliberate architecture, and continuous verification. &lt;/li&gt;
&lt;li&gt;skipping these, especially when AI does the typing, leads to rigid, unmaintainable systems.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  models as humans reading a book
&lt;/h2&gt;

&lt;p&gt;1️⃣ when you are reading a book, you carry your attention with yourself. you tend to forget irrelevant information and retrieve the important one. this keeps your mind/attention on the track.&lt;br&gt;
2️⃣ but models don't have the same capabilities as human's brain. they go offtrack when fed up everything at once. models do not act phase by phase. models do hallucinations as they do a lot of things at once.&lt;br&gt;
3️⃣ indexing the book before reading, skipping irrelevant chapters &amp;amp; focusing attention on most important words can give you the whole context. this is how we as humans treat our books.&lt;/p&gt;

&lt;p&gt;tweaking this same thinking to LLM models, what if we can provide them relevant information &amp;amp; stop telling them everything at once (context bloat).&lt;/p&gt;

&lt;p&gt;❓what if we ask our models to do the thing which they are really good at? what if we tell them to do work in phases? what if we spawn an army of agents to do work &amp;amp; collaborate on the tasks after they break down the tasks?&lt;/p&gt;

&lt;p&gt;😎 YES, this is exactly like tackling a big problem in smaller sub problems which we proudly call as divide &amp;amp; conquer in computer science's world&lt;/p&gt;

&lt;h2&gt;
  
  
  become an orchestra not an oracle
&lt;/h2&gt;

&lt;p&gt;assume having a god-model which performs really well when it starts its work but forgets things as it proceeds with the task. that is what an oracle tries to do &amp;amp; hence we see a lot of errors also being generated from a god agent. doing everything at once will drift the agent and going offtrack in code-generation means a lot more iterations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;⚡️that's why we built &lt;a href="https://traycer.ai/" rel="noopener noreferrer"&gt;TraycerAI&lt;/a&gt;  :- The ultimate AI product planner&lt;br&gt;
the architecture: an ensemble of specialists&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;instead of a single "god-model" that tries to be a jack-of-all-trades, Traycer operates as a coordinated ensemble. we’ve decoupled intelligence from retrieval, ensuring that our "thinkers" aren't distracted by the "noise" of raw data gathering.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the orchestrator (sonnet-4.5): acts as the conductor. it handles high-level reasoning, complex planning, and task decomposition. it doesn’t get its hands dirty with file searching; it directs the flow.&lt;/li&gt;
&lt;li&gt;the critics (GPT-5.1): specializing in code analysis and verification. while one model builds the plan, another, with a different "personality" and training bias, critiques the output to catch regressions.&lt;/li&gt;
&lt;li&gt;the scouts (grok-4.1-fast &amp;amp; parallel.ai): these are our high-speed units. they fan out across your codebase and the web in parallel to gather context. they provide the "raw facts" back to the orchestrator without adding their own editorial bias.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  defining the loops: outer vs. inner (again)
&lt;/h2&gt;

&lt;p&gt;to understand Traycer, you have to understand where it sits in the development lifecycle. most AI tools are focused on the inner loop, but Traycer owns the outer loop.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;the inner loop: the "How"&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;this is the tactical layer. it’s the act of writing code, patching lines, and running local tests. it’s where your code-gen agents live. it answers the question: "Can you write this specific function for me?"&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;the outer loop: the "Why" and "What"&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Traycer lives here. the outer Loop is the strategic layer that governs the entire change process. it doesn’t just write code; it manages the intent.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;strategic planning: before a single line is written, Traycer decomposes a high-level prompt (e.g., "Add rate limiting") into a phased implementation spec.&lt;/li&gt;
&lt;li&gt;context synthesis: It determines which files matter across a massive repository, long before a code-gen agent starts its work.&lt;/li&gt;
&lt;li&gt;final verification: after the inner loop finishes, the outer loop steps back in to verify the changes against the original architectural constraints.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;📍 &lt;strong&gt;key takeaway:&lt;/strong&gt; by separating these loops, Traycer ensures that the "Thinking" (Outer Loop) is never compromised by the "Doing" (Inner Loop). you get the speed of parallel agents with the oversight of a senior architect.&lt;/p&gt;




&lt;p&gt;if you are building end to end products, features, you should definitely checkout our EPIC mode in Traycer which:-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;capture user's intent really really well&lt;/li&gt;
&lt;li&gt;generate LLD diagrams, wireframes like a senior engineer&lt;/li&gt;
&lt;li&gt;interviews you like an software architect before generating code&lt;/li&gt;
&lt;li&gt;and verifies like a senior QA to detect edge cases&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&amp;amp; you will get amazed how accurate your features, products will come out&lt;/p&gt;

</description>
      <category>ai</category>
      <category>rag</category>
      <category>programming</category>
      <category>product</category>
    </item>
    <item>
      <title>Vibe coding method that actually works</title>
      <dc:creator>SinghDevHub</dc:creator>
      <pubDate>Tue, 13 Jan 2026 08:58:23 +0000</pubDate>
      <link>https://forem.com/singhdevhub/vibe-coding-method-that-actually-works-2pbb</link>
      <guid>https://forem.com/singhdevhub/vibe-coding-method-that-actually-works-2pbb</guid>
      <description>&lt;h2&gt;
  
  
  📍 Engineering's current situation
&lt;/h2&gt;

&lt;p&gt;You might have heard this already a ton of times that software engineers are gonna get replaced with AI systems, agents and tools.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4cmcdqmvm80wb3y0b4hw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4cmcdqmvm80wb3y0b4hw.png" alt="Software engineers meme" width="469" height="298"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Well, We will not comment too much on this particular thing as no one is clear about this. But few predictions/things already happening in the industry:-&lt;/p&gt;

&lt;p&gt;1️⃣ small teams are getting really really effective/productive&lt;br&gt;
2️⃣ people with agency and intent are getting preferred over people who are very skilled or smart&lt;br&gt;
3️⃣ companies will move even faster than before&lt;br&gt;
4️⃣ softwares building themselves &lt;/p&gt;

&lt;p&gt;😳 Examples:-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://www.claude.com/product/claude-code" rel="noopener noreferrer"&gt;claude code&lt;/a&gt; is being built using claude&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://traycer.ai/" rel="noopener noreferrer"&gt;Traycer&lt;/a&gt; is being built using TraycerAI&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But there is one common thing in every organisation that it is not about:- &lt;br&gt;
❓&lt;strong&gt;should you use AI or not?&lt;/strong&gt; It is about:- &lt;strong&gt;how you should use AI.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;A lot of people don't know if they are doing vibe coding or doing AI Assisted coding. Yep, there is a huge difference in Vibe Coding &amp;amp; AI Assisted coding.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  📍 Usual way of vibe coding
&lt;/h2&gt;

&lt;p&gt;Vibe coding workflow generally looks like this:- &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You have an agentic IDE (cursor, windsurf) or CLI agents like (claude code, gemini cli)&lt;/li&gt;
&lt;li&gt;You put a prompt in the chat window&lt;/li&gt;
&lt;li&gt;Your agent reads the prompt, plans and then start generating code&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This method works on very small features or when you are starting afresh, because:-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;starting with fresh context or instance of agent, works for some time &lt;/li&gt;
&lt;li&gt;as you keep on generating code, agents starts drifting from the actual intent that human asked the agent to code for&lt;/li&gt;
&lt;li&gt;small &amp;amp; simple features can be implemented by most of the LLM models like claude, grok etc&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  📍 When things actually break ?
&lt;/h2&gt;

&lt;p&gt;But as soon as someone starts working on larger codebases, complex features or end to end products, Agents struggle a lot because:-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;they start drifting away from human given prompt or intent&lt;/li&gt;
&lt;li&gt;they start coding worse as context window fills up&lt;/li&gt;
&lt;li&gt;they start hallucinations with confidence with no verification loops&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 Because of which, users generate thousands of lines of code and realise nothing works. And then refactoring that codebase is a very tedious task and a lot of iterations comes into the play.&lt;/p&gt;

&lt;p&gt;And this struggle is causing developers a pseudo productivity boost, a lot of iterations and headaches. &lt;/p&gt;

&lt;p&gt;So how should we vibe code then ??&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw1m5sriryxmapd3e5v0k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw1m5sriryxmapd3e5v0k.png" alt="vibe coding meme" width="640" height="640"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;No, above picture doesn't refer to how you should code 🤪&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Coding is still a fundamental problem solving method where structure wins even after so many code generation LLM models. Catching it up using first principles it is:-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;writing a PRD (product/feature requirement doc)&lt;/li&gt;
&lt;li&gt;dividing a PRD into Specs&lt;/li&gt;
&lt;li&gt;making Tech docs and then sub-tasks&lt;/li&gt;
&lt;li&gt;assigning sub tasks to your team so that they can finally code&lt;/li&gt;
&lt;li&gt;verification of the implementations&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And this is where Inner loop solves your problem, which is code generation.&lt;/p&gt;

&lt;p&gt;But have you noticed which part is missing ???&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Yes, exactly the outer loop.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  📍 The Outer Loop
&lt;/h2&gt;

&lt;p&gt;Missing pieces because of which LLMs are still not capable of coding are:- &lt;br&gt;
1️⃣ writing a PRD (product/feature requirement doc)&lt;br&gt;
2️⃣ dividing a PRD into Specs&lt;br&gt;
3️⃣ making Tech docs and then sub-tasks&lt;br&gt;
⚠️ - assigning sub tasks to your team so that they can finally code (using Inner loop or LLMs here)&lt;br&gt;
4️⃣ verification of the implementations&lt;/p&gt;

&lt;p&gt;This is where a new field shines:- Spec Driven Development.&lt;/p&gt;

&lt;p&gt;A lot of products are trying to solve Spec driven development which does generate PRDs, plan really well, verifies each agent's code step and prevent agents drifting away from user' intent.&lt;/p&gt;

&lt;p&gt;Products which are in this space are:- &lt;strong&gt;Traycer, Kiro, Spec-kit&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;But my friends are obsessed with &lt;a href="https://traycer.ai/" rel="noopener noreferrer"&gt;Traycer&lt;/a&gt; because of their features like EPIC mode which is highly intuitive.&lt;/p&gt;

&lt;p&gt;Traycer solved few problems which no one is able to solve yet:-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;capturing human intent from a normal simple prompt&lt;/li&gt;
&lt;li&gt;prevents agents from deviating from the intent&lt;/li&gt;
&lt;li&gt;consume less tokens and prevents context bloating&lt;/li&gt;
&lt;li&gt;verifies your each change so that you dont ship hallucinations with confidence&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Here is a sneak peak how TraycerAI EPIC mode looks like:-&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjjwpzilr7g7iukhyqgij.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjjwpzilr7g7iukhyqgij.png" alt="Traycer EPIC mode" width="800" height="475"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;TraycerAI is my partner nowadays because it acts my senior engineer instead of just an agent. Highlighting few features:-&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It starts with a simple prompt&lt;/li&gt;
&lt;li&gt;Then it interviews you around problem statement, tech stack, edge cases and other high level questions&lt;/li&gt;
&lt;li&gt;Then it generates PRDs, Specs, Tech flow, Wireframes, Sequence and user flow diagrams&lt;/li&gt;
&lt;li&gt;And finally, it breaks the plan into smaller tickets so that you can handover these tickets to any AI agent like claude, grok, cursor IDE etc&lt;/li&gt;
&lt;li&gt;and then it verifies each and every change and prevents your agents  to drift/deviate in false direction&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;📍 Recently we shipped products like:- &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;building your own redis &lt;/li&gt;
&lt;li&gt;building my own whatsapp that supports semantic search across messages&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And many more projects we are building and loving the EPIC mode ❤️&lt;/p&gt;

</description>
      <category>ai</category>
      <category>mcp</category>
      <category>claude</category>
      <category>cursorai</category>
    </item>
    <item>
      <title>Right way to vibe code that actually works</title>
      <dc:creator>SinghDevHub</dc:creator>
      <pubDate>Sun, 11 Jan 2026 13:05:49 +0000</pubDate>
      <link>https://forem.com/singhdevhub/right-way-to-vibe-code-that-actually-works-3oah</link>
      <guid>https://forem.com/singhdevhub/right-way-to-vibe-code-that-actually-works-3oah</guid>
      <description>&lt;h2&gt;
  
  
  📍 Engineering's current situation
&lt;/h2&gt;

&lt;p&gt;You might have heard this already a ton of times that software engineers are gonna get replaced with AI systems, agents and tools.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4cmcdqmvm80wb3y0b4hw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4cmcdqmvm80wb3y0b4hw.png" alt="Software engineers meme" width="469" height="298"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Well, We will not comment too much on this particular thing as no one is clear about this. But few predictions/things already happening in the industry:-&lt;/p&gt;

&lt;p&gt;1️⃣ small teams are getting really really effective/productive&lt;br&gt;
2️⃣ people with agency and intent are getting preferred over people who are very skilled or smart&lt;br&gt;
3️⃣ companies will move even faster than before&lt;br&gt;
4️⃣ softwares building themselves &lt;/p&gt;

&lt;p&gt;😳 Examples:-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://www.claude.com/product/claude-code" rel="noopener noreferrer"&gt;claude code&lt;/a&gt; is being built using claude&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://traycer.ai/" rel="noopener noreferrer"&gt;Traycer&lt;/a&gt; is being built using TraycerAI&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But there is one common thing in every organisation that it is not about:- &lt;br&gt;
❓&lt;strong&gt;should you use AI or not?&lt;/strong&gt; It is about:- &lt;strong&gt;how you should use AI.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;A lot of people don't know if they are doing vibe coding or doing AI Assisted coding. Yep, there is a huge difference in Vibe Coding &amp;amp; AI Assisted coding.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  📍 Usual way of vibe coding
&lt;/h2&gt;

&lt;p&gt;Vibe coding workflow generally looks like this:- &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You have an agentic IDE (cursor, windsurf) or CLI agents like (claude code, gemini cli)&lt;/li&gt;
&lt;li&gt;You put a prompt in the chat window&lt;/li&gt;
&lt;li&gt;Your agent reads the prompt, plans and then start generating code&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This method works on very small features or when you are starting afresh, because:-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;starting with fresh context or instance of agent, works for some time &lt;/li&gt;
&lt;li&gt;as you keep on generating code, agents starts drifting from the actual intent that human asked the agent to code for&lt;/li&gt;
&lt;li&gt;small &amp;amp; simple features can be implemented by most of the LLM models like claude, grok etc&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  📍 When things actually break ?
&lt;/h2&gt;

&lt;p&gt;But as soon as someone starts working on larger codebases, complex features or end to end products, Agents struggle a lot because:-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;they start drifting away from human given prompt or intent&lt;/li&gt;
&lt;li&gt;they start coding worse as context window fills up&lt;/li&gt;
&lt;li&gt;they start hallucinations with confidence with no verification loops&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 Because of which, users generate thousands of lines of code and realise nothing works. And then refactoring that codebase is a very tedious task and a lot of iterations comes into the play.&lt;/p&gt;

&lt;p&gt;And this struggle is causing developers a pseudo productivity boost, a lot of iterations and headaches. &lt;/p&gt;

&lt;p&gt;So how should we vibe code then ??&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw1m5sriryxmapd3e5v0k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw1m5sriryxmapd3e5v0k.png" alt="vibe coding meme" width="640" height="640"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;No, above picture doesn't refer to how you should code 🤪&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Coding is still a fundamental problem solving method where structure wins even after so many code generation LLM models. Catching it up using first principles it is:-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;writing a PRD (product/feature requirement doc)&lt;/li&gt;
&lt;li&gt;dividing a PRD into Specs&lt;/li&gt;
&lt;li&gt;making Tech docs and then sub-tasks&lt;/li&gt;
&lt;li&gt;assigning sub tasks to your team so that they can finally code&lt;/li&gt;
&lt;li&gt;verification of the implementations&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And this is where Inner loop solves your problem, which is code generation.&lt;/p&gt;

&lt;p&gt;But have you noticed which part is missing ???&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Yes, exactly the outer loop.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  📍 The Outer Loop
&lt;/h2&gt;

&lt;p&gt;Missing pieces because of which LLMs are still not capable of coding are:- &lt;br&gt;
1️⃣ writing a PRD (product/feature requirement doc)&lt;br&gt;
2️⃣ dividing a PRD into Specs&lt;br&gt;
3️⃣ making Tech docs and then sub-tasks&lt;br&gt;
⚠️ - assigning sub tasks to your team so that they can finally code (using Inner loop or LLMs here)&lt;br&gt;
4️⃣ verification of the implementations&lt;/p&gt;

&lt;p&gt;This is where a new field shines:- Spec Driven Development.&lt;/p&gt;

&lt;p&gt;A lot of products are trying to solve Spec driven development which does generate PRDs, plan really well, verifies each agent's code step and prevent agents drifting away from user' intent.&lt;/p&gt;

&lt;p&gt;Products which are in this space are:- &lt;strong&gt;Traycer, Kiro, Spec-kit&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;But my friends are obsessed with &lt;a href="https://traycer.ai/" rel="noopener noreferrer"&gt;Traycer&lt;/a&gt; because of their features like EPIC mode which is highly intuitive.&lt;/p&gt;

&lt;p&gt;Traycer solved few problems which no one is able to solve yet:-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;capturing human intent from a normal simple prompt&lt;/li&gt;
&lt;li&gt;prevents agents from deviating from the intent&lt;/li&gt;
&lt;li&gt;consume less tokens and prevents context bloating&lt;/li&gt;
&lt;li&gt;verifies your each change so that you dont ship hallucinations with confidence&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Here is a sneak peak how TraycerAI EPIC mode looks like:-&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjjwpzilr7g7iukhyqgij.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjjwpzilr7g7iukhyqgij.png" alt="Traycer EPIC mode" width="800" height="475"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;TraycerAI is my partner nowadays because it acts my senior engineer instead of just an agent. Highlighting few features:-&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It starts with a simple prompt&lt;/li&gt;
&lt;li&gt;Then it interviews you around problem statement, tech stack, edge cases and other high level questions&lt;/li&gt;
&lt;li&gt;Then it generates PRDs, Specs, Tech flow, Wireframes, Sequence and user flow diagrams&lt;/li&gt;
&lt;li&gt;And finally, it breaks the plan into smaller tickets so that you can handover these tickets to any AI agent like claude, grok, cursor IDE etc&lt;/li&gt;
&lt;li&gt;and then it verifies each and every change and prevents your agents  to drift/deviate in false direction&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;📍 Recently we shipped products like:- &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;building your own redis &lt;/li&gt;
&lt;li&gt;building my own whatsapp that supports semantic search across messages&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And many more projects we are building and loving the EPIC mode ❤️&lt;/p&gt;

</description>
      <category>softwaredevelopment</category>
      <category>programming</category>
      <category>ai</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Everyone is a Product engineer now</title>
      <dc:creator>SinghDevHub</dc:creator>
      <pubDate>Wed, 07 Jan 2026 08:50:54 +0000</pubDate>
      <link>https://forem.com/singhdevhub/everyone-is-a-product-engineer-now-5g67</link>
      <guid>https://forem.com/singhdevhub/everyone-is-a-product-engineer-now-5g67</guid>
      <description>&lt;p&gt;😭 Everything is AI generated now a days, but this blog is not because I  am sharing my observations about what is happening in the industry.&lt;/p&gt;

&lt;p&gt;😎 With each new day passing we are seeing new and new AI tools and people are trying every new tool in the hope that this tool will finally fix their life.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fueyplu9iece2wqtjcdns.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fueyplu9iece2wqtjcdns.png" alt="rage on AI" width="250" height="231"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;😭 But after they try they come to know that they still can't earn without work. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqqwe28dgvre0yq0blq43.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqqwe28dgvre0yq0blq43.png" alt="more rage on AI" width="291" height="269"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;🤪 Personally, people after trying GPT, Claude, Grok they know exactly which agent is for what task. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;someone is good at coding&lt;/li&gt;
&lt;li&gt;someone at rage baiting on X&lt;/li&gt;
&lt;li&gt;someone at reasoning&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 But all of them are missing to capture user's intent. That's when i came across Spec Drive Development (SDD) which made me a product engineer from a software developer.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fliuz1foqx7jc4ikq9u2c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fliuz1foqx7jc4ikq9u2c.png" alt="happy meme" width="150" height="150"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Now, i hardly write any code, but always explaining my intent to these AI tools. But it is working honestly.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;I am using &lt;a href="http://traycer.ai/" rel="noopener noreferrer"&gt;http://traycer.ai/&lt;/a&gt; from past few months and now i do write  prompt it does:-&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;generate specs&lt;/li&gt;
&lt;li&gt;generate phases&lt;/li&gt;
&lt;li&gt;verifies each step&lt;/li&gt;
&lt;li&gt;gives me wireframes to visualise before writing any code&lt;/li&gt;
&lt;li&gt;biggest part is i am spending lot less to get far good results on large codebases as compared to using one model on the entire code like claude or grok&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F99ivvceit4bewpjyxr89.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F99ivvceit4bewpjyxr89.png" alt="Spec driven development" width="800" height="475"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Traycer is an orchestration of agents, sub-agents that does my work. The only thing is i have to think about my intent and requirements.&lt;/p&gt;

&lt;p&gt;🔥 I am using its YOLO mode, sometimes phase breakdown and wireframes are cherry on the top.&lt;/p&gt;

&lt;p&gt;😏 Now everytime i have to think what to fix, build and ship. I have to think what and why instead of how. I mean i used to think how when using claude code, cursor because they drift too much when your code grows.&lt;/p&gt;

&lt;p&gt;For example:-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;claude code goes to walk after compaction&lt;/li&gt;
&lt;li&gt;grok is still trying to do rage bait on X&lt;/li&gt;
&lt;li&gt;GPT generates too much code that it doesn't understand after a point of time&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These folks are good when handled carefully by a parent like Traycer. &lt;/p&gt;

&lt;p&gt;See traycer in action:- &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcpjppv38ww0bw396kr6c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcpjppv38ww0bw396kr6c.png" alt="spec driven development traycer ai" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I can clearly see the user flows through sequence diagrams, flow charts&lt;/li&gt;
&lt;li&gt;I can see wireframes/UI before even code generation has started&lt;/li&gt;
&lt;li&gt;My intent is separated into smaller tickets that i can edit&lt;/li&gt;
&lt;li&gt;Then i can choose my children to do the work (claude, grok, gpt) and verify at the completion of each phase&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So far, I am loving this product. and paying too less i think sometimes.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flyeb4qyu4lrvug4dxzlh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flyeb4qyu4lrvug4dxzlh.png" alt="meme" width="500" height="333"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>programming</category>
      <category>ai</category>
      <category>genai</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Basics of Stack, Heap, memory management, VM &amp; JVM</title>
      <dc:creator>SinghDevHub</dc:creator>
      <pubDate>Mon, 05 May 2025 16:08:45 +0000</pubDate>
      <link>https://forem.com/singhdevhub/basics-of-stack-heap-memory-management-vm-jvm-1png</link>
      <guid>https://forem.com/singhdevhub/basics-of-stack-heap-memory-management-vm-jvm-1png</guid>
      <description>&lt;p&gt;🚀 Have you ever wondered when you open up any program like VLC Player (music player) &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What happens? &lt;/li&gt;
&lt;li&gt;How it comes on your PC's screen?&lt;/li&gt;
&lt;li&gt;How it uses your resources and runs on your machine?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Well, there are many workers who are responsible of running a process.&lt;/p&gt;

&lt;p&gt;📔 But wait before starting anything, what is a program and process ?&lt;/p&gt;

&lt;h3&gt;
  
  
  📍 Program
&lt;/h3&gt;

&lt;p&gt;Whenever someone writes code lines in a file (let say java) which we call a set of instructions&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Test&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
         &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello world"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Above code written is nothing but we are trying to tell the computer that load the Test class, call the main() method and print "Hello world" on our screen. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Now this set of instructions File is called a program (a fancy word) and when we run (execute) our program&lt;/li&gt;
&lt;li&gt;it takes some resources like some share of CPU, memory and storage and program runs. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Running program is called a process, yes another fancy word&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;😢 Note:- Well, Interesting thing is our computer doesn't understand ENGLISH at all. It only understand bits which have only two values like 1 and 0.&lt;/p&gt;

&lt;p&gt;😎 That is where your compiler comes into the picture.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1psce1ezbieoft2y22va.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1psce1ezbieoft2y22va.png" alt="Compiler" width="640" height="488"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, what the heck is compiler ?&lt;/p&gt;

&lt;h3&gt;
  
  
  📍 Compiler
&lt;/h3&gt;

&lt;p&gt;Again, compiler is an another fancy word but a translator and inspector. Compiler is a program written by compiler programmers which convert english written files (code files like java, js, c++ etc files) to assembly code.&lt;/p&gt;

&lt;p&gt;😅 Assembly code is another type of format which is more closer to the machine (our PC), for example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;section .text
global main_entry_point

main_entry_point:
    sub rsp, 40
    mov rax, [address_of_System_out_object_pointer]
    lea rdx, [address_of_hello_world_string_object]
    mov rdi, rax
    mov rsi, rdx
    call address_of_native_println_implementation
    add rsp, 40
    ret
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Above code is assembly code which is hard to write for software engineers but easy for assembler to understand and convert to machine code. And machine code is what machines understands.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;01010101                  ; push rbp
01001000 10001001 11100101    ; mov rbp, rsp
01001000 10000011 11101100 00101000 ; sub rsp, 0x28
01001000 10111000 &amp;lt;64 bits of address_ptr&amp;gt; ; mov rax, address_of_System_out_object_pointer
01001000 10001001 01000101 11111000 ; mov [rbp-0x8], rax
01001000 10111010 &amp;lt;64 bits of address_str&amp;gt; ; mov rdx, address_of_hello_world_string_object
01001000 10001001 01010101 11110000 ; mov [rbp-0x10], rdx
01001000 10001011 01000101 11111000 ; mov rax, [rbp-0x8]
01001000 10001001 11100111    ; mov rdi, rax
01001000 10001011 01010101 11110000 ; mov rdx, [rbp-0x10]
01001000 10001001 11010110    ; mov rsi, rdx
11101000 &amp;lt;32 bits of offset&amp;gt;  ; call address_of_native_println_implementation
11001001                  ; leave
11000011                  ; ret
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;📍 Process of conversion so far looks like&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcfbc3xtugns7scagkydm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcfbc3xtugns7scagkydm.png" alt="Running process" width="800" height="281"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Above picture shows the execution of C++ program, where C++ file is compiled by g++ (compiler of C++) which is a program responsible of converting C++ code to assembly code.&lt;/li&gt;
&lt;li&gt;Then assembler is responsible of converting assembly code to machine code which finally runs on our machine.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;✅ But here is a very important point to notice, which is C++ is not a language like JAVA which is write once run anywhere (WORA).&lt;/p&gt;

&lt;p&gt;Because when C++ code is converted, it is directly converted to machine code with no intervention. Machine code generation is generated according to the architecture &amp;amp; operating system that our machine has.&lt;/p&gt;

&lt;p&gt;💯 For ex: we have x86, arm based processors which has different nature of understanding code at machine level. Hence you can't run same code running on Apple mac M1 (arm based processor) on x86 based processor (intel i9).&lt;/p&gt;

&lt;p&gt;That's why, in JAVA there is one intermediator step introduced which is virtual machine &amp;amp; byte code.&lt;/p&gt;

&lt;h3&gt;
  
  
  📍 Virtual machine &amp;amp; Byte Code
&lt;/h3&gt;

&lt;p&gt;Virtual machine as name suggests is a machine which is not real but mimic our original machine. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F916cio4u7iraydo56kdj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F916cio4u7iraydo56kdj.png" alt="Virtual machine" width="800" height="639"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;⭐ Virtual machine is a software written which takes few %age of CPU, memory and storage and runs as a mini computer inside your main computer. Hence, we can run multiple virtual machines on a single host (main) machine.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pros of VM:-&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;software developer using VM has a lot of control because we can tweak stuff at software level which is hard to do at hardware level&lt;/li&gt;
&lt;li&gt;VM can be made secure and can act as intermediate step before your program goes to hardware and execute&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;❤️ Due to above reasons a Virtual machine specific to Java was introduced which is Java virtual machine (JVM)&lt;/p&gt;

&lt;p&gt;People wrote JVM for x86, arm architectures. People wrote for windows, linux and other Operating systems. Which is why due to the availibility of JVM on all machines JAVA can run on any machine.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;📍 This is how JAVA program runs on our machines:-&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbkrf7j87am83qxf1zqia.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbkrf7j87am83qxf1zqia.png" alt="Java program execution" width="800" height="280"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;As you can see that intermediate step here is JVM which is a program written by developers which takes intermediate code file (byte code) and JVM understands byte code and is responsible of converting bytecode to machine code and then it runs.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;📍 Here you can see:-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;compiler doesn't care about what underlying OS or architecture (x86, arm) is&lt;/li&gt;
&lt;li&gt;It just knows how to convert and it does&lt;/li&gt;
&lt;li&gt;Rest work is for JVM which is built according to the underlying OS and architecture&lt;/li&gt;
&lt;li&gt;Hence software developers (who focuses on Main.java) they don't care about what their OS and architecture is, JVM will handle everything.&lt;/li&gt;
&lt;li&gt;On the other hand, if you would have noticed C++ code differs on windows and linux. libraries also do differ with x86 and arm architecutres&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  📍 Let's see the conversion of code in Java
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Java file that we write
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// File: Test.java&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Test&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello world"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;File that compiler translates to (Don't worry about the details, we dont have to fear this syntax)
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Compiled&lt;/span&gt; &lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="s"&gt;"Test.java"&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Test&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Default constructor added by javac&lt;/span&gt;
  &lt;span class="nc"&gt;Test&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="nl"&gt;Code:&lt;/span&gt;
       &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;aload_0&lt;/span&gt;       &lt;span class="c1"&gt;// Load 'this' onto the operand stack&lt;/span&gt;
       &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;invokespecial&lt;/span&gt; &lt;span class="err"&gt;#&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="c1"&gt;// Method java/lang/Object."&amp;lt;init&amp;gt;":()V (Call superclass constructor)&lt;/span&gt;
       &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;        &lt;span class="c1"&gt;// Return from constructor&lt;/span&gt;

  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;java&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;lang&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]);&lt;/span&gt;
    &lt;span class="nl"&gt;Code:&lt;/span&gt;
       &lt;span class="c1"&gt;// --- The interesting part ---&lt;/span&gt;
       &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;getstatic&lt;/span&gt;     &lt;span class="err"&gt;#&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="c1"&gt;// Field java/lang/System.out:Ljava/io/PrintStream;&lt;/span&gt;
                            &lt;span class="c1"&gt;// Pushes the static field System.out (a PrintStream object) onto the stack&lt;/span&gt;

       &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ldc&lt;/span&gt;           &lt;span class="err"&gt;#&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="c1"&gt;// String "Hello world"&lt;/span&gt;
                            &lt;span class="c1"&gt;// Pushes the String "Hello world" from the constant pool onto the stack&lt;/span&gt;

       &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;invokevirtual&lt;/span&gt; &lt;span class="err"&gt;#&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="c1"&gt;// Method java/io/PrintStream.println:(Ljava/lang/String;)V&lt;/span&gt;
                            &lt;span class="c1"&gt;// Pops the string and PrintStream object, calls the println method&lt;/span&gt;

       &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;        &lt;span class="c1"&gt;// Return void from main&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;JVM who converts bytecode to machine code and runs on the machine&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  📍 When Program runs
&lt;/h3&gt;

&lt;p&gt;When program runs (machine code) on your machine. It takes memory, cpu and storage while running. while running, process treats memory as stack and heap which acts as static and dynamic memory respectively.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fljz9vjbtyunwe21uqv5n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fljz9vjbtyunwe21uqv5n.png" alt="Stack, heap division" width="800" height="658"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Stack
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.*&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Main&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;printHello&lt;/span&gt;&lt;span class="o"&gt;(){&lt;/span&gt;
      &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello world"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
      &lt;span class="nc"&gt;Car&lt;/span&gt; &lt;span class="n"&gt;bmw&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Car&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;printHello&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;stack memory holds local variables, function parameters, pointer to  dynamic memory if any list, object (bmw in above example) is created&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnz54uzjhbxun21mdy1r8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnz54uzjhbxun21mdy1r8.png" alt="Stack heap call" width="800" height="891"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Above picture states that first we call main() then we push printHello() to stack and then we push println() which prints the output. Then we create an object on heap because objects are created on Heap memory and pointer to the object memory location is stored in the stack itself.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:- Stack flushes automatically as soon as function completes its execution while Heap does not flushes automatically&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To flush the heap, there is another program written which runs and flushes the memory on heap allocated in our java program&lt;/li&gt;
&lt;li&gt;that program is called garbage collection&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  CPU cycles, memory representation:- binary numbers and Hexadecimal base and Hertz
&lt;/h3&gt;

&lt;p&gt;When we look at the clock we measure time using seconds, minutes or hours. Similarly, the unit of work for our CPU is called its CPU cycle.&lt;/p&gt;

&lt;p&gt;Every CPU has its clock speed or frequency. for example AMD ryzen 7 9800x3D has 4.7GHz (1Giga = 1 billion) as its base frequency. Which means in one seconds it can run 4.7 billion instructions.&lt;/p&gt;

&lt;p&gt;Instructions are nothing but our code lines that we write in simple words.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Question:- Can you think how much approximate work CPU has to do to perform 2+4 calculation ?&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;📍 Next comes memory representation&lt;/p&gt;

&lt;p&gt;Memory is represented in hexadecimal base. What is hexadecimal base ??&lt;/p&gt;

&lt;p&gt;in decimal space we have numbers as 0,1,2,3,4,5,6,7,8,9&lt;br&gt;
in binary space we have numbers as 0,1&lt;br&gt;
in hexadecimal space we have 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F&lt;/p&gt;

&lt;p&gt;Conversion of binary, hexa, deci base to decimal numbers that we humans understand:-&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdrw7hnqm7xu2xfv6uaap.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdrw7hnqm7xu2xfv6uaap.png" alt="decimal" width="790" height="408"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flzleodkukt4nif9y0d5b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flzleodkukt4nif9y0d5b.png" alt="binary" width="726" height="412"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We represent memory in hexadecimal base (16 base, because we have 16 numbers instead of 10 numbers that we have in decimal base)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;0x45FBC memory means:-
&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0gb7bojz0sxjo7swqjg1.png" alt="hexadecimal" width="800" height="866"&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>java</category>
      <category>jvm</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Writing USB driver for Linux in C</title>
      <dc:creator>SinghDevHub</dc:creator>
      <pubDate>Wed, 04 Dec 2024 10:42:20 +0000</pubDate>
      <link>https://forem.com/singhdevhub/writing-usb-driver-for-linux-in-c-5g64</link>
      <guid>https://forem.com/singhdevhub/writing-usb-driver-for-linux-in-c-5g64</guid>
      <description>&lt;h2&gt;
  
  
  How USB got detected when we plug it in
&lt;/h2&gt;

&lt;p&gt;I am running Ubuntu in VM:- &lt;code&gt;https://www.youtube.com/watch?v=O19mv1pe76M&lt;/code&gt; (How to setup on Mac m1)&lt;/p&gt;

&lt;p&gt;When we plug in our USB, ever thought what happens. Well, whenever we connect devices like USB in our PCI slot (Peripheral component interface) it gets detected in kernel space by the device controller which doesn't depend on the driver existence.&lt;/p&gt;

&lt;p&gt;Although, default drivers exist in most of the linux distros. After plugging the device, hardware controller driver do detect the USB and it translates the low level information for the higher layers adhering to the USB protocol.&lt;/p&gt;

&lt;p&gt;This information is detected and passed towards generic USB core layer(usbcore) in kernel layer which completes the device detection.&lt;/p&gt;

&lt;p&gt;These controller drivers are typically coming from these categories:-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;EHCI (Enhanced Host Controller Interface) for USB 2.0.&lt;/li&gt;
&lt;li&gt;XHCI (eXtensible Host Controller Interface) for USB 3.0 and later.&lt;/li&gt;
&lt;li&gt;OHCI (Open Host Controller Interface) for older USB 1.1 systems.&lt;/li&gt;
&lt;li&gt;UHCI (Universal Host Controller Interface) for certain USB 1.1 implementations.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Let's go in some more depth, how information is detected and retrieved
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg1ki1fmbs4hd01low6xi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg1ki1fmbs4hd01low6xi.png" alt="USB Driver" width="800" height="466"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Install some of the things if not installed already&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;apt get make
&lt;span class="nb"&gt;sudo &lt;/span&gt;apt-get &lt;span class="nb"&gt;install &lt;/span&gt;linux-headers-&lt;span class="si"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;uname&lt;/span&gt; &lt;span class="nt"&gt;-r&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;sudo &lt;/span&gt;apt &lt;span class="nb"&gt;install &lt;/span&gt;build-essential
&lt;span class="nb"&gt;sudo &lt;/span&gt;apt &lt;span class="nb"&gt;install &lt;/span&gt;gcc-13
&lt;span class="nb"&gt;sudo &lt;/span&gt;apt &lt;span class="nb"&gt;install &lt;/span&gt;g++-13
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Device Firmware Initialization: The PCI device's firmware writes its configuration data (like Vendor ID, BARs) into its PCI configuration space.&lt;/li&gt;
&lt;li&gt;PCI Controller Enumeration: The PCI controller scans devices and collects this configuration data.&lt;/li&gt;
&lt;li&gt;Kernel PCI Subsystem: The Linux kernel reads the configuration space data during boot or when a hot-plugged device is detected.&lt;/li&gt;
&lt;li&gt;Device Drivers: The kernel matches the Vendor ID and Device ID with the appropriate driver and initializes it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Note:- every driver, code, documentation and implementation can be referred in linux source code.&lt;/p&gt;

&lt;p&gt;follow this to build linux system in local system for the reference:-&lt;br&gt;
&lt;code&gt;https://phoenixnap.com/kb/build-linux-kernel&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We will be writing USB driver for our Sandisk Pendrive which will talk to the USB controller using the interface given in the linux.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To register and de-register the usb we have usbcore apis which are present in &lt;code&gt;linux/usb.h&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;usb_register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;usb_driver&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;driver&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;usb_deregister&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;usb_driver&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="c1"&gt;// #include &amp;lt;stdio.h&amp;gt; Cant use them as they operate in user mode, in kernel mode we cant use them&lt;/span&gt;
&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;linux/usb.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;linux/module.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;usb_device&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;device&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;usb_device_id&lt;/span&gt; &lt;span class="n"&gt;skel_table&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;USB_DEVICE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mh"&gt;0x0781&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x5567&lt;/span&gt;&lt;span class="p"&gt;)},&lt;/span&gt;
    &lt;span class="p"&gt;{}};&lt;/span&gt;
&lt;span class="n"&gt;MODULE_DEVICE_TABLE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;usb&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;skel_table&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;skel_probe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;usb_interface&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;interface&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;usb_device_id&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;printk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;KERN_INFO&lt;/span&gt; &lt;span class="s"&gt;"Pen drive probed&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;skel_disconnect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;usb_interface&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;interface&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;usb_put_dev&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;device&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;printk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;KERN_INFO&lt;/span&gt; &lt;span class="s"&gt;"Pen drive removed&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;usb_driver&lt;/span&gt; &lt;span class="n"&gt;skel_driver&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"usb_driver"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;probe&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;skel_probe&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;disconnect&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;skel_disconnect&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id_table&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;skel_table&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;supports_autosuspend&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;__init&lt;/span&gt; &lt;span class="nf"&gt;usb_skel_init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;usb_register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;skel_driver&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;pr_err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"usb registeration failed with %s&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;skel_driver&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;printk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;KERN_INFO&lt;/span&gt; &lt;span class="s"&gt;"USB initialised&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;module_init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;usb_skel_init&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;__exit&lt;/span&gt; &lt;span class="nf"&gt;usb_skel_exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;usb_deregister&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;skel_driver&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;module_exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;usb_skel_exit&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="n"&gt;MODULE_LICENSE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"GPL"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;MODULE_AUTHOR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"lovepreet"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;MODULE_DESCRIPTION&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"USB pendrive registration driver"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;If getting&lt;br&gt;
insmod: ERROR: could not insert module usb_driver.ko: Key was rejected by service&lt;/p&gt;

&lt;p&gt;follow:- &lt;code&gt;https://askubuntu.com/questions/762254/why-do-i-get-required-key-not-available-when-install-3rd-party-kernel-modules&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Understanding more about usb structs (defined in linux/usb.h though) in sequence :-
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;usb_device&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;usb_device_descriptor&lt;/span&gt; &lt;span class="n"&gt;descriptor&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;usb_host_config&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;actconfig&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;usb_host_config&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;usb_config_descriptor&lt;/span&gt; &lt;span class="n"&gt;desc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;usb_interface&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;interface&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;USB_MAXINTERFACES&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;usb_interface&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;usb_host_interface&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;altsetting&lt;/span&gt; &lt;span class="cm"&gt;/* array */&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;cur_altsetting&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;usb_host_interface&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;usb_interface_descriptor&lt;/span&gt; &lt;span class="n"&gt;desc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;usb_host_endpoint&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;endpoint&lt;/span&gt; &lt;span class="cm"&gt;/* array */&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;usb_host_endpoint&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;usb_endpoint_descriptor&lt;/span&gt;  &lt;span class="n"&gt;desc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Now we can use these hierarchical structs to print some more information about the USB&lt;/p&gt;

&lt;p&gt;Complete code for the driver is:-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="c1"&gt;// #include &amp;lt;stdio.h&amp;gt; Cant use them as they operate in user mode, in kernel mode we cant use them&lt;/span&gt;
&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;linux/usb.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;linux/module.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;usb_device&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;device&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;usb_device_id&lt;/span&gt; &lt;span class="n"&gt;skel_table&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;USB_DEVICE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mh"&gt;0x0781&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mh"&gt;0x5567&lt;/span&gt;&lt;span class="p"&gt;)},&lt;/span&gt;
    &lt;span class="p"&gt;{}};&lt;/span&gt;
&lt;span class="n"&gt;MODULE_DEVICE_TABLE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;usb&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;skel_table&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;skel_probe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;usb_interface&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;interface&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;usb_device_id&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;usb_host_interface&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;iface_desc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;usb_endpoint_descriptor&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;endpoint&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;iface_desc&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;interface&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;cur_altsetting&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;printk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;KERN_INFO&lt;/span&gt; &lt;span class="s"&gt;"Pen i/f %d now probed: (%04X:%04X)&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
           &lt;span class="n"&gt;iface_desc&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;desc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;bInterfaceNumber&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
           &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;idVendor&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;idProduct&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;printk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;KERN_INFO&lt;/span&gt; &lt;span class="s"&gt;"ID-&amp;gt;bNumEndpoints: %02X&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;iface_desc&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;desc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;bNumEndpoints&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;printk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;KERN_INFO&lt;/span&gt; &lt;span class="s"&gt;"ID-&amp;gt;bInterfaceClass: %02X&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;iface_desc&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;desc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;bInterfaceClass&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;iface_desc&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;desc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;bNumEndpoints&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="n"&gt;endpoint&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;iface_desc&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;endpoint&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;desc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;printk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;KERN_INFO&lt;/span&gt; &lt;span class="s"&gt;"ED[%d]-&amp;gt;bEndpointAddress: 0x%02X&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;endpoint&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;bEndpointAddress&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;printk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;KERN_INFO&lt;/span&gt; &lt;span class="s"&gt;"ED[%d]-&amp;gt;bmAttributes: 0x%02X&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;endpoint&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;bmAttributes&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;printk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;KERN_INFO&lt;/span&gt; &lt;span class="s"&gt;"ED[%d]-&amp;gt;wMaxPacketSize: 0x%04X (%d)&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;endpoint&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;wMaxPacketSize&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="n"&gt;endpoint&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;wMaxPacketSize&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;device&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;interface_to_usbdev&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;interface&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;skel_disconnect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;usb_interface&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;interface&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;usb_put_dev&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;device&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;printk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;KERN_INFO&lt;/span&gt; &lt;span class="s"&gt;"Pen drive removed&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;usb_driver&lt;/span&gt; &lt;span class="n"&gt;skel_driver&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"usb_driver"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;probe&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;skel_probe&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;disconnect&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;skel_disconnect&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id_table&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;skel_table&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;supports_autosuspend&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;__init&lt;/span&gt; &lt;span class="nf"&gt;usb_skel_init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;usb_register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;skel_driver&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;pr_err&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"usb registeration failed with %s&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;skel_driver&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;printk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;KERN_INFO&lt;/span&gt; &lt;span class="s"&gt;"USB initialised&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;module_init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;usb_skel_init&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;__exit&lt;/span&gt; &lt;span class="nf"&gt;usb_skel_exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;usb_deregister&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;skel_driver&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;module_exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;usb_skel_exit&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="n"&gt;MODULE_LICENSE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"GPL"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;MODULE_AUTHOR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"lovepreet"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;MODULE_DESCRIPTION&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"USB pendrive registration driver"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Manually binding the USB with own driver instead of usb-storage driver
&lt;/h2&gt;

&lt;p&gt;If you are not able to see the usb being attached with usb_driver and getting attached with usb-storage&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;lsusb &lt;span class="nt"&gt;-t&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then follow these steps:-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;plug pen drive&lt;/li&gt;
&lt;li&gt;use &lt;code&gt;sudo dmesg&lt;/code&gt; and see what is the usb sequence like
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;[&lt;/span&gt;   68.923193] systemd-journald[338]: Time jumped backwards, rotating.
&lt;span class="o"&gt;[&lt;/span&gt;  113.120352] usb 1-5: new high-speed USB device number 3 using xhci_hcd
&lt;span class="o"&gt;[&lt;/span&gt;  113.246983] usb 1-5: New USB device found, &lt;span class="nv"&gt;idVendor&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;0781, &lt;span class="nv"&gt;idProduct&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;5567, &lt;span class="nv"&gt;bcdDevice&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; 1.00
&lt;span class="o"&gt;[&lt;/span&gt;  113.246989] usb 1-5: New USB device strings: &lt;span class="nv"&gt;Mfr&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;1, &lt;span class="nv"&gt;Product&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;2, &lt;span class="nv"&gt;SerialNumber&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;3
&lt;span class="o"&gt;[&lt;/span&gt;  113.246990] usb 1-5: Product: Cruzer Blade
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we can see that it is usb 1-5, so we have to use this and have to unbind usb-storage&lt;/p&gt;

&lt;p&gt;run these commands&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;sudo su&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;sudo echo -n "1-5" &amp;gt; /sys/bus/usb/drivers/usb/unbind&lt;/code&gt; (eject the usb)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;sudo echo -n "1-5" &amp;gt; /sys/bus/usb/drivers/usb/bind&lt;/code&gt; (connect/binds again)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;echo -n "1-5" &amp;gt; /sys/bus/usb/drivers/usb-storage/unbind&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Now restart the OS&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, try&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;sudo insmod usb_driver.ko&lt;/li&gt;
&lt;li&gt;plug in your pen drive and check &lt;code&gt;sudo dmesg&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;You will be able to see the USB logs being probed and all&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Interesting Fact:- Now USB drive will not show in the files to transfer data because our custom driver hasn't implemented data read and write yet.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs9ghgqtr1q7x8u7dgfzs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs9ghgqtr1q7x8u7dgfzs.png" alt="USB driver testing" width="800" height="317"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;resources:-&lt;br&gt;
&lt;code&gt;https://sysplay.github.io/books/LinuxDrivers/book/Content/Part11.html&lt;/code&gt;&lt;br&gt;
&lt;code&gt;https://docs.kernel.org/driver-api/usb/writing_usb_driver.html&lt;/code&gt;&lt;/p&gt;

</description>
      <category>programming</category>
      <category>lowcode</category>
      <category>c</category>
      <category>linux</category>
    </item>
    <item>
      <title>Implementing Parallel Merge Sort: 25sec vs 1.5sec</title>
      <dc:creator>SinghDevHub</dc:creator>
      <pubDate>Wed, 02 Oct 2024 08:41:55 +0000</pubDate>
      <link>https://forem.com/singhdevhub/implementing-parallel-merge-sort-25sec-vs-15sec-3b8k</link>
      <guid>https://forem.com/singhdevhub/implementing-parallel-merge-sort-25sec-vs-15sec-3b8k</guid>
      <description>&lt;p&gt;🥵 When i was in my college days, one constant thing that I didn't get why the heck are we learning CS subjects like operating systems, computer networks and data structures and algorithms.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4zrtwb4hx02lgde06yn8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4zrtwb4hx02lgde06yn8.png" alt="Computer science memes" width="448" height="248"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;🔥 Yes, exactly I was like this only till i discovered how to dig deeper in CS subjects and how I can use problem solving with my cs subjects.&lt;/p&gt;

&lt;p&gt;⚡️ Last weekend, I was getting bored hence I was thinking constantly about what I should implement. So, i went back to my college days and thought of tweaking some basic sorting algorithm.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;AND, Yes i implemented a Multithreaded Merge sort algorithm which took 1.5 sec on my mac m1 pro in comparison to 25 sec taken by a normal Merge sort algorithm.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you prefer videos, Click on this one where You can go through the implementation details of the algorithm with theory:- &lt;br&gt;
&lt;strong&gt;&lt;u&gt;&lt;a href="https://www.youtube.com/watch?v=t43U7UuVAHQ&amp;amp;t=247s" rel="noopener noreferrer"&gt;https://www.youtube.com/watch?v=t43U7UuVAHQ&amp;amp;t=247s&lt;/a&gt;&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  🫡 Merge Sort Theory (Recursion)
&lt;/h2&gt;

&lt;p&gt;The fastest sorting algorithm has O(nlogn) time complexity and the there are two cool algorithms in sorting space: Merge sort &amp;amp; Quick Sort. &lt;/p&gt;

&lt;p&gt;Because both of em uses recursion which is a super cool thing in Data structures and Algorithms subject (We will combine recursion with multithreading further in parallel merge sort)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F413vf47aekd7ylzp2ma8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F413vf47aekd7ylzp2ma8.png" alt="Recursion meme" width="800" height="695"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Merge sort relies on recursion where we divide array/list into two parts and continue to do so, until we get single element array (Base case of recursion). &lt;/li&gt;
&lt;li&gt;Now, Single element array is always a sorted array and this is the base case where we assume that both left and right arrays are sorted &lt;/li&gt;
&lt;li&gt;then we try to combine them both in a resulting array and return to the upper step of our recursion tree&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnqbvnwqhwntr2im9edgf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnqbvnwqhwntr2im9edgf.png" alt="merge sort" width="726" height="471"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Now, you can notice one thing which is whenever we are dividing the array in two parts both of them are being processed in an independent manner, right?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Code of Merge sort goes like:-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;"mergeSort.hpp"&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;&lt;span class="n"&gt;MergeSort&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;MergeSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;MergeSort&lt;/span&gt;&lt;span class="o"&gt;::~&lt;/span&gt;&lt;span class="n"&gt;MergeSort&lt;/span&gt;&lt;span class="p"&gt;(){}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;MergeSort&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;recursiveSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;=&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="n"&gt;recursiveSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;recursiveSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;=&lt;/span&gt;&lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;=&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]){&lt;/span&gt;
            &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;push_back&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
            &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;push_back&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
            &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;=&lt;/span&gt;&lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;push_back&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
        &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;while&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;=&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;push_back&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
        &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;MergeSort&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="n"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;recursiveSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;🚀 You can go through the code and it will be easy to gothrough it if you can dry run a case&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  📍 Parallel merge sort (recursion + multithreading)
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Now, there is a catch, why can't we use our 8 cores machine parallel power. instead of waiting for left part of the array to get sorted we can hand over the right array to different thread right.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Confused?? dont worry let's look at it:-&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6jzodzkw2h2it42985zy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6jzodzkw2h2it42985zy.png" alt="Parallel merge sort" width="800" height="502"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Code of the same is:-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;"parallelMergeSort.hpp"&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;thread&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;iostream&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="n"&gt;ParallelMergeSort&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ParallelMergeSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
    &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;ParallelMergeSort&lt;/span&gt;&lt;span class="o"&gt;::~&lt;/span&gt;&lt;span class="n"&gt;ParallelMergeSort&lt;/span&gt;&lt;span class="p"&gt;(){}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;ParallelMergeSort&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;recursiveSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kr"&gt;thread&lt;/span&gt; &lt;span class="nf"&gt;thread_1&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;]{&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;recursiveSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;);});&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kr"&gt;thread&lt;/span&gt; &lt;span class="nf"&gt;thread_2&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;]{&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;recursiveSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;);});&lt;/span&gt;
    &lt;span class="n"&gt;thread_1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;join&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;thread_2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;join&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;


    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;push_back&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
            &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;push_back&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
            &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;push_back&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
        &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;push_back&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
        &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;ParallelMergeSort&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
   &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;==&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kr"&gt;thread&lt;/span&gt; &lt;span class="nf"&gt;thread_1&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;recursiveSort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);});&lt;/span&gt;
   &lt;span class="n"&gt;thread_1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;join&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can find the complete code at:- &lt;a href="https://github.com/singhdevhub-lovepreet/parallelMergeSort" rel="noopener noreferrer"&gt;https://github.com/singhdevhub-lovepreet/parallelMergeSort&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:- We have also used threshold of threads here, because depending on your machine our capacity of creating new threads might get exhaust. Hence, we have to make sure when you reach a low size array, sort that in normal manner, there is no need to spawn more threads&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now, you can look that we are delegating our work to different threads and waiting for them to get finish their work. Catch here is that because we dont have any shared resource where two threads can modify stuff at the same time.&lt;/p&gt;

&lt;p&gt;Both left and right modifications in threads are independent of each other and hence we dont have to worry about synchronization premitives/acquiring a lock here.&lt;/p&gt;

&lt;p&gt;The difference in sorting 10^7 elements is:- &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhv4aoot4c4rc3nswj8f7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhv4aoot4c4rc3nswj8f7.png" alt="Sorting benchmarking" width="800" height="121"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's it for today Guys. Follow for more.....&lt;/p&gt;

&lt;p&gt;And If you have come so far, you can also subscribe to our newsletter:- &lt;a href="https://www.serversidedigest.com/" rel="noopener noreferrer"&gt;https://www.serversidedigest.com/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thanks ❤️&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>computerscience</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
    <item>
      <title>Javascript Promises in depth with V8 engine internals</title>
      <dc:creator>SinghDevHub</dc:creator>
      <pubDate>Mon, 09 Sep 2024 15:29:02 +0000</pubDate>
      <link>https://forem.com/singhdevhub/javascript-promises-in-depth-with-v8-engine-internals-1jlb</link>
      <guid>https://forem.com/singhdevhub/javascript-promises-in-depth-with-v8-engine-internals-1jlb</guid>
      <description>&lt;p&gt;😅 Being a single threaded language most of the people think that Javascript or NodeJS is slow and not scalable. Python and JS be like:-&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwm875am9g3fklpcdywg3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwm875am9g3fklpcdywg3.png" alt="Javascript Python meme" width="500" height="669"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Well, it depends. If your service is I/O (input output like file reading, DB call, Http call etc) heavy which means it has dependency on other resource being read or written. &lt;/p&gt;

&lt;p&gt;⚡️ There, NodeJS performs good as compared to other stacks in general when your service is IO heavy instead of CPU heavy.&lt;/p&gt;

&lt;p&gt;❓ This is because NodeJS or JS has something like handing over its work to the underlying thread of the Operating system to carry out IO  execution. &lt;/p&gt;

&lt;p&gt;It passes the callback (a function) to that thread and continue with the execution of other instructions in the code, which is called async programming also in fancy terms.&lt;/p&gt;

&lt;p&gt;Well Well Well, Let's take a step back and understand what we wrote above:-&lt;/p&gt;

&lt;h2&gt;
  
  
  Blocking nature of JS
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffzmmdjhm0t3mca4ldtty.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffzmmdjhm0t3mca4ldtty.png" alt="JS blocking nature" width="564" height="581"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Javascript is a single threaded language and hence can work on one task at a time unlike Java where multiple threads are present to handle workload in parallel (multithreading).&lt;/li&gt;
&lt;li&gt;So, if one function/instruction is running, next instruction will wait for this instruction to get completed and we can't run them in parallel.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's see how one instruction blocks another one:- &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fihqfmao4tfm9b2olktfg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fihqfmao4tfm9b2olktfg.png" alt="Step 1 of blocking nature of js" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvjfw404j0c66ge9zlmqk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvjfw404j0c66ge9zlmqk.png" alt="Step 2 of blocking nature of js" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fphvaki6ii47cvs21glv1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fphvaki6ii47cvs21glv1.png" alt="Step 3 of blocking nature of js" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmcb4jhikmc4joux9uas8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmcb4jhikmc4joux9uas8.png" alt="Step 4 of blocking nature of js" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;✅ You can refer video explanation (in english) with code examples and v8-engine internals here:- &lt;a href="https://www.youtube.com/watch?v=JN89L2SqPA8" rel="noopener noreferrer"&gt;https://www.youtube.com/watch?v=JN89L2SqPA8&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Non blocking nature
&lt;/h2&gt;

&lt;p&gt;Now, in JS runtime environment (NodeJS) we have some C++ APIs that have some functions like setTimeout, Promises, localStorage which are executed by the threads of our Operating systems, not by the main JS thread.&lt;/p&gt;

&lt;p&gt;You can see the right box having C++ APIs here:- &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Favb8o2uwc34y1w8dfc7j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Favb8o2uwc34y1w8dfc7j.png" alt="C++ Nodejs APIs" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Whenever, we perform IO through any C++ API like executing a promise , performing HTTP call or DB operation etc JS main thread hands over the main work to the OS thread and the main thread of JS continues working on other code and doesn't wait for the Async code to run. &lt;/li&gt;
&lt;li&gt;It provides a callback (a function) to execute when call stack gets  empty (call stack has only the sync code and async code goes to microtask or task queue)&lt;/li&gt;
&lt;li&gt;Event loop, loops through these queues when call stack gets empty &amp;amp; put the callbacks from these queues to the main stack and returns the result.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6k0d46gv5wnfpdpdkafk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6k0d46gv5wnfpdpdkafk.png" alt="Promise execution in task queue" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu1ejkz5yweg92eahxt1n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu1ejkz5yweg92eahxt1n.png" alt="setTimeout execution" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For more in depth details of How promises are implemented in C++ code, refer this:- &lt;a href="https://www.youtube.com/watch?v=JN89L2SqPA8" rel="noopener noreferrer"&gt;https://www.youtube.com/watch?v=JN89L2SqPA8&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;🔥 Follow for more such articles...&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>How to implement a Distributed Lock using Redis</title>
      <dc:creator>SinghDevHub</dc:creator>
      <pubDate>Sat, 31 Aug 2024 09:55:27 +0000</pubDate>
      <link>https://forem.com/singhdevhub/how-to-implement-a-distributed-lock-using-redis-he</link>
      <guid>https://forem.com/singhdevhub/how-to-implement-a-distributed-lock-using-redis-he</guid>
      <description>&lt;h2&gt;
  
  
  I am Dumb
&lt;/h2&gt;

&lt;p&gt;Well, whenever we work in our local system everything works as butter. That is why we call &lt;strong&gt;"No better place than 127.0.0.1"&lt;/strong&gt; but WAKE UP TO THE REALITY&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsxrsvvc5ed65enwtmcaj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsxrsvvc5ed65enwtmcaj.png" alt="Madara Uchiha" width="300" height="225"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Well things not always work in production as expected. Mostly when you are running multiple instances of your application.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo9l7vpx9h8qz583c29x0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo9l7vpx9h8qz583c29x0.png" alt="Microservices" width="800" height="378"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;🚀 As you can see that if multiple instances of our application are running and let's say that our client make a request to mark a user as a paid user in our DB. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Client will request our server&lt;/li&gt;
&lt;li&gt;Request will reach at our load balancer&lt;/li&gt;
&lt;li&gt;And one of the instance will get the request and will make a write query into our DB&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It seems alright right? No problem till now right.&lt;/p&gt;

&lt;p&gt;Well, yes till now there is no problem. But what if we want to write a business logic like:-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;fetch the user from the DB&lt;/li&gt;
&lt;li&gt;check if user is a free user or already paid&lt;/li&gt;
&lt;li&gt;if free, then mark it paid and save in the db&lt;/li&gt;
&lt;li&gt;if paid, send "Already paid" in the response.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;⚡️ As we know that (assume we are using MySQL here) MySQL DBs are &lt;a href="https://www.geeksforgeeks.org/acid-properties-in-dbms/" rel="noopener noreferrer"&gt;ACID  compliant&lt;/a&gt; which means any query will be atomic and isolated. which means MySQL query will be run in atomic way, either it will pass or fail. But it will not quit in between.&lt;/p&gt;

&lt;p&gt;🤔 But there is one issue here. Think, Think....&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Step 1: We are fetching user (Atomic transaction)&lt;/li&gt;
&lt;li&gt;Step 2: Running some business logic in the code&lt;/li&gt;
&lt;li&gt;Step 3: Updating the MySQL record if user not paid (Atomic transaction)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What will happen if at Step 2, one more request comes to cancel the payment, and then that query runs first and marks user as free, then step 3 runs and user marked as Paid.&lt;/p&gt;

&lt;p&gt;🕺🏻 Hurray, User got access to our Products without even paying.&lt;/p&gt;

&lt;h2&gt;
  
  
  Locking
&lt;/h2&gt;

&lt;p&gt;✅ Here comes the saviour, Locks&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft1sv0xd1hnz71ebieayr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft1sv0xd1hnz71ebieayr.png" alt="Operating systems locks meme" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;🔐 Lock is a structure that allows only one thread at a time to enter a critical section (block of code that should not be accessed by multiple workers i.e. threads)&lt;/p&gt;

&lt;p&gt;Hence, we will acquire lock before and release after the completion of the operation:-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Step 0: try-acquire() lock&lt;/li&gt;
&lt;li&gt;Step 1: If acquired, We are fetching user (Atomic transaction)&lt;/li&gt;
&lt;li&gt;Step 2: Running some business logic in the code&lt;/li&gt;
&lt;li&gt;Step 3: Updating the MySQL record if user not paid (Atomic transaction)&lt;/li&gt;
&lt;li&gt;Step 4: release() the lock&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  😅 Problem
&lt;/h2&gt;

&lt;p&gt;Now, here comes the problem which is if we will use some in memory lock data structure or any memory based lock it will be eligible for one instance for our application. what about the other instances running the same code and updating in the DB?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Well here comes the concept of Distributed locking&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  🔓 Distributed Locking
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Finudfknyfwyuzrragn28.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Finudfknyfwyuzrragn28.png" alt="Distributed Locking" width="800" height="421"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here lock acts as a centralised service, where if one instance of our service acquires the lock then others can't on the same key.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;WHAT KEY COULD BE HERE IN PAYMENT SERVICE?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;🔓 For a user making payment key could be the combination of = "PAYMENT_" + user_id + amount&lt;/p&gt;

&lt;p&gt;And this will be unique per user. And this key will remain same in case of user making payment or cancelling payment. Hence when one is happening other action can't proceed because both actions will try to acquire on same key.&lt;/p&gt;

&lt;p&gt;💭 What what the heck is Key, Acquire lock, release lock. And most importantly how redis is being in use?&lt;/p&gt;




&lt;h2&gt;
  
  
  🎈 Using Redis to implement Distributed Locking
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Using a single instance of Redis:-
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwxnd0ems2mhw4lxh3vgc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwxnd0ems2mhw4lxh3vgc.png" alt="Redis single instance lock" width="800" height="627"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But here are the few problems with a single redis instance:-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A single instance may fail &amp;amp; lock acquired may not be released &lt;/li&gt;
&lt;li&gt;If two instances are used (master-replica) when one client will acquire lock on one instance&lt;/li&gt;
&lt;li&gt;master has to communicate the same with replica to sync. This communication itself is an async communication&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🚀 So, if lock is acquired on master, and while communication to replica if master goes down before syncing with replica. Replica will become master where Lock on the same key will be available to acquire that was acquired on the master earlier.&lt;/p&gt;

&lt;p&gt;Two instances of our services will be able to acquire the lock on redis even having two instances (master-replica).&lt;/p&gt;

&lt;h3&gt;
  
  
  Using Redlock Algorithm:-
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Acquiring Lock&lt;/strong&gt;:- We will try to acquire lock on multiple redis instances with lock expiration time&lt;br&gt;
&lt;strong&gt;Validation of Lock&lt;/strong&gt;:- lock will be considered as acquired if major redis instances got lock acquired for the client&lt;br&gt;
&lt;strong&gt;Releasing Lock&lt;/strong&gt;:- When releasing the lock, all instances releases the lock&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1fck00ujw1mt6prbfsh5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1fck00ujw1mt6prbfsh5.png" alt="Redlock algorithm" width="800" height="743"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And yes that's it.&lt;/p&gt;

&lt;p&gt;❤️ &lt;em&gt;Thanks for the read, and subscribe to our newsletter for more such articles:-&lt;/em&gt; &lt;a href="https://www.serversidedigest.com/" rel="noopener noreferrer"&gt;https://www.serversidedigest.com/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For more information:- &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Jedis in Java:- &lt;a href="https://redis.io/docs/latest/develop/connect/clients/java/jedis/" rel="noopener noreferrer"&gt;https://redis.io/docs/latest/develop/connect/clients/java/jedis/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Redis Client in Golang:- &lt;a href="https://github.com/redis/go-redis" rel="noopener noreferrer"&gt;https://github.com/redis/go-redis&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>webdev</category>
      <category>tutorial</category>
      <category>java</category>
      <category>redis</category>
    </item>
    <item>
      <title>Multithreading: Event Loops vs Thread Pools and more...</title>
      <dc:creator>SinghDevHub</dc:creator>
      <pubDate>Fri, 15 Dec 2023 09:35:30 +0000</pubDate>
      <link>https://forem.com/singhdevhub/multithreading-event-loops-vs-thread-pools-and-more-48di</link>
      <guid>https://forem.com/singhdevhub/multithreading-event-loops-vs-thread-pools-and-more-48di</guid>
      <description>&lt;p&gt;😩 Whenever we think of running a program we always focus on running the code but we do not care about who is running the code. &lt;/p&gt;

&lt;p&gt;Do you know who runs the code, who carries the program to the RAM and how it executes?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiexsjxu46fftrpg24t8g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiexsjxu46fftrpg24t8g.png" alt="Multithreading meme" width="558" height="695"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;😎 So, There is something called Thread. Who is responsible to run your program/code by using main memory (RAM), CPU and storage or in general our machine's resources.&lt;/p&gt;

&lt;p&gt;Threads are nothing but a worker who has Thread block, stack and access to heap.&lt;/p&gt;

&lt;p&gt;But what the heck is stack, heap and Thread block?&lt;/p&gt;

&lt;h2&gt;
  
  
  📌 Stacks, Heaps, Thread Blocks
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftcr7zsxxnl2f6xkcysgv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftcr7zsxxnl2f6xkcysgv.png" alt="Stack overflow meme" width="460" height="452"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Whenever you write a function
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, when we'll run this code we will declare the function in our main memory (RAM) and a pointer to this function will be stored in the stack.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3qs9xlb5p56vu9dap2w7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3qs9xlb5p56vu9dap2w7.png" alt="Stack vs Heap" width="800" height="456"&gt;&lt;/a&gt;&lt;br&gt;
Img Ref:- &lt;a href="https://www.alexhyett.com/stack-vs-heap-memory/" rel="noopener noreferrer"&gt;Link&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;✨ So, stack is a scope level memory space where your variables like a and b (Local variables) will be stored and heap is a dynamic memory where the pointers, dynamic arrays and objects (instances of classes) gets stored.&lt;/p&gt;

&lt;p&gt;📌 Stack gets cleared when a function's executions gets completed like it will be flushed but heap requires a manual cleaning in some programming languages and in some we have garbage collection.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzxu5rpxrvyohaiumtip9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzxu5rpxrvyohaiumtip9.png" alt="Meme" width="560" height="575"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Don't worry, follow along you'll get for sure, and please like and save the article to support us&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;😎 Now, we know stack, heap which is used to store on the go things when your program runs.&lt;/p&gt;

&lt;p&gt;But what about our man who carries whole stuff?? Thread right.&lt;/p&gt;

&lt;p&gt;So, thread has thread block &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fad0x2p31wk2syl8ij5pj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fad0x2p31wk2syl8ij5pj.png" alt="Thread Block" width="466" height="309"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, thread block has some fields and a kind of storage unit only which has some info to run a program and access some shared space or its own space. Shared space is heap memory space and its own space is stack.&lt;/p&gt;



&lt;p&gt;💀 Now, comes the part what is an Event Loop and Thread pool.&lt;/p&gt;

&lt;p&gt;When someone works in Javascript he/she might have known that JavaScript is a single threaded language which simply means is that whatever your code is it will be carried by a single worker.&lt;/p&gt;

&lt;p&gt;But if someone is working in Java, C, C++ etc there we have multiple threads that can be used in parallel.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx326tviqtexwhprk6adu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx326tviqtexwhprk6adu.png" alt="Meme What is that" width="624" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Have some patience guys, you will understand 💯&lt;/p&gt;
&lt;h2&gt;
  
  
  📌 Multithreading
&lt;/h2&gt;

&lt;p&gt;Now, let's say you have two functions (in JAVA)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Main&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
     &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello world"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
     &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;substract&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;){&lt;/span&gt;
     &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="o"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;😎 Now, Thread 1 can carry add() function and start its execution and Thread 2 can carry substract() function and start its execution right? Because both are independent.&lt;/p&gt;

&lt;p&gt;So, there is something called context switching. It is like if Thread 1 is let's say is waiting for some user's input then Thread 2 will be running and context will be shifted from Thread 1 to 2.&lt;/p&gt;

&lt;p&gt;😎 Current state of Thread 1 will be stored in its block and Thread 2 will continue its running. The main aim of Multithreading is to make sure CPU is not going in idle phase. It should be continuously in use.&lt;/p&gt;

&lt;p&gt;😩 But in Javascript we only have Thread 1, How we can parallelize the things there.&lt;/p&gt;

&lt;p&gt;🫶 Here we have the Event Loop.&lt;/p&gt;

&lt;h2&gt;
  
  
  📌 Event Loop vs Thread Pools
&lt;/h2&gt;

&lt;p&gt;Now let's say the code in JS is like:-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
    &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I am lazy&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I am active&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we can see there is one block who is waiting for 1000ms (1sec) and after that it will run. But there are other code lines which are independent and can run meanwhile. So,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
         &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I am lazy&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;can go in waiting phase and meanwhile&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I am active&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;can run. So, setTimeout() will go in eventLoop and it will wait for the sync code to run. And this waiting code is async code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F08eemoueggr9uusgrrxy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F08eemoueggr9uusgrrxy.png" alt="Event loops" width="800" height="463"&gt;&lt;/a&gt;&lt;br&gt;
Img ref:- &lt;a href="https://itnext.io/javascript-runtime-js-engine-event-loop-call-stack-execution-contexts-heap-and-queues-4826d064ad76" rel="noopener noreferrer"&gt;Link&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;❤️ Here, event loop will keep looping and checking if there is any program to run in the callback queue (in this queue async code will come). After all the sync code completion (when stack of sync code gets empty) async code will run.&lt;/p&gt;

&lt;p&gt;😩 It is a kind of Blocking thing right? what if sync code get's into infinite loop then all the async code will stop right? Yes. that is the problem, that is why timeouts needs to be configured carefully.&lt;/p&gt;

&lt;p&gt;Now, coming to the threadpools:- &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1x20ezinnuaqigwvmsrv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1x20ezinnuaqigwvmsrv.png" alt="Thread Pools" width="800" height="423"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;🥹 Now the question comes is, when we read a Big file and make it async and how does than other sync code runs if the async code is also suppose to run in the future.&lt;/p&gt;

&lt;p&gt;Here is where we have Thread Pools. Like, To read a file you will do this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;filePath&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;test.txt&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;fileContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;filePath&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;utf-8&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, our OS has threads and Javascript will give the work to read the file to one of the thread from the thread pools of our OS and our OS has some system calls which it will use to read the files and will save to a buffer.&lt;/p&gt;

&lt;p&gt;This buffer will be in memory only&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;byte[] buff;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and there is a callback function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;which will be called once the file read is completed. and then Javascript main thread will copy content from the buff[] to data&lt;/p&gt;




&lt;p&gt;So, in JS, everything seems in parallel but it is not.&lt;/p&gt;

&lt;p&gt;🔥 And that is it for today. We learnt about&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Threads, Stack, Heaps&lt;/li&gt;
&lt;li&gt;Thread Blocks&lt;/li&gt;
&lt;li&gt;Multithreading, context switching&lt;/li&gt;
&lt;li&gt;Event loops, Thread pool&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Follow for more in depth articles 🫶&lt;/p&gt;

&lt;p&gt;Visit my YouTube channel for more info: &lt;a href="https://www.youtube.com/@SinghDevHub" rel="noopener noreferrer"&gt;Link&lt;/a&gt; [Language: Hindi, India 🇮🇳]&lt;/p&gt;

</description>
      <category>java</category>
      <category>webdev</category>
      <category>programming</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
