Background process c - c

I am making a shell and I am confused as to what to do if a command is to be put in the background.
I have parsed my commands, and fork works for commands that are in the foreground. I have it so it can be determined if a command is to be put in the background. I'm not really sure what to do in the first else if of my code. Any pointers on how to approach background commands would be appreciated.
pid_t childpid;
int status;
childpid = fork();
if (childpid >= 0) // fork succeeded
{
if (childpid == 0 && background == 0) // fork() returns 0 to the child
{
if (execv(path, strs) < 0)
{
perror("Error on execv.");
}
exit(0); // child exits
}
else if (childpid == 0 && background ==1)
{
// What goes here?
}
else // fork() returns new pid to the parent
{
wait(&status);
}
}
else // fork returns -1 on failure
{
perror("fork"); // display error message
exit(0);
}

The child doesn't care if it's run in the background or not, so just call exec as usual. It's in the parent process you have to behave differently.
First of all you can no longer use wait as that will block the parent process. Instead you can use waitpid with a negative pid and the WNOHANG flag to check for terminated child processes without blocking.
Another common solution that doesn't involve calling waitpid at regular intervals is to use the SIGCHLD signal, which will be raised when a child process is stopped or terminated.

Related

Background process killing with Parent process in C

I have the following code in my main function
pid_t pid;
pid = fork(); //Two processes are made
if (pid > 0 && runBGflag==0) //Parent process. Waits for child termination and prints exit status
{
int status;
if (waitpid(pid, &status, 0) == pid && WIFEXITED(status))
{
printf("Exitstatus [");
for (int i = 0; i < noOfTokens; i++)
{
printf("%s ", commands[i]);
}
printf("\b] = %d\n", WEXITSTATUS(status));
}
}
else if (pid == 0) //Child process. Executes commands and prints error if something unexpected happened
{
if (runBGflag==1) insertElement(getpid(),ptr);
execvp(commands[0], commands);
printf ("exec: %s\n", strerror(errno));
exit(1);
}
In a nutshell, a child process is made and if the runBackGround flag is set, the parent process will not wait for the child process to exit, but rather continue running. If a background process is made, the PID of the background process is stored in a list. At a later point, this function is called
void delete_zombies(void)
{
pid_t kidpid;
int status;
char buffer[1337];
while ((kidpid = waitpid(-1, &status, WNOHANG)) > 0)
{
removeElement(kidpid,buffer,1337);
printf("Child %ld terminated\n", kidpid);
printf("its command was %s\n",buffer);
}
}
This function simply checks if any child processes have died and in that case deletes them. It will then search for the childs PID in the list, remove it and print it out.
The problem is, the delete_zombies function will find that a child has died and will then try to remove it from the list, but it only finds an empty list, as if the child process never inserted its PID into the list.
This is really strange, because delete_zombies only finds a dead child process, when there was one created with the background flag set, so we know insertElement must have been called, but strangely when the parent checks in the list nothing is there
Is the cause for that, that child process and parent process have seperate lists, or is the PID maybe wrong?

Running in same child process two times

Can I be running in the same child process two times in the same fork? Ex.
pid_t pid;
pid = fork();
if (pid == 0){
some code here
}else{
some code here
}
wait(NULL)
if (pid ==0){
some code here
}else{
some core here
}
When you leave the if/else, the code runs in both the parent and child. The next if/else again switches to different code in each.
pid_t pid;
pid = fork();
if (pid == 0){
// child code here
}else{
// parent code here
}
wait(NULL) // runs in both
if (pid ==0){
// more child code here
}else{
// more parent code here
}
Unless the child forks another child, wait(NULL) in the child will return immediately (it will return -1 and set errno to ECHILD to indicate that it has no children to wait for). But in the parent process it will wait for the child to exit. So the second if/else block will run immediately in the child, while it will not run in the parent until the child exits.

Which process is getting exited and why is close(0) call followed after exit(0)

if (log_daemon) {
pid_t pid;
log_init();
pid = fork();
if (pid < 0) {
log_error("error starting daemon: %m");
exit(-1);
} else if (pid)
exit(0);
close(0);
open("/dev/null", O_RDWR);
dup2(0, 1);
dup2(0, 2);
setsid();
if (chdir("/") < 0) {
log_error("failed to set working dir to /: %m");
exit(-1);
}
}
I have above c program, and couldnt figure out what does the exit(0); do in this case, which process does it exit? and what does the close(0); is followed for? Will the close(0); even execute?
Is this code just to test whether a child process can be created?
UPDATE:
ok, I got it from this question Start a process in the background in Linux with C.
Basically, close(0); does is closes current standard input for child process and opens /dev/null as input device. This way child process will behave as a deamon and will not read anything from terminal or standard input.
The fork returns the process id in the parent process, and 0 in the child process. The main calling process is exiting becausepid == 0 so if (pid) is true in the parent, and false in the child. The child then proceeds to close(0), etc.

How to properly fork() a process

I'm trying to understand how to properly used fork() and execvp() to execute a command. So far I have the following code:
When I run ./test vim myFile.c it correctly opens myFile.c but I get strange behavior. It seems as though there are two processes running because whenever I enter anything it seems to happen twice. Why is this?
int main (int argc, char* argv[]) {
int fdin, pid, w, status;
fdin = 0;
if ((pid = fork()) < 0)
errorExit (EXIT_FAILURE);
execvp(argv[0],argv);
do {
w = waitpid(cpid, &status, WUNTRACED | WCONTINUED);
if (w == -1) {
perror("waitpid");
exit(EXIT_FAILURE);
}
if (WIFEXITED(status)) {
printf("exited, status=%d\n", WEXITSTATUS(status));
} else if (WIFSIGNALED(status)) {
printf("killed by signal %d\n", WTERMSIG(status));
} else if (WIFSTOPPED(status)) {
printf("stopped by signal %d\n", WSTOPSIG(status));
} else if (WIFCONTINUED(status)) {
printf("continued\n");
}
} while (!WIFEXITED(status) && !WIFSIGNALED(status))
}
When you call fork(), you create two almost completely identical processes, the parent process and the child process. The only difference between these two processes is the return value of fork(), which returns 0 to the child process and the pid of the child to the parent.
Hence, assuming fork succeeded, fork will return a nonnegative integer to both the parent and child process in line 4. Then, both the parent and child process will execute line 6, the execvp, and hence, you end up with two different processes running your vim myFile.c, causing all the problems you described.
The standard idiom is something like:
if ((pid = fork()) < 0) {
// Handle fork error
}
else if (pid == 0) {
// Child process
execvp(...);
}
else {
// Parent process
w = waitpid(pid, ...);
}
Since the return value for fork is 0 for the child, after fork succeeds, the test (pid == 0) will be true for the child, so execvp will be called.
For the parent, fork returns the pid of the child, so the check (pid == 0), which still get executed, is false, so the else condition is executed, causing the parent to wait for the child.
Both parent and child in your program get execvp():
if ((pid = fork()) < 0)
errorExit (EXIT_FAILURE);
execvp(argv[0],argv);
You should check, if you are in parent with pid != 0, and if you are in child otherwise.
You should look at the return value of fork, after a successful fork you will have two running processes at the same position in your program. The child process will get a return value of 0, the parent will get a return value which is the pid of the child. Most likely you want to do different things in the child process and the parent process.
You might also want to think again about how execvp is called. Do you really want to give "./test" as the first argument to execvp?

How to make parent wait for all child processes to finish?

I'm hoping someone could shed some light on how to make the parent wait for ALL child processes to finish before continuing after the fork. I have cleanup code which I want to run but the child processes need to have returned before this can happen.
for (int id=0; id<n; id++) {
if (fork()==0) {
// Child
exit(0);
} else {
// Parent
...
}
...
}
pid_t child_pid, wpid;
int status = 0;
//Father code (before child processes start)
for (int id=0; id<n; id++) {
if ((child_pid = fork()) == 0) {
//child code
exit(0);
}
}
while ((wpid = wait(&status)) > 0); // this way, the father waits for all the child processes
//Father code (After all child processes end)
wait waits for a child process to terminate, and returns that child process's pid. On error (eg when there are no child processes), -1 is returned. So, basically, the code keeps waiting for child processes to finish, until the waiting errors out, and then you know they are all finished.
POSIX defines a function: wait(NULL);. It's the shorthand for waitpid(-1, NULL, 0);, which will suspends the execution of the calling process until any one child process exits.
Here, 1st argument of waitpid indicates wait for any child process to end.
In your case, have the parent call it from within your else branch.
Use waitpid() like this:
pid_t childPid; // the child process that the execution will soon run inside of.
childPid = fork();
if(childPid == 0) // fork succeeded
{
// Do something
exit(0);
}
else if(childPid < 0) // fork failed
{
// log the error
}
else // Main (parent) process after fork succeeds
{
int returnStatus;
waitpid(childPid, &returnStatus, 0); // Parent process waits here for child to terminate.
if (returnStatus == 0) // Verify child process terminated without error.
{
printf("The child process terminated normally.");
}
if (returnStatus == 1)
{
printf("The child process terminated with an error!.");
}
}
Just use:
while(wait(NULL) > 0);
This ensures that you wait for ALL the child processes and only when all have returned, you move to the next instruction.

Resources