<?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 Ravindra Bhati on Medium]]></title>
        <description><![CDATA[Stories by Ravindra Bhati on Medium]]></description>
        <link>https://medium.com/@ravindrabhati919?source=rss-303bba3f9865------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*q-PnvdayrXWN4daLmd2ryA.jpeg</url>
            <title>Stories by Ravindra Bhati on Medium</title>
            <link>https://medium.com/@ravindrabhati919?source=rss-303bba3f9865------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 06:23:15 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ravindrabhati919/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[Check Spelling in Your Code While You Type in Xcode]]></title>
            <link>https://ravindrabhati919.medium.com/check-spelling-in-your-code-while-you-type-in-xcode-1d9edea45663?source=rss-303bba3f9865------2</link>
            <guid isPermaLink="false">https://medium.com/p/1d9edea45663</guid>
            <category><![CDATA[swift-programming]]></category>
            <category><![CDATA[swiftui]]></category>
            <category><![CDATA[xcode11]]></category>
            <category><![CDATA[ios-app-development]]></category>
            <dc:creator><![CDATA[Ravindra Bhati]]></dc:creator>
            <pubDate>Fri, 24 Apr 2020 03:31:48 GMT</pubDate>
            <atom:updated>2020-04-24T03:31:48.834Z</atom:updated>
            <content:encoded><![CDATA[<p>Just wondering if there is a way to enable spell checking in Xcode.😳</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*ZDsPwvh2y-uClCyDQIOO9g.jpeg" /><figcaption>Life is like a little book written with a whole lot of surprise. spell a word that doesn’t fit in and that’s a spell in desguise.</figcaption></figure><p>Xcode finally adds the long-awaited support for spell checking. It works natively, without any extensions, framework and third-parties.</p><blockquote>Basically, everything inside your source editor can be spell checked: names of variables, methods and classes, string literals (including your localizable strings) and comments.</blockquote><p><strong>Here is how to use spell checking with Xcode</strong></p><p><strong><em>“⌘ + ;” to find next misspelled word. Works for code, strings and comments:</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lh9uGapgzfw3N9zjDVJ0HQ.png" /></figure><p><strong><em>“⌘ +</em>Shift<em>+ :” to show spelling editor with corrections.</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/716/1*mO7Dyk4GGR9YBXJyW1BABQ.png" /></figure><h4><strong><em>Always-enabled mode: from the menu open Edit &gt; Format &gt; Spelling and Grammar &gt; Check Spelling While Typing.</em></strong></h4><p>I watched video on youtube created by @<a href="https://medium.com/u/a98485461425">Sean Allen</a> -:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2Fb_HFXergIz8%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Db_HFXergIz8&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2Fb_HFXergIz8%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/022e1fa28bac2be622de9689b7c15bf5/href">https://medium.com/media/022e1fa28bac2be622de9689b7c15bf5/href</a></iframe><h3>Thanks for reading 🙌🏼</h3><p>Hope you enjoyed 👍🏻</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1d9edea45663" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[40+  CoreData Interview Questions And Answers.]]></title>
            <link>https://ravindrabhati919.medium.com/40-coredata-interview-questions-and-answers-3ff345c90347?source=rss-303bba3f9865------2</link>
            <guid isPermaLink="false">https://medium.com/p/3ff345c90347</guid>
            <category><![CDATA[database]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[interview-questions]]></category>
            <category><![CDATA[sqlite]]></category>
            <category><![CDATA[core-data]]></category>
            <dc:creator><![CDATA[Ravindra Bhati]]></dc:creator>
            <pubDate>Wed, 04 Sep 2019 07:32:19 GMT</pubDate>
            <atom:updated>2021-01-04T07:26:23.072Z</atom:updated>
            <content:encoded><![CDATA[<h3>Core-Data Interview Questions And Answers.</h3><h3>In this article, I will try to add as many Core data related to Interview Q&amp;A. I will keep on updating this article. Hope it helps you guys 🙂.</h3><figure><img alt="In the iOS Application for saving data we are using Core Data." src="https://cdn-images-1.medium.com/max/1024/1*ySUS6PGMRaEkcQcU4jZd0w.jpeg" /><figcaption>In the <strong>iOS</strong> Application for saving data, we are using Core Data<strong>.</strong></figcaption></figure><blockquote>Hello, my name is <a href="https://in.linkedin.com/in/ravindra-bhati-454548126/"><strong>Ravindra Bhati</strong></a>, please, let me throw in the <strong>disclaimer</strong>, however, that it is still too early to tell. As you will see during this or future posts, English it’s not my first language, so please if you are sensitive to grammatical crimes this is the best time for you to response on that, I ‘ll Correct as Soon.</blockquote><p>I am an IOS developer, a self-taught one, a lot of curiosity and passion for coding. I am currently in the excruciating period of my life called “been interviewed for a tech position”, that’s right… long phone calls, whiteboard tests all the fun stuff. One wrong answer during your first interviews can determine if you get the job, or just a <strong>“thank you for participate”.</strong></p><ol><li><strong><em>What is Core Data?</em></strong></li></ol><p><strong>Core Data</strong> is one of the most popular frameworks provided by Apple for iOS and macOS apps. <strong>Core data</strong> is used to manage the model layer object in our application. You can treat <strong>Core Data</strong> as a framework to save, track, modify and filter the <strong>data</strong> within iOS apps, however, <strong>Core Data</strong> is not a Database.</p><p><strong><em>2. What is an object graph in Core Data?</em></strong></p><p>An <strong>object graph</strong> is nothing more than a collection of <strong>objects</strong> that are connected with one another. The <strong>Core Data</strong> framework excels at managing complex <strong>object graphs</strong>. The <strong>Core Data</strong> framework takes care of managing the life cycle of the <strong>objects</strong> in the <strong>object graph.</strong></p><p><strong><em>3. What is NSManagedObjectContext?</em></strong></p><p>The <strong>NSManagedObjectContext</strong> object manages a collection of model objects, instances of the NSManagedObject class. It is possible for an application to have multiple managed object contexts. Each managed object context is backed by a persistent store coordinator.</p><p><strong><em>4. What is the core data stack?</em></strong></p><p>Core Data consist of following stack objects:</p><ol><li>A managed object model.</li><li>A managed object context.</li><li>A persistent store coordinator.</li><li>A persistent store (storage).</li></ol><p><strong><em>5.</em></strong> <strong><em>Is Core Data == SQLite or some wrapper?</em></strong></p><p>Core data API is provided by apple for persisting data in SQL, XML, file system. It is not same as SQLite which involves disk I/O (input/output) operations. Most of the time core data uses in-memory objects (<strong>managed object context</strong>).</p><p><strong><em>6.</em></strong> <strong><em>What types of stores does core data support?</em></strong></p><p><em>Binary</em>, <em>XML</em>, and <em>SQLite</em>.</p><p>7.<strong> <em>What is Entity Inheritance?</em></strong></p><p><em>An entity is a class definition in Core Data. The classic example is an Employee or a Company. In a relational database, an entity corresponds to a table.</em></p><blockquote><strong>NOTE:- </strong><em>Be careful with entity inheritance when working with SQLite persistent stores. All entities that inherit from another entity exist within the same table in SQLite. This factor in the design of the SQLite persistent store can create a performance issue.</em></blockquote><p><strong><em>8. What is an abstract entity in core data?</em></strong></p><p>An Entity can be <strong>abstract</strong>, in which case it is never directly attached to a managed object.</p><p>An <em>abstract</em> object (in programming) or entity (in <em>Core Data</em>) is an object or entity that is never <strong>instantiated.</strong></p><p>An entity is abstract if you will not create any instances of that entity. You typically make an entity abstract if you have a number of entities that all represent specializations of (inherit from) a common entity that should not itself be instantiated.</p><blockquote><strong><em>For example:-</em></strong> in the Employee entity, you could define Person as an abstract entity and specify that only concrete sub-entities (Employee and Customer) can be instantiated. By marking an entity as abstract in the Entity pane of the Data Model inspector, you are informing Core Data that it will never be instantiated directly.</blockquote><p><strong><em>9. What is a fetchedResultcontroller?</em></strong></p><p>A controller that you use to manage the results of a Core Data fetch request and display data to the user in <strong>UITableView</strong>.</p><p><strong><em>10. What is an NSManagedObjectId?</em></strong></p><p>A managed object ID uniquely identifies the same managed object both between managed object contexts in a single application, and in multiple applications (as in distributed systems). Identifiers contain the information needed to exactly describe an object in a persistent store (like the primary key in the database), although the detailed information is not exposed. The framework completely encapsulates the “external” information and presents a clean object oriented interface.</p><p><strong><em>11. What is Persistent store?</em></strong></p><p>A persistent store is a repository in which managed objects may be stored. You can think of a persistent store like a database data file where individual records each hold the last-saved values of a managed object. Core Data persistent store file are: <strong>binary</strong>, <strong>XML</strong>, and <strong>SQLite</strong></p><p><strong>12. What is NSPersistentStoreCoordinator?</strong></p><p>An object persists data to disk and ensures the persistent store(s) and the data model are compatible. It mediates between the persistent store(s) and the managed object context(s) and also takes care of loading and caching data. That’s right. Core Data has caching built-in.</p><blockquote>Persistent Store Coordinator is actually the instances of “<strong>NSPersistentStoreCoordinator”</strong> class.👈👈👈</blockquote><p>13.<strong> <em>Can we do Multithreading with core data?</em></strong></p><p>We can do multithreading in core data with multiple contexts, example background context for long-running tasks (batch inserting/fetching/updating) and update UI on the main thread.</p><p>14.<strong> <em>What is the minimum necessary classes and the relationship between them?</em></strong></p><p>We need <strong>NSManagedObject</strong>, <strong>NSManagedObjectContext</strong> and a <strong>Persistence store container</strong>. These classes/objects are the main building blocks of the core data stack.</p><p>Data operations like insert, update and delete are performed in managed object context. Managed object context can contain one or more managed object model instances. Persistence store coordinator communicates between managed object context and store.</p><p>15<strong> <em>Will you ever pass a managed object from one context to another context?</em></strong></p><p><strong><em>Or🤝🤝🤝</em></strong></p><p><strong>16. <em>How to transfer manage object from one thread to another thread?</em></strong></p><p>No, <a href="https://developer.apple.com/documentation/coredata/nsmanagedobject"><strong><em>NSManagedObject</em></strong></a> instances are not intended to be passed between queues. Doing so can result in corruption of the data and termination of the application. When it is necessary to hand off a managed object reference from one queue to another, it must be done through <a href="https://developer.apple.com/documentation/coredata/nsmanagedobjectid"><strong><em>NSManagedObjectID</em></strong></a> instances.</p><blockquote>According to the Core Data concurrency rule, each thread must have its own managed object context. This is because <a href="http://goo.gl/6EJCpN"><strong><em>NSManagedObjectContext</em></strong></a><strong><em> </em></strong>and <a href="http://goo.gl/mZQQLm"><strong><em>NSManagedObject</em></strong></a>, the two most fundamental objects in Core Data, is not thread-safe. They shouldn’t be initialized in one thread and accessed from a different thread.</blockquote><p><strong><em>17. What is lazy initialization, how does this relate to core data, situations when this can be handy?</em></strong></p><p>When working with core data it is recommended to use a lazy keyword for lazy initialization of persistent container.</p><p>Due to lazy keyword the object will be initialized only when accessed. It is memory efficient technique used with some of the classes like <strong>DateFormatter</strong>, <strong>Calendar</strong> also. It also suits a business requirement where you have a model with it’s attributes.</p><p><strong>18</strong>.<strong> <em>Can the NSPersistentStoreCoordinator have more persistent stores?</em></strong></p><p>Yes, but can’t create relationships between objects in different stores.</p><p><strong>19</strong>.<strong> <em>How to read only a few attributes of an entity?</em></strong></p><p>We can use the NSFetchRequest class’s property “setPropertiesToFetch”. We can pass an array of properties in string format in setPropertiesToFetch method.</p><p><strong>20</strong>.<strong> <em>What is NSAsynchronousFetchRequest?</em></strong></p><p>An asynchronous fetch allows developers to execute a fetch request without blocking the Managed Object Context for the duration of the fetch. As an extra feature, the asynchronous fetch is cancelable by the user and provides progress reporting through <strong>progress</strong>.</p><p>To fetch the data asynchronously in a background queue, Core Data provides the object <a href="https://developer.apple.com/documentation/coredata/nsasynchronousfetchrequest"><strong>NSAsynchronousFetchRequest</strong></a><strong>.</strong></p><p><strong><em>21. What is Faulting</em></strong> <strong><em>mechanism in Core Data?</em></strong></p><p><em>“When you retrieve an object from an </em><strong><em>NSManagedObjectContext</em></strong><em> (MOC) you can’t tell (in the normal course of its use) whether it’s a fault or a realized object”. A fault will be converted into a realized object (“fired”) automatically by the Core Data framework in most cases when it is necessary to do so,</em></p><p><strong><em>For example:- </em></strong><em>when accessing a property of the object. If you need to fire a fault yourself, you can do so by invoking its willAccessValueForKey: method with a nil argument.</em></p><blockquote><strong><em>♥♥️♥️When you retrieve an object from an NSManagedObjectContext (MOC) you can’t tell (in the normal course of its use) whether it’s a fault or a realized object*** — Yes we can, use –isFault</em></strong></blockquote><p><strong><em>22. What is NSManagedObjectModel?</em></strong></p><p>❍ it contains information about the models or entities of the object graph, what attributes they have, and how they relate to one another.</p><p>❍ The NSManagedObjectModel object knows about the data model by loading one or more data model files during its initialization.</p><p><strong><em>23. What is an Attribute?</em></strong></p><p>An attribute is a piece of information attached to a particular entity. For example, an Employee entity could have attributes for the employee’s name, position and salary. In a database, an attribute corresponds to a particular field in a table.</p><p><strong><em>24. What is the relationship?</em></strong></p><p>A <strong>relationship</strong> is a link between multiple entities. In Core Data, relationships between two entities are called <strong>to-one relationships</strong>, while those between one and many entities are called <strong>to-many relationships</strong>.</p><blockquote><strong>For example:- </strong>a Manager can have a <strong>to-many relationship</strong> with a set of employees, whereas an individual Employee will usually have a <strong>to-one relationship</strong> with his manager.</blockquote><p><strong><em>25. What is NSManagedObject?</em></strong></p><p>An <strong>NSManagedObject</strong> is a run-time representation of a Core Data entity. You can read and write to its attributes using <strong>Key-Value Coding</strong>.</p><p><strong>26. What is Concurrency?</strong></p><p>Concurrency is the ability to work with the data on more than one queue at the same time. If you choose to use concurrency with Core Data, you also need to consider the application environment.</p><p>For the most part, AppKit and UIKit are not thread-safe. In macOS in particular, Cocoa bindings and controllers are not threadsafe — if you are using these technologies, multithreading may be complex.</p><p><strong><em>There are two concurrency types a managed object context can use:-</em></strong></p><p><em>🌲 Private Queue Concurrency</em></p><p><em>🌲 Main Queue Concurrency</em></p><p><strong>27.</strong> <strong>What is Private Queue Concurrency?</strong></p><p><strong>Private 🗝 Queue</strong> specifies the context that will be associated with a private dispatch queue instead of the main queue. This is the type of queue you just used to move the export operation off of the main queue so it would no longer interfere with the UI.</p><p><strong>28.</strong> <strong>What is Main Queue Concurrency?</strong></p><p><strong>Main Queue</strong>, the default type, specifies that the context will be associated with the main queue. This type is what the main context <strong>(core data stack.mainContext)</strong> uses. Any UI operation, such as creating the fetched results controller for the table view, must use a context of this type.</p><p>Contexts and their managed objects must <strong>only</strong> be accessed from the correct queue. <strong>NSManagedObjectContext</strong> has <strong>perform(_:)</strong> and <strong>performAndWait(_:)</strong> to direct work to the correct queue.</p><p><strong>29</strong>.<strong> What is Wrappers?</strong></p><p>Wrapper libraries provide some much needed syntactic sugar and convenience methods to Core Data’s verbose and complicated APIs.</p><blockquote><strong><em>For example:-</em></strong> to insert a new managed object into a managed object context, it’s a class method.</blockquote><p><strong><em>30. What are Synchronizers in Core data?</em></strong></p><p>Whereas adapters synchronize information through an existing, general-purpose interface such as REST, synchronizers use a more direct protocol, offering better integration and performance at the expense of portability and generality.</p><p><strong>31. Clean way to save observers on Core Data objects?</strong></p><p><em>A subclass NSManagedObject for an entity and override the awakeFromFetch and awakeFromInsert methods. the awakeFromInsert gets called when you first insert the entity, so you could move your existing code to add the observers to there. The awakeFromFetch is where you would add the observers when fetching.</em></p><p><strong><em>32.</em></strong> <strong><em>What is the difference between transformable and binary data?</em></strong></p><p><em>With a transformable attribute, you read and write instances of any class that can be converted to and from NSData. The actual data storage is the same as with a binary attribute, but Core Data uses an NSValueTransformer to convert to/from NSData when necessary.</em></p><blockquote><strong><em>For example:- </em></strong><em>say your managed object has an image attribute where it would be convenient to read and write UIImage directly. Except, UIImage can’t be saved in Core Data. But UIImage can be converted to and from NSData. So, if you used a transformable attribute you could read and write UIImage while still keeping NSData in the data store.</em></blockquote><p><strong>33. How can I encrypt Core-Data contents on an iPhone?</strong></p><p><em>You can encrypt individual properties in your Core Data model entities by making them </em><a href="https://developer.apple.com/library/ios/samplecode/PhotoLocations/Introduction/Intro.html"><strong><em>transformable properties</em></strong></a><em>, then creating an NSValueTransformer subclass which will encrypt and decrypt the data for that property. While this is not the whole-database decryption that you’re looking for, it will have a much lower memory footprint than decrypting an entire database into memory. Additionally, it will allow the decryption to be done lazily, rather than all upfront, so your application will load much faster. Depending on the encryption used, I would even expect that the on-disk data accesses for loading each entity would be slower than the decryption process for the properties, so you won’t see that much of a performance penalty when accessing the properties.</em></p><blockquote>Transformable properties like this are very easy to use because you read and write to them as normal, while the encryption/decryption goes on behind the scenes.</blockquote><p><strong>34</strong>. <strong><em>How to Save custom class into Core data?</em></strong></p><p><strong>You have a couple of options:</strong></p><p>👉🏻 Don’t tell Core Data how to transform the data. In this case, Core Data will attempt to call <strong>encodeWithCoder:</strong> on your object to convert it to <strong>NSData</strong>. That’s why you get the error that mentions this method — it’s trying to call the method on your class, but that method doesn’t exist. In this case, <strong>your class must conform to NSCoding</strong> for the transformation to occur.</p><p>👉🏻 Tell Core Data on how to transform the data. In this case you create a subclass of <strong>NSValueTransformer</strong> that performs the transformation. You configure this on the attribute, either in the Core Data model editor or in code. In this case, <strong>you must have a custom transformer class</strong> that knows how to perform the transformation.</p><p><strong><em>35. What is transformable in core data?</em></strong></p><p>Transformable attributes are useful for storing non standard object types within Core Data.</p><p><em>For Example:- I provide code Below that lets you store UIImages as An Attribute within core data. The image Data is Converted to and From an NSData Instance that contains the image’s PNG Representation .this is all handled transparently for you by a custom NSValueTransformer.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/643/1*d72IvdspgQAuQT5D8ShFng.png" /></figure><p><strong><em>36 What is Uniquing?</em></strong></p><p><em>Core Data ensures that — in a given managed object context — an entry in a persistent store is associated with only one managed object. The technique is known as Uniquing. Without uniquing, you might end up with a context maintaining more than one object to represent a given record.</em></p><p><strong><em>37. What is an Entity?</em></strong></p><p><em>A Managed object model allows core data to map from records in a persistent store to managed objects that you use in your Application. The model is a collection of entity description objects(instance of </em><strong><em>NSEntityDescription</em></strong><em>).</em></p><p><strong><em>38. How to synchronize contexts?</em></strong></p><p>The Core Data framework uses notifications to notify objects of changes taking place in a managed object context. Every managed object context posts three types of notifications to notify objects about the changes taking place in the managed object context:</p><p>🌴 NSManagedObjectContextObjectsDidChangeNotification</p><p>🌴 NSManagedObjectContextWillSaveNotification</p><p>🌴 NSManagedObjectContextDidSaveNotification</p><p><strong>☄️ Managed Object Context Did Change</strong></p><p>The NSManagedObjectContextObjectsDidChangeNotification notification is broadcast every time a managed object in the managed object context changes. Every time a managed object is inserted, updated, or deleted from a managed object context, the managed object context posts an <strong>NSManagedObjectContextObjectsDidChangeNotification</strong> notification.</p><p><strong>☄️ Managed Object Context Will Save</strong></p><p>As the name of the <strong>NSManagedObjectContextWillSaveNotification</strong> notification suggests, this notification is posted before a save operation is performed.</p><p><strong>☄️Managed Object Context Did Save</strong></p><p>The managed object context performing the save operation posts an <strong>NSManagedObjectContextDidSaveNotification</strong> notification after successfully saving its changes.</p><p><strong><em>39. what is the Difference between core data and SQLite?</em></strong></p><p><strong>SQLite:</strong></p><p>🌿 SQLite is database itself like we have SQL Server.</p><p>🌿 Have Data Constraints features.</p><p><strong>🌿 </strong>Operates on data, store in Disk.</p><p>🌿 Can Drop table and Edit data Without loading them in memory.</p><p>🌿 Slow as Compared to core data.</p><p><strong>CoreData:</strong></p><p>🌱 core data is ORM(Object graph model) which create a layer between the database and the UI. It speeds up the process of interaction as we don’t have to Write Queries, just work with ORM and let ORM handled the backend. For save or retrieval of large data because of it’s abilities to handle the less processing speed of the device.</p><p>🌱 Don’t have Data Constraints if required need to implement by business logic.</p><p>🌱 Operates on in memory(data needs to be loaded from disk to memory)</p><p>🌱 Need to load entire data if we need update drop table.</p><p><strong><em>40. What is Transient Property?</em></strong></p><p><em>Transient properties are properties on an </em><strong><em>NSManagedObject</em></strong><em> that are not persisted t the object-store. They are calculated at runtime, usually on the basis of other property Values.</em></p><h3>Thank You!</h3><p>I hope you will like this. 😊 😊 😊 😊😊 Thanks for reading. 🎉🎉🎉🎉</p><p><em>If you having any query regarding this tutorial ? or want to know something more about it. get me on:- </em><a href="https://in.linkedin.com/in/ravindra-bhati-454548126/"><strong><em>@Ravindra Bhati</em></strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3ff345c90347" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Process Scheduling?]]></title>
            <link>https://ravindrabhati919.medium.com/process-scheduling-3a0240efef18?source=rss-303bba3f9865------2</link>
            <guid isPermaLink="false">https://medium.com/p/3a0240efef18</guid>
            <category><![CDATA[c-programming]]></category>
            <category><![CDATA[operating-systems]]></category>
            <category><![CDATA[process]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Ravindra Bhati]]></dc:creator>
            <pubDate>Mon, 15 Oct 2018 19:09:01 GMT</pubDate>
            <atom:updated>2018-10-15T19:09:01.602Z</atom:updated>
            <content:encoded><![CDATA[<h4>♠️♠️What is the Process Scheduling? | How to Works Process Scheduling 🚀 ?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/911/1*pffJ8cIYHiZUplW7v_T9zg.png" /></figure><p>The act of determining which process is in the <strong>ready</strong> state, and should be moved to the <strong>running</strong> state is known as <strong>Process Scheduling</strong>.⏱️⏱️</p><p>The prime aim of the process scheduling system is to keep the CPU busy all the time⏱️ and to deliver minimum response time for all programs. For achieving this, the scheduler must apply appropriate rules for swapping processes <strong>IN </strong>and <strong>OUT </strong>of CPU.</p><p>⛳⛳Scheduling fell into one of the two general categories:</p><ul><li><strong>Non Pre-emptive Scheduling:</strong> When the currently executing process gives up the CPU voluntarily.</li><li><strong>Pre-emptive Scheduling:</strong> When the operating system decides to favour another process, pre-empting the currently executing process.</li></ul><h3>♠️♠️What are Scheduling Queues?</h3><ul><li>All processes, upon entering into the system, are stored in the <strong>Job Queue</strong>.</li><li>Processes in the <strong>Ready </strong>state are placed in the <strong>Ready Queue</strong>.</li><li>Processes waiting for a device to become available are placed in <strong>Device Queues</strong>. There are unique device queues available for each I/O device.</li></ul><p>A new process is initially put in the <strong>Ready queue</strong>. It waits in the ready queue until it is selected for execution(or dispatched). Once the process is assigned to the CPU and is executing, one of the following several events can occur:</p><ul><li>The process could issue an I/O request, and then be placed in the <strong>I/O queue</strong>.</li><li>The process could create a new sub-process and wait for its termination.</li><li>The process could be removed forcibly from the CPU, as a result of an interrupt, and be put back in the ready queue.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/718/1*unMpO2GJLxKjA1qi3HzAKQ.png" /><figcaption><strong>In the first two cases, the process eventually switches from the waiting state to the ready state, and is then put back in the ready queue. A process continues this cycle until it terminates, at which time it is removed from all queues and has its PCB and resources deallocated.</strong></figcaption></figure><h3>🍎🍎Types of Schedulers</h3><p>There are three types of schedulers available:</p><ol><li>Long Term Scheduler</li><li>Short Term Scheduler</li><li>Medium Term Scheduler</li></ol><p>Let’s discuss about all the different types of Schedulers in detail:</p><p>✔️✔️<strong><em>Long Term Scheduler :- </em></strong>Long term scheduler runs less frequently. Long Term Schedulers decide which program must get into the job queue. From the job queue, the Job Processor, selects processes and loads them into the memory for execution. Primary aim of the Job Scheduler is to maintain a good degree of Multi-programming. An optimal degree of Multi-programming means the average rate of process creation is equal to the average departure rate of processes from the execution memory.</p><p>✔️✔️<strong><em>Short Term Scheduler :- </em></strong>The <strong>short</strong>-<strong>term scheduler</strong>(also known as the CPU <strong>scheduler</strong>) decides which of the ready, in-memory processes is to be executed (allocated a CPU) after a clock interrupt, an I/O interrupt, an <strong>operating system</strong> call or another form of signal.</p><blockquote>This is also known as CPU Scheduler and runs very frequently. The primary aim of this scheduler is to enhance CPU performance and increase process execution rate. 🙂</blockquote><p>✔️✔️<strong><em>Medium Term Scheduler :- </em></strong>This scheduler removes the processes from memory (and from active contention for the CPU), and thus reduces the degree of multi-programming. At some later time, the process can be reintroduced into memory and its execution van be continued where it left off. This scheme is called <strong>swapping</strong>. The process is swapped out, and is later swapped in, by the medium term scheduler.</p><p>Swapping may be necessary to improve the process mix, or because a change in memory requirements has over-committed available memory, requiring memory to be freed up. This complete process is descripted in the below diagram:</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*G8d_JQqiHpKJ9z0ArLXQQg.png" /><figcaption><strong>Addition of Medium-term scheduling to the queueing diagram.</strong></figcaption></figure><h3>🎯🎯What is Context Switch?</h3><ol><li>Switching the CPU to another process requires <strong>saving</strong> the state of the old process and <strong>loading </strong>the saved state for the new process. This task is known as a <strong>Context Switch</strong>.</li><li>The <strong>context</strong> of a process is represented in the <strong>Process Control Block(PCB)</strong> of a process; it includes the value of the CPU registers, the process state and memory-management information. When a context switch occurs, the Kernel saves the context of the old process in its PCB and loads the saved context of the new process scheduled to run.</li><li>Context switch time is <strong>pure overhead</strong>, because the <strong>system does no useful work while switching</strong>. Its speed varies from machine to machine, depending on the memory speed, the number of registers that must be copied, and the existence of special instructions(such as a single instruction to load or store all registers). Typical speeds range from 1 to 1000 microseconds.</li><li>Context Switching has become such a performance <strong>bottleneck</strong> that programmers are using new structures(threads) to avoid it whenever and wherever possible.</li></ol><h3>Operations on Process</h3><p><strong>Process</strong> termination is an <strong>operation</strong> in which a <strong>process</strong> is terminated after the execution of its last instruction. This <strong>operation</strong> is used to terminate or end any <strong>process</strong>. When a <strong>process</strong> is terminated, the resources that were being utilized by the <strong>process</strong> are released by the <strong>operating</strong> system.🎯🎯</p><blockquote><strong>Below we have discussed the two major operation Process Creation and Process Termination</strong><em>.</em>🍎🍎</blockquote><h3>Process Creation</h3><p>Through appropriate system calls, such as fork or spawn, processes may create other processes. The process which creates other process, is termed the <strong>parent</strong> of the other process, while the created sub-process is termed its <strong>child</strong>.</p><p>Each process is given an integer identifier, termed as process identifier, or PID. The parent PID (PPID) is also stored for each process.</p><p>On a typical UNIX systems the process scheduler is termed as <strong>sched</strong>, and is given PID 0. The first thing done by it at system start-up time is to launch <strong>init</strong>, which gives that process PID 1. Further Init launches all the system daemons and user logins, and becomes the ultimate parent of all other processes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/859/1*pUpHgVRLqjI_-QLNJGwQwQ.png" /></figure><p>A child process may receive some amount of shared resources with its parent depending on system implementation. To prevent runaway children from consuming all of a certain system resource, child processes may or may not be limited to a subset of the resources originally allocated to the parent.</p><p>🎳🎳There are two options for the parent process after creating the child :</p><ul><li>Wait for the child process to terminate before proceeding. Parent process makes a <strong>wait()</strong> system call, for either a specific child process or for any particular child process, which causes the parent process to block until the <strong>wait()</strong> returns. UNIX shells normally wait for their children to complete before issuing a new prompt.</li><li>Run concurrently with the child, continuing to process without waiting. When a UNIX shell runs a process as a background task, this is the operation seen. It is also possible for the parent to run for a while, and then wait for the child later, which might occur in a sort of a parallel processing operation.</li></ul><p><strong><em>There are also two possibilities in terms of the address space of the new process:</em></strong></p><ol><li>The child process is a duplicate of the parent process.</li><li>The child process has a program loaded into it.</li></ol><p>🔮🔮To illustrate these different implementations, let us consider the <strong>UNIX</strong> operating system. In UNIX, each process is identified by its <strong>process identifier</strong>, which is a unique integer. A new process is created by the <strong>fork</strong> system call. The new process consists of a copy of the address space of the original process. This mechanism allows the parent process to communicate easily with its child process. Both processes (the parent and the child) continue execution at the instruction after the fork system call, with one difference: <strong>The return code for the fork system call is zero for the new(child) process, whereas the(non zero) process identifier of the child is returned to the parent.</strong></p><p>Typically, the <strong>execlp system call</strong> is used after the fork system call by one of the two processes to replace the process memory space with a new program. The execlp system call loads a binary file into memory — destroying the memory image of the program containing the execlp system call — and starts its execution. In this manner the two processes are able to communicate, and then to go their separate ways.</p><p><strong><em>Below is a C program to illustrate forking a separate process using UNIX(made using Ubuntu):</em></strong></p><pre>#include&lt;stdio.h&gt;<br><br>void main(int argc, char *argv[])<br>{<br>    int pid;<br><br>    /* Fork another process */<br>    pid = fork();<br><br>    if(pid &lt; 0)<br>    {<br>        //Error occurred<br>        fprintf(stderr, &quot;Fork Failed&quot;);<br>        exit(-1);<br>    }<br>    else if (pid == 0)<br>    {<br>        //Child process<br>        execlp(&quot;/bin/ls&quot;,&quot;ls&quot;,NULL);<br>    }<br>    else<br>    {<br>        //Parent process<br>        //Parent will wait for the child to complete<br>        wait(NULL);<br>        printf(&quot;Child complete&quot;);<br>        exit(0);<br>    }<br>}</pre><p><strong>Numerical Tip:</strong> <em>If </em><strong><em>fork</em></strong><em> is called for </em><strong><em>n</em></strong><em> times, the number of child processes or new processes created will be: </em><strong><em>2n – 1</em></strong><em>.</em>🐎🐎</p><h3><strong>Process Termination</strong></h3><p>😍😍By making the exit(system call), typically returning an int, processes may request their own termination. This int is passed along to the parent if it is doing a wait(), and is typically zero on successful completion and some non-zero code in the event of any problem.</p><p>Processes may also be terminated by the system for a variety of reasons, including :</p><ul><li>The inability of the system to deliver the necessary system resources.</li><li>In response to a KILL command or other unhandled process interrupts.</li><li>A parent may kill its children if the task assigned to them is no longer needed i.e. if the need of having a child terminates.</li><li>If the parent exits, the system may or may not allow the child to continue without a parent (In UNIX systems, orphaned processes are generally inherited by init, which then proceeds to kill them.)</li></ul><p>When a process ends, all of its system resources are freed up, open files flushed and closed, etc. The process termination status and execution times are returned to the parent if the parent is waiting for the child to terminate, or eventually returned to init if the process already became an orphan.</p><p>The processes which are trying to terminate but cannot do so because their parent is not waiting for them are termed <strong>zombies</strong>. These are eventually inherited by init as orphans and killed off.😍😍</p><h3>Thank You !</h3><p>I hope you will like this. 😊 Thanks for reading. 🎉</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3a0240efef18" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>