software Interrupt Service routine in C for windows operating system

故事扮演 提交于 2019-12-11 19:55:58

问题


#include <stdio.h>
#include <time.h>
#include <windows.h>

void Task()
{
    printf("Hi");
}

int main ( ) {
    time_t t;
    clock_t start, end;
    long i;
    long count;
    double x = 0.0;
    count = 2;

    start = clock();

    time(&t);

    printf(ctime(&t));
    printf( "Counting to %ld\n", count );

    if(count)
    {
        Task();
    }

    end = clock();

    printf( "That took %f seconds and I counted up to %ld", (((double)(end-start)/CLOCKS_PER_SEC)), count );
    printf( "\nThat also took %d clock tics\n ", clock());
    return 0;
} 

I want to get the start time and end time taken to execute the Task function. I am trying to create interrupt for the Task function but displaying Hi in the program. I am not successful with that. So could you please anyone can guide me regarding this.


回答1:


Try starting with the Multimedia Timers. Another possible approach might be using CreateTimerQueueTimer() and friends.




回答2:


There is no way of having interrupts in user-mode, only kernel-mode drivers can service interrupt requests.

However you can have a callback function called by the OS in a periodic way. On Windows you can achieve this using the multimedia times (however declared obsolete by Microsoft) or timer queue timers (check this, for example: http://msdn.microsoft.com/en-us/library/windows/desktop/ms682485%28v=vs.85%29.aspx).

Here is an old test program I wrote that uses the Multimedia timers (obsolete but they still work on recent Windows versions...):

#include <stdio.h>
#include <windows.h>

volatile long timer = 0;

// Will be called every 1 ms
void CALLBACK timer_func(UINT uTimerID, UINT uMsg, DWORD *dwUser,
    DWORD *dw1, DWORD *dw2)
{
    timer++;
}

int main(int argc, char *argv[])
{
    MMRESULT id = timeSetEvent(1, 0, (LPTIMECALLBACK) timer_func, NULL, TIME_PERIODIC);
    printf("Waiting 10 seconds... ");
    fflush(stdout);
    Sleep(10000);
    printf("ok. Timer = %ld.\n", timer);
    timeKillEvent(id);
    return 0;
}

If you just want to precisely measure how long a function call lasts, just use QueryPerformanceCounter() and QueryPerformanceFrequency():

#include <windows.h>
#include <stdio.h>

void task()
{
    // do something...
}

int main()
{
    LARGE_INTEGER start, stop, freq;

    QueryPerformanceCounter(&start);
    task();
    QueryPerformanceCounter(&stop);
    QueryPerformanceFrequency(&freq);

    double time_len = (stop.QuadPart - start.QuadPart) / (double) freq.QuadPart;
    printf("Task length: %0.8f seconds.\n", time_len);
}



回答3:


New answer after discussion (see comments of my previous answer): you can implement an equivalent to the GetStopWatch() function you want this way:

#include <windows.h>
#include <stdio.h>
#include <stdint.h>

// assuming we return times with microsecond resolution
#define STOPWATCH_TICKS_PER_US  1

uint64_t GetStopWatch()
{
    LARGE_INTEGER t, freq;
    uint64_t val;

    QueryPerformanceCounter(&t);
    QueryPerformanceFrequency(&freq);
    return (uint64_t) (t.QuadPart / (double) freq.QuadPart * 1000000);
}

void task()
{
    // do something...
}

int main()
{
  uint64_t start = GetStopWatch();
  task();
  uint64_t stop = GetStopWatch();

  printf("Elapsed time (microseconds): %lld\n", stop - start);
}

Hope this helps.



来源:https://stackoverflow.com/questions/19771253/software-interrupt-service-routine-in-c-for-windows-operating-system

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!