Question about background processes

Hi!

First of all, let me warn you I'm quite new to the world of LINUX and Operating Systems understanding, so that's why I pose these newbie and stupid qustions...

Anyway, I'm trying to build my own simple shell in C and I'm getting some problems in implementing the background process ('&') functionality.

The parsing of the input command is taken care of, the thing is, when forking a new process after a previous forking of a background process, the new child process shows a parent PID as the PID of that background process and not the PID of the shell process...

I think the problem resides in the fact that i actually DON'T KNOW what a background process really is... I just tell a background process from a 'normal' one by not calling/ calling the wait() system call:

int main(void)
{

(...)

    while (! done) {   

(...)
	
		int pid;
		
		pid = fork();				
				
		if(pid == 0){
			execvp(path, elmntpointer);		

		}else if(pid > 0){
			if(command->background)
				printf("%d Child Process Done\n", pid);
			else{
				wait(NULL);
				printf("%d Child Process Done\n", pid);
			}
			
			exit(0);
		}else{
			fprintf(stderr,"Fork Failed\n");
			printf("Fork Failed\n");
			exit(-1);
		}	
	    }
	}
	
	if(line)
	    free(line);
    }
    return 0;
}

I also get some problems with the exit() system call... When I call it, the process that is terminated is the one of the shell!

Can you help me with something here?

Thanks!
Damiao

Unix Daemon Server Programming

Looks to me you're doing quite fine...

I don't see how that's happening in the code below. But there is a bug...

Pretty much you have it right. The confusion may be what fork() does. fork() clones the process; the new process has a pid of 0. You do not (normally) want to exit() after a fork() call. The exception is when exec() fails in the new process.

There are also some other considerations in creating a background process. Which process will have control of the terminal? Where will the background process receive "stdin" from? Normally, you should close stdin (fd[0]) before spawning the background process.

See my comments below.

pid = fork();				
				
		if(pid == 0){
			execvp(path, elmntpointer);		
                        /* YOU NEED ERROR HANDLING HERE -- if path is not found, for instance. Then you need an exit().  */

		}else if(pid > 0){
			if(command->background)
				printf("%d Child Process Done\n", pid);
			else{
				wait(NULL);
				printf("%d Child Process Done\n", pid);
			}
			/* THIS EXIT IS WRONG */
			exit(0);
		}else{
			fprintf(stderr,"Fork Failed\n");
			printf("Fork Failed\n");
                        /* THIS EXIT IS WRONG */
			exit(-1);
		}	
	    }

Hi,I am also having problems in background processes for my shell...
how do i set the control terminal of my bg process?do i need to use the signals SIGTTIN and SIGTTOU?

Your question was a bit vague, so forgive me if this does not answer your question.

Usually, the background process does not (not should not) have a controlling TTY. If it does, it is because the parent process did not close all TTY-open file descriptors (such as stderr and stdin) after the fork() and before the exec().

ok...i am developing a new shell ..just for fun and learning...
how do i disconnect my bg process from tty...?
Currently i just fork and execl the child and parent doesn't wait.
On receiving sigcld,i print that child is done...but is there any way to see the exit status?
and abt the controlling termnal...i want to know how to connect nd disconnect and also how to interact with a control terminal from background...pls throw some light... :slight_smile:

After the fork(), in the child process, close your file descriptors, then do the exec. In the parent process, I believe you can call waitpid() inside the signal handler. You should be able to get the status there.

As to how to "connect/disconnect" or "interact" with a control terminal from the background? (like the 'fg/bg' commands) That I don't know.

You know, you could aways download bash or zsh and analyze the source code... maybe strip the code until you see how they do it. Or would that be cheating?

:slight_smile:
thanks for the info..the fg/bg is what i am looking for...
and ya ...abt the other shells...bash seems to be very complex...but I didn't check any code..just heard from a frnd..so i am going to check em...

csh might be a bit more straightforward to look at, but there are also many books which construct a crude shell as an exercise. The original Bourne shell is infamous for its unorthodox coding pratices and it didn't have job control, otherwise that should also have been interesting.

You might also want to check the manual pages for setpgid() if you really want to dissociate the forked process from the parent.

setpgid creates new process group with child as leader.I don't want tat.I want the child to be able to return SIGCLD to the parent process.