Skip to content
Snippets Groups Projects
Commit 7458f409 authored by 최민정's avatar 최민정
Browse files

result

parents 85376be2 b37404c2
No related branches found
No related tags found
No related merge requests found
......@@ -5,8 +5,15 @@
#include <sys/stat.h>
#include <unistd.h>
#include <linux/limits.h>
#include <signal.h>
#include "built_in.h"
#include "signal_handlers.h"
#include "commands.h"
int pid_list[512];
void add_pid(int pid){pid_list[sizeof(pid_list)] = pid;}
int do_cd(int argc, char** argv) {
if (!validate_cd_argv(argc, argv))
......@@ -35,16 +42,21 @@ int do_pwd(int argc, char** argv) {
int do_fg(int argc, char** argv) {
if (!validate_fg_argv(argc, argv))
return -1;
int pid;
char curdir[PATH_MAX];
int pid = getpid();
getcwd(curdir , PATH_MAX);
printf("%s running\t %s\n",pid,curdir);
signal(19,SIG_DFL);
while(pid == NULL)
pid = pid_list;
kill(pid,18);
return 0;
}
int validate_cd_argv(int argc, char** argv) {
if (argc != 2) return 0;
if (strcmp(argv[0], "cd") != 0) return 0;
......
......@@ -2,7 +2,7 @@
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include "commands.h"
#include "built_in.h"
......@@ -12,6 +12,9 @@ static struct built_in_command built_in_commands[] = {
{ "fg", do_fg, validate_fg_argv }
};
int pid_list[512];
int n_pid=0;
static int is_built_in_command(const char* command_name)
{
static const int n_built_in_commands = sizeof(built_in_commands) / sizeof(built_in_commands[0]);
......@@ -29,25 +32,53 @@ static int is_built_in_command(const char* command_name)
* Description: Currently this function only handles single built_in commands. You should modify this structure to launch process and offer pipeline functionality.
*/
void creation_process(const char *Path, char *const argv[]){
pid_t pid;
int background(int argc, char** argv){
int pid, buf;
if(!validate_bg(argc,argv))
return -1;
const char use_path = Path +
buf = process_creation(argv);
if(buf == -1)
return -1;
pid = fork();
if(buf>0)
pid = buf;
if(pid>=0){
if(pid == 0)
execv(Path, argv);
else
}
else{
printf("Sorry, Fork failed.\n");
add_pid(pid);
printf("%d",pid);
return 1;
}
int validate_bg(int argc, char** argv){
if(argc != 1)
return 0;
if(strcmp(argv[0], "&") != 0)
return 0;
if(access(argv[0],F_OK) == 0)
return 0;
return 1;
}
int process_creation(struct single_command (*commands)[]){
int pid;
struct single_command *com=(*commands);
char path[4096];
strcpy(path,(*com).argv[0]);
pid = fork();
if(pid>=0){
if(pid == 0)
execv(path,(*com).argv);
return pid;
}else{
printf("Sorry, Fork Failed!");
return -1;
}
}
int evaluate_command(int n_commands, struct single_command (*commands)[512])
{
......@@ -70,6 +101,13 @@ int evaluate_command(int n_commands, struct single_command (*commands)[512])
return 0;
} else if (strcmp(com->argv[0], "exit") == 0) {
return 1;
}else if(strcmp(com->argv[n_commands], "&") == 0){
if(background(com->argc,com->argv) == -1)
return -1;
return 0;
}else if (process_creation((commands))==-1){
return 0;
}else{
fprintf(stderr, "%s: command not found\n", com->argv[0]);
return -1;
......@@ -92,7 +130,8 @@ void free_commands(int n_commands, struct single_command (*commands)[512])
}
free(argv);
}
memset((*commands), 0, sizeof(struct single_command) * n_commands);
}
}
......@@ -5,14 +5,19 @@
#include "commands.h"
#include "built_in.h"
#include "utils.h"
#include "signal_handlers.h"
#include <signal.h>
int main()
{
char buf[8096];
while (1) {
signal(SIGINT,catch_sigint);
signal(SIGTSTP,catch_sigtstp);
fgets(buf, 8096, stdin);
struct single_command commands[512];
int n_commands = 0;
mysh_parse_command(buf, &n_commands, &commands);
......
#include "signal_handlers.h"
#include <signal.h>
void catch_sigint(int signalNo)
{
while(1)
sleep(1);
signal(signalNo,SIG_IGN);
}
void catch_sigtstp(int signalNo)
{
signal(18,SIG_DFL);
}
......@@ -12,7 +12,7 @@ void mysh_parse_command(const char* command,
strcpy(buf, command);
char *saveptr = NULL;
char *tok = strtok_r(buf, "|", &saveptr);
char *tok = strtok_r(buf, "|", &saveptr);//pipe
int ti = 0;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment