System Programming
Deamon Process
A daemon process is a process which runs in background and has no controlling terminal. A daemon (also known as background processes) is UNIX and it is known as services and agents in windows. Since a daemon process usually has no controlling terminal so almost no user interaction is required. Daemon processes are used to provide services that can well be done in background without any user interaction. Daemons are processes that are often started when the system is bootstrapped (boot time) and terminate only when the system is shut down.
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Project of deamon process
1. Page 1 of 8
Semester Project
. Deamon Process .
BSCS (B) Semester 8th
Batch 2016- 2020
Course # System Programming
Submitted by Submitted to
Abu-Bakr G1F16BSCS0076 Pro. Umer Iftikhar
Zain Khalid G1F16BSCS0143
University Of Central Punjab Gujranwala Campus
2. Page 2 of 8
Deamon Process
A daemon process is a process which runs in background and has no controlling
terminal. A daemon (also known as background processes) is UNIX and it is
known as services and agents in windows. Since a daemon process usually has
no controlling terminal so almost no user interaction is required. Daemon
processes are used to provide services that can well be done in background without
any user interaction. Daemons are processes that are often started when the system is bootstrapped
(boot time) and terminate only when the system is shut down.
Command used to check deamon processes
ps -axj
-a option shows the status of processes owned by others,
-x shows processes that don't have a controlling terminal.
-j option displays the job-related information: the
o session ID
o process group ID
o controlling terminal
o terminal process group ID.
Characteristics
A daemon is long-lived, often created at system startup and runs until the system is shut down
It runs in the background and has no controlling terminal. The lack of a controlling terminal
ensures that the kernel never automatically generates any terminal-related signals for a daemon
Almost all daemons have names that end with the letter "d". For example, httpd the daemon that handles
the Apache server,or, sshd which handles SSH remote access connections.
Some popular Categories of deamons are as follow
KERNEL DAEMONS
Deamons jo kernel leve services provide kertatay hain, and cannot be manuplated by system
adminstrator jesay k example next page
For architectural reasons,a few parts of the Linux kernel are managed as if they were user processes. On
older kernels, these processes could be identified by their low PIDs and names that start with k, such as
kupdate, kswapd, keventd, and kapm. The naming is less consistent under the 2.6 kernels, but ps always
shows the names of kernel threads in square brackets. For the most part, these processes dealwith various
aspects of I/O, memory management, and synchronization of the disk cache. They cannot be manipulated
by the system administrator and should be left alone. Daemons that include an N parameter in their names
3. Page 3 of 8
Port numbers are not arbitrary. All machines must agree about which services go with which ports;
otherwise, requests will constantly be directed to the wrong port. If you are creating a site-specific
service, pick a high port number (greater than 1023) that is not already listed in the services file.
If you are familiar with the implementation of the kernel, it is occasionally useful to change these
processes’ execution priorities. However,this is not a standard administrative task. Daemons (as shown
by ps) run separately on each CPU of a multi-CPU system; the N tells you which copy goes with which
CPU.
Major kernel daemons
Daemon Function
ksoftirqd/N
Handles software interrupts when the load is high
kacpid
Deals with the ACPI subsystem
kblockd/N
Blocks subsystem work
aio/N
Retries asynchronous I/Os
kswapdN
Moves pages to swap
ata/N
Does processing for serial ATA support
scsi_eh_N
Performs SCSI error handling
kjournald
Supports journaling filesystems
events/N
Does generic work queue processing
PRINTING DAEMONS
Several printing systems are in common use, and each has its own family of commands and daemons that
provide printing-related services. In some cases,the families have been hybridized; in others
cases,multiple variants run on a single system.
cupsd:scheduler for the Common UNIX Printing System
CUPS provide a portable printing facility by implementing version 1.1 of the Internet Printing Protocol. It
allows remote users to print to their offices (or vice versa) by using a web interface. CUPS has become
quite popular and is most systems’ default printing manager. It is flexible enough to allow for remote
authentication.
lpd: manage printing
lpd is responsible for the old-style BSD print spooling system. It accepts jobs from users and forks
processes that perform the actual printing. lpd is also responsible for transferring print jobs to and
from remote systems. lpd can sometimes hang and then must be manually restarted.
4. Page 4 of 8
FILE SERVICE DAEMONS
The following daemons are part of the NFS(Network File System) or Samba file sharing systems. We
give only a brief description of their functions here. NFS is described in detail in Chapter 16, and Samba is
covered starting on page 828.
rpc.nfsd:serve files
rpc.nfsd runs on file servers and handles requests from NFS clients. In most NFS implementations, nfsd is
really just a part of the kernel that has been dressed up as a process for scheduling reasons. Linux actually
sports two different implementations, one of which follows this convention and one of which runs in user
space. The kernel implementation is more popular and is most distributions’ default.
rpc.nfsd accepts a single argument that specifies how many copies of itself to fork.
rpc.mountd:respond to mount requests
rpc.mountd accepts filesystem mount requests from potential NFS clients. It verifies that each client has
permission to mount the requested directories.
amd and automount: mount filesystems on demand
amd and automount are NFS automounters, daemons that wait until a process attempts to use a filesystem
before they actually mount it. The automounters later unmount the filesystems if they have not been
accessed in a specified period of time.
rpc.lockd and rpc.statd:manage NFS locks
Although rpc.lockd and rpc.statd are distinct daemons, they always run as a team. rpc.lockd maintains
advisory locks (a la flock and lockf) on NFS files. rpc.statd allows processes to monitor the status of other
machines that are running NFS. rpc.lockd uses rpc.statd to decide when to attempt to communicate with a
remote machine.
rpciod: cache NFS blocks
rpciod caches read and write requests on NFS clients. It performs both read-ahead and write-behind
buffering and greatly improves the performance of NFS. This daemon is analogous to the biod and
nfsiod daemons found on other systems, although it is structurally somewhat different.
smbd:provide file and printing service to Windows clients
smbd is the file and printer server in the Samba suite. It provides file and printer sharing service
through the Windows protocol known variously as SMB or CIFS.
5. Page 5 of 8
ADMINISTRATIVE DATABASE DAEMONS
Several daemons are associated with Sun’s NIS administrative database system. Sharing System Files.
Although NIS originated at Sun, it is now used on many other vendors’ systems as well, including
Linux.
ELECTRONIC MAIL DAEMONS
In addition to the core sendmail and Postfix mail delivery systems, which are both in widespread use,
severaldaemons facilitate remote access to mailboxes.
sendmail: transport electronic mail
sendmail’s tasks include accepting messages from users and remote sites, rewriting addresses, expanding
aliases, and transferring mail across the Internet. sendmail is an important and very complex daemon.
smtpd: Simple Mail TransportProtocoldaemon
smtpd listens on port 25 for incoming email messages and forwards them to your back-end transport
system for further processing.
popd:basic mailbox server
The popd daemon implements the Post Office Protocol (POP). This protocol is commonly used by non-
Linux systems to receive electronic mail.
imapd: deluxe mailbox server
The imapd daemon implements the Internet Message Access Protocol,IMAP,which is a more festive and
featureful alternative to POP. It allows PC-based users (or Linux users with IMAP-enabled mail readers)
to access their email from a variety of locations, with mail folders being stored on the Linux server. Check
out www.imap.org for more information about IMAP.
REMOTE LOGIN AND COMMAND EXECUTION DAEMONS
The ability to log in and execute commands over the net was one of the earliest motivations for the
development of UNIX networking, and this facility is still a bread and-butter component of system
administration today. Unfortunately, it took the UNIX community severaldecades to achieve a mature
appreciation of the security implications of this technology. Modern production systems should be
using SSH (sshd) and virtually nothing else.
6. Page 6 of 8
Daemon Process Design
A daemon process can be developed just like any other process but there is one thing that differentiates it
with any other normal process ie having no controlling terminal. This is a major design aspect in creating
a daemon process. This can be achieved by
Create a normal process (Parent process)
Create a child process from within the above parent process
The process hierarchy at this stage looks like: TERMINAL -> PARENT PROCESS -> CHILD
PROCESS
Terminate the parent process.
The child process now becomes orphan and is taken over by the init process.
init (short for initialization) is the first process started during booting of the
computer system. Init is a daemon process that continues running until the
system is shut down.
Call setsid () function to run the process in new session and have a new group.
After the above step we can say that now this process becomes a daemon process without having
a controlling terminal.
Change the working directory of the daemon process to root and close stdin, stdout and stderr file
descriptors.
Let the main logic of daemon process run.
The Implementation of deamon process programmatically
Before starting the implementation ofdeamon process let’s discuss the basic knowledge ofa fork ()
system call that is used to create the child process and parent also. fork () system creates a child
process that is exact replica ofthe parent process. This newprocess is referred as ‘child’ process.
This system call gets called once (in parent process) but returns twice (once in parent and second time in
child). Note that after the fork () system call, whether the parent will run first or the child is non-
deterministic. It purely depends on the context switch mechanism. This call returns zero in child while
returns PID of child process in the parent process.
Following are some important aspects of this call
The child has its own unique process ID,and this PID does not match the ID of any existing
process group.
The child’s parent process ID is the same as the parent’s process ID.
The child does not inherit its parent’s memory locks.
Process resource utilization and CPU time counters are reset to zero in the child.
The child’s set of pending signals is initially empty.
The child does not inherit semaphore adjustments from its parent.
The child does not inherit record locks from its parent.
The child does not inherit timers from its parent.
The child does not inherit outstanding asynchronous I/O operations from its parent, nor does it
inherit any asynchronous I/O contexts from its parent.
7. Page 7 of 8
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
The variables are named argc (argument count) and argv (argument vector)
int main (int argc, char* argv[])
{
FILE *fp= NULL;
pid_t process_id = 0;
pid_t sid = 0;
// Create child process
process_id = fork ();
// Indication of fork () failure
if (process_id < 0){
printf ("fork failed! n");
// Return failure in exit status
exit (1);
}
// PARENT PROCESS. Need to kill it.
if (process_id > 0) {
printf ("process_id of child process %d n", process_id);
// return success in exit status
exit (0);
}
//unmask the file mode
umask (0);
//set new session
sid = setsid ();
if (sid < 0) {
// Return failure
exit (1);
}
// Change the current working directory to root.
chdir ("/");
// Close stdin. stdout and stderr
close (STDIN_FILENO);
close (STDOUT_FILENO);
close (STDERR_FILENO);
// Open a log file in write mode.
fp = fopen ("Log.txt", "w+");
while (1) {
//Dont block context switches, let the process sleep for some time
sleep (1);
fprintf (fp, "Logging info...n");
fflush(fp);
// Implement and call some function that does core work for this daemon.
}
fclose(fp);
return (0);
}
Following is the way through which the code was compiled and executed:
8. Page 8 of 8
$ gcc -Wall deamon.c -o deamon
$ sudo. /deamon
process_id of child process 2936
When we check the log.txt file located in the root directory, you could see that this daemon
process is running.
$ tail -f /Log.txt
Logging info...
Logging info...
Logging info...
Logging info...
Logging info...
Logging info...
Batch programming
Unix basic commands
Scripts..
System Calls (Important Topic)
30 MCQ’s
Shell Programming (Q.NO 2 program-3program+theoratical)
System Calls (Q,NO 4,5,6)