Problem Clearing Terminal

This is a discussion on Problem Clearing Terminal within the C Programming forums, part of the General Programming Boards category; Hi - I am writing a terminal application in C on Ubuntu 8.10 and I'm having some problems when clearing ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    2

    Problem Clearing Terminal

    Hi - I am writing a terminal application in C on Ubuntu 8.10 and I'm having some problems when clearing the terminal screen to update my UI.

    I am using the "magic incantation":

    Code:
    printf("\033[2J");
    printf("\033[0;0f");
    at the beginning of my UI function so that every time the UI is printed, the terminal is initially cleared before printing so that my values are updated.

    Problem is - this seems to work for a while, like maybe 30-40 secs and then the terminal seems to stop clearing and the UI is printed continually down the terminal screen before the terminal seems to crash and not print anything more.

    Really confusing me - don't really want to have to use system(clear) as from what I gather this is a pretty amateur way to program?

    Anyone any ideas?

    **UPDATE**

    Just as an update - replaced the "magic incantation" above with

    Code:
    system("clear");
    to see what happened and it did the exact same thing. Works perfectly for about 30-40 secs and then stops clearing the terminal and prints down the screen.

    Puzzled.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That is rather strange. Are you sure that it's the clearing of the screen that goes wrong, and not something else.

    I say that because there are quite a few applications that would use this scheme (or some variant closely related), and they seem, to work fine - for example top will print the screen many times over during a day, if you keep it running all the time.

    What is the exact setup - are you by any chance connecting to a remote machine over a network or some such, where loosing a character in transmission may cause this sort of problem.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    2
    hey thanks for your reply.

    the program has three threads.

    One to populate a link list of structs with data - its pulling in 802.11 packets and stripping some data from them.

    The second thread calculates some metrics from the data in these linked lists roughly every 5 seconds.

    Then the third thread runs the function seen below to print the UI - and yeah the problem is like I said above.

    Code:
    void *printGUI()
    {
    	while(1)
    	{	
    		//printf("\033[2J");
    		//printf("\033[0;0f");
    
    		system("clear");
    
    		printf("ACCESS POINTS\n");
    		printf("=============\n\n");
    
    		struct accessPoint *ap;
    		//pthread_mutex_lock( &G.mutex1 );		
    		list_for_each(&G.apList.accessPoints, ap, apList)
    		{
    			
    			
    			printf("%02x:%02x\n", ap->mac[4],ap->mac[5]);
    			 
    			
    			printf("Pwr:\t\tAverage: %d\tVariance: %d\tStanDev: %lf\n",ap->avgpower, ap->varpower, ap->sdpower);
    					
    
    		}
    		//pthread_mutex_unlock( &G.mutex1 );	
    
    		
                    printf("USERS\n");
                    printf("=====\n\n");
    
                    struct station *sta;
    		//pthread_mutex_lock( &G.mutex1 );
    		list_for_each(&G.staList.stations, sta, staList)
    		{
    			printf("%02x:%02x\n", sta->mac[4],sta->mac[5]);
    			
    			
    			printf("Pwr:\t\tAverage: %d\tVariance: %d\tStanDev: %lf\n",sta->avgpower,sta->varpower, sta->sdpower);
    		
    		}
    		//pthread_mutex_unlock( &G.mutex1 );
    
    		sleep(2);
    		
    		
    	}
    }

  4. #4
    Maz
    Maz is offline
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    This sounds like on of the following:
    1. Memory leak => running out of memory at some point
    2. filedescriptor (or some other leak)
    3. deadlock.

    My suggestions:
    1. run with valgrind (which can help, but does not give absolute truth)
    2. Check that you check the returnvalues!!
    3. Most common mistakes I've fallen into:

    Code:
    void func(int foo)
    {
    //we're going to modify some global data => lock
        sem_wait() or mutex_lock()
        //we should not accept foo which is larger than X
        if(foo>X)
            return;
        //do stuff
        sem_post() or mutex_unlock()
    }
    Here we do not unlock mutex (or post semaphore) when we exit function after some errorcondition...

    and
    Code:
    void func1(void)
    {
    
        //here we use many different resources, protected by mutexes
        mutex_lock( MUTEX1 );
    
        //do things
    
        mutex_lock( MUTEX2 );
    
        //do more things
        mutex_unlock( MUTEX2);
        //do something
        mutex_unlock( MUTEX1);
    }
    
    void func2(void)
    {
         //here we use same resources as in abowe func
         mutex_lock(MUTEX2);
         //do something
         mutex_lock( MUTEX1);
         //do something
         mutex_unlock( MUTEX1 );
         // do something
         mutex_unlock( MUTEX2 );
    }
    What's wrong with latter example? It seems fine. We lock and unlock mutexes just fine, right?

    Not quite. Think what happens if our program proceeds as follows:

    thread1:
    executing func1
    locking MUTEX1

    <context switch>

    thread 2
    executing func2
    locking MUTEX2
    .
    .
    .
    waiting MUTEX1

    <context switch>

    thread1:
    continuing func1
    .
    .
    .
    waiting MUTEX2

    But but but. Now MUTEX2 is locked by thread2, which is waiting for MUTEX1, locked by thread1... And this happens only when scheduling happens to interrupt thread 1 or 2 between locking mutexes, and allowing the other thread to lock the other mutex... Oh, the joys of randomness...



    EDIT:

    but it is impossible to say without digging through the whole code with sharp eyes... One of the reasons why I hate threads so much nowadays.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  3. Running Program Problem
    By warfang in forum C++ Programming
    Replies: 10
    Last Post: 03-28-2007, 02:02 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  5. Problem clearing screen
    By tim545666 in forum C++ Programming
    Replies: 9
    Last Post: 02-10-2002, 09:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21