![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Linux RedHat, Ubuntu, SUSE, Fedora, Debian, Mandriva, Slackware, Gentoo linux, PCLinuxOS. All Linux questions here! |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Background processes in a dummy shell... | icer | High Level Programming | 6 | 12-05-2007 01:37 PM |
| oracle background processes | vijayasawant | Linux | 0 | 10-19-2004 01:34 PM |
| Background processes return 127 sporadically | max_largo | Shell Programming and Scripting | 2 | 05-22-2003 10:49 AM |
| Running two processes in background | jacob_gs | Shell Programming and Scripting | 6 | 05-13-2002 08:40 AM |
| Background processes | korndog | UNIX for Advanced & Expert Users | 2 | 09-20-2001 06:56 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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: Code:
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;
}
Can you help me with something here? Thanks! Damiao Last edited by Perderabo; 02-08-2008 at 10:38 AM. Reason: Switch quote tags to code tags |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
#3
|
||||
|
||||
|
Quote:
Quote:
Quote:
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. Code:
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);
}
}
|
|
#4
|
|||
|
|||
|
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? |
|
#5
|
||||
|
||||
|
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(). |
|
#6
|
|||
|
|||
|
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... |
|
#7
|
||||
|
||||
|
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? |
||||
| Google The UNIX and Linux Forums |