Find the answer to your Linux question:
Results 1 to 2 of 2
Hi to all, I wrote a multithread program and I want to create a timer for each thread to do some set and reset kind of actions in each thread. ...
  1. #1
    Just Joined!
    Join Date
    Aug 2009
    Posts
    2

    creating multi timer for each thread in mulithread programming

    Hi to all,


    I wrote a multithread program and I want to create a timer for each thread to do some set and reset kind of actions in each thread. I cant use global variables and cant use mutex locks also.. because in my application i have to run more than 5000 threads in 8 minutes(this is not a problem now).

    i tried a sample code as follows.. I dont kno will it create n number of timer. this code may help u to understand my requirement.

    Code:
    void udpserver();
    void logoff_signal(int i)
    {
            printf("system is logging off \n");
            udpserver(1000);
    }
    
    void hibernate_signal(int i)
    {
            printf("System is Hibernating \n");
            udpserver(1000);
    }
    
    void idle_starts(int i)
    {
            printf("idle time over(system is idle)!!!!!!!!after  sec!!!!!!!!\n");
    
            struct itimerval logoff, hibernate;
    
            logoff.it_value.tv_sec = 60;
    
            hibernate.it_value.tv_sec = 120;
    
            setitimer(ITIMER_REAL, &logoff, 0);
            signal(SIGALRM, logoff_signal);
    
            setitimer(ITIMER_REAL, &hibernate, 0);
            signal(SIGALRM, hibernate_signal);
    }
    
    int random_generator()
    {
            return (rand() % MAX_RAND) + 1;
    }
    
     //thread init function
    void *thread_func(void *arg)
    {
            int portid = (int)arg;
            char* threadstarttime = timestamp(); //get thread start time
    
            struct itimerval idle;
            int randomtime = random_generator();
            printf("random time is %d\n", randomtime);
            idle.it_interval.tv_sec=0;
            idle.it_interval.tv_usec = 0;
            idle.it_value.tv_sec = randomtime;
            idle.it_value.tv_usec = 0;
    
            setitimer(ITIMER_REAL, &idle,0);  //start timer for idle time
            signal(SIGALRM,idle_starts);
    
    }
    
    
    
    int main(int argc , char* argv[])
    {
    
            pthread_t tid[atoi(argv[1])];
            int i , j;
    
            int portnu = atoi(argv[2]);
    
            if(argc != 3)
            {
                    printf("Give number of agents and starting port number as command line arguments\n");
    
                    exit(1);
            }
    
            //creating threads
            for(i = 0; i < atoi(argv[1]); i++)
            {
                    pthread_create(&tid[i], NULL, &thread_func, (void *)portnu+i);
            }
    
            for(j = 0; j < atoi(argv[1]); j++)
            {
                    pthread_join(tid[j], NULL);
            }
    
    return 0;
    }



    s this way is better or shall i use creat_timer(). And I want to pass some more arguments like portnu to signal handler idle_starts. How to do achieve all this.. If you have any idea except sleep and wait will be aprreciated. please guide me..


    thanks in advance..

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Please move this question to the Linux Programming & Scripting forum. It really isn't a RedHat/Fedora question.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...