I got a personal message from Ali, and I thought it would be better if it was placed here, besides the reply message is too long for the private message system to send it.
Quote:
ali wrote on 08-11-2004 10:49 AM:
Think a program: executing back OS like services. How can I make this program with C/c++. Can you give me sample.
|
A back OS service? are you thinking of a deamon running in the background and not appearing in any joblist?
What OS is this to be run under ? windows/linux/OSF2/HP-UX ?
I have a solution for the *nix world, but windows is a different matter, it tends to not follow the ANSI standard so some of the system dependant calls are implemented different according to which compiler is beeing used.
*nix ei:
Code:
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <signal.h>
/*
* Just a simple deamon making function, as suggested in
* Unix Network programming, second edition.
* fork() and close() all file descriptors with
* stderr/stdout/stdin/etc, and finaly chroot your
* self to be running on your own.
*/
void deamon_init(const char* pname, int facility)
{
int i;
unsigned long pid;
if((pid=fork())!=0)
exit(0);
setsid();
signal(SIGHUP, SIG_IGN);
if((pid=fork())!=0)
exit(0);
chdir("/");
umask(0);
for(i=0; i < MAXFD; i++)
close(i);
}
int main(int argv, char* argc[])
{
deamon_init(argv[0],0);
/*
* rest of program will have no access to stdin/stdout/stderr
* And will only appear when the OS is questioned for hidden
* processes
*/
sleep(50);
return 0;
}
Altho in the future a question like this would be better, if it were placed in the c/c++ programming section, so otheres in a simular situation can benefit from the solution