I want to write a function that shall execute periodic. means if i set time 1 sec that function should execute in each 1 sec. let us call that function func1. But i dnt want to wait inside the func1 for that 1 sec. Meanwhile i want to run another function say func2 in background . some thing like less priority function. Whnever the time come to execute the periodic function func1 it has to go to func1 and then again in the waiting time , the second function func2 will resume.
I wanted to know how to do it.. Can somebody tell me is it possible using signal in linux.
Lookup the POSIX fork() and exec() functions if you don't mind the new functions being run under a different process (different program instance).
Originally Posted by ranmahs
Those are 2 pretty easy ways to run sequential stuff (as far as processes go).
If you want to make it under the roof of the same process, google/wikipedia something called "multi-threading." That seems to be what you want to do.
For that, lookup the POSIX pthreads - using functions like pthread_join() and pthread_create() one can create and manipulate threads, setting them to do particular sub-routines.
The OS will time slice the instance (context switching) which will switch the threads and you'll have what appears to be two things happening at the same time.
NOTE: Without explicit control, such as pthread_join() or something similar, threads are NOT guaranteed a specific order of execution, EVEN IF the priority is higher. The OS may disregard that temporarily for whatever reason. Make sure you keep track of that. You will probably also need semaphores at some point, which can give you very explicit direct control over threads. This eliminates something called race conditions, which you should also lookup.
Even if you have 10 threads in mononumeric priority, there's no guarantee that they will run in the order of 1-10.
Also lookup (google/wikipedia) thread scheduling algorithms and process scheduling algorithms - as there are quite a bit of ways to manage multiple threads and processes.
Multi-threading is wonderful, and is needed in today's computing...but it can really bite you in the ass if you're not careful.
thanx 4 ur kind reply
hi.. i know the basics of thread.. but not much about the scheduling.. but unfortunately all the literature i gone through are not meeting my requirement.. means they all r sleeping or waiting till the next time slice comes..