Project/app_code/background.c
void background(char **argv)
{

	pid_t pid;	//used by fork
	pid_t ret;	//process id used for return check from many system calls
	int status;
	parse_background(&b_argv,argv,&b_tmp_argc);	//parse the input.
	
	/*---create child process---*/
	pid = fork();

	switch (pid)
	{
		case -1: printf("\nfork error\n");	//error
			 exit(20);
			 break;
		
		/*setpgid used to change child process group to separate pgrp whose leader is child*/
		case 0:  if(setpgid(getpid(),0) < 0)
			 {
			 	puts("setpgid error");
				exit(21);
			 }
			/*check for pipe implemetation if any*/
			 if(check_pipe(b_argv))
			 {
				pipeline(b_argv);
			 }
			 else
			 {
			 	execvp(b_argv[0],b_argv);
			 	printf("\nbackground exec failed\n");
			 	exit(22);
			 }
			 break;
		default: /*both parent and child try to make child to change its pgid
			   as the order of fork is unknown*/
			 if(setpgid(pid,0)<0)
			 {
			 //	puts("setpgid error");
			//	exit(23);
			 };
			 ret = waitpid(pid,&status,WNOHANG|WUNTRACED);
			 if(ret < 0)
			 {
				printf("\nwaitpid error\n");
				exit(26);
			 }
			 else if(ret > 0)
			 {
			 	pr_exit(status);
			 }


	                 return;
			 break;
	}
}
/*main function which create new process group and thus make the job to run in bg*/
main function which create new process group and thus make the job to run in bg
main function which create new process group and thus make the job to run in bg
void background(char **argv)
{
	pid_t pid;	
	pid_t ret;	
	int status;
	parse_background(&b_argv,argv,&b_tmp_argc);	
	pid = fork();
	switch (pid)
	{
		case -1: printf("\nfork error\n");	
			 exit(20);
			 break;
		case 0:  if(setpgid(getpid(),0) < 0)
			 {
			 	puts("setpgid error");
				exit(21);
			 }
			 if(check_pipe(b_argv))
			 {
				pipeline(b_argv);
			 }
			 else
			 {
			 	execvp(b_argv[0],b_argv);
			 	printf("\nbackground exec failed\n");
			 	exit(22);
			 }
			 break;
		default: 
			 if(setpgid(pid,0)<0)
			 {
			 };
			 ret = waitpid(pid,&status,WNOHANG|WUNTRACED);
			 if(ret < 0)
			 {
				printf("\nwaitpid error\n");
				exit(26);
			 }
			 else if(ret > 0)
			 {
			 	pr_exit(status);
			 }
	                 return;
			 break;
	}
}
void background ( char * * argv ) { pid_t pid ; pid_t ret ; int status ; parse_background ( & b_argv , argv , & b_tmp_argc ) ; pid = fork ( ) ; switch ( pid ) { case -1 : printf ( " \n \n " ) ; exit ( 20 ) ; break ; case 0 : if ( setpgid ( getpid ( ) , 0 ) < 0 ) { puts ( " " ) ; exit ( 21 ) ; } if ( check_pipe ( b_argv ) ) { pipeline ( b_argv ) ; } else { execvp ( b_argv [ 0 ] , b_argv ) ; printf ( " \n \n " ) ; exit ( 22 ) ; } break ; default : if ( setpgid ( pid , 0 ) < 0 ) { } ; ret = waitpid ( pid , & status , WNOHANG | WUNTRACED ) ; if ( ret < 0 ) { printf ( " \n \n " ) ; exit ( 26 ) ; } else if ( ret > 0 ) { pr_exit ( status ) ; } return ; break ; } }
main function which create new process group and thus make the job to run in bg
main function which create new process group and thus make the job to run in bg
//used by fork //process id used for return check from many system calls //parse the input. /*---create child process---*/ //error /*setpgid used to change child process group to separate pgrp whose leader is child*/ /*check for pipe implemetation if any*/ /*both parent and child try to make child to change its pgid
			   as the order of fork is unknown*/ //	puts("setpgid error"); //	exit(23);