The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > OS Specific Forums > Linux
Google UNIX.COM


Linux RedHat, Ubuntu, SUSE, Fedora, Debian, Mandriva, Slackware, Gentoo linux, PCLinuxOS. All Linux questions here!

More UNIX and Linux Forum Topics You Might Find Helpful
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

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 02-08-2008
Registered User
 

Join Date: Feb 2008
Posts: 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;
}
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

Last edited by Perderabo; 02-08-2008 at 10:38 AM. Reason: Switch quote tags to code tags
Reply With Quote
Forum Sponsor
  #2  
Old 02-13-2008
agn agn is offline
Registered User
 

Join Date: Feb 2008
Posts: 97
Unix Daemon Server Programming
Reply With Quote
  #3  
Old 02-13-2008
otheus's Avatar
Moderator ala Mode
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 655
Quote:
Originally Posted by neimaD View Post
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...
Looks to me you're doing quite fine...


Quote:
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 don't see how that's happening in the code below. But there is a bug...

Quote:
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:
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.

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);
		}	
	    }
Reply With Quote
  #4  
Old 08-19-2008
Registered User
 

Join Date: May 2008
Location: Cochin
Posts: 13
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?
Reply With Quote
  #5  
Old 08-20-2008
otheus's Avatar
Moderator ala Mode
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 655
Thumbs down

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().
Reply With Quote
  #6  
Old 08-22-2008
Registered User
 

Join Date: May 2008
Location: Cochin
Posts: 13
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...
Reply With Quote
  #7  
Old 08-23-2008
otheus's Avatar
Moderator ala Mode
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 655
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?
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 11:01 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0