Daemon problem

This is a discussion on Daemon problem within the Linux Programming forums, part of the Platform Specific Boards category; I am making program that can change wallpapers in Gnome desktop when you switch workspace and for periodic time. Assume ...

  1. #1
    Registered User gandalf_bar's Avatar
    Join Date
    Oct 2003
    Posts
    92

    Daemon problem

    I am making program that can change wallpapers in Gnome desktop when you switch workspace and for periodic time. Assume that you have 3 workspace and for each workspace ( or virtual desktop ) you have each list. So for workspace number one, you have 4 hot chicks themed wallpapers ( Britney.png, Madonna,jpg, etc ). Workspace number two, you have 3 Super Hero themed wallpapers ( Superman.jpg, Spiderman2.png, etc ). Workspace 3, you have 2 Cartoon wallpapers.

    Assume for 00:00 time, you are in workspace ( ws ) 2, you got Spiderman2.png wallpaper. If you change to ws 1, you will get Britney.png wallpaper, if you change to ws3, you will get Donald.png. But you don't change the ws. you just wait one minute. 01:00. Now you wallpaper change in your current ws( that is number 2 ) to Superman.jpg. If you change to ws 1, you will get Madonna.png wallpaper not Britney anymore, if you change to ws3, you will get MickeyMouse.png not Donald anymore.

    What is the best way to randomize this list? Threads? So for this case, I am making three threads. Thread 1 randomize 4 wallpapers. Thread 2 randomize 3 wallpapers. Thread 3 randomize 2 wallpapers. Or just forking the child process?

    That is question number one.

    This is the rough daemon source code:

    Code:
    string wallpaper;
    int workspace, dump;
    const int forever = 1;
    while( forever )
    {
           //check if the workspace change
           workspace = whatWorkspaceIsIt();
           //if still the same
           if( previous_workspace == workspace )
           {
                   //check if has passed 60 seconds
                   dump = getCurrentTime() - previousTime;                    
                   if( dump >= 60 )
                   {
                           //here we ask the information from thread or child process that randomize 
                           wallpaper = getCurrentWallpaper( workspace );  
                           changeTheWallpaper( wallpaper );
                           previousTime = getCurrentTime(); 
                    }
           }
            else
            {
                    previousTime = getCurrentTime();
                    wallpaper = getCurrentWallpaper( workspace );
                    changeTheWallpaper( wallpaper );
             }
    
             previous_workspace = workspace;
    }
    This daemon take up too much CPU usage. How do I press it down beside use nice function? How do I improve my algorithm for this daemon? To make it short, my daemon checking all the time what workspace is it. I don't surprise if the daemon has checked what workspace it is for more than ten times under one seconds. The problem is that to get clue what workspace it is, I have to forking another process to find out. Then I read the stdout from the program. By that way, only I can know what workspace it is.

    Ideally it would be better if I can hacked the workspace swithcer source code. So the wallpaper changer is included in workspace switcher. So if you change the workspace , it will automatically change the wallpaper. But it is in C. And I am still newbie...... And it is not easy to read somebody sourcecode.
    A man asked, "Who are you?"
    Buddha answered, "I am awaked."

  2. #2
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    It took up too much cpu because it is always comparing dump to 60. What you should do, when you know sixty seconds has elapsed, tell your daemon to sleep for 60 seconds. This way your program won't get scheduled and won't check dump to 60 until at least 60 seconds has passed. Do you see what I am saying?
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  3. #3
    Registered User gandalf_bar's Avatar
    Join Date
    Oct 2003
    Posts
    92
    The problem is.... when I use your approach.... :
    00:00, I am in ws 2.... my wallpaper is Superman.jpg now.
    If I wait for 60 seconds ( my daemon sleep for 60 seconds ) and I don't change ws, that would be no problem. Then my daemon wake up after 60 seconds. 01:00. Ok, time to change wallpaper now. Now my wallpaper is SpiderMan2.png. Happily ever after.
    Now here's come the problem:
    If after 15 seconds, that is 00:15, I change to ws 1, the daemon didn't change wallpaper because it is still sleeping ( it is still under 60 seconds ). But it <b> should </b> change the wallpaper to Britney.jpg or Madonna.png. Or if I change to ws 3 after 15 seconds ( under 60 seconds ), it <b> should </b> change the wallpaper to Donald.jpg or MickeyMouse.png. But it didn't because the daemon is still sleeping.
    That is the problem.
    ws = workspace or virtual desktop
    wallpaper in workspace 1 = Britney.jpg, Madonna.png ( hot chick wallpapers )
    wallpaper in workspace 2 = Superman.jpg, SpiderMan2.png ( superhero wallpapers )
    wallpaper in workspace 3 = Donald.jpg, MickeyMouse.png ( cartoon wallpapers )
    A man asked, "Who are you?"
    Buddha answered, "I am awaked."

  4. #4
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    Then you need to wake your program with a signal when you change from a ws then. I don't know how you would go about doing that, though.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. daemon problem please help
    By fairyjerry14 in forum C Programming
    Replies: 2
    Last Post: 09-07-2007, 02:43 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM

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