uCdot
search uCdot:
 
Embedded Linux and uClinux Developer Forum
 
uCdot
- FAQ
- Dev Boards
- Submit Story
- Submit FAQ
- Submit Dev Board
- Topics
- Authors
- About

- Preferences
- Older Stuff
- Past Polls
- Discussions
- Journals
- Messages

Embedded Linux
Mailing Lists
uClinux-dev (search)
Coldfire (search)
MTD
Microblaze (search)
ELUG
BDM-devel
Blackfin

Embedded Linux
Sites
uClinux.org
uClinux-Dist
uClibc
uClinux Directory
LinuxDevices
ARMulator
uClinux-elf-tools
Colilo
Kernel Archives
H8-uClinux
TLDP
Microblaze uClinux
BDM Tools
SkyEye (emulator)
LOM
SETR live CD
Blackfin uClinux

Embedded Linux
Companies
SecureComputing
SDCS
CodePoet
Arcturus
Cadenux
ARMtwister
uClinux.net
Xiptech
senTec
embedded^cl
Cwlinux
emlix
TimeSys
eSpark Infotech
SSV Embedded Systems
Embedded Minds
PeerSec Networks
Vortech Consulting
swissEmbedded
Synertronixx
Mbedthis Software
.vantronix
Aday
GraceLabs
Pengutronix
metux ITS
Codito Technologies
Firmix Software
PetaLogix
NuDesign
Merritt Technologies
WindRiver
OpenGear
Rubico
Analog Devices
Artila Electronics
Vyatta
Embest Info&Tech
Katalix Systems
WorkWare Systems
Kdev
Intellimetrix
Virtual Cogs
SYSGO
coresystems
ExactCODE
KOAN
EzHomeTech
Linux4biz
Linkodas
Trego
EMBEST
Boardcon
HITEG
FemtoLinux
Prosoft World
Witech

 
Starting a background application in uClinux
FAQ Sometimes it is useful to start an application which is backgrounded and no longer associated with the starting terminal. Usually this is referred to as daemonizing.

Stevens in his book "Advanced programming in the unix environment" presents the classic daemonizing code. I have modified it here to create an application which when run from the command line, detaches from the command line and writes a message to syslog every 5 seconds.

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <syslog.h>


int main(int argc, char *argv[])
{
	char c;
	int _argc = 0;
	char *_argv[3];
	pid_t pid;

	if ((pid = fork()) < 0) {
		fprintf(stderr, "fork failed\n");
		exit(1);
	} else if (pid != 0) {
		exit(0);
	}

	setsid();
	chdir("/");
	umask(0);
	close(0);
	close(1);
	close(2);
	
	for (;;) {
		sleep(5);
		syslog(LOG_INFO, "Still sleeping\n");
	}
}

The trouble with the above is that it doesn't work under uClinux because there is no fork, only vfork. If we replace the fork call above with vfork, then the parent application never terminates. The child won't background until exec is called.

We can thus modify the classic daemonizing program to exec a program which can then be the background process while the parent process terminates. The trick is to exec the original program itself with an argument which indicates that it is the child process. The code below illustrates.

/*
 * (c) Matthew Natalier, 2003
 *
 * If you want to use this, tell me that you love me :-)
 *
 */
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>
#include <sys/types.h>
#include <unistd.h>


int execed = 0;

int main(int argc, char *argv[])
{
	char c;
	int _argc = 0;
	char *_argv[3];
	pid_t pid;

	while ((c=getopt(argc, argv, "D"))  > 0) {
		switch(c) {
			case 'D':
				execed = 1;
				break;
			default:
				fprintf(stderr, "You probably don't want to pass "
					"options to this\n");
				exit(1);
		}
	}

	if (!execed) {
		if ((pid = vfork()) < 0) {
			fprintf(stderr, "vfork failed\n");
			exit(1);
		} else if (pid != 0) {
			exit(0);
		}

		_argv[_argc++] = argv[0];
		_argv[_argc++] = "-D";
		_argv[_argc++] = NULL;
		execv(_argv[0], _argv);
		/* Not reached */
		fprintf(stderr, "Couldn't exec\n");
		_exit(1);

	} else {
		setsid();
		chdir("/");
		umask(0);
		close(0);
		close(1);
		close(2);
	}
	
	for (;;) {
		sleep(5);
		syslog(LOG_INFO, "Still sleeping\n");
	}
}

The best thing about the above code is that it will work on uClinux or standard Linux systems without modification.

angryman

JFFS2 is running very slowly | Understanding the GPL  >

 

 
uCdot Login
Nickname:

Password:

[ Create a new account ]

Related Links
  • Linux
  • SnapGear
  • uClinux
  • angryman
  • More on FAQ
  • Also by davidm
  • This discussion has been archived. No new comments can be posted.
    Starting a background application in uClinux | Login/Create an Account | Top | Search Discussion
    Threshold:
    The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.

    The Embedded Linux and uClinux Developer Forum is hosted by: SnapGear If you explain so clearly that nobody can misunderstand, somebody will.

    [ home | contribute story | older articles | past polls | faq | authors | preferences ]