?
Solved

A program ends when time is expired.

Posted on 2003-02-21
7
Medium Priority
?
239 Views
Last Modified: 2010-04-15
Hi. Experts:
I want to write a program about two processes executed synchronally.One process(#1) is to count down from 60 seconds to 0 second.The other process(#2) is to ask user  many questions,to wait for user's answers and display the total of corrected results when process(#1)'s action is finished.
How to create some threads and respond to each other?
Thanks!!
0
Comment
Question by:TKD
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 1

Expert Comment

by:Managu
ID: 7998156
What purpose is this for?  What platform is this on?  Do you want the timer thread to do anything other than count down?  If not, it might be easier to just set up a signal handler with a 60 second alarm.  E.g.

#include <unistd.h>
#include <signal.h>

volatile int alarm_triggered=0;

void alarm_handler(int signum) {
 alarm_triggered=1;
}

int main() {

// do some various initialization

// set up the alarm handler
signal(SIGALRM, alarm_handler);

// set other stuff up

// start the countdown.  Set at 60 seconds
alarm(60);

// do stuff while our time hasn't expired
do {
    // stuff
} while (!alarm_triggered);

// time's up.  Do some more stuff afterwards

// ok, done
return 0;
}

The alarm(int) call sets up a timer for the given number of seconds.  When that time has expired, a SIGALRM is raised.  Thus, we set up a signal handler to receive the SIGALRM and react to it.  In this case, the handler I wrote just sets a flag.

Once 60 seconds expires, the signal handler is called, sets its flag, and then returns the program to where it left off.

I'm not sure if this works properly under Windows, but there's a good chance that it does.  ANSI C defines the signal call, and POSIX defines the alarm call.
0
 
LVL 8

Expert Comment

by:akshayxx
ID: 7998381
managu's comment should do the needful, let us know if got what he meant
0
 

Author Comment

by:TKD
ID: 7998956
hi.Managu
I work on the WIN 2k platform.
The code that you give me doesn't compile correctly.
I repeat my problem:
Two processes are executed synchronally.Not one after another.
To trigger another process to display its result when one process finished.
How could I do?
Thanks!!
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 2

Expert Comment

by:_Rob_
ID: 8422260
Here is two sample programs.

The first one creates a global event, waits five seconds, signals the event, closes the event (it still lives if other processes have "created" it too).

#include <stdio.h>
#include <windows.h>

int main( int argc, char* argv[] )
{
     HANDLE hEvent = CreateEvent( NULL, FALSE, FALSE, "SynkApps" );
     if( NULL == hEvent )
     {
          printf( "Failed to create global event!\n" );
          return 0;
     }

     Sleep( 5000 );

     // Signal to app2 that we are finished
     SetEvent( hEvent );

     CloseHandle( hEvent );

     return 0;
}

The next app creates the event (gets the same event as the first app if it was started before this), wait until the event is signaled or 30 seconds have gone by, informs you what happened and closes the event.

#include <stdio.h>
#include <windows.h>

int main( int argc, char* argv[] )
{
     HANDLE hEvent = CreateEvent( NULL, FALSE, FALSE, "SynkApps" );
     if( NULL == hEvent )
     {
          printf( "Failed to create global event!\n" );
          return 0;
     }

     printf( "Waiting for app1 to finish ... (will give up after 30 secs)\n" );
     DWORD dwReturn = WaitForSingleObject( hEvent, 30L * 1000L );
     if( dwReturn == WAIT_TIMEOUT )
     {
          printf( "I gave up on him\n" );
     }
     else
     {
          printf( "App1 finished!\n" );
     }

     CloseHandle( hEvent );

     return 0;
}

/rob
0
 

Author Comment

by:TKD
ID: 8440932
Hi:
How can I do anything else when I execute this statement[  DWORD dwReturn = WaitForSingleObject( hEvent, 30L * 1000L );]?I don't only want to wait for,
I want to do anything else and also want timer to count down synchronously.How could I to set or modify?
Use SetEvent,...
Thanks!!
0
 
LVL 20

Expert Comment

by:jmcg
ID: 10014642
Nothing has happened on this question in over 10 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
PAQ, refund points (interesting answers, but did not satisfy asker).

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0
 

Accepted Solution

by:
SpazMODic earned 0 total points
ID: 10050746
PAQed, with points refunded (70)

SpazMODic
EE Moderator
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses
Course of the Month8 days, 13 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question