background process

I have made a shell that accept a command and parameters. It is working properly. I have tryed to implement background process in main(). But i dont know to implement them. Can anyone give me a lille example??

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

/* The following define's belongs to read_command(..) */
#define MAXPARMS	20
#define MAXPARMLEN	255
#define MAXINPUT	255
#define shell		"cesh"

/* The Command line parser */

/*------------------------------------------------------------------*/
/* read_command:                                                    */
/*   Print the prompt and get one characters from the users which   */
/*   makes up an entire command line entry                          */
/*------------------------------------------------------------------*/

void read_command(char * command, char * parameters[])
{
  char input[MAXINPUT];
  char * parm;
  char c;
  int i;

  /* initialize the whole parameters array to NULL pointers */
  for (i=0;i<MAXPARMS;i++)
    {
      parameters=NULL;
    }

  i=0;
  input[0]='\0';

  /* loop until some characters are entered on the command line */
  do
    {
      printf("%s> ", shell);
      c=getchar();
      while ((c != '\n') && (i < MAXINPUT-1))
        {
          input=c;
          i++;
          c=getchar();
        }
      input='\0';
    } while (strlen(input)==0);

  /* copy the first "word" on the command line into the command var */
  /* this also initializes the use of the strtok function */
  strcpy(command, strtok(input, " "));
  parameters[0]=malloc((MAXPARMLEN+1)*sizeof(char));
  strcpy(parameters[0], command);
  i=1;

  /* separate all of the "words" on the command line. Each becomes */
  /* a string pointed to by the parameters array using strtok. */
  do
    {
      if (parm=strtok(NULL, " "))
        {
          parameters=malloc((MAXPARMLEN+1)*sizeof(char));
          strcpy(parameters,parm);
        }
      else
        {
          parameters=NULL;
        }
      i++;
    } while((parameters[i-1]!=NULL) && (i < MAXPARMS));

  return;
}

int main(void)
{
char name[10];
char *argv[MAXPARMS];

read_command(name, argv);

/*background process*/
int pid, status;
int BGRND = 0;

do 
{
	if (strcmp(argv[0], "&")==0))
	{
		BGRND = 1;
	}
	if ( (pid = fork() < 0 )
			return 0;

	if(pid == 0 )
	{
		if ((execvp(argv[0], argv)) == -1)
			printf("%s: Command not found.\n", tokenstr);
			return 0;
	}
	if(BGRND == 0)
	{
		while (waitpid(-1, &status, 0) != pid);
	}
	BGRND = 0;
}while(strcmp(name,"exit));

return 0;
}

Do you mean just running something in the background? You do that by placing an ampersand (&) after the command. Like if I wanted to run the date command in the background (which would be silly, but whatever), I'd type date & at the prompt.

Click here for a more detailed explanation.

Oombera ... Yes that what i want to do but i want to make my own code. I am trying to learn simple shell programming. I have made a new code to replace the first version i post in here. Iam not sure about that background process should be implement like this??+

<code>

int main(void)
{
char name[10];
char *argv[MAXPARMS];
int i;
int PID, status;
int BGRND = 0;

read_command(name, argv);

/background process/

if ((strcmp(name, "back")==0) & (strcmp(argv[1], "&")==0))

{
BGRND = 1;

if \( \(PID = fork\(\)\) &lt; 0 \)
		return 0;

if\(PID == 0 \)
\{
	if \(\(execvp\(argv[1], argv\)\) == -1\)
		perror\("Fejl"\);
		//printf\("%s: Command not found.\\n", tokenstr\);
		return 0;
\}
if\(BGRND == 0\)
\{
	while \(waitpid\(-1, &status, 0\) != PID\);
\}
BGRND = 0;

}

return 0;
}</code>

I see a lot of errors in your code. I do not believe that it works. In fact when I look at code like this...

I don't even believe that it will compile at all.

In general terms, you are on the right track. If we assume a shell without job control, "background" just means that we do not wait for the child.

A few comments...look at your line
if ( (pid = fork() < 0 )
The parentheses do not match up. It is critical that the test is done right. It should be
if ( (pid = fork()) < 0 )

When a system call fails, it will return a -1. I prefer explicitly testing for that. This is how you handled the execvp() system call. Also when a system call fails, it sets errno to an error code. If you display this value, it can help in debugging. And there is a sys_errlist that can decode errno. I like to do that as well. Also errors should go to stderr, not stdout. And exit codes should indicate whether or not the program worked. Put all of that together and this is how I would do it...

if ( (pid=fork()) == -1 ) {
         fprintf(stderr, "fork failed \n");
         fprintf(stderr, "errno = %d \n", errno);
         fprintf(stderr, "%s\n", sys_errlist[errno]);
         exit(1);
}

Now to use errno and sys_errlist. you are supposed to include errno.h. But sometimes another include picks them up.

There is no need to waitpid in a loop. Just wait for the process once.

And it looks like you are reading a command once, and then looping on it forever. You need to read a new command inside your loop.