LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Closed Thread
  Search this Thread
Old 08-22-2009, 04:07 AM   #1
naveenisback
Member
 
Registered: Jun 2009
Posts: 80
Blog Entries: 1

Rep: Reputation: 16
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;
}
 
Old 08-22-2009, 09:41 AM   #2
orgcandman
Member
 
Registered: May 2002
Location: new hampshire
Distribution: Fedora, RHEL
Posts: 600

Rep: Reputation: 110Reputation: 110
did you even read the responses from your previous post, asking the exact same thing?
 
Old 08-22-2009, 09:33 PM   #3
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
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/
 
  


Closed Thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
creating a timer for each thread naveenisback Programming 4 08-22-2009 11:13 PM
creating linux timer ankushgupta Programming 8 08-15-2009 06:13 AM
Creating a countdown timer window in Fedora Core statman Linux - Software 0 06-18-2005 04:42 PM
Creating a milisecond timeout timer for threads cardias Programming 5 08-05-2004 07:18 AM
Problems creating a milisecond timeout timer for threads cardias Linux - Software 1 08-04-2004 03:10 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration