Sponsored Content
Full Discussion: Daemon
Top Forums Programming Daemon Post 302107552 by rv1125 on Monday 19th of February 2007 06:42:42 AM
Old 02-19-2007
you must use 2 parts:
1. tcp server
2. fork() for daemonize your server

if you wish to use more than 1 tcp session from outside, than you must use fork() or pthread_create(). The pthread version is better (less memory and faster). Example code is some big, but you can find it in many places.

For go to daemon, you can use this code (create header for it and use toDaemon call):

//
// close all FDs >= a specified value
//
void
daemon::closeall(int fd) {
#ifndef SOLARIS
int fdlimit = sysconf(_SC_OPEN_MAX);
while(fd < fdlimit) close(fd++);
#endif //SOLARIS
}

// detach process from user and disappear into the background
// returns -1 on failure, but you can't do much except exit in that case
// since we may already have forked. This is based on the BSD version,
// so the caller is responsible for things like the umask, etc.
int
daemon::toDaemon(int nochdir, int noclose) {
switch(fork()) {
case 0:
break;
case -1:
return -1;
default:
_exit(0); // exit the original process
}
if(setsid() < 0) // shoudn't fail
return -1;
// dyke out this switch if you want to acquire a control tty in
// the future -- not normally advisable for daemons
switch(fork()) {
case 0:
break;
case -1:
return -1;
default:
_exit(0);
}
if(!nochdir) chdir("/");
if(!noclose) {
closeall(0);
open("/dev/null",O_RDWR);
dup(0); dup(0);
}
return 0;
}

// fork2() -- like fork, but the new process is immediately orphaned
// (won't leave a zombie when it exits)
// Returns 1 to the parent, not any meaningful pid.
// The parent cannot wait() for the new process (it's unrelated).

// This version assumes that you *haven't* caught or ignored SIGCHLD.
// If you have, then you should just be using fork() instead anyway.

int
daemon::fork2() {
pid_t pid;
int status;

if(!(pid = fork())) {
switch (fork()) {
case 0:
return 0;
case -1:
_exit(errno); // assumes all errnos are <256
default:
_exit(0);
}
}
if(pid < 0 || waitpid(pid,&status,0) < 0) return -1;
if(WIFEXITED(status)) {
if(WEXITSTATUS(status) == 0) return 1;
else errno = WEXITSTATUS(status);
} else {
errno = EINTR; // well, sort of :-)
}
return -1;
}
 

9 More Discussions You Might Find Interesting

1. Programming

daemon

I want to write background running program. How to use daemon function. Please send me source code. Thanks. (1 Reply)
Discussion started by: bat_oyu
1 Replies

2. Shell Programming and Scripting

Creating a Daemon??

How in the world do you create a daemon and get it to start with a startup script? could someone tell me in detail im going nuts...thanks (1 Reply)
Discussion started by: nmajin
1 Replies

3. Programming

Daemon...Zombie?? Please help me

Hello, i am very very puzzled, im doing this project for school, its a deamon logger, but anyways I'm supposed to run the daemon, let it run on the backgroun, and then run a different program (from command like prompt). but when i run my daemon, it never goes back to the nova> prompt. :( i dont... (3 Replies)
Discussion started by: Kacyndra
3 Replies

4. Shell Programming and Scripting

daemon scripts

Hello, id like to know how can i do for making my script a daemon. Im not sure, but once , i might read about a "daemon" command. Thats true? Whats it for? It isnt in my man. (2 Replies)
Discussion started by: Jariya
2 Replies

5. Programming

Creating a Daemon

how to convert a c program into a Daemon. thanks in advance svh (2 Replies)
Discussion started by: svh
2 Replies

6. AIX

rsync daemon

Does anyone out there use rsync on AIX 5.2, I've installed and would like to run in server mode, I've setup in services and inetd.conf and refreshed inetd. The rsync daemon however does not start, I've also tried rsync --daemon, this just returns to the # prompt. The rsync command itself appears to... (1 Reply)
Discussion started by: gefa
1 Replies

7. Programming

How to write daemon?

Hi , I want to know how to write a daemon process. I also want to know the concept behind daemon processes. Any material or sample program will be great :) . Thanks in advance -sg (2 Replies)
Discussion started by: sg6876
2 Replies

8. Programming

Help with task daemon

believe it or not but this is my first c program (i've worked with java, C#, php though) I am trying to make a daemon that checks if mplayer is running(it's for a projection room) and if it is not then to run mplayer with a file.. So far it's not working and I don't know why Help and comments... (5 Replies)
Discussion started by: james2432
5 Replies

9. UNIX for Advanced & Expert Users

stunnel4 daemon

If my web browser does not support SSL.This means that I can't connect like this "https://xxxxx". My question is how can i make a stunnel4 daemon in client mode ? like when i connect to "http://localhost:3000" in the web browser i am navigating througth "https://www.randomweb.com". I need to know... (0 Replies)
Discussion started by: pepeleches
0 Replies
VFORK(2)							System Calls Manual							  VFORK(2)

NAME
vfork - spawn new process in a virtual memory efficient way SYNOPSIS
pid = vfork() int pid; DESCRIPTION
Vfork can be used to create new processes without fully copying the address space of the old process, which is horrendously inefficient in a paged environment. It is useful when the purpose of fork(2) would have been to create a new system context for an execve. Vfork differs from fork in that the child borrows the parent's memory and thread of control until a call to execve(2) or an exit (either by a call to exit(2) or abnormally.) The parent process is suspended while the child is using its resources. Vfork returns 0 in the child's context and (later) the pid of the child in the parent's context. Vfork can normally be used just like fork. It does not work, however, to return while running in the childs context from the procedure that called vfork since the eventual return from vfork would then return to a no longer existent stack frame. Be careful, also, to call _exit rather than exit if you can't execve, since exit will flush and close standard I/O channels, and thereby mess up the parent processes standard I/O data structures. (Even with fork it is wrong to call exit since buffered data would then be flushed twice.) SEE ALSO
fork(2), execve(2), sigvec(2), wait(2), DIAGNOSTICS
Same as for fork. BUGS
This system call will be eliminated when proper system sharing mechanisms are implemented. Users should not depend on the memory sharing semantics of vfork as it will, in that case, be made synonymous to fork. To avoid a possible deadlock situation, processes that are children in the middle of a vfork are never sent SIGTTOU or SIGTTIN signals; rather, output or ioctls are allowed and input attempts result in an end-of-file indication. 4th Berkeley Distribution June 30, 1985 VFORK(2)
All times are GMT -4. The time now is 11:06 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy