Re: 'hacking' some of XFCE goodies

Ondrej Mihalyi <[email protected]>
Newsgroups gmane.comp.desktop.xfce.goodies.devel
Organization MFF UK Praha
Message-ID <20050918111835.00004a0c@ACER_ONDRA>
When you say you fork and then call system(), it was not what I meant.  More direct way is to call a variation of a function exec to run the top program (execl, execv, ... try 'man 3 exec' into console).  The advantage is that you spawn only the forked process, which upon calling something like execlp("top", "top", "options", "moreoptions", NULL) will replace your code in the forked child by the code of 'top' and execute it with given parameters.  (don't forget, that first parameter in unixes starts with the path to file name of the executable, therefore I wrote "top" twice in the argument list of function execlp)

BUT remember, 'top' doesn't have an option to save output to a file, so you have to redirect it's output to a file.
After fork(), but before exec(), you must open a file by function open() (similar to fopen, but returns integer - file descriptor instead of FILE*), call close(1) (close standard output), call dup2(filedesc, 1), close(filedesc).

Code snippet:
fork();
int fd = open("filename", O_CREAT|O_WRONLY|O_TRUNC);
close(1);
dup2(fd, 1);
close(fd);
execlp("top", "top", "options", "moreoptions", NULL);

Then you don't have to kill the new process, just in your main program call the function waitpid(pid_of_forked_process, &status, flags) to get rid of the defunct process (zombie) - you always have to call some variation of wait() function after calling fork to get rid of the zombie process.

If you set flags in waitpid to WNOHANG, waitpid becomes non blocking and you can periodically check if your spawned process finished and only then reread the top.lista file.

Even better way is to use system pipes in your program to directly read the output of top (not through file), but it's more complicated.  

I really hope I didn't cofuse you :) Ask me if you want more info.  Maybe you should read somewhere about linux system programming, it would sure help.

Cheers,

Ondrej

On Sat, 17 Sep 2005 19:11:42 +0200
> I had a problem with the number of "xfce4-panel <defunct>" growing to 
> milions but I set a kill()er for them and now I think it works without 
> any problems..
> The trace can be found in /tmp dir - where are the top.pid top.old and 
> top.lista files.
> 
> The forked child does a system() call to `top` to retrieve info on 
> running processes and channels it to top.lista.
> The top.pid contains the forked child's pid, to trace the status of it,
> when it's over a kill() signal is sent to get rid of the zombie.
> Then the program retrieves data from top.lista, stores the parsed 
> version in top.old and forks a new process.
> While the process is running again, the top.old is used for data source 
> and so on...
> 
> The updated (and I hope final) version is already available at the 
> standard location:
> http://szift.org/xfce/xfce4-cpugraph-plugin-szifted.tar.bz2
> 
> Have a nice run :)
> 
> --
> Best wishes
> _ukasz Hejnak
> _______________________________________________
> xfce-goodies-dev mailing list
> [email protected]
> http://lists.berlios.de/mailman/listinfo/xfce-goodies-dev
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.