Processes Timeouts Forks
Processes Timeouts Forks
Kernal
Most daemons are created when the system is booted and stay alive
as long as the system running
Daemons provide various system services such as printing (daemon
lpd), ftp (daemon ftpd), telnet (daemon telnetd) and rlogin
(daemon rlogind) services
Dr Qahhar Muhammad Qadir May 16, 2022 3 / 21
Processes, timeouts, and forks
Superserver daemon inetd
ps command
safeen@safeen-Lenovo$ ps aux | grep tftp
safeen 24153 0.0 0.0 2032 280 pts/27 S 21:05 0:00 ./tftp_ser 2222
safeen 24159 0.0 0.0 4680 820 pts/27 S+ 21:06 0:00 grep --color=auto tftp
#include <signal.h>
struct sigaction {
void (*sa_handler)(int);
void (*sa_sigaction)(int, siginfo_t *, void *);
sigset_t sa_mask;
int sa_flags;
void (*sa_restorer)(void);
};
The fork system call creates a copy of the current process and
resumes processing of both the original and the new process at
exactly the same location
In both processes the next instruction executed is the one
immediately after return from the fork.
we can use fork in a server to create child processes which deal with
each request;
the parent process can continue handling new requests – it continues
forever;
each child exits when the request is fulfilled;
we need to make sure children all terminate properly.
All descriptors open in the parent before the call to fork are shared
with the child after fork returns.
Example:
Parent calls accept and then calls fork
Socket shared between the parent and child
Child reads and writes the socket
Parent Closes the socket
A process makes a copy of itself
A process wants to execute another program
pid_t pid;
int listenfd, connfd;
listenfd = Socket( ... );
/* fill in sockaddr_in{} with server’s well-known port */
Bind(listenfd, ... );
Listen(listenfd, LISTENQ);
for ( ; ; ) {
connfd = Accept (listenfd, ... ); /* probably blocks */
if( (pid = fork()) == 0)
{
close(listenfd); /* child closes listening socket */
doit(connfd); /* process the request */
close(connfd); /* done with this client */
exit(0); /* child terminates */
}
close(connfd); /* parent closes connected socket */
}
client server
listenfd
connect() connection
connfd
connfd
connfd
Status Qadir
Dr Qahhar Muhammad of client/server after parent and child close appropriate sockets May 16, 2022 17 / 21
Processes, timeouts, and forks
Zombies and terminating children
Zombies are child processes that occur when the parent process exits
without calling wait or waitpid on the child process
The kernel keeps the exit information for these child processes until
the parent process call these functions
When children created by fork, the kernel expects the parent to
exectute waitpid for each child process
A way to handle zombies is to send the SIGCHLD signal to the parent
and calling waitpid when the child terminates.
When using UDP, the socket is fully identified by the port. Since the
server is communicating with several clients at once, each one must use a
different port. (This is not necessary in TCP, because the several sockets
can use the same port on the server.)
The main process must use the “well-known port”;
Each child process in the server gets a new port from the OS;
The client must switch to using the new port at the server;
it finds out what this new port is from the address stored in the first
reply.