stable v2

This commit is contained in:
Laureηt 2021-04-21 15:12:19 +02:00
parent db5b7a582d
commit 480adc8c90
6 changed files with 215 additions and 98 deletions

31
jobs.c
View file

@ -3,7 +3,7 @@
#include <string.h> #include <string.h>
#include "jobs.h" #include "jobs.h"
void ajouter(list *l_ptr, int pid, int id, char **seq) void ajouter(list *l_ptr, int pid, int id, char **seq, int state)
{ {
cell *new_cell = malloc(sizeof(*new_cell)); cell *new_cell = malloc(sizeof(*new_cell));
@ -22,6 +22,7 @@ void ajouter(list *l_ptr, int pid, int id, char **seq)
new_cell->pid = pid; new_cell->pid = pid;
new_cell->id = id; new_cell->id = id;
new_cell->cmd = cmd; new_cell->cmd = cmd;
new_cell->state = state;
if (*l_ptr == NULL) if (*l_ptr == NULL)
{ {
@ -33,8 +34,6 @@ void ajouter(list *l_ptr, int pid, int id, char **seq)
} }
*l_ptr = new_cell; *l_ptr = new_cell;
printf("[%d] %d %s\n", id, pid, cmd);
} }
void supprimer(list *l_ptr, int pid) void supprimer(list *l_ptr, int pid)
@ -46,7 +45,6 @@ void supprimer(list *l_ptr, int pid)
{ {
cell *cursor2free = cursor; cell *cursor2free = cursor;
*l_ptr = cursor->next; *l_ptr = cursor->next;
printf("[%d] (%d) done: %s\n", cursor->id, cursor->pid, cursor->cmd);
free(cursor2free); free(cursor2free);
} }
else else
@ -63,7 +61,6 @@ void supprimer(list *l_ptr, int pid)
} }
} }
cell *cursor_next = cursor->next->next; cell *cursor_next = cursor->next->next;
printf("[%d] (%d) done: %s\n", cursor->id, cursor->pid, cursor->cmd);
free(cursor->next); free(cursor->next);
cursor->next = cursor_next; cursor->next = cursor_next;
} }
@ -88,29 +85,17 @@ void initialiser(list *l_ptr)
void liberer(list *l_ptr) void liberer(list *l_ptr)
{ {
free(*l_ptr); cell* cursor2free;
*l_ptr = NULL; cell* cursor = *l_ptr;
}
int est_vide(list l_ptr)
{
return l_ptr == NULL;
}
int contiens(list *l_ptr, int pid)
{
cell *cursor = *l_ptr;
while (cursor != NULL) while (cursor != NULL)
{ {
if (cursor->pid == pid) cursor2free = cursor;
{
return 1;
}
cursor = cursor->next; cursor = cursor->next;
free(cursor2free);
} }
return 0; free(*l_ptr);
*l_ptr = NULL;
} }
cell *trouver(list *l_ptr, int pid) cell *trouver(list *l_ptr, int pid)

4
jobs.h
View file

@ -8,16 +8,16 @@ struct cell
int pid; int pid;
char *cmd; char *cmd;
cell *next; cell *next;
int state;
}; };
typedef cell *list; typedef cell *list;
void ajouter(cell **list, int pid, int id, char **cmd); void ajouter(cell **list, int pid, int id, char **cmd, int state);
void supprimer(cell **list, int pid); void supprimer(cell **list, int pid);
void afficher(cell **list); void afficher(cell **list);
void initialiser(list *list); void initialiser(list *list);
void liberer(list *list); void liberer(list *list);
int contiens(list *l_ptr, int pid);
cell *trouver(list *l_ptr, int pid); cell *trouver(list *l_ptr, int pid);
#endif #endif

BIN
minishell

Binary file not shown.

View file

@ -21,14 +21,22 @@ jmp_buf start;
void handler_print(int signal_num, siginfo_t *info) void handler_print(int signal_num, siginfo_t *info)
{ {
if (contiens(&jobs, info->si_pid)) cell* job = trouver(&jobs, info->si_pid);
{ // pid dans la lsite des proccess en fond if (job)
{ // pid dans la lite des proccess en fond
if (job->state)
{
job->state = 0;
siglongjmp(start, signal_num);
}
if (prompting) if (prompting)
{ // si il n'y a pas d'execution de procces actuellement { // si il n'y a pas d'execution de proccess actuellement
printf("\n"); printf("\n");
} }
supprimer(&jobs, info->si_pid); printf("[%d] (%d) done: %s\n", job->id, job->pid, job->cmd);
supprimer(&jobs, job->pid);
job_id--; job_id--;
if (prompting) if (prompting)
@ -38,34 +46,34 @@ void handler_print(int signal_num, siginfo_t *info)
} }
} }
void handler_restart(int signal_num, siginfo_t *info) void handler_restart(int signal_num)
{ {
printf("\n"); printf("\n");
siglongjmp(start, signal_num); siglongjmp(start, signal_num);
} }
void handler_stop(int signal_num, siginfo_t *info) void handler_stop(int signal_num)
{ {
if (!prompting) if (!prompting)
{ {
kill(pidFils, SIGTSTP); kill(pidFils, SIGTSTP);
ajouter(&jobs, pidFils, job_id++, *(cmd->seq)); ajouter(&jobs, pidFils, job_id++, *(cmd->seq), 1);
printf("[%d] (%d) suspended: %s\n", job_id, pidFils, *(cmd->seq));
siglongjmp(start, signal_num); siglongjmp(start, signal_num);
} }
else else
{ {
handler_restart(signal_num, info); handler_restart(signal_num);
} }
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
extern int errno; extern int errno;
initialiser(&jobs); initialiser(&jobs);
// gestion de SIGCHLD // gestion des signaux
struct sigaction action; struct sigaction action;
sigemptyset(&action.sa_mask); sigemptyset(&action.sa_mask);
action.sa_flags = SA_SIGINFO | SA_RESTART; action.sa_flags = SA_SIGINFO | SA_RESTART;
@ -76,7 +84,7 @@ int main(int argc, char *argv[])
action.sa_handler = handler_stop; action.sa_handler = handler_stop;
sigaction(SIGTSTP, &action, NULL); sigaction(SIGTSTP, &action, NULL);
// variables de "pwd" // variables pour afficher le path
char initcd[256], currentcd[256]; char initcd[256], currentcd[256];
getcwd(initcd, 256); getcwd(initcd, 256);
@ -157,7 +165,8 @@ int main(int argc, char *argv[])
{ // instructions du père { // instructions du père
if (cmd->backgrounded) if (cmd->backgrounded)
{ // on sauvegarde le pid, si background { // on sauvegarde le pid, si background
ajouter(&jobs, pidFils, job_id++, *(cmd->seq)); ajouter(&jobs, pidFils, job_id++, *(cmd->seq), 0);
printf("[%d] %d\n", job_id, pidFils);
} }
else else
{ // on attend le fils, si foreground { // on attend le fils, si foreground
@ -178,5 +187,6 @@ int main(int argc, char *argv[])
} }
} }
liberer(&jobs);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

BIN
minishell2 Executable file

Binary file not shown.

122
minishell2.c Normal file
View file

@ -0,0 +1,122 @@
#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include <setjmp.h>
#include "readcmd.h"
#include "jobs.h"
struct cmdline *cmd;
int pid_fils;
int job_id = 0;
list jobs;
int prompting = 0;
jmp_buf goto_prompt;
void handler_sigchld(int signal_num, siginfo_t *info)
{
cell *job = trouver(&jobs, info->si_pid);
if (job)
{
if (prompting)
{
printf("\n");
}
printf("[%d] (%d) done: %s\n", job->id, job->pid, job->cmd);
supprimer(&jobs, job->pid);
job_id--;
if (prompting)
{
siglongjmp(goto_prompt, signal_num);
}
}
}
int main(int argc, char *argv[])
{
initialiser(&jobs);
extern int errno;
// gestion des signaux
struct sigaction action;
sigemptyset(&action.sa_mask);
action.sa_flags = SA_SIGINFO | SA_RESTART;
action.sa_handler = handler_sigchld;
sigaction(SIGCHLD, &action, NULL);
// loop principal
while (1)
{
sigsetjmp(goto_prompt, 1);
prompting = 1;
printf(">>> ");
cmd = readcmd();
prompting = 0;
if (cmd == NULL)
{ // EOF
break;
}
else if (cmd->seq[0] == NULL)
{ // empty
continue;
}
else if (!strcmp(cmd->seq[0][0], "exit"))
{ // "exit"
break;
}
pid_fils = fork();
if (pid_fils == -1)
{ // fork fail ?
fprintf(stderr, "ERROR: forking failed, (%d) %s\n", errno, strerror(errno));
exit(errno);
}
if (pid_fils == 0)
{ // instructions du fils
if (cmd->backgrounded)
{ // background
action.sa_handler = SIG_IGN;
sigaction(SIGINT, &action, NULL); // on ignore SIGINT
}
execvp(cmd->seq[0][0], cmd->seq[0]);
exit(errno); // si execlp échoue on exit avec une erreur
}
else
{ // instructions du père
if (cmd->backgrounded)
{ // background
ajouter(&jobs, pid_fils, ++job_id, *(cmd->seq), 0);
printf("[%d] %d\n", job_id, pid_fils);
}
else
{ // foreground
int wait_code;
pid_t id_fils = waitpid(pid_fils, &wait_code, 0); // on attend la fin de l'exec du fils
if (id_fils == -1)
{ // wait fail ?
fprintf(stderr, "ERROR: waiting for %d failed, (%d) %s\n", wait_code, errno, strerror(errno));
exit(errno);
}
if (wait_code)
{ // execvp fail ?
fprintf(stderr, "ERROR: %d's execution failed, (%d) %s\n", pid_fils, wait_code, strerror(wait_code));
}
}
}
}
return EXIT_SUCCESS;
}