{"id":5882,"date":"2023-05-20T00:01:18","date_gmt":"2023-05-20T07:01:18","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5882"},"modified":"2023-05-27T08:27:55","modified_gmt":"2023-05-27T15:27:55","slug":"your-own-time-delay-function","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5882","title":{"rendered":"Your Own Time Delay Function"},"content":{"rendered":"<p>I&#8217;ve written about <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=69\">my own <em>delay()<\/em> function<\/a>, which uses processor clock ticks to calculate a time delay in milliseconds. Thanks to the <em>clock_gettime()<\/em> function, I can write a new time-delay function that operates from nanoseconds &mdash; or close to it. I hope.<br \/>\n<!--more--><br \/>\n<a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5875\">Last week&#8217;s Lesson<\/a> demonstrated how the <em>clock_gettime()<\/em> function returns a nanosecond value. The call is precise, but measuring the interval between calls is subject to a delay. Therefore, a nanosecond delay function won&#8217;t be accurate to a specific nanosecond value. This limitation doesn&#8217;t mean I won&#8217;t write the thing anyway.<\/p>\n<p>Off the top of my head, I can&#8217;t think why someone would want to delay a program for a single nanosecond. And I&#8217;m not going to dive into the quagmire of how many clock cycles each CPU instruction takes per nanosecond. Regardless, this Lesson is just for fun.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2023_05_20-Lesson.c\" rel=\"noopener\" target=\"_blank\">2023_05_20-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;time.h&gt;\r\n\r\n#define MAX_N 999999999L\r\n\r\nvoid nano_delay(long ns)\r\n{\r\n    struct timespec res;\r\n    long end;\r\n\r\n    <span class=\"comments\">\/* avoid out of range values *\/<\/span>\r\n    if( ns&lt;1 || ns&gt;MAX_N )\r\n        return;\r\n\r\n    <span class=\"comments\">\/* obtain the current value *\/<\/span>\r\n    clock_gettime(CLOCK_REALTIME,&amp;res);\r\n    <span class=\"comments\">\/* calculate end time *\/<\/span>\r\n    end = res.tv_nsec + ns;\r\n    <span class=\"comments\">\/* wait for the end time *\/<\/span>\r\n    while(1)\r\n    {\r\n        clock_gettime(CLOCK_REALTIME,&amp;res);\r\n        if( res.tv_nsec &gt; end )\r\n            break;\r\n    }\r\n}\r\n\r\nint main()\r\n{\r\n    long delay;\r\n\r\n    for( delay=10; delay&lt;MAX_N; delay*=10 )\r\n    {\r\n        printf(\"Delay = %ld\\n\",delay);\r\n        nano_delay(delay);\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>nano_delay()<\/em> function accepts a <em>long int<\/em> as its argument, a delay in nanoseconds. An <em>if<\/em> test immediately weeds out values less than one and greater than the value of <code>MAX_N<\/code> or 999,999,999.<\/p>\n<p>After the <em>clock_gettime()<\/em> function fetches the current time, variable <code>end<\/code> is assigned the current nanosecond value plus the delay value (<code>ns<\/code>). An eternal <em>while<\/em> loop spins until the current nanosecond value fetched from <em>clock_gettime()<\/em> is greater than the <code>end<\/code> value.<\/p>\n<p>In the <em>main()<\/em> function, the <em>nano_delay()<\/em> function is called using powers of ten values: 10, 100, 1000, and so on. Here&#8217;s output from a sample run:<\/p>\n<p><code>Delay = 10<br \/>\nDelay = 100<br \/>\nDelay = 1000<br \/>\nDelay = 10000<br \/>\nDelay = 100000<br \/>\nDelay = 1000000<br \/>\nDelay = 10000000<br \/>\nDelay = 100000000<\/code><\/p>\n<p>I doubt anyone can notice any pauses inthe output, given that it takes the computer longer to write to the terminal than the delay. The final value may show a barely noticeable pause.<\/p>\n<p>This code contains a flaw that could disrupt the timing. For example, if the current nanosecond value stored in <code>res.tv_nsec<\/code> is 750,000,000 (commas added for readability). And the delay is 500,000,000, the value of variable <code>end<\/code> is 1,250,000,000. This value is out of range for <code>res.tv_nsec<\/code>. The <em>if<\/em> test in the endless <em>while<\/em> loop will never be true.<\/p>\n<p>I&#8217;ve run the code a few times and not had any issues, though the math puzzles me. I&#8217;ll try to address this issue in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5884\">next week&#8217;s post<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One way I put the <em>clock_gettime()<\/em> function to work is to create a delay function that operates on nanoseconds. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5882\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-5882","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5882","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5882"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5882\/revisions"}],"predecessor-version":[{"id":5896,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5882\/revisions\/5896"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5882"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5882"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5882"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}