You can execute a background task -use '&' after 1 single command without args-
This commit is contained in:
		
							parent
							
								
									894f44e4ca
								
							
						
					
					
						commit
						3c402d4be4
					
				
					 1 changed files with 22 additions and 6 deletions
				
			
		
							
								
								
									
										28
									
								
								mini_shell.c
									
										
									
									
									
								
							
							
						
						
									
										28
									
								
								mini_shell.c
									
										
									
									
									
								
							| 
						 | 
					@ -73,7 +73,7 @@ void execution(char *arg1, char *arg2, char *arg3, char **env)
 | 
				
			||||||
			if((pid = fork()) == 0)
 | 
								if((pid = fork()) == 0)
 | 
				
			||||||
			{
 | 
								{
 | 
				
			||||||
				int fd0,fd1;
 | 
									int fd0,fd1;
 | 
				
			||||||
				if(strcmp(arg2, "<") == 0)
 | 
									if(strcmp(arg2, "<") == 0) // On prend arg3 en entrée standard
 | 
				
			||||||
				{
 | 
									{
 | 
				
			||||||
					if((fd0 = open(arg3, O_RDONLY)) == -1) 
 | 
										if((fd0 = open(arg3, O_RDONLY)) == -1) 
 | 
				
			||||||
					{
 | 
										{
 | 
				
			||||||
| 
						 | 
					@ -82,14 +82,23 @@ void execution(char *arg1, char *arg2, char *arg3, char **env)
 | 
				
			||||||
					}
 | 
										}
 | 
				
			||||||
					close(0); dup(fd0); // Redirection de l'entrée standard
 | 
										close(0); dup(fd0); // Redirection de l'entrée standard
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
				if(strcmp(arg2, ">") == 0)
 | 
									if(strcmp(arg2, ">") == 0) // On redirige la sortie vers un fichier (arg3)
 | 
				
			||||||
				{
 | 
									{
 | 
				
			||||||
					if((fd1 = open(arg3, O_WRONLY | O_CREAT | O_TRUNC , 0666)) == -1)
 | 
										if((fd1 = open(arg3, O_WRONLY | O_CREAT | O_TRUNC , 0666)) == -1)
 | 
				
			||||||
					{
 | 
										{
 | 
				
			||||||
						erreur("Redirection sortie standard vers fichier");
 | 
											erreur("Redirection sortie standard vers fichier");
 | 
				
			||||||
						exit(EXIT_FAILURE);
 | 
											exit(EXIT_FAILURE);
 | 
				
			||||||
					}
 | 
										}
 | 
				
			||||||
					close(1); dup(fd1);
 | 
										close(1); dup(fd1); // Redirection de la sortie standard
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									if(strcmp(arg2, "&") == 0) // Exécution en arrière plan, on a rien en entrée
 | 
				
			||||||
 | 
									{
 | 
				
			||||||
 | 
										if((fd0 = open("/dev/null", O_RDONLY)) == -1)
 | 
				
			||||||
 | 
										{
 | 
				
			||||||
 | 
											erreur("Ouverture /dev/null");
 | 
				
			||||||
 | 
											exit(EXIT_FAILURE);
 | 
				
			||||||
 | 
										}
 | 
				
			||||||
 | 
										close(0); dup(fd0);
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
				execvpe(arg1, options, env);
 | 
									execvpe(arg1, options, env);
 | 
				
			||||||
				erreur("execvpe n'a pas fonctionné");
 | 
									erreur("execvpe n'a pas fonctionné");
 | 
				
			||||||
| 
						 | 
					@ -97,9 +106,16 @@ void execution(char *arg1, char *arg2, char *arg3, char **env)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			else
 | 
								else
 | 
				
			||||||
			{
 | 
								{
 | 
				
			||||||
				waitpid(pid, &status, 0 );
 | 
									if(strcmp(arg2, "&") == 0) // Si exécution en arrière plan
 | 
				
			||||||
				if(status != 0)
 | 
									{
 | 
				
			||||||
					erreur("Soucis avec le fils");
 | 
										signal(SIGCHLD, SIG_IGN);
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									else // Sinon on attend la fin d'exécution du fils
 | 
				
			||||||
 | 
									{
 | 
				
			||||||
 | 
										waitpid(pid, &status, 0 );
 | 
				
			||||||
 | 
										if(status != 0)
 | 
				
			||||||
 | 
											erreur("Soucis avec le fils");
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue