Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Shell Background Processing in C

Posted on 2011-02-18
8
Medium Priority
?
4,119 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
5 Comments
 
LVL 12

Expert Comment

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

Author Comment

by:RobStl10
ID: 34933969
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
ID: 34934170
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
ID: 34941506
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
ID: 34942210
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

783 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