-1

I want to call a function which creates files periodically on every 5 seconds in C. The function code is below which creates files.

void file_create(int time) {
        snprintf(filename, sizeof(filename),"%s_%d", "data", index);
        FILE *fp = fopen (filename, "w");
        index++;
}

file name will be data_0, data_1, data_2 and goes on on every call.

how to call the above function periodically every 5 seconds from main function if i put an infinite loop like below

while(1){
    file_create();
    Sleep(5000);
}

this is not allowing to execute the lines below the while loop.

How to achieve this by calling above function periodically without holding execution of rest of the program ?

paulsm4
  • 114,292
  • 17
  • 138
  • 190
Vimalan E
  • 351
  • 3
  • 12
  • Did you think about using a separate thread or does it have to be in the same thread? – Gerhardh Jun 02 '20 at 13:59
  • 1
    Are you aware of [timerfd_create](https://man7.org/linux/man-pages/man2/timerfd_settime.2.html) ? – Gerhardh Jun 02 '20 at 14:01
  • @Milag Uppercase means Windows and Microsoft's C lib plus Windows API. Glibc won't be available. – Lundin Jun 02 '20 at 14:05
  • 1
    Everyone says multithreading but you can also use an event loop. Which are also large topics. Look up libuv. – Qix - MONICA WAS MISTREATED Jun 02 '20 at 14:05
  • Please mark the posting with some windows tag. – Milag Jun 02 '20 at 14:13
  • It looks like you're on Windows. Sleep(() is a [blocking call](https://en.wikipedia.org/wiki/Blocking_(computing)), which explains why you're "not executing the lines below the while loop". You definitely want to consider [Threads](https://www.bogotobogo.com/cplusplus/multithreading_win32A.php), or [Timers](https://learn.microsoft.com/en-us/windows/win32/winmsg/using-timers). [Here](https://stackoverflow.com/questions/12280224/how-to-set-a-timer-in-a-console-win32-app) is a link for using Windows Timers in a console mode all (where there's no Windows event loop). – paulsm4 Jun 02 '20 at 17:02
  • ALSO: you have a RESOURCE LEAK. Be sure to call `fclose()` for every time you open!!!! – paulsm4 Jun 02 '20 at 17:03

2 Answers2

0

You are looking for a multithread system. You have to create one task which executes the mentioned while loop and another one which executes the other parts of your current programm.

KDD2020
  • 49
  • 6
0

You do this with multi-threading, which is a big topic by itself. I'd recommend to study how POSIX threads (pthreads) work before anything else.

In Windows, that means that the thread should waiting for a custom event "close the thread" for 5000ms. If you get the event, stop the thread. Otherwise in case of timeout, perform the periodic task. Windows uses a different thread API than POSIX.

And also different Sleep(ms) (Windows) vs sleep(s) (POSIX) functions...

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • (Also in raw Windows API, there's a "poor man's timer" with the WM_TIMER event, which I wouldn't really recommend to use.) – Lundin Jun 02 '20 at 14:02