LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   creating a timer (https://www.linuxquestions.org/questions/programming-9/creating-a-timer-749480/)

naveenisback 08-22-2009 04:07 AM

creating a timer
 
Hi to all,


I am very sad today because of timer program problem.. I will explain my requirement in code.

OS:linux
language : C


i have to create a timer and also have to keep track of time. that means after 2 min I have to display as monitor is off after 3 min i hav to display as system shutdown.. i cant use sleep, wait calls.. because I am running multithread program(~1000 threads).

I used setitimer().. but it is working for 1 timer..
The following code mat help u to know what i need.

Code:


  struct Poweroff  Power;

void shutdown_signal(int i)
{

        signal(SIGALRM, shutdown_signal);
        printf("system is shutting down \n");
}

void udpserver(int);
void logoff_signal(int i)
{
        signal(SIGALRM, logoff_signal);
                struct itimerval shutdown;
        printf("system is logging off \n");
        //udpserver(1000);
        shutdown.it_value.tv_sec = Power.Shutdowntime;
        setitimer(ITIMER_REAL, &shutdown, 0);
        signal(SIGALRM, shutdown_signal);

}

void dim_signal(int i)
{
        signal(SIGALRM, dim_signal);
        printf("system is dim \n");
}

void timers()
{
        if(Power.Logofftime < Power.Dimtime)
        {
                struct itimerval logoff;
                printf("dim is high\n");
                logoff.it_interval.tv_sec=0;
                logoff.it_interval.tv_usec = 0;

                                                 
  logoff.it_value.tv_sec = Power.Logofftime*60;

                setitimer(ITIMER_REAL, &logoff, 0);
                signal(SIGALRM, logoff_signal);
        }
        else
        {
                struct itimerval dim;
                printf("dim is low\n");
                dim.it_interval.tv_sec=0;
                dim.it_interval.tv_usec = 0;
                dim.it_value.tv_sec = Power.Dimtime*60;

                setitimer(ITIMER_REAL, &dim, 0);
                signal(SIGALRM, dim_signal);
        }
}




void idle_starts(int i)
{

        printf("idle time over(system is idle)!!!!!!!!after  sec!!!!!!!!\n");

        timers();
void fortimer(int portid)
{
        printf("portid is %d", portid);
        parse_first("currentpowersettings.xml", portid, &Power);
        printf("Dim time is %d\n", Power.Dimtime);
        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);
}

// to generate idle_time
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

        fortimer(portid);

        createsocket(portid, threadstarttime);
}

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;
}


orgcandman 08-22-2009 09:41 AM

did you even read the responses from your previous post, asking the exact same thing?

crabboy 08-22-2009 09:33 PM

Please post your thread in only one forum. Posting a single thread in the most relevant forum will make it easier for members to help you and will keep the discussion in one place. This thread is being closed because it is a duplicate.

http://www.linuxquestions.org/questi...thread-749198/


All times are GMT -5. The time now is 03:51 AM.