Skip to content

Commit 36a2765

Browse files
author
hyqooo
committed
SIGTSTP doesn't work
1 parent 32734fe commit 36a2765

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

shell.c

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ char *detpath(char *);
5151
char **args_proc(char *, struct tokens *);
5252

5353
int redirection(char *, int);
54+
void parent_sig_handler();
55+
void child_sig_handler();
5456

5557
const int MAX_PATH_SIZE = 128;
5658

@@ -127,11 +129,16 @@ int shell_exec(struct tokens *tokens){
127129
cpid = fork();
128130
/* cpid > 0 - parent process, cpid == 0 - child process, cpid < 0 - error */
129131
if (cpid > 0) {
132+
parent_sig_handler();
133+
134+
pid_t pid = getpid();
135+
setpgid(pid, 0);
136+
pid_t pgid = getpgid(0);
137+
tcsetpgrp(0, pgid);
138+
130139
wait(&status);
131140
} else if (cpid == 0){
132-
/* set process group id */
133-
// setpgid(getpid(), 0);
134-
//tcsetpgrp(0, getpgid(getpid()));
141+
child_sig_handler();
135142
/* executes program according to path and given arguments */
136143
execv(path, args);
137144
exit(0);
@@ -150,6 +157,18 @@ int shell_exec(struct tokens *tokens){
150157
return 1;
151158
}
152159

160+
void parent_sig_handler(){
161+
signal(SIGINT, SIG_IGN);
162+
signal(SIGTSTP, SIG_IGN);
163+
signal(SIGQUIT, SIG_IGN);
164+
}
165+
166+
void child_sig_handler(){
167+
signal(SIGINT, SIG_DFL);
168+
signal(SIGTSTP, SIG_DFL);
169+
signal(SIGQUIT, SIG_DFL);
170+
}
171+
153172
/* processes arguments to determine what is this redirection or merely arguments passing */
154173
char** args_proc(char *path, struct tokens *tokens){
155174
int i;

0 commit comments

Comments
 (0)