Thread: Periodic program

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    3

    Periodic program

    Hi

    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.

    Regards
    Shamnar

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Rochester, NY
    Posts
    196
    Quote Originally Posted by ranmahs View Post
    Hi

    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.

    Regards
    Shamnar
    Lookup the POSIX fork() and exec() functions if you don't mind the new functions being run under a different process (different program instance).

    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.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    3
    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..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM