<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Akhand Mishra on Medium]]></title>
        <description><![CDATA[Stories by Akhand Mishra on Medium]]></description>
        <link>https://medium.com/@akhandmishra?source=rss-2514cc8a06a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*28BnB_FA7IaifVCO4YodZQ.jpeg</url>
            <title>Stories by Akhand Mishra on Medium</title>
            <link>https://medium.com/@akhandmishra?source=rss-2514cc8a06a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 05:23:43 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@akhandmishra/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Important System calls every programmer should know]]></title>
            <link>https://medium.com/@akhandmishra/important-system-calls-every-programmer-should-know-8884381ceadb?source=rss-2514cc8a06a------2</link>
            <guid isPermaLink="false">https://medium.com/p/8884381ceadb</guid>
            <category><![CDATA[operating-systems]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[coding-interviews]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[system-call]]></category>
            <dc:creator><![CDATA[Akhand Mishra]]></dc:creator>
            <pubDate>Thu, 02 Aug 2018 13:44:22 GMT</pubDate>
            <atom:updated>2019-12-06T05:37:53.255Z</atom:updated>
            <content:encoded><![CDATA[<p>Recently I took an <a href="https://in.udacity.com/course/introduction-to-operating-systems--ud923"><strong>Introduction to Operating System</strong></a><strong> </strong>course and a few questions came to my mind, like how many system calls are there? Which one are the most common ones? Well, it’s surprising that <em>Linux kernel 3.0 </em>has whopping 326 system calls. However, it’s not efficient to go through all the system calls that exists, we’ll look at some of the common ones, so, here’s the<strong> System calls every programmer should know.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/659/1*SAWr16VHsHs1m_bVSP1lfQ.jpeg" /></figure><p>We all know that system calls are basically a way in which a program requests a service from the kernel of the OS which may include hardware-related services like accessing files from hard disk, creation, and execution of new processes, etc. You can read more about operating systems at <a href="https://medium.com/@akhandmishra/operating-system-introduction-ba5bb9c438bc">Operating System: Introduction</a>.</p><p>All the system calls that we’re discussing here are grouped into three categories, these are:-</p><ul><li><strong>System Calls for Process Management</strong></li><li><strong>System Calls for Signalling</strong></li><li><strong>System Calls for Protection</strong></li></ul><h3>System Calls for Process Management</h3><p>Some important process management system calls are <em>fork, wait, exec, getpid, brk. The fork </em>system call is basically used for creating a new process identical to parent process including all the file descriptors, stack pointer, program counter, and all other registers too, i.e. everything. Fork returns the child’s PID in the parent process and 0 in the child process if it encounters some error it returns -1. If you want to read more about <strong><em>fork</em></strong> like its description, argument, return type, error, just execute</p><pre>man 2 fork</pre><p>command in your Linux terminal and you can see Linux Programmer’s manual something like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/717/1*wSkYM5yBZSyjwJto3FnYEg.png" /><figcaption>Typical output of a man command</figcaption></figure><p>The<strong><em> </em></strong>system call <strong><em>wait</em></strong><em> </em>is used in the parent process to wait for the child to get terminated, stopped by a signal or resumed by a signal. It returns PID of the child which is being terminated, stopped or resumed and takes a pointer to a status variable as an argument. If you need to wait for a particular child (PID), you can use <strong><em>waitpid</em></strong><em> </em>which is another variant of <strong><em>wait</em></strong> system call. The <strong><em>wait </em></strong>system call is necessary otherwise the child process will go into the <strong><em>zombie</em></strong> state. The <strong><em>execve</em></strong> (or <strong><em>exec, execv)</em></strong> system call exit the current process that it is running and starts a new program. It takes three arguments as input, by convention the first parameter is the filename (string) which points to the program that is going to be executed, the second parameter is an array of string which is the argument vector for the program which is going to be executed by <strong><em>execve</em></strong>, with a null pointer at the end. The <strong><em>exec </em></strong>system call replaces the core image of the process with the image of the new process, hence <strong><em>exec </em></strong>never returns anything, except when some error occurs. The system call <strong><em>getpid</em></strong> simply returns the PID of the current process. The system call <strong><em>brk</em></strong><em> </em>used to increase or decrease the process’s data segment. It returns the new address where the data segment of the process is to end. However, <strong><em>brk</em></strong> is not defined by the<strong> POSIX </strong>standard and hence programmers are encouraged to use the <strong>malloc</strong> library procedure for dynamically allocating memory.</p><p>I think it’s enough for the theory, let’s do some coding by making use of fork, execv, wait, getpid and exit system call.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fa6a6b95b81c737519e4d77a77df4a49/href">https://medium.com/media/fa6a6b95b81c737519e4d77a77df4a49/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/96377873b268a3c825e1cc008f25fe3b/href">https://medium.com/media/96377873b268a3c825e1cc008f25fe3b/href</a></iframe><p>In the above code, I’ve added a 5 seconds sleep time at two places, one in forkExample.c and other in execExample.c, so that during sleep I can execute</p><pre>ps -a</pre><p>command in the terminal, which gives the list of all the active processes.</p><p>Now, when we first execute our forkExample program, its output during its first 5-seconds sleep is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/964/1*uCbBr2o_B0a5CxhPwAeVHA.png" /><figcaption><strong>forkExample output during its 5 second sleep</strong></figcaption></figure><p>meanwhile, the output of <strong><em>ps -a </em></strong>is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/808/1*bmWCjIo-vnN0n_xQ-Onpyg.png" /><figcaption><strong>ps -a output</strong></figcaption></figure><p>Clearly, in the second image, we can see, the PID of forkExample is 4290 which is the same as in the first image.</p><p>After the first sleep, fork is called and child process is created which again goes into 5-seconds sleep, during this the output of program and <strong><em>ps -a </em></strong>are as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/964/1*qtX_gVYUjrTygNQ1JO-9iw.png" /><figcaption><strong>forkExample output during its child’s 5 second sleep</strong></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/752/1*EJeVBCIR9jypJ8ao-g0YoQ.png" /><figcaption><strong>ps -a output</strong></figcaption></figure><p>in the above image, we can see that a child process is created with PID 4297 which is the same as in forkExample output.</p><p>After the completion of the child process’s 5-seconds sleep, the rest of the parent process is executed. In the image below we can clearly see the value returned by the child process, i.e. 52.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/967/1*zhvGih7EJsMn3-tL9iorrA.png" /><figcaption><strong>forkExample output</strong></figcaption></figure><h3>System Calls for Signalling</h3><p>A Signal in an operating system is a limited form of inter-process communication, it is a way in which the operating system sends a notification to a process or a thread to notify it of an event, for example when CTRL + C is pressed in the terminal, the terminal sends an interrupt signal to the running foreground process. Let’s take an example to understand better.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3cdc70a3ddce7dbdeac487f6808d9873/href">https://medium.com/media/3cdc70a3ddce7dbdeac487f6808d9873/href</a></iframe><p>In this example, a handler is assigned to the <strong>SIGINT </strong>signal, wiz. Interrupt from the keyboard, which occurs when <strong>CTRL + C</strong> is pressed. The output of the program is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*RPA0l8zrl6vDhWWVF1eURA.gif" /><figcaption><strong>output</strong></figcaption></figure><p>In the above example, when <strong>CTRL + C </strong>is pressed, the <strong>SIGINT</strong> signal is sent to the <em>signalExample </em>process which doesn’t stop the process instead it invokes the handler. However, when <strong>CTRL + \ </strong>is pressed, the <strong>SIGQUIT </strong>signal is sent to the process which is not handled manually like <strong>SIGINT</strong> and hence default action is performed and the process is stopped. Handlers are sometimes used to ignore some specific signals, for example, a background process is supposed to ignore <strong>CTRL + C </strong>interrupt signal from the keyboard.</p><p>When a signal is generated, the state of the process is pushed into its stack and the signal handler is invoked. When the signal handling procedure is done, it calls <strong><em>sigreturn</em></strong> to continue where it left off before the signal. The system call <strong><em>sigaction </em></strong>is often used in place of <strong><em>signal. </em></strong>Apart from <strong>SIGINT</strong> and <strong>SIGQUIT, </strong>there are few more pre-defined signals like SIGPIPE, SIGPOLL, SIGRTMIN, SIGRTMAX, SIGSEGV, SIGSTOP, etc. You can read more about these signals at <a href="https://en.wikipedia.org/wiki/Signal_(IPC)"><strong>Signal (IPC)</strong></a>.</p><p>There is also an <strong><em>alarm </em></strong>system call, which sends <strong>SIGALRM</strong> signal after a specific time interval. The <strong><em>pause </em></strong>system call causes the calling process (or thread) to sleep until a signal is delivered and meanwhile the operating system can schedule other processes to the CPU.</p><h3>System Calls for Protection</h3><p>Permissions are important to keep our system secure, every user should have well-defined permissions for each file. Every file in UNIX has 11-bit mode protection, of which 9 bits are read-write-executable bits for the owner, group, and others. Suppose if you’re the owner of a file and want to change its permission to read, write and execute for the owner, read and execute for your group and execute for others, you can run the following command in the terminal.</p><pre>chmod u=rwx,g=rx,o=r myfile</pre><p>where u, g, and o stands for the user, group and owner respectively, you can also run</p><pre>chmod 754 myfile</pre><p>command which is in octal permissions notion.</p><p>Following is the octal permission notion table for <strong>chmod </strong>command:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/479/1*an62lrrel9rs7mXWHxAG5A.png" /><figcaption><strong>Octal permission notion table</strong></figcaption></figure><p>where r, w, and x stands for read, write and executable.</p><p>The other two bits are set-group-id and set-user-id bits. To understand better, suppose we have an executable program which performs such operations which only a superuser is authorised to do so (such as creating directories), in such cases the SETUID bit is set on the executable program so that when the program is run, the program will have the privileges of the owner instead of the real user for the duration of the execution. Similarly, if we want to execute the program with the privileges of a group then the SETGID bit is set. 02000 and 04000 are the SETGID and SETUID bits. So, in the above example, if we want to set the SETUID bit, with the same read write and execute permissions for owner, group, and other, we can execute</p><pre>chmod 4754 myfile</pre><p>command in the terminal. Similarly, if we want to set SETGID bit, we can execute</p><pre>chmod 2754 myfile</pre><p>command in the terminal.</p><p>We can also run the following command in the terminal in order to check all the permission granted to a particular file.</p><pre>ls -l myfile</pre><p>The system call <strong><em>getuid , getgid , geteuid , </em></strong>and<strong><em> getegid</em></strong> are used to get real and effective UID and GID of a file. To change the owner and group of a file the system call <strong><em>chown </em></strong>can<strong><em> </em></strong>be<strong><em> </em></strong>used, it takes the file path, owner’s UID and group’s GID as the input argument. The <strong><em>lchown</em></strong> and <strong><em>fchown</em></strong> are another variant of <strong><em>chown</em></strong> system call and differ only in how the file is specified.</p><p>These were some important and commonly used system calls that every programmer should know.</p><p><strong><em>Good luck and happy learning!</em></strong></p><p>Follow me here on Medium <a href="https://medium.com/u/2514cc8a06a">Akhand Mishra</a></p><p>On a lighter note, remember: <strong>If you are acting like a sheep do not blame the shepherd. You cannot herd lions. Wake up and roar and you are free.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8884381ceadb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Operating System: Introduction]]></title>
            <link>https://medium.com/@akhandmishra/operating-system-introduction-ba5bb9c438bc?source=rss-2514cc8a06a------2</link>
            <guid isPermaLink="false">https://medium.com/p/ba5bb9c438bc</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[introduction]]></category>
            <category><![CDATA[operating-systems]]></category>
            <dc:creator><![CDATA[Akhand Mishra]]></dc:creator>
            <pubDate>Sat, 28 Jul 2018 22:29:05 GMT</pubDate>
            <atom:updated>2018-07-28T22:29:05.042Z</atom:updated>
            <content:encoded><![CDATA[<h3>What is an Operating System?</h3><p>An operating system is a layer of system software between applications and hardware that abstracts (i.e. simplify what hardware actually looks like) and arbitrates (ie. manage, overseas and control the hardware use).</p><h3>Functions of Operating System</h3><ul><li><strong>Hides hardware complexity: </strong>OS hides hardware complexity for applications as well as for application developers. Application developers, for example, don’t need to worry about disk sectors or block when saving a file to the disk. In fact mechanism for reading/writing a file on a hard drive is different from reading/writing the file on a USB drive or SSD which is all handled by OS. Similarly, OS abstracts the network resource and provide a higher level abstraction called <em>Socket </em>and provide some services to send and receive packets from that socket.</li><li><strong>Resource Management: </strong>OS also manages the resources used by the application. It decides how many and which one of the above resources will be used by the application, allocates memory to them, schedules these task to the CPU that can execute them, controls access to the applications to various devices in the system, etc. Overall OS is responsible for all types of resource management and allocation to these applications.</li><li><strong>Provides isolations and protection: </strong>When multiple apps are co-running on the same hardware, the OS must ensure that each of them must make adequate progress and they don’t hurt each other. For example, OS allocates different part of the physical memory to different applications and makes sure that they don’t access each other’s memory.</li></ul><p>Some examples of Operating Systems are Windows, Linux, MacOS, iOS, Android, Symbian, etc.</p><h3>Elements of OS</h3><ul><li><strong>Abstractions: </strong>Abstraction is the simplification of what hardware actually looks like, some OS abstractions are a process, thread, file, socket, memory page, etc. I’ll be discussing them in future posts.</li><li><strong>Mechanism: </strong>To operate on the above abstraction the OS must know the mechanism i.e the implementation or the steps involved which determine how to perform some activities. Some common mechanisms are: create, schedule, open, write, allocate, etc.</li><li><strong>Policy: </strong>Policy is the ways to choose which activity to perform, maximum number of sockets that an application can have access to, how much memory to allocate, etc. Some common policies are Least Recently Used (LRU), Earliest deadline first (EDF), etc.</li></ul><h3>OS Design Principles</h3><ul><li><strong>Separation of Mechanism and Policy: </strong>Mechanisms determine how to do something; policies determine what will be done. For example, the timer construct (see Section 1.5.2) is a mechanism for ensuring CPU protection, but deciding how long the timer is to be set for a particular user is a policy decision.</li><li><strong>Optimize for the common case: </strong>Where will the OS be used? What will the user want to execute on that machine? What are the workload requirements?</li></ul><h3>Kernel and OS</h3><p>The OS is the package that communicates directly with hardware and our application, whereas kernel is the lowest level of the OS and is the core of the OS with complete control over everything in the system. it is one of the first programs loaded on start-up (after the bootloader). It handles the rest of start-up as well as input/output requests from software, translating them into data-processing instructions for the CPU.</p><h3>User/Kernel Protection boundary</h3><p><strong>User Mode: </strong>In User mode, the executing code has no ability to directly access hardware or reference memory. Code running in user mode have to request services from kernel via <em>System Calls</em> to access hardware or memory. Most of our application executes in this mode.</p><p><strong>Kernel Mode:</strong> In kernel mode, the executing code has complete and unrestricted access to the underlying hardware. It can execute any CPU instruction and reference any memory address. Kernel mode is generally reserved for the lowest-level, most trusted functions of the operating system.</p><p>To achieve its goal of controlling and managing hardware resources on behalf of applications, the operating system must have special privileges to have direct access to the hardware. OS must operate in kernel mode. Whereas all the applications operate in user mode. If an application wants OS to perform a certain task or service like allocating memory, accessing some files from the disk, access to a network resource, etc it has to explicitly invoke System Calls, which is a mode of communication between applications and OS. OS also support <em>signal</em> which is a mechanism for the OS to pass notifications into the application.</p><p>A bit, called the mode bit, is added to the hardware of the computer to indicate the current mode: kernel(0) or user(1). With the mode bit, we are able to distinguish between a task that is executed on behalf of the operating system and one that is executed on behalf of the user.</p><p><strong>System Calls: </strong>A System call is a programmatic way in which a computer program requests a service from the kernel of the OS. This may include hardware related services like accessing files from hard disk, creation and execution of new processes, etc.</p><h3>System calls flow chart</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/0*Ai-nXOebDHQyrAdM.jpeg" /><figcaption><em>System calls flow chart</em></figcaption></figure><p>Initially, (1) the application process executes in User Mode, while executing (2) if an application needs to access some file or needs some input from the user or maybe the application wants to make a network call, our application will make a system call, the control will be passed to kernel and the mode bit will be set to 0. The OS will perform whatever operation was specified in the system call after that OS changes the mode bit back to 1 and returns the result to process. The system call involves the change in execution context to kernel mode and then back to user mode, therefore the system call is not a cheap operation.</p><p>Before making a system call the application process must:</p><ul><li><em>Write Argument:</em><strong> </strong>In order to make a system call, some data (data bundles to send, file address to read/write, etc) needs to be passed to the kernel which is passed as a system call arguments.</li><li><em>Save relevant data to a well-defined location: </em>Well-defined location is necessary so that kernel can determine which arguments, how many arguments it should retrieve and their location in memory.</li><li>Make <em>System call with specific system call number</em>, which is necessary to access arguments and other relevant data.</li></ul><p>The arguments can be passed directly or they can be passed indirectly by specifying their address.</p><h3>User/Kernel transition</h3><p>Applications may need access to certain types of hardware or need to request some changes in the allocation of hardware resources, etc which can only be done by Operating System.</p><p><em>Hardware provides support</em> for user/kernel transition, for example, the hardware will cause a trap if an application from unprivileged mode try to perform some instruction or some memory access or which it doesn’t have permission, for example, application process cannot change content of some registers and give itself more CPU or allocate more memory. After the trap, the control is passed to the operating system and mode bit is set to 0, at this point OS checks what causes the tarp and can choose whether to grant or deny that specific request that causes the trap.</p><p>Performing all these tasks require a number of instructions which makes a system call an expensive operation.</p><p>Accessing memory is about 100 times slower than accessing the cache. Accessing cache is close to cycles of the processor which makes it very fast. The application performance is very dependant on the cache, so when control is passed to the OS, it brings its own content to the cache memory in order to perform system call, replacing application’s content. So after the execution of system call, the application will no longer be able to access its data in the cache and it will have to go the memory to access its data which makes user/kernel transition even more expensive.</p><h3>Basic Operating System services</h3><p>The operating system provides a number of services, some are directly linked to the underlying hardware like scheduling or memory management while others are linked with higher-level abstractions like file system.</p><p>Some example services provided by the operating system for the convenience of the user are:</p><ul><li><strong>Program Execution:</strong> The operating system provides an environment where the user can conveniently run programs. Running a program involves the allocating and deallocating memory, CPU scheduling, etc, the user does not have to worry about these things. They are taken care of by the operating systems.</li><li><strong>I/O Operations:</strong> One of the important jobs of an Operating System is to manage various I/O devices including mouse, keyboards, touchpad, disk drives, display adapters, USB devices, Bit-mapped screen, LED, Analog-to-digital converter, On/off switch, network connections, audio I/O, printers etc. The operating system hides the user the details of underlying hardware for efficiently and protected use of I/O operations.</li><li><strong>File System: </strong>The user process may need to read/write file from the disk. The operating system provides this service. The user does not have to worry about secondary storage management. The user gives a command for reading or writing to a file and sees his/her task accomplished.</li><li><strong>Communications: </strong>There are instances where processes need to communicate with each other to exchange information. It may be between processes running on the same computer or running on the different computers. By providing this service the operating system relieves the user of the worry of passing messages between processes.</li><li><strong>Error Detection:</strong> An error is one part of the system may cause malfunctioning of the complete system. To avoid such a situation the operating system constantly monitors the system for detecting the errors. This relieves the user of the worry of errors propagating to various part of the system and causing malfunctioning. This service cannot be allowed to be handled by user programs because it involves monitoring and in cases altering an area of memory or deallocation of memory for a faulty process. Or maybe relinquishing the CPU of a process that goes into an infinite loop. These tasks are too critical to be handed over to the user programs. A user program if given these privileges can interfere with the correct (normal) operation of the operating systems.</li><li><strong>Protection: </strong>Considering a computer system having multiple users and concurrent execution of multiple processes, the various processes must be protected from each other’s activities. Protection refers to a mechanism or a way to control the access of programs, processes, or users to the resources defined by a computer system.</li></ul><p>OS makes all these services available via system calls, following are some popular System calls for windows and Linux.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/0*l5OgAQiOhL_xKRAy.png" /><figcaption><em>Windows and Linux System Calls</em></figcaption></figure><h3>Different types of operating system architecture</h3><ul><li><strong>Monolithic Architecture: </strong>Monolithic OS is one of the oldest OS architecture, in this OS every possible service an application can require or any type of hardware will demand is already a part of the operating system. All the basic services of OS like process management, file management, memory management, exception handling, process communication etc. are all present inside the kernel only. It may also include several file system one for sequential workloads and other for random I/O. This makes OS very large.</li><li><em>Advantages: </em>Everything is included in the operating system, i.e. all the abstractions, services, etc and everything is packaged at the same time because of that there are some possibilities of some inlining and compile-time optimization</li><li><em>Disadvantages: </em>This types of OS is very large so there are too many states, too much code which is hard to maintain, debug, upgrade and require more memory making less memory available for applications.</li><li><strong>Modular Architecture: </strong>It most commonly used approach today. It has a number of basic services and APIs already part of it but also everything (file system, scheduler, etc) can be customized as a module. The operating system specifies certain interfaces which the module must implement to be apart of the operating system so that depending on our need we can customize various services by installing modules dynamically.</li><li><em>Advantages: </em>It’s easier to maintain and upgrade, it has a smaller code base and it’s less resource intensive which means it can leave more resources and memory for the application to use which can lead to better performance.</li><li><em>Disadvantages: </em>Because of its modularity and customizability this can reduce some opportunities for optimizations, which can have some effects on performance although not very significant. Maintainance can be tough as the modules may come from different code bases and may contain some bugs.</li><li><strong>Microkernel: </strong>Microkernel only includes most primitive services at the operating system level. The microkernel support some basic low-level address space management, thread management, and inter-process communication (IPC). Everything else like file system, device drivers, etc will run outside of the operating system at the user level. For this reason, microkernel requires lots of interprocess interactions, so microkernel itself support inter-process communication (IPC) as one of its core abstractions.</li><li><em>Advantages: </em>As it doesn’t include many abstractions it’s size is very small, it can not only lead to lower overheads and better performance but it may be very easy to test and verify the code.</li><li><em>Disadvantages: </em>As it has very frequent interactions which require lots of System calls which require a lot of protection mode changes i.e. the execution context changes many times from user mode to kernel mode which is an expensive operation. Software complexity is also very high as it’s very hard to find common components of software.</li></ul><h3>Summary</h3><p>In this post, we learned about different elements of the operating system i.e <strong>abstractions, mechanisms, </strong>and<strong> policies</strong>. we discussed different services provided by the operating system, we also learned about communications between applications and OS via <strong>System Calls</strong>, and then very briefly we learned about different types of OS architecture which are <strong>Monolithic, Modular </strong>and<strong> Microkernel.</strong></p><h3>Next: <a href="https://medium.com/@akhandmishra/operating-system-process-and-process-management-108d83e8ce60">Process and Process Management</a></h3><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ba5bb9c438bc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Operating System: Process and Process Management]]></title>
            <link>https://medium.com/@akhandmishra/operating-system-process-and-process-management-108d83e8ce60?source=rss-2514cc8a06a------2</link>
            <guid isPermaLink="false">https://medium.com/p/108d83e8ce60</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[process]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[operating-systems]]></category>
            <dc:creator><![CDATA[Akhand Mishra]]></dc:creator>
            <pubDate>Sat, 28 Jul 2018 22:24:08 GMT</pubDate>
            <atom:updated>2018-07-28T22:33:18.937Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Process:</strong> In simple words, a process is an instance of an executing application. An application is a file containing a list of instructions stored in the disk (often called an executable file), in flash memory, maybe in the cloud but it’s not executing, it’s a <em>static</em> entity. When an application is launched it is loaded into the memory and it becomes a process, so it is an <em>active</em> entity with a program counter specifying the next instruction to execute and a set of associated resources. If the same program is launched more than once than multiple processes will be created executing the same program but will be having a very different state.</p><p>A process <em>encapsulates</em> all the data for running application, this includes the <strong>text</strong>, the code of the program, a <strong>data</strong> section, which contains global variables and data which are available when the process is first initialized. As text and the data are available when the process is first initialized they are called <em>static</em> states and are available when the process first loads.</p><p>The process also encapsulates the process <strong>stack</strong> which contains temporary data (such as function parameters, return addresses, and local variables), it is a dynamic part of the process state which grows and shrinks during execution in Last-in-First-out order. Suppose we are executing a function “A” and want to call another function “B” from this function, for this, we have to save our current state (state of function “A”) to the stack and jump to execute function “B”, after the execution of function “B”, the state of the previous function (“A”) from which the function “B” was called is restored from the top of the stack and function “A” can continue its execution from that vary instruction it had left (program counter was also saved).</p><p>A process may also include a <strong>heap</strong>, which is the memory that is dynamically allocated during process run time. <strong>Text</strong> and <strong>data</strong>, <strong>stack</strong> and <strong>heap</strong> are the types of state of the process.</p><p>Every single element of the process has to be uniquely identified by its address, so an OS abstraction is used to encapsulate all of the process data in an address space. The address space is defined by a range of address from <strong>V0</strong> to some <strong>Vmax</strong>, and different types of process state will appear in different part of this address space. The address space from V0 to Vmax does not correspond to actual address space in a physical location, instead, they are virtual addresses. The memory management hardware and operating system components responsible for memory management like <em>page tables</em> maps these virtual addresses to actual physical addresses.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/0*MJe3qd_LxrMEyWjL.jpeg" /><figcaption><em>Process’s address space</em></figcaption></figure><p>All process may not need all the address space, as there are can be many processes running at a time and we may not have enough space in the physical memory (i.e. in the RAM). In order to deal this situation, operating system dynamically decides which portion of address space will be present where in physical memory. Multiple processes may share physical memory by temporarily swapping some portion of their address space into the disk, this portion of address space will be brought back into the memory whenever needed. The page table maintains the mapping from virtual address to the physical address (RAM or Disk), it also validates whether a particular memory access request by a process is allowed to perform or not.</p><p>Before the execution of a process, its source code must be compiled, the compilation of the code results in the conversion of the high-level code to binary instructions, a register in CPU is maintained which indicates the address of the next instruction to be executed for this process, we call it <strong>Program Counter (PC)</strong>. There are some other registers in the CPU which stores other information of the process like addresses for data or some status information. There is also a stack pointer which points to the top of the stack used by the process. To maintain all of this process for every single process an operating system maintains a <strong>Process Control Block (PCB).</strong></p><h3>Process Control Block (PCB)</h3><p>A Process Control Block is the data structure that operating system maintains for every single process. The PCB must contain process states like <em>program counter</em>,<em> stack pointer</em>, all the value of the <em>CPU register</em>, various memory mapping from virtual to physical memory, it may also include a list of open files, information which is necessary for scheduling of the process like how much time this particular process has executed on CPU in the past and how much time it should be allocated to execute in the future.</p><p>A PCB is created at the very moment a process is created with some initializations like PC points to the first instruction that needs to be executed. Certain field of the process changes as the state of the process changes for example whenever a process asks for more memory, the OS will allocate more memory to the process and update certain values of the PCB like <em>Page table</em>,<em> virtual memory</em> limits, etc. Some values of the process change too often like the value of program counter which changes on the execution of every single instruction. As updating such changes in PCB can be an expensive task, their values are stored and updated in CPU registers which are very fast. However, OS updates all the values maintained by CPU registers to the PCB whenever that particular process is no longer running on the CPU.</p><p>Suppose OS is managing two processes p1 and p2, currently, p1 is running and p2 is idle, their PCBs are stored somewhere in the memory. The process p1 is currently running means that CPU registers hold the values that correspond to the state of p1. After some time suppose OS decides to interrupt p1, so OS will update all the state information fields of the process p1 including the CPU registers to the PCB of p1. After this the OS will restore the PCB of p2 from the memory, i.e. OS will update all the CPU register from the PCB of p2 and will start executing process p2. After some time if process p2 is interrupted the PCB of p1 will be restored and CPU registers will be updated from the value of PCB of p1 and p1 will start executing from the exact same point where it was interrupted earlier by the operating system. Each time this swapping is performed we call it <strong>context switch</strong>.</p><h3>Context Switch</h3><p>It is a mechanism used by operating system to switch execution from the context of one process to the context of another process.</p><p>Context switching is an expensive operation because of the number of instructions involved in loading and restoring values of fields of PCB from the memory. Also when a process p1 is executing a lot of its data is stored in the <em>CPU cache</em> as accessing the cache is much much faster as compared to accessing from the memory. When the data we want is present in the cache we say that <em>cache is hot. </em>When CPU will switch the context from process p1 to process p2, the process p2 will replace process p1’s cache with its own cache, so next time when the context will switch from the process p2 back to the process p1, p1 will not find its data in the cache and has to access it from the memory, so it will incur cache misses, so we call it<em> cold cache.</em></p><h3>Process Lifecycle</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/0*gbMa8zMgZTDImMU6.png" /><figcaption><em>Process lifecycle</em></figcaption></figure><ul><li><strong>New: </strong>This is the initial state when a process is first started/created. In this state, OS will perform admission control, and OS will allocate and initiate process control block and some additional resources. After this, the state of the process changes to ready.</li><li><strong>Ready State: </strong>The process is waiting in a queue to be assigned to a processor. Ready processes are waiting to have the processor allocated to them by the operating system so that they can run. A process may come into this state from Start state, from the running state or from the waiting state.</li><li><strong>Running State:</strong> Once the process has been assigned to a processor by the OS scheduler, the process state is set to running and the processor executes its instructions. From this state, a number of things can happen, a running process can be interrupted and the context switch is performed and the running process will move back to the ready state. Another possibility is that the running process needs to perform some long operations like reading data from the disk, or waiting for some events, maybe some timer or taking input from the keyboard, at that time the process enters the waiting state, when the event occurs or the I/O operation completes, the process will become ready again. Finally, when the process completes all the operations in the program or when it encounters some error, it will return corresponding exit code and the process will be terminated.</li><li><strong>Terminated State: </strong>Once the process finishes its execution, or it encounters some error, it is moved to the terminated state where it waits to be removed from main memory.</li></ul><h3>Process Creation</h3><p>In the operating system, a process can create a child process. Hence all processes come from a single root in which a creating process is the parent process and the created process is the child of that process. Once the initial boot process is done the and operating system is loaded, it will create some initial process. When user logs into the system a user shell process are created, and when the user types in the command (emacs, nano, etc) then new process get spawned from that shell parent process. So the final relationship looks like a tree.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/0*HFuejvYE1mcHsCjs.jpg" /><figcaption><em>Process Creation tree</em></figcaption></figure><h3>The mechanism for process creation</h3><p>Most operating systems support two basic mechanisms for process creation</p><ul><li><strong>Fork: </strong>With this mechanism, the operating system will create a new child process with PCB and then it copies all the values of parent’s PCB to child’s PCB. After that, both the child and the parent continues their execution at instruction just after the fork call because both processes contain exact same values in their PCB which also includes program counter.</li><li><strong>Exec: </strong>This<strong> </strong>replaces the child’s image and loads the new program. Child’s PCB contains the new initialized value and program executes from the beginning.</li></ul><p>The mechanism of creating a new program is like calling the <strong>fork</strong> which creates a child process with exact same PCB as that of the parent and then calling <strong>exec</strong> which replaces the child’s image with the new program’s image.</p><h3>CPU Scheduling</h3><p>At a time there can be multiple processes waiting in the ready queue. The <strong>CPU scheduler </strong>determines which of the currently running process should be dispatched to the CPU for execution, and how long it should take.</p><p>In order to manage the CPU, the operating system must be able to <strong>preempt</strong> i.e. to interrupt the current running process and save is current context. This operation is called preemption. Then operating system must run the scheduling algorithm in order to choose the next process to run. And at last, once the process is chosen the operating system must <strong>dispatch</strong> this process to the CPU and switch to its context. OS must make sure that CPU is spending more time on running processes and not executing scheduling algorithm, dispatching, preempting or doing some other OS operations. Hence it is important to have an efficient design and as well as efficient implementation of the various algorithms involved for example scheduling. Also, efficient data structures that are required to represent waiting processes in the ready queue or any other information (like the priority of the processes, how long the algorithm ran in the past) that are relevant to make scheduling decisions.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/0*kwsCnUT8ZaR-DUob.png" /><figcaption><em>Number of ways a process can go to ready state from CPU</em></figcaption></figure><h3>Inter-process communication</h3><p>Many applications are structured as multiple processes, so these multi processes have to able to interact with each other in order to achieve a common goal. As we have already studied that operating system isolates the process from each other in order to protect each other’s memory space, OS controls the amount of CPU each application gets. So some communication mechanism is required to build without sacrificing the protection. These mechanisms are called inter-process communication (IPC). Their task is to transfer data/info between address spaces without sacrificing the protection and isolation that OS provides. As communication can vary from the continuous stream of data sharing, periodic data sharing, a single piece of data sharing, etc so the IPC mechanism has to be flexible with good performance.</p><ul><li><strong>Message-Passing IPC: </strong>In this mechanism, the operating system establishes a communication channel (like shared buffer), and processes interact with each other by writing or sending data to the channel and reading or receiving data from the channel.</li><li><em>Advantage: </em>Advantage of this mechanism is that OS manages both writing and reading data from the channel and provides APIs. So both process uses the exact same APIs.</li><li><em>Disadvantage: </em>One disadvantage of this mechanism is that data has to first copy from sending process memory space to shared channel and then back to receiving process memory space.</li><li><strong>Shared Memory IPC: </strong>In this mechanism, the OS creates a shared memory channel and then maps it to each process memory space, and then processes are allowed to read and write to the channel as if they would do to any memory space that is part of their memory space.</li><li><em>Advantage:</em> The advantage of this process is that OS is not involved in the communication.</li><li><em>Disadvantage: </em>As OS is not involved in the communication this mechanism does not support fixed and well-defined APIs for reading and writing data, so this mechanism is error-prone and sometimes the developers have to re-implement the code.</li></ul><h3>Summary</h3><p>In this post, we learned how the processes are represented in the OS, we learned about the process abstractions like address space and PCB, we learned some key mechanism that operating system supports to manage processes like process creation and scheduling. We also learned about process lifecycle, context switching, and inter-process communication.</p><h3>Next: <a href="https://medium.com/@akhandmishra/operating-system-threads-and-concurrency-aec2036b90f8">Threads and Concurrency</a></h3><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=108d83e8ce60" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Operating System: Threads and Concurrency]]></title>
            <link>https://medium.com/@akhandmishra/operating-system-threads-and-concurrency-aec2036b90f8?source=rss-2514cc8a06a------2</link>
            <guid isPermaLink="false">https://medium.com/p/aec2036b90f8</guid>
            <category><![CDATA[operating-systems]]></category>
            <category><![CDATA[threads]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[concurrency]]></category>
            <dc:creator><![CDATA[Akhand Mishra]]></dc:creator>
            <pubDate>Sat, 28 Jul 2018 22:20:07 GMT</pubDate>
            <atom:updated>2018-07-28T22:34:01.362Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Definition: </strong><em>A Thread also called lightweight process, is basic CPU utilization; it compromises a thread ID, a program counter, a register set, and a stack. A thread is an entity within a process that can be scheduled for execution.</em></p><p>If we want a process to be able to execute on multiple CPUs at a time, to take advantage of the multi-core systems, the process must have several execution-context called threads. A thread is an active entity which executes a part of a process. Multiple threads execute simultaneously with each other which results in the execution of a single whole process. As several threads of a process execute at the same time they are required to maintain coordination with each other. Coordination between the threads is required in order to share system resources like I/O devices, memory, and of course CPU. The threads of a process are the part of the same virtual address space (i.e. they share all virtual to physical mapping), they share all the data, code and file. However, they will be executing the different instruction, they will be accessing the different portion of the address space or different in other ways. A different thread has a different stack, a different stack pointer register, a different <a href="https://wildcse.blogspot.in/2018/03/operating-system-process-and-process.html#programCounter">Program Counter</a>, and other registers. The <a href="https://wildcse.blogspot.in/2018/03/operating-system-process-and-process.html#PCB">Process Control Block (PCB)</a> of a multi-threaded process is more complex than a single threaded process as shown in the image below. It contains all the information which is shared among the threads and separate execution context of all threads.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/0*cgAbgEkofShV4B5z.png" /><figcaption><em>PCB: Single-threaded and multi-threaded process</em></figcaption></figure><h3>Benefits of Multi-threading</h3><p><strong>Parallelization: </strong>In multi-processor architecture<strong>, </strong>different threads can execute different instructions at a time, which result in parallelization which speeds up the execution of the process.</p><p><strong>Specialization: </strong>By specializing different threads to perform the different task, we can manage threads, for example, we can give higher priority to those threads which are executing the more important task. Also in multi-processor architecture specialization leads to the <a href="https://wildcse.blogspot.in/2018/03/operating-system-process-and-process.html#cacheishot">hotter</a> cache which improves performance.</p><p><strong>Efficient: </strong>Multi-threaded programs are more efficient as compared to multi-process programs in terms of resource requirement as threads share address space while process does not, so multi-process programs will require separate memory space allocation. Also, Multi-threaded programs incur lower overhead as inter-thread communication is less expensive.</p><p><strong>Hide Latency: </strong>As context switching among the process is a costly operation, as all the threads of a process share the same virtual to physical address mapping and other resources, context switch among the thread is a cheap operation and require less time. When the number of thread is greater than the number of CPU and a thread is in idle state (spending the time to wait for the result of some interrupt) and its idle time is greater than two times the time required for switching the context to another thread, it will switch the switch context to another thread to hide idling time.</p><p><em>Operating system support threads by various abstractions</em></p><ul><li>A <em>data structure</em> is needed to store thread information which allows us to identify specific thread and keeps track of their resource usage.</li><li>Mechanism to <em>create</em> and <em>manage</em> threads.</li><li>Mechanism to safely <em>coordinate</em> among the threads running concurrently in the same address space. As threads share virtual and physical address spaces, the concurrently running threads may override each other’s input or each other’s result. It might also be possible that one thread is reading the input while other is writing at the same time.</li></ul><h3>Synchronisation Mechanisms:</h3><p>To deal with concurrency issues a mechanism is needed to execute threads in an exclusive manner to ensure threads access data and other resources one at a time, for this, we use a<strong> mutex </strong>which is nothing but mutual exclusion object which allows multiple threads to share resources like file access or memory access, but not simultaneously.</p><p>A waiting mechanism is also needed for threads which are waiting for other threads to complete, specifying what are they waiting for so that they are not required to keeps on checking whether they are allowed to execute the operation or not, they will be notified whenever they are allowed. This type of inter-thread coordination is generally handled by <strong>condition variable</strong>.</p><h4>Thread and Thread Creation</h4><p>A thread data structure contains information about thread identity, register values like program counter, stack pointer, etc, stack and other attributes which help thread management system to manage threads like scheduling threads, debugging threads, etc.</p><p>For thread creation, Birrell proposes a <strong>Fork call</strong> with two parameters <strong>proc</strong> and <strong>args</strong>, where proc is the procedure the created thread will start executing with the arguments args. When a thread calls a fork, a new thread is created i.e. a new thread data structure is created with its program counter initialized to the first instruction of the <em>proc</em> argument and args arguments are available on its stack. After the end of execution of the child thread, the result is returned to the parent thread for this we need another mechanism. Birrell also proposes a <strong>Join</strong> <strong>call </strong>which takes thread Id as an argument. When Join is called from parent thread after fork call with child’s thread id as an argument, it blocks the parent until the child completes its execution, the join call also returns the child’s result to the parent thread, at that point all the resources that were allocated to the child is freed and execution of child thread is terminated.</p><h4>Mutual exclusion API</h4><p>For mutual exclusion of execution of concurrent threads, the operating system supports <strong>mutex. </strong>A mutex is like a lock which is used whenever the thread is accessing the data or resources that are shared among different threads. When a thread locks a mutex it has exclusive access to the shared resources. Other threads attempting to lock the mutex (as other threads may also need to perform operations that require exclusive access to shared resources) are not going to succeed and have to wait till the thread which has locked the mutex (i.e. the owner thread) completes its task. The <em>data structure</em> of mutex at least contains information about its lock status (whether the mutex is locked or not), list of all the blocked threads which are waiting for the mutex to be free, i.e. the owner thread to complete its work. The mutex data structure must also maintain some information about the owner thread.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/191/0*svSRhEy1eRPopIzf.jpeg" /><figcaption><em>Mutex Data Structure</em></figcaption></figure><p>The portion of the code performed by the thread under the locked state of the mutex is called <strong>critical section. </strong>The critical section contains the code which performs the operations which require only one thread at a time to perform. When the owner thread exits the critical section it releases the mutex and other waiting thread can lock it for their exclusive access to shared resources.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/533/0*_4XbusS7y0Ob1-ZE.jpeg" /><figcaption><em>Birrell’s and Common Mutex Lock API</em></figcaption></figure><h4>Conditional Variable API</h4><p>To represent a conditional variable there is a data structure to represent a conditional variable. This data structure contains a reference to the corresponding mutex for implementation of <em>wait </em>API and list of all the waiting threads for implementation of <em>signal</em> and <em>broadcast</em> API. There is a <strong>wait</strong> construct which takes <em>mutex</em> and <em>condition</em> as arguments. There is a <strong>Signal </strong>construct which takes condition as an argument and notifies one waiting thread on the fulfillment of the condition. There is also <strong>broadcast </strong>construct which notifies all the waiting threads on the condition.</p><p>In the implementation of the wait API, first the associated mutex is released and the thread is pushed into the waiting thread list present in the data structure. Then it waits for the notification, at some point when the notification is received, it removes the thread from the waiting queue and re-acquire the mutex and finally exits the execution of wait construct.</p><h3>Deadlocks</h3><p><em>Deadlock</em> is a situation where a set of processes are blocked because each process is holding a resource and waiting for another resource acquired by some other process.</p><p>Suppose there are two thread T1 and T2. In order to accomplish their task, fun1 and fun2, for which both of them need two resources A and B. A situation may occur in which one thread say T1 acquires a resource A and T2 on another core (CPU) acquires resource B. As for the successful execution of their respective task they need another resource for which both threads will keep on waiting for another to thread to complete which is not going to happen. We call this situation a <em>Deadlock</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/214/0*RZh3p3EFGtwezBcF.jpeg" /><figcaption><em>The deadlock between two threads</em></figcaption></figure><h4>Deadlock Prevention</h4><p>Three common deadlock prevention methods are:</p><ol><li><strong>Lock Ordering:</strong> Lock ordering is done by ordering the manner in which the resources are acquired which can prevent deadlocks in some cases like above. Lock ordering is a simple yet effective deadlock prevention mechanism. However, it can only be used if you know about all locks needed ahead of taking any of the locks. This is not always the case.</li><li><strong>Lock Timeout:</strong> Another deadlock prevention mechanism is to put a timeout on lock attempts meaning a thread trying to obtain a lock will only try for so long before giving up. If a thread does not succeed in taking all necessary locks within the given timeout, it will backup, free all locks taken, wait for a random amount of time and then retry. The random amount of time waited serves to give other threads trying to take the same locks a chance to take all locks, and thus let the application continue running without locking.</li><li><strong>Deadlock Detection: </strong>Deadlock detection is a heavier deadlock prevention mechanism aimed at cases in which lock ordering isn’t possible, and lock timeout isn’t feasible. Every time a thread takes a lock it is noted in a data structure (map, graph etc.) of threads and locks. Additionally, whenever a thread requests a lock this is also noted in this data structure. When a thread requests a lock but the request is denied, the thread can traverse the lock graph to check for deadlocks.</li></ol><h3>User and Kernel threads</h3><p><strong>Kernel threads</strong> are supported within the kernel of the OS itself. All modern OSs support kernel-level threads, allowing the kernel to perform multiple simultaneous tasks and/or to service multiple kernel system calls simultaneously. There is no thread management code in the application area. Kernel threads are supported directly by the operating system. Any application can be programmed to be multithreaded. All of the threads within an application are supported within a single process.</p><p><strong>User threads</strong> are above the kernel and without kernel support. These are the threads that application programmers use in their programs. the thread management kernel is not aware of the existence of threads. The thread library contains code for creating and destroying threads, for passing message and data between threads, for scheduling thread execution and for saving and restoring thread contexts.</p><p>There are several relationships between user-level threads and kernel-level threads, three most common relationship are:</p><p><strong>One-to-one: </strong>In this model, each user-level thread is associated with a kernel-level thread. When a user process creates a user-level thread, there is a kernel-level thread either created or already present.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/0*PFeiMNQ-SrXumqCe.png" /><figcaption><em>one-to-one multi-threading</em></figcaption></figure><ul><li><em>Advantages: </em>Operating system can directly see, manage, synchronize, schedule, or block threads. Since OS already supports the threading application can directly take advantage of this support.</li><li><em>Disadvantage: </em>For every operation, the application has to do a system call which involves changing the user to kernel mode which is an expensive operation. This model is not much flexible as applications have to rely on thread support provided by OS. As OS may have limits on the number of threads it can create or may have any other policies.</li></ul><p><strong>Many-to-one: </strong>In this model, all the user level threads are mapped to a single kernel level thread. At user-level, there is a thread management library which is present at the user-level which decides which of the user-level thread will actually be mapped to the kernel thread.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/0*lR9z7vCoUQZyLu-t.png" /><figcaption>Many-to-one multi-threading</figcaption></figure><ul><li><em>Advantages: </em>As all of the management is done by thread management library present at the user-level, everything is done by user-level thread library like scheduling, synchronization, context change, etc. So no kernel support is required hence no constraints on limits, policies, etc. Also, we don’t need to do frequent system calls which is an expensive task.</li><li><em>Disadvantages: </em>OS does not have any information regarding the scheduling done at the user-level, so OS may block the entire process if one user-level thread blocks on I/O.</li></ul><p><strong>Many-to-many: </strong>In this model, some threads are mapped directly to kernel threads while in some cases many threads are mapped to a single kernel thread like many-to-one and are managed by the thread management library at user-level.<strong> </strong>This model is the best of both many-to-one and one-to-one, and have advantages of both models. However, this model requires coordination between user-level thread manager and kernel-level thread manager.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/0*EPljv37Z9VqNIhVh.png" /><figcaption><em>many-to-many multi-threading</em></figcaption></figure><h3>Summary</h3><p>In this post, we learned about threads, several mechanisms related to them like mutexes, conditional variable. We also discussed briefly deadlocks and different modes of multi-threading, their problems, their solutions, and the various design approaches.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=aec2036b90f8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Making a simple Optical Character Recognition in 12 lines of code.]]></title>
            <link>https://medium.com/@akhandmishra/making-a-simple-optical-character-recognition-in-12-lines-of-code-40bc17624215?source=rss-2514cc8a06a------2</link>
            <guid isPermaLink="false">https://medium.com/p/40bc17624215</guid>
            <category><![CDATA[computer-vision]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[microsoft]]></category>
            <dc:creator><![CDATA[Akhand Mishra]]></dc:creator>
            <pubDate>Mon, 23 Jul 2018 18:40:51 GMT</pubDate>
            <atom:updated>2018-07-25T08:44:22.929Z</atom:updated>
            <content:encoded><![CDATA[<p>Optical character recognition is the recognition of typed, handwritten or printed text and converting them into text. OCR can be used to automate various task involving humans, like in banking, OCR is being used to process checks without human involvement, generating content of documents from their scanned images, it can also be helpful for visually impaired people, etc.</p><p>For this OCR we’ll be using <strong>Microsoft’s Computer Vision API</strong>. We’ll do a post request in python and in response, we’ll get output in JSON format.</p><p>To get started you are required to have a Microsoft account, and after that, you can get a free subscription to computer vision API for 30 days. You have to acquire your secret subscription key which looks similar to this 98f714r6vb2e193018b28fg1u9b3b0d7e7.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/16ada3bb428380d2a607bcc293c8b319/href">https://medium.com/media/16ada3bb428380d2a607bcc293c8b319/href</a></iframe><p>Microsoft OCR API is quite flexible and we can define many parameters depending upon our use cases, here we defining two parameters, the language which is English in our case (defined by ‘unk’) and whether to detect the orientation of text or not, which is defined as true in our case. We also need URL of the image on which we want to run our OCR (we can also upload a local image for OCR), so we’ll define url of the image.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/199ec93e3883c277ea07a3e227f94d51/href">https://medium.com/media/199ec93e3883c277ea07a3e227f94d51/href</a></iframe><p>Following is the image at above link</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*e-M_Pl79qZYtqwg1.jpg" /></figure><p>Now we’ll import requests for making a post request mentioning ocr_url, headers, params and json.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7cef392b6f6eaadac2d93d2232946bd9/href">https://medium.com/media/7cef392b6f6eaadac2d93d2232946bd9/href</a></iframe><p>The JSON output of the above script contains data about bounding box coordinates, orientation and text angle, for each word line by line. Here’s the output.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4da2d38fae026d556ff23ac2c161cbe6/href">https://medium.com/media/4da2d38fae026d556ff23ac2c161cbe6/href</a></iframe><p>Enjoy!</p><p><strong>P.S:</strong> Just in case if you need any clarification do post a comment.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=40bc17624215" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>