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.

80
jobs.c
View file

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

8
jobs.h
View file

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

BIN
minishell

Binary file not shown.

View file

@ -14,6 +14,7 @@
int prompting = 0;
int job_id = 1;
list jobs;
pid_t pidFils;
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
printf("\n");
}
// TODO: trouver id, cmd... à partir pid
printf("[%d] --> (%d) %s\n", info->si_pid, signal_num, strsignal(signal_num));
supprimer(&jobs, info->si_pid);
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);
}
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[])
{
@ -47,11 +69,13 @@ int main(int argc, char *argv[])
// gestion de SIGCHLD
struct sigaction action;
sigemptyset(&action.sa_mask);
action.sa_flags = SA_SIGINFO; //| SA_RESTART;
action.sa_flags = SA_SIGINFO | SA_RESTART;
action.sa_handler = handler_print;
sigaction(SIGCHLD, &action, NULL);
action.sa_handler = handler_restart;
sigaction(SIGINT, &action, NULL);
action.sa_handler = handler_stop;
sigaction(SIGTSTP, &action, NULL);
// variables de "pwd"
char initcd[256], currentcd[256];
@ -60,7 +84,6 @@ int main(int argc, char *argv[])
// loop principal
while (1)
{
if (sigsetjmp(start, 1) == 2)
{ // si on provient du jump
continue;
@ -82,6 +105,11 @@ int main(int argc, char *argv[])
{ // ligne vide, on skip
continue;
}
else if (!strcmp(cmd->seq[0][0], "bg"))
{
kill(pidFils, SIGCONT);
continue;
}
else if (!strcmp(cmd->seq[0][0], "exit"))
{
break;
@ -90,7 +118,7 @@ int main(int argc, char *argv[])
{ // cd
int ret = 0;
if (cmd->seq[0][1] == NULL)
{ // vide
{ // vide, sans path
ret = chdir(initcd);
}
else
@ -109,7 +137,7 @@ int main(int argc, char *argv[])
continue;
}
pid_t pidFils = fork();
pidFils = fork();
if (pidFils == -1)
{ // si le fork échoue
@ -119,6 +147,11 @@ int main(int argc, char *argv[])
if (pidFils == 0)
{ // 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]);
exit(errno); // si execlp échoue on exit avec une erreur
}
@ -126,28 +159,18 @@ int main(int argc, char *argv[])
{ // instructions du père
if (cmd->backgrounded)
{ // on sauvegarde le pid, si background
ajouter(&jobs, pidFils, job_id++);
ajouter(&jobs, pidFils, job_id++, cmd->seq);
}
else
{ // on attend le fils, si foreground
jmp_buf env;
setjmp(env); // sauvegarde pour un jump
int codeTerm;
pid_t idFils = waitpid(pidFils, &codeTerm, 0); // on attend la fin de l'exec du fils
if (idFils == -1)
{ // si le wait fail
if (errno == 4)
{
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)
{ // si l'exec a échoué

View file

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