<?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>DEV Community: Dilawar Singh</title>
    <description>The latest articles on DEV Community by Dilawar Singh (@dilawar).</description>
    <link>https://dev.to/dilawar</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%2F30204%2F13d14c67-92ee-448e-9283-072257382a37.png</url>
      <title>DEV Community: Dilawar Singh</title>
      <link>https://dev.to/dilawar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dilawar"/>
    <language>en</language>
    <item>
      <title>A python script to uninstall pkg on MacOS</title>
      <dc:creator>Dilawar Singh</dc:creator>
      <pubDate>Thu, 07 Nov 2024 22:53:27 +0000</pubDate>
      <link>https://dev.to/dilawar/a-python-script-to-uninstall-pkg-on-macos-1nne</link>
      <guid>https://dev.to/dilawar/a-python-script-to-uninstall-pkg-on-macos-1nne</guid>
      <description>&lt;p&gt;Script is here &lt;a href="https://github.com/dilawar/Scripts/blob/master/pkg_uninstall.py" rel="noopener noreferrer"&gt;https://github.com/dilawar/Scripts/blob/master/pkg_uninstall.py&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Usage
&lt;/h2&gt;

&lt;p&gt;A sample run is shown below. Pass the pkg name as first argument to this script. The script will print the names of installed packages that matches the query. You select the pkg that you want to remove.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dilawar@halwa ~/Scripts (master)&amp;gt; sudo python3 pkg_uninstall.py clamav
0: com.cisco.ClamAV.programs
1: com.cisco.ClamAV.libraries
2: com.cisco.ClamAV.documentation
Select a package to uninstall 0            
Uninstalling com.cisco.ClamAV.programs
Forgot package 'com.cisco.ClamAV.programs' on '/'.
dilawar@halwa ~/Scripts (master)&amp;gt; sudo python3 pkg_uninstall.py clamav
0: com.cisco.ClamAV.libraries
1: com.cisco.ClamAV.documentation
Select a package to uninstall 0
Uninstalling com.cisco.ClamAV.libraries
Forgot package 'com.cisco.ClamAV.libraries' on '/'.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that’s it.&lt;/p&gt;

</description>
      <category>macos</category>
      <category>pkg</category>
      <category>python</category>
    </item>
    <item>
      <title>An example of `boost::fiber` library</title>
      <dc:creator>Dilawar Singh</dc:creator>
      <pubDate>Thu, 07 Nov 2024 22:43:00 +0000</pubDate>
      <link>https://dev.to/dilawar/an-example-of-boostfiber-library-cgl</link>
      <guid>https://dev.to/dilawar/an-example-of-boostfiber-library-cgl</guid>
      <description>&lt;p&gt;Fiber is &lt;em&gt;just a thread implemented in user space&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Fibers are easier to reason about than threads. And have much cheaper context switching cost. Fibers are well suited for handling concurrent IO operations where a processor mostly wait for data, and threads usually have pretty big context switching cost. So multiple fibers running in a single thread is an effective solution.&lt;/p&gt;

&lt;p&gt;Here is a simple program I wrote to explore fibers. You can find the full example below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/dilawar/playground/blob/a5fba9f21ff121249c71bff75ee8964c1016aa3f/BOOST/fiber.cpp" rel="noopener noreferrer"&gt;playground/fiber.cpp at a5fba9f21ff121249c71bff75ee8964c1016aa3f · dilawar/playground&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This program has two functions: &lt;code&gt;print_a&lt;/code&gt; prints &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;print_b&lt;/code&gt; prints &lt;code&gt;b&lt;/code&gt; and then launches a thread that prints &lt;code&gt;B&lt;/code&gt; (in detached mode).&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="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;print_a&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"a"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;boost&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;this_fiber&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;yield&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="nf"&gt;print_b&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"b"&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="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;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"B"&lt;/span&gt;&lt;span class="p"&gt;);&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;detach&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;boost&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;this_fiber&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;yield&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;Following is the &lt;em&gt;main&lt;/em&gt; function. We created a shared variable &lt;code&gt;i&lt;/code&gt; initialized to 0. We create two &lt;code&gt;detach&lt;/code&gt;ed fibers. The first one keeps calling &lt;code&gt;print_a&lt;/code&gt; till &lt;code&gt;i &amp;lt; 20&lt;/code&gt;. Similarly, the second one loops on &lt;code&gt;print_b&lt;/code&gt; till &lt;code&gt;i &amp;lt; 20&lt;/code&gt;.  Both increment &lt;code&gt;i&lt;/code&gt; by 1. When &lt;code&gt;i = 20&lt;/code&gt;, both fibers should be able to &lt;code&gt;join&lt;/code&gt;.&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="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&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;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;boost&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;fibers&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;fiber&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;]()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;print_a&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;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="n"&gt;detach&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="n"&gt;boost&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;fibers&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;fiber&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;]()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;do&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;print_b&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="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="n"&gt;detach&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"X"&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s guess the output of this program. It is most likely to be the same as if &lt;code&gt;std::thread&lt;/code&gt;s were used instead of fiber.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;X&lt;/code&gt; is printed first? &lt;strong&gt;Yes&lt;/strong&gt;. Note that &lt;code&gt;detach()&lt;/code&gt; is called on each fiber so neither of their functions is called. They are put in the background. Control passes to the fiber manager at &lt;code&gt;return 0;&lt;/code&gt; when it asks the fibers to &lt;code&gt;join&lt;/code&gt;. In fact, you can put more computations after the &lt;code&gt;printf("X");&lt;/code&gt; statement, and it would be computed before any fiber is called.&lt;/p&gt;

&lt;p&gt;As soon as we try to return from the &lt;code&gt;main&lt;/code&gt;, the fiber manager is asked to &lt;code&gt;join&lt;/code&gt; the fibers. The first fiber &lt;em&gt;awakes&lt;/em&gt;, &lt;code&gt;a&lt;/code&gt; is printed, and the fiber &lt;code&gt;yield&lt;/code&gt;s the control to the manager. The fiber manager then wakes up the second fiber (who was waiting in the queue) that prints &lt;code&gt;b&lt;/code&gt; and also launch a thread in the background that prints &lt;code&gt;B&lt;/code&gt;. We can not be sure if &lt;code&gt;B&lt;/code&gt; will be printed immediately after the &lt;code&gt;b&lt;/code&gt; (it is a &lt;code&gt;std::thread&lt;/code&gt;).  &lt;code&gt;print_b&lt;/code&gt; yields the control and goes to sleep. The fiber manager wakes up first fiber again that calls &lt;code&gt;print_a&lt;/code&gt; again, and &lt;code&gt;a&lt;/code&gt; is printed, and so on. Note that &lt;code&gt;i&lt;/code&gt; is incremented every time either of the fibers is called.&lt;/p&gt;

&lt;p&gt;When &lt;code&gt;i&lt;/code&gt; hits 20, both fibers terminate and &lt;code&gt;joined&lt;/code&gt;, and the main function &lt;code&gt;return 0;&lt;/code&gt;. So we have &lt;code&gt;print_a&lt;/code&gt; called ten times, and &lt;code&gt;print_b&lt;/code&gt; is also called ten times. In the output, we should have ten &lt;code&gt;a&lt;/code&gt;s, ten &lt;code&gt;b&lt;/code&gt;s, and 10 &lt;code&gt;B&lt;/code&gt;s. &lt;code&gt;B&lt;/code&gt; may not strictly follow &lt;code&gt;b&lt;/code&gt;, but &lt;code&gt;b&lt;/code&gt; must come after &lt;code&gt;a&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Here are a few runs of the program. Note that the location of &lt;code&gt;B&lt;/code&gt; is not deterministic.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;XababBabBabBababBBabBabBabBabBB&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;XababBabBabBabBabBabBabaBbBabBB&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;XababBabBabBabBabBabBabBabBabBB&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;XababBabBabBabBabBabBabBabBabBB&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;XababBabBabBBababBabBabBabBabBB&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;XababBabBabBabBabBabBabBabBabBB&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;XababBabBabBababBBabBabBababBBB&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;XababBabBabBababBBabBabBabBabBB&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;XababBabBabBababBBabBabBabBabBB&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;XababBabBabBabBabBababBBabBabBB&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;XababBabBabBabBabBabBabBabBabBB&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;A great talk by Nat on Boost fibers &lt;a href="https://youtu.be/e-NUmyBou8Q" rel="noopener noreferrer"&gt;https://youtu.be/e-NUmyBou8Q&lt;/a&gt;
&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>boost</category>
      <category>cpp</category>
      <category>fiber</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
