Thread: Run Error, Commands Executing Simultaneously..

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    2

    Run Error, Commands Executing Simultaneously..

    I have a problem with the way my actual program runs. Ive included the code for the delay function that i wrote as well, although i know the function works fine by itself.

    My problem is that when the code runs through it appears to do more than one command at the same time, and i need it to stop, finish all the current commands, then perform the next command.

    Code:
    #include <stdio.h>
    #include <sys/time.h>
    
    void Delay(double DTime);
    
    void main()
    {
    	char Word[] = "Goodbye World\n";
    	int Counter;
    	
    	printf("Hello");
    	Delay(5.0);
    	printf("World... ");
    	
    	printf("Good\n");
    	Delay(5.0);
    	printf("Day\n");
    	
    	for (Counter = 0; Counter < strlen(Word); Counter++)
    	{
    		printf("%c", Word[Counter]);
    		Delay(0.5);
    	}	
    }
    
    void Delay(double DTime)
    {
    	struct timeval TData, ETData;
    	long Milliseconds, Seconds;
    	gettimeofday(&TData, NULL);
    	Milliseconds = ((DTime * 100000) - (int)DTime) + TData.tv_usec;
    	Seconds = TData.tv_sec + (DTime / 1);
    	if (Milliseconds > 999999)
    	{
    		Milliseconds -= 1000000;
    		Seconds += 1;
    	}
    	for (gettimeofday(&ETData, NULL); ETData.tv_usec < Milliseconds; gettimeofday(&ETData, NULL));
    	for (gettimeofday(&ETData, NULL); ETData.tv_sec < Seconds; gettimeofday(&ETData, NULL));
    }

    Now This Outputs The Following:
    {5 Second Delay} HelloWorld... Good
    {5 Second Later} Day
    {0.5 Seconds Later} Goodbye World


    Ive been at this all night, so before i go to sleep i figured i should ask the experts. You guys really seem to know your stuff so i thank anyone that took the time to even just read this. Its much appreciated.
    Thank again, Raz

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    I think you should use the timer mechanism offered by the OS, or signals...this busy waiting isn't a very good way to create a timer. ie. your timer starts, and it should delay for 1 second, along comes some processes with higher priority than yours. your process is swapped out and doesn't get a chance to restart for 3 seconds. Your timer never comes. since it was swapped out, and wasn't allowed to update itself. Not to mention the processor could be doing alot better things than this. and also gettimeofday is a system call, which causes a switch to kernel mode in the processor, then a switch back once the call has succeeded, so its fairly expensive....and imagine the amount of times your calling it in say 5 seconds.

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    int main()
    {
    	char Word[] = "Goodbye World\n";
    	int Counter;
    	
    	printf("Hello");
    	Delay(5.0);
    	printf("World... ");
    	
    	printf("Good\n");
    	Delay(5.0);
    	printf("Day\n");
    	
    	for (Counter = 0; Counter < strlen(Word); Counter++)
    	{
    		printf("%c", Word[Counter]);
    		fflush(stdout);
    		Delay(0.5);
    	}	
    	getchar();
    	return 0;
    }
    use usleep (unistd.h) rather than Delay of your own which takes lots of processing time which means waste of processing time. And one more thing, u forgot to put include<string.h> in your program. You should have got a warning message from the compiler about that. Look after those sort of stuff sometime which saves you too much time on debugging stuff..

    ssharish2005
    Last edited by ssharish2005; 12-09-2006 at 06:36 PM.

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    ssharish, note that usleep(), among other functions, is rather implementation-dependant.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How I can Run exe file in C++
    By palang in forum C++ Programming
    Replies: 2
    Last Post: 05-10-2006, 11:55 AM
  2. MSVC: run w/o debugger vs run w/ debuger
    By skorman00 in forum C++ Programming
    Replies: 2
    Last Post: 01-24-2006, 09:49 PM
  3. Program to run in Dos only
    By ihsir in forum C++ Programming
    Replies: 2
    Last Post: 01-19-2002, 06:16 PM
  4. dos closes right away after executing
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 01-18-2002, 03:59 AM
  5. Trying to make rand different per program run
    By Dreamerv3 in forum C++ Programming
    Replies: 6
    Last Post: 01-18-2002, 03:26 AM