Solved

Shell Background Processing in C

Posted on 2011-02-18
8
2,570 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
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Folder Permission Powershell 4 57
Best way to split and output to csv in bash 2 65
What is sub-make ? 2 61
outlook office 365 8 122
Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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 goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

803 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