{"id":142,"date":"2023-06-28T06:23:59","date_gmt":"2023-06-28T06:23:59","guid":{"rendered":"http:\/\/localhost\/embeddedwala\/?p=142"},"modified":"2023-07-27T04:16:11","modified_gmt":"2023-07-27T04:16:11","slug":"multiple-delays-using-one-timer-in-c-a-crucial-interview-question-solution","status":"publish","type":"post","link":"https:\/\/embeddedwala.com\/beta\/codes\/multiple-delays-using-one-timer-in-c-a-crucial-interview-question-solution\/","title":{"rendered":"Multiple Delays Using One Timer in C: A Crucial Interview Question Solution"},"content":{"rendered":"\t\t<div data-elementor-type=\"wp-post\" data-elementor-id=\"142\" class=\"elementor elementor-142\" data-elementor-post-type=\"post\">\n\t\t\t\t\t\t\t\t\t<section data-particle_enable=\"false\" data-particle-mobile-disabled=\"false\" class=\"elementor-section elementor-top-section elementor-element elementor-element-98d9d82 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"98d9d82\" data-element_type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-d2984c3\" data-id=\"d2984c3\" data-element_type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t\t\t<section data-particle_enable=\"false\" data-particle-mobile-disabled=\"false\" class=\"elementor-section elementor-inner-section elementor-element elementor-element-2e81bb7 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"2e81bb7\" data-element_type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-inner-column elementor-element elementor-element-a8b35c0\" data-id=\"a8b35c0\" data-element_type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t\t\t<div class=\"elementor-element elementor-element-f3ffe8e elementor-widget elementor-widget-text-editor\" data-id=\"f3ffe8e\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<h2 id=\"Introduction\"><span class=\"ez-toc-section\" id=\"1-Introduction\"><\/span><strong>1. Introduction<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2><p>Delays and precise timing are essential aspects of many applications, ranging from real-time systems to task scheduling. To efficiently handle delays and execute tasks based on specific time intervals, the use of timers is crucial. In this article,\u00a0<strong>we have a interview question<\/strong>.\u00a0<strong>Presented with a code snippet that simulates the behavior of MCU timer functions using standard C timer functions.<\/strong>\u00a0The code demonstrates the implementation of a timer-based delay mechanism, allowing precise timing control in a simulated environment.<\/p><div id=\"ez-toc-container\" class=\"ez-toc-v2_0_51_1 ez-toc-wrap-left counter-hierarchy ez-toc-counter ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title ez-toc-toggle\" style=\"cursor: pointer\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\" role=\"button\"><label for=\"item-69e1cab6498e7\" ><span class=\"\"><span style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #337ab7;color:#337ab7\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #337ab7;color:#337ab7\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input aria-label=\"Toggle\" aria-label=\"item-69e1cab6498e7\"  type=\"checkbox\" id=\"item-69e1cab6498e7\"><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/embeddedwala.com\/beta\/codes\/multiple-delays-using-one-timer-in-c-a-crucial-interview-question-solution\/#1-Introduction\" title=\"1. Introduction\">1. Introduction<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/embeddedwala.com\/beta\/codes\/multiple-delays-using-one-timer-in-c-a-crucial-interview-question-solution\/#2-Overview-of-the-Code\" title=\"2. Overview of the Code\">2. Overview of the Code<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/embeddedwala.com\/beta\/codes\/multiple-delays-using-one-timer-in-c-a-crucial-interview-question-solution\/#3-Understanding-the-Timer-Handling-Mechanism\" title=\"3. Understanding the Timer Handling Mechanism\">3. Understanding the Timer Handling Mechanism<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/embeddedwala.com\/beta\/codes\/multiple-delays-using-one-timer-in-c-a-crucial-interview-question-solution\/#4-Setting-up-Timers\" title=\"4. Setting up Timers\">4. Setting up Timers<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/embeddedwala.com\/beta\/codes\/multiple-delays-using-one-timer-in-c-a-crucial-interview-question-solution\/#5-Processing-Delays\" title=\"5. Processing Delays\">5. Processing Delays<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/embeddedwala.com\/beta\/codes\/multiple-delays-using-one-timer-in-c-a-crucial-interview-question-solution\/#6-Handling-Delay-Overflow\" title=\"6. Handling Delay Overflow\">6. Handling Delay Overflow<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/embeddedwala.com\/beta\/codes\/multiple-delays-using-one-timer-in-c-a-crucial-interview-question-solution\/#7-Code-Optimization\" title=\"7. Code Optimization\">7. Code Optimization<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/embeddedwala.com\/beta\/codes\/multiple-delays-using-one-timer-in-c-a-crucial-interview-question-solution\/#8-Usage-Example\" title=\"8. Usage Example\">8. Usage Example<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/embeddedwala.com\/beta\/codes\/multiple-delays-using-one-timer-in-c-a-crucial-interview-question-solution\/#9-Conclusion\" title=\"9. Conclusion\">9. Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2 id=\"Overview of the Code\"><span class=\"ez-toc-section\" id=\"2-Overview-of-the-Code\"><\/span><strong>2. Overview of the Code<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2><p>The presented code focuses on efficiently processing delays using timers in C programming. It consists of functions for timer handling, delay processing, and the main function. The code aims to execute tasks based on specified time intervals with minimal overhead.<\/p><h2 id=\"Understanding the Timer Handling\"><span class=\"ez-toc-section\" id=\"3-Understanding-the-Timer-Handling-Mechanism\"><\/span><strong>3. Understanding the Timer Handling Mechanism<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2><p>The timer_handler function is responsible for handling the timer expiration event. When a timer expires, the function is triggered and prints the current delay value. This mechanism allows for precise timing and synchronization of tasks.<\/p><h2 id=\"Setting up Timers\"><span class=\"ez-toc-section\" id=\"4-Setting-up-Timers\"><\/span><strong>4. Setting up Timers<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2><p>The set_timer function creates and sets timers for specific delay intervals. It utilizes functions like timer_create, timer_settime, and related timer-related structures. The struct sigevent, struct itimerspec, and timer_t data structures play a crucial role in configuring and managing timers.<\/p><p>\u00a0<\/p><p><strong>Code conveys how the C timer functions are utilized to mimic the behavior of hardware timers typically found in microcontroller units (MCUs). Assess the logic behind the code, identify the key components, and discuss how the simulated timers and interrupt handling are employed to achieve precise delays.<\/strong><\/p><p>This question aims to assess your understanding of timer mechanisms, interrupt handling, and your ability to adapt and simulate MCU-specific functionality in a software-based environment.<\/p><h2 id=\"Processing Delays\"><span class=\"ez-toc-section\" id=\"5-Processing-Delays\"><\/span><strong>5. Processing Delays<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2><p>The process_delays function is designed to efficiently process a queue of delay values. It iterates through the queue, identifies the smallest delay value, and sets a timer accordingly. This ensures that delays are processed in an optimal order, minimizing overall execution time.<\/p><h2 id=\"Handling Delay Overflow\"><span class=\"ez-toc-section\" id=\"6-Handling-Delay-Overflow\"><\/span><strong>6. Handling Delay Overflow<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2><p>To prevent delays from causing excessive execution time, the code includes a maximum elapsed time limit. The elapsed_time variable keeps track of the overall elapsed time. If the maximum elapsed time is reached, the delay processing loop breaks, preventing indefinite execution.<\/p><h2 id=\"Code Optimization\"><span class=\"ez-toc-section\" id=\"7-Code-Optimization\"><\/span><strong>7. Code Optimization<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2><p>The code can be further optimized by considering factors such as data structures, loop efficiency, and memory usage. One possible optimization is employing data structures that provide efficient searching for the smallest delay. Careful consideration of algorithmic complexity and code structure can lead to improved performance.<\/p><h2 id=\"Usage Example\"><span class=\"ez-toc-section\" id=\"8-Usage-Example\"><\/span><strong>8. Usage Example<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2><p>A usage example can be demonstrated by initializing the time_queue array with specific delay values. The process_delays function is then called to process the delays and execute tasks accordingly. The expected output should reflect the proper execution of tasks at the specified time intervals.<\/p><p><strong>Code :<\/strong><\/p><div><pre>#include &lt;stdio.h&gt;<br \/>#include &lt;stdint.h&gt;<br \/>#include &lt;time.h&gt;<br \/>#include &lt;signal.h&gt;<br \/>\nvolatile sig_atomic_t trigger_done = 0;\nint current_delay = 0;\nint elapsed_time = 0;\nint max_elapsed_time = 30; \/\/ Maximum elapsed time in seconds\n\nvoid timer_handler(int sig) {\n    printf(\"Time Delay: %d\\n\", current_delay);\n    trigger_done = 1;\n}\n\n\/*\n    This timer function is intented to be the hardware timer function\n    in the MCU\n*\/\nvoid set_timer(uint8_t time_out) {\n    struct sigaction sa;\n    struct sigevent sev;\n    struct itimerspec its;\n    timer_t timerid;\n\n    sa.sa_handler = timer_handler;\n    sa.sa_flags = 0;\n    sigemptyset(&amp;sa.sa_mask);\n    sigaction(SIGALRM, &amp;sa, NULL);\n\n    sev.sigev_notify = SIGEV_SIGNAL;\n    sev.sigev_signo = SIGALRM;\n    sev.sigev_value.sival_ptr = &amp;timerid;\n    timer_create(CLOCK_REALTIME, &amp;sev, &amp;timerid);\n\n    its.it_value.tv_sec = time_out;\n    its.it_value.tv_nsec = 0;\n    its.it_interval.tv_sec = 0;\n    its.it_interval.tv_nsec = 0;\n    timer_settime(timerid, 0, &amp;its, NULL);\n}\n\nvoid process_delays(uint8_t timer_queue[], uint8_t size) {\n    uint8_t sub = 0;\n    for (uint8_t trav_delays = 0; trav_delays &lt; size; trav_delays++) {\n        trigger_done = 0;\n        uint8_t smallest_idx = 0;\n        int min = timer_queue[0] - sub;\n        uint8_t i = 0;\n        for (i = 0; i &lt; size; i++) {\n            if ((min &gt; (timer_queue[i] - sub)) &amp;&amp; ((timer_queue[i] - sub) &gt; 0) || min &lt;= 0) {\n                min = timer_queue[i] - sub;\n                smallest_idx = i;\n            }\n        }\n        current_delay = timer_queue[smallest_idx];\n        if (min &lt; 0) {\n            break;\n        }\n        else {\n            set_timer(min);\n        }\n        while (trigger_done == 0);\n\n        sub += min;\n        elapsed_time += min;\n    }\n}\n\nint main() {\n    uint8_t time_queue[] = {9, 4, 3, 9, 1, 1, 2};\n    process_delays(time_queue, sizeof(time_queue) \/ sizeof(time_queue[0]));\n    return 0;\n}\n<\/pre><\/div><h2 id=\"Conclusion\"><span class=\"ez-toc-section\" id=\"9-Conclusion\"><\/span><strong>9. Conclusion<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2><p>By implementing this code, developers can efficiently handle delays and execute tasks based on precise time intervals. The use of timers provides a reliable and accurate mechanism for time-based operations. Experimentation and further optimization can be explored to tailor the code for specific requirements and performance enhancements.<\/p><p>In conclusion, understanding the efficient processing of delays using timers in C programming is essential for various applications. By exploring the details of the code implementation, developers gain valuable insights into effectively handling delays, optimizing code for performance, and executing tasks at precise time intervals. This knowledge can be applied to a wide range of projects, ensuring accurate timing and efficient task execution.<\/p>\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t\t\t\t<\/div>\n\t\t","protected":false},"excerpt":{"rendered":"<p>1. Introduction Delays and precise timing are essential aspects of many applications, ranging from real-time&#8230;<\/p>\n","protected":false},"author":1,"featured_media":143,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29],"tags":[45,124,19,123],"class_list":["post-142","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-codes","tag-code","tag-delay","tag-interview-questions","tag-timer"],"acf":[],"views":266,"featured_image_urls":{"full":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/Multiple-Delays-Using-One-Timer-in-C.png",520,350,false],"thumbnail":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/Multiple-Delays-Using-One-Timer-in-C.png",150,101,false],"medium":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/Multiple-Delays-Using-One-Timer-in-C.png",300,202,false],"medium_large":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/Multiple-Delays-Using-One-Timer-in-C.png",520,350,false],"large":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/Multiple-Delays-Using-One-Timer-in-C.png",520,350,false],"1536x1536":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/Multiple-Delays-Using-One-Timer-in-C.png",520,350,false],"2048x2048":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/Multiple-Delays-Using-One-Timer-in-C.png",520,350,false],"tp-image-grid":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/Multiple-Delays-Using-One-Timer-in-C.png",520,350,false],"covernews-slider-full":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/Multiple-Delays-Using-One-Timer-in-C.png",520,350,false],"covernews-slider-center":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/Multiple-Delays-Using-One-Timer-in-C.png",520,350,false],"covernews-featured":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/Multiple-Delays-Using-One-Timer-in-C.png",520,350,false],"covernews-medium":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/Multiple-Delays-Using-One-Timer-in-C.png",505,340,false],"covernews-medium-square":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/Multiple-Delays-Using-One-Timer-in-C.png",371,250,false],"_nx_notification_thumb":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/Multiple-Delays-Using-One-Timer-in-C.png",100,67,false]},"author_info":{"display_name":"embeddedwala","author_link":"https:\/\/embeddedwala.com\/beta\/author\/embeddedwala\/"},"category_info":"<a href=\"https:\/\/embeddedwala.com\/beta\/category\/codes\/\" rel=\"category tag\">Codes<\/a>","tag_info":"Codes","comment_count":"0","_links":{"self":[{"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/posts\/142","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/comments?post=142"}],"version-history":[{"count":4,"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/posts\/142\/revisions"}],"predecessor-version":[{"id":147,"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/posts\/142\/revisions\/147"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/media\/143"}],"wp:attachment":[{"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/media?parent=142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/categories?post=142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/tags?post=142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}