-
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.
-
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?
-
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 )
-
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.