Solved

Shell Background Processing in C

Posted on 2011-02-18
8
2,144 Views
Last Modified: 2014-07-26
Hey Experts!

I am currently having issues running a background process in my shell that I have created.  Whenever I invoke the background process, by typing an '&' symbol after the commands, the program runs the process in the background, then exits my program.  Can anyone explain why this is happening?

Here is my function that handles new processes:
 
void initProcess()
{
	int status = 0;
    int pid = fork();

    if (pid == 0) {
		// child process
		if(background) {
			fclose(stdin); // close child's stdin
			fopen("/dev/null", "r"); // open a new stdin that is always empty
		}
        execvp(*commandArguments,commandArguments);
       
        // If an error occurs, print error and exit
       fprintf (stderr, "unknown command: %s\n", commandArguments[0]);
       exit(0);
       
    } else {
        // parent process, waiting on child process
        //waitpid(pid, &status, 0); //old code..
		
		if (background) {
			printf("starting background job %d\n", pid);
			childpids [nChildren] = pid;
			nChildren++;
			execvp(*commandArguments,commandArguments);
		} else { 
			waitpid(pid, &status, 0);
		} 
		
        if (status != 0) 				//process exits if error occurs
            fprintf  (stderr, "error: %s exited with status code %d\n", commandArguments[0], status);
    }
}

Open in new window


I do not think that I am implementing this idea correctly.. Any help would be greatly appreciated!!! :D   ~Rob
0
Comment
Question by:RobStl10
  • 3
  • 2
8 Comments
 
LVL 12

Expert Comment

by:JESii
Comment Utility
Have you tried running it without the trailing '&' to see what happens?
0
 

Author Comment

by:RobStl10
Comment Utility
Well, if I don't use the '&' symbol, it runs fine as a normal process.  My issue only occurs when I use the '&' symbol.  It prints out "Starting background process X", where X is the pid of the process.  But then exits my shell.  

Should I include more code so that you are able to see the entire shell process?
0
 
LVL 12

Expert Comment

by:JESii
Comment Utility
Actually, if you are running in the background, then I'm not sure that you see any output - in the background, the task runs disconnected from a terminal.

Try closing stdout and then opening it to a file (similarly to what you did with stdin except not /def/null ) and see if you get some output.
0
 

Accepted Solution

by:
RobStl10 earned 0 total points
Comment Utility
I found out the issue.  I am supposed to be closing stdin, opening /dev/null, then executing the command.  This should only be done in the child process though..  

My issue was that I would exit(0), inside the parent process.  This is why my shell would exit, then accept more intput.  I have fixed my code, so please take a look if you are interested :)
void initProcess()
{
	int status = 0;
    int pid = fork();
	//printf("PID: %i\n", pid);
	background = 0;
	
	if(strcmp(commandArguments[commandWordCount-1], "&") == 0) {
		background = 1;
		commandArguments[commandWordCount-1] = NULL;
		commandWordCount -= 1;
	}

	//printf("background: %i\n", background);
    if (pid == 0) {
		// child process
		if(background) {
			//printf("Child background process...");
			
			fclose(stdin); // close child's stdin
			fopen("/dev/null", "r"); // open a new stdin that is always empty
			execvp(*commandArguments,commandArguments);

			
			// If an error occurs, print error and exit
			fprintf (stderr, "unknown command: %s\n", commandArguments[0]);
			exit(1);
			
		} else {
			execvp(*commandArguments,commandArguments);
			
			// If an error occurs, print error and exit
			fprintf (stderr, "unknown command: %s\n", commandArguments[0]);
			exit(1);
		}
               
    } else {
        // parent process, waiting on child process
		
		if (background) {
			
			printf("starting background job %d\n", pid);
			childpids [nChildren] = pid;
			nChildren++;
						
		} else { 
			waitpid(pid, &status, 0);
		} 
		
        if (status != 0) 				//process exits if error occurs
            fprintf  (stderr, "error: %s exited with status code %d\n", commandArguments[0], status);
    }
}

Open in new window

0
 
LVL 12

Expert Comment

by:JESii
Comment Utility
Excellent! Glad you got it working; and thanks for the updated code.  It looks like properly setting the background variable was a key element...
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now