Ok guys so I have my first dummy shell almost done except for one tiny part: I do not know how to run a process in the background, from the code!
I already know how to do that in a normal shell:
$ program &
However, no clue when it comes to how to program that thing.
A very abstract description of how my program works would be the following:
while (1) // main loop
{
// variables here
// read command line
// tokenize the command and its arguments
i = fork();
if (i == 0)
{
// run the process with its arguments here
exit(1);
}
wait();
}
Ok, I got all of that working perfectly. I just need to find out a way for it to read the & as a signal saying "run this process in the background!" But I have no idea how.
I understood your idea.You want to run the process as background inside the code.But,I don't know which process you want to run.You can pass the & as an argument to this program from command line by using "&".You can get the argument by using argv[1].
If you want to run the process background programmatically , then you must make the process as a daemon.
I :
The first step is that change the current directory as root "/" because it provide the
facility to do all the operation from the root
If you want to mount a file system then you are unable to do this with out changing cur_dir into a root dir * what are all the path name you planned to give that should be in a absolute path name
II:
* Create a process and kill the parent of that process
* It will make the living process \( child \) as a child of init\(\) and its process id will become 1
* Though a we never need a parent for a daemon
III :
* Daemon should be a session leader
* This can be achieved using setsid() in C
IV :
* We don't need an interaction of user for daemon process
* So we close all the descriptor
* We should assure that no other file descriptor would be assign for this process
V :
* Output and the monitoring the daemon will be done using a log files
* It is better to log the information in a /var/log/ directory structure
VI :
* Daemon should always in a running state
* So we should secure the daemon from the signals
* We should ignore all the signals except SIGHUP
* As a convention SIGHUP uses for reading configuration file
VII :
* Have a termination handler
* We should do some operation whenever we terminate the daemon
* Like releasing the memory closing the configuration file etc .
Here I provide an example code :
/* header files */
#include <stdio.h>
#include <ctype.h>
#include <signal.h>
#include <sys/wait.h>
/* Global variables */
...
/* Function prototypes: */
...
void terminate (int signum); /* clean up before termination */
int
main (void)
{
...
if (chdir (ROOT_DIR)) /* change to directory containing data files */
{
fprintf (stderr, "`%s': ", ROOT_DIR);
perror (NULL);
exit (1);
}
/* Daemonize a process */
switch (fork ())
{
case -1: /* can't fork */
perror ("fork()");
exit (3);
case 0: /* child, process becomes a daemon: */
close (STDIN_FILENO);
close (STDOUT_FILENO);
close (STDERR_FILENO);
if (setsid () == -1) /* request a new session (job control) */
{
exit (4);
}
break;
default: /* parent returns to calling process: */
return 0;
}
/* Establish signal handler to clean up before termination: */
if (signal (SIGTERM, terminate) == SIG_IGN)
signal (SIGTERM, SIG_IGN);
signal (SIGINT, SIG_IGN);
signal (SIGHUP, SIG_IGN);
/* Main program loop */
/* Operation done here */
while (keep_going)
{
...
}
return 0;
}
/* Termination handler */
void terminate (int signum)
{
keep_going = 0;
signal (signum, termination_handler);
}