This commit is contained in:
Laureηt 2021-04-21 13:50:58 +02:00
parent 5b3e7f5ed4
commit 37451f22b7
6 changed files with 102 additions and 55 deletions

BIN
jobs

Binary file not shown.

88
jobs.c
View file

@ -3,78 +3,106 @@
#include <string.h> #include <string.h>
#include "jobs.h" #include "jobs.h"
void ajouter(list* l_ptr, int pid, int id) { void ajouter(list *l_ptr, int pid, int id, char*** cmd)
{
cell *new_cell = malloc(sizeof(*new_cell));
char *name_with_extension;
name_with_extension = malloc(sizeof(char)*256);
cell* new_cell = malloc(sizeof(*new_cell)); char** cursor = *cmd;
strcpy(name_with_extension, *cursor);
cursor++;
strcat(name_with_extension, *cursor);
cursor++;
char* test = *cursor;
new_cell->pid = pid; new_cell->pid = pid;
new_cell->id = id; new_cell->id = id;
if (*l_ptr == NULL) { if (*l_ptr == NULL)
{
new_cell->next = NULL; new_cell->next = NULL;
} else { }
else
{
new_cell->next = *l_ptr; new_cell->next = *l_ptr;
} }
*l_ptr = new_cell; *l_ptr = new_cell;
printf("[%d] %d\n", id, pid); printf("[%d] %d\n", id, pid);
} }
void supprimer(list* l_ptr, int pid) { void supprimer(list *l_ptr, int pid)
{
cell* cursor = *l_ptr;
if (cursor->pid == pid) { cell *cursor = *l_ptr;
cell* cursor2free = cursor;
*l_ptr = cursor->next; if (cursor->pid == pid)
{
cell *cursor2free = cursor;
*l_ptr = cursor->next;
free(cursor2free); free(cursor2free);
} else { }
while (cursor->next != NULL) { else
if (cursor->next->pid == pid) { {
while (cursor->next != NULL)
{
if (cursor->next->pid == pid)
{
break; break;
} else { }
else
{
cursor = cursor->next; cursor = cursor->next;
} }
} }
cell* cursor_next = cursor->next->next; cell *cursor_next = cursor->next->next;
free(cursor->next); free(cursor->next);
cursor->next = cursor_next; cursor->next = cursor_next;
} }
} }
void afficher(list* l_ptr) { void afficher(list *l_ptr)
{
cell* cursor = *l_ptr; cell *cursor = *l_ptr;
while (cursor != NULL) { while (cursor != NULL)
{
printf("[%d] %d\n", cursor->id, cursor->pid); printf("[%d] %d\n", cursor->id, cursor->pid);
cursor = cursor->next; cursor = cursor->next;
} }
} }
void initialiser(list* l_ptr) { void initialiser(list *l_ptr)
{
*l_ptr = NULL; *l_ptr = NULL;
} }
void liberer(list* l_ptr) { void liberer(list *l_ptr)
{
free(*l_ptr); free(*l_ptr);
*l_ptr = NULL; *l_ptr = NULL;
} }
int est_vide(list l_ptr) { int est_vide(list l_ptr)
{
return l_ptr == NULL; return l_ptr == NULL;
} }
int contiens(list* l_ptr, int pid) { int contiens(list *l_ptr, int pid)
{
cell* cursor = *l_ptr;
while (cursor != NULL) { cell *cursor = *l_ptr;
if (cursor->pid == pid) {
while (cursor != NULL)
{
if (cursor->pid == pid)
{
return 1; return 1;
} }
cursor = cursor->next; cursor = cursor->next;

10
jobs.h
View file

@ -5,22 +5,18 @@ typedef struct cell cell;
struct cell { struct cell {
int id; int id;
int pid; int pid;
int state;
char* cmd; char* cmd;
cell* next; cell* next;
}; };
typedef cell* list; typedef cell* list;
void ajouter(cell** list, int pid, int id); void ajouter(cell** list, int pid, int id, char*** cmd);
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); int contiens(list* l_ptr, int pid);
cell* trouver(list* l_ptr, int pid);
#endif #endif
// state:
// 0: running
// 1: suspended

BIN
minishell

Binary file not shown.

View file

@ -14,6 +14,7 @@
int prompting = 0; int prompting = 0;
int job_id = 1; int job_id = 1;
list jobs; list jobs;
pid_t pidFils;
jmp_buf start; jmp_buf start;
@ -25,9 +26,16 @@ void handler_print(int signal_num, siginfo_t *info)
{ // si il n'y a pas d'execution de procces actuellement { // si il n'y a pas d'execution de procces actuellement
printf("\n"); printf("\n");
} }
// TODO: trouver id, cmd... à partir pid
printf("[%d] --> (%d) %s\n", info->si_pid, signal_num, strsignal(signal_num)); printf("[%d] --> (%d) %s\n", info->si_pid, signal_num, strsignal(signal_num));
supprimer(&jobs, info->si_pid); supprimer(&jobs, info->si_pid);
job_id--; job_id--;
if (prompting)
{ // on réaffiche un prompt si besoin
siglongjmp(start, signal_num);
}
} }
} }
@ -37,6 +45,20 @@ void handler_restart(int signal_num, siginfo_t *info)
siglongjmp(start, signal_num); siglongjmp(start, signal_num);
} }
void handler_stop(int signal_num, siginfo_t *info)
{
if (!prompting)
{
kill(pidFils, SIGTSTP);
//ajouter(&jobs, pidFils, job_id++, cmd);
siglongjmp(start, signal_num);
}
else
{
handler_restart(signal_num, info);
}
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
@ -47,11 +69,13 @@ int main(int argc, char *argv[])
// gestion de SIGCHLD // gestion de SIGCHLD
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;
action.sa_handler = handler_print; action.sa_handler = handler_print;
sigaction(SIGCHLD, &action, NULL); sigaction(SIGCHLD, &action, NULL);
action.sa_handler = handler_restart; action.sa_handler = handler_restart;
sigaction(SIGINT, &action, NULL); sigaction(SIGINT, &action, NULL);
action.sa_handler = handler_stop;
sigaction(SIGTSTP, &action, NULL);
// variables de "pwd" // variables de "pwd"
char initcd[256], currentcd[256]; char initcd[256], currentcd[256];
@ -60,7 +84,6 @@ int main(int argc, char *argv[])
// loop principal // loop principal
while (1) while (1)
{ {
if (sigsetjmp(start, 1) == 2) if (sigsetjmp(start, 1) == 2)
{ // si on provient du jump { // si on provient du jump
continue; continue;
@ -82,6 +105,11 @@ int main(int argc, char *argv[])
{ // ligne vide, on skip { // ligne vide, on skip
continue; continue;
} }
else if (!strcmp(cmd->seq[0][0], "bg"))
{
kill(pidFils, SIGCONT);
continue;
}
else if (!strcmp(cmd->seq[0][0], "exit")) else if (!strcmp(cmd->seq[0][0], "exit"))
{ {
break; break;
@ -90,7 +118,7 @@ int main(int argc, char *argv[])
{ // cd { // cd
int ret = 0; int ret = 0;
if (cmd->seq[0][1] == NULL) if (cmd->seq[0][1] == NULL)
{ // vide { // vide, sans path
ret = chdir(initcd); ret = chdir(initcd);
} }
else else
@ -109,7 +137,7 @@ int main(int argc, char *argv[])
continue; continue;
} }
pid_t pidFils = fork(); pidFils = fork();
if (pidFils == -1) if (pidFils == -1)
{ // si le fork échoue { // si le fork échoue
@ -119,6 +147,11 @@ int main(int argc, char *argv[])
if (pidFils == 0) if (pidFils == 0)
{ // instructions du fils { // instructions du fils
if (cmd->backgrounded)
{
action.sa_handler = SIG_IGN;
sigaction(SIGINT, &action, NULL); // on ignore SIGINT
}
execvp(cmd->seq[0][0], cmd->seq[0]); execvp(cmd->seq[0][0], cmd->seq[0]);
exit(errno); // si execlp échoue on exit avec une erreur exit(errno); // si execlp échoue on exit avec une erreur
} }
@ -126,27 +159,17 @@ 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++); ajouter(&jobs, pidFils, job_id++, cmd->seq);
} }
else else
{ // on attend le fils, si foreground { // on attend le fils, si foreground
jmp_buf env;
setjmp(env); // sauvegarde pour un jump
int codeTerm; int codeTerm;
pid_t idFils = waitpid(pidFils, &codeTerm, 0); // on attend la fin de l'exec du fils pid_t idFils = waitpid(pidFils, &codeTerm, 0); // on attend la fin de l'exec du fils
if (idFils == -1) if (idFils == -1)
{ // si le wait fail { // si le wait fail
if (errno == 4) fprintf(stderr, "ERROR: waiting for %d failed, (%d) %s\n", codeTerm, errno, strerror(errno));
{ exit(errno);
longjmp(env, 1); // si interruption du wait, on jump
}
else
{
fprintf(stderr, "ERROR: waiting for %d failed, (%d) %s\n", codeTerm, errno, strerror(errno));
exit(errno);
}
} }
if (codeTerm) if (codeTerm)

View file

@ -1,2 +1,2 @@
sleep 5 sleep 5
echo bonjourr printf "\n\nbonjourr\n\n"