-
problem with function
hi all, now i face with big problem. i write a program that run two functions at the same time. in my program i have one clock function which i want the clock function still run. and i still can work in my program at the same time. but i have no idea how to do that. please if anyone have a solution. thanks in advanced.
-
Can post the code?
You could use fork() or thread?
-
it long code. difficult to show you. but i can explain to you more easy. example:
i want to calculate sth. the user input number and get the result and repeat it again and again, but at the same time, i want my clock function running too.
-
Ok what is clock() function?is it clock() function from time.h?
What's that clock function going to do while you're doing getting input,repeating.etc?
-
my clock function is the clock, which it start when the program start.
00:00:00 start till program end.
-
You are not answering what I'm asking!
Is it the clock() function from standard libary time.h?
And what's the purpose the that clock function? If you just want to know how long your computation takes, it's not necessary to run them in parallel.
-
sorry for misunderstand your question. that clock function i create by myself. clock.h.
-
To run 2 pieces of code simultaneously, you have to, as Bayint Naung suggested, use something like fork() or threads. This is a considerable undertaking, so google for some tutorials.
But, if you simply want to know how long your program was running, you do something like:
Code:
int main(void)
{
time_t start, end;
start = time(NULL);
// rest of program
end = time(NULL);
printf("Execution took %ld seconds\n", end - start);
return 0;
}
-
Sounds like you could use the alarm() function. You could have it raise SIGALRM every second which could trigger your clock() function.
-
If you're talking about waiting for user-keyboard input while at the same time displaying a clock that's updating every second, then you have to start using non standard I/O functions such as kbhit(). This returns an indication of whether a key has been hit without waiting. You need to put that in a loop along with your clock displayer. Basically you are managing keyboard, mouse inputs yourself.
I used to do these things years ago with old compilers. I'm not sure if these facilities are available now.