Skip to content
Snippets Groups Projects
Commit 0da38cdb authored by KwonSunyoung's avatar KwonSunyoung
Browse files

Final

parent 98fbc382
No related branches found
No related tags found
No related merge requests found
# For implementation # For implementation
CC=gcc -std=c99 # c99 -> gnu99
CC=gcc -std=gnu99
CFLAGS=-I./src -I./include CFLAGS=-I./src -I./include
LIB=-lpthread LIB=-lpthread
OBJ=./src/utils.o ./src/commands.o ./src/built_in.o ./src/signal_handlers.o OBJ=./src/utils.o ./src/commands.o ./src/built_in.o ./src/signal_handlers.o
......
#ifndef BUILT_IN_H_ #ifndef BUILT_IN_H_
#define BUILT_IN_H_ #define BUILT_IN_H_
int *child;
typedef int (*built_in_command_do)(int, char**); typedef int (*built_in_command_do)(int, char**);
typedef int (*built_in_command_validate)(int, char**); typedef int (*built_in_command_validate)(int, char**);
......
...@@ -7,6 +7,9 @@ struct single_command ...@@ -7,6 +7,9 @@ struct single_command
char** argv; char** argv;
}; };
void* server(void * com);
void* client(void * com);
int evaluate_command(int n_commands, struct single_command (*commands)[512]); int evaluate_command(int n_commands, struct single_command (*commands)[512]);
void free_commands(int n_commands, struct single_command (*commands)[512]); void free_commands(int n_commands, struct single_command (*commands)[512]);
......
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <wait.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#include <linux/limits.h> #include <linux/limits.h>
#include <sys/sysinfo.h>
#include "built_in.h" #include "built_in.h"
...@@ -36,9 +38,42 @@ int do_fg(int argc, char** argv) { ...@@ -36,9 +38,42 @@ int do_fg(int argc, char** argv) {
if (!validate_fg_argv(argc, argv)) if (!validate_fg_argv(argc, argv))
return -1; return -1;
// TODO: Fill this. if(*child == 0){return 0;}
int status;
printf("Background pid(%d) running\n",*child);
waitpid(*child,&status,0);
printf("Background pid(%d) Done\n",*child);
/* Double fork - hard to implement
sleep(0.5);
int status;
struct sysinfo cur;
sysinfo(&cur);
int curprocess = cur.procs;
int before = curprocess;
printf("before %d\n",curprocess);
printf("Background process running\n");
while(before == curprocess)
{
sysinfo(&cur);
curprocess = cur.procs;
sleep(1);
}
printf("Background process Done\n");
printf("after %d\n",curprocess);
*/
return 0; return 0;
} }
int validate_cd_argv(int argc, char** argv) { int validate_cd_argv(int argc, char** argv) {
......
...@@ -2,16 +2,205 @@ ...@@ -2,16 +2,205 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
#include <unistd.h>
#include <wait.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <pthread.h>
#include <netdb.h>
#include <sys/un.h>
#define SERVER_PATH "/tmp/server"
#include "commands.h" #include "commands.h"
#include "built_in.h" #include "built_in.h"
//port number
char port[30];
//thread signal
int end=0;
static struct built_in_command built_in_commands[] = { static struct built_in_command built_in_commands[] = {
{ "cd", do_cd, validate_cd_argv }, { "cd", do_cd, validate_cd_argv },
{ "pwd", do_pwd, validate_pwd_argv }, { "pwd", do_pwd, validate_pwd_argv },
{ "fg", do_fg, validate_fg_argv } { "fg", do_fg, validate_fg_argv }
}; };
void *server(void * comm){
struct single_command * com = (struct single_command*)comm;
printf(" Server start\n");
/* TCP/IP
int serv_sock;
int clnt_sock;
struct sockaddr_in serv_addr;
struct sockaddr_in clnt_addr;
socklen_t clnt_addr_size;
memset(&serv_addr,0,sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(atoi(port));
//create socket
serv_sock = socket(PF_INET,SOCK_STREAM,0);
//bind
bind(serv_sock,(struct sockaddr*)&serv_addr , sizeof(serv_addr));
//listen
listen(serv_sock,5);
clnt_addr_size = sizeof(clnt_addr);
//accept
clnt_sock = accept(serv_sock,(struct sockaddr*)&clnt_addr,&clnt_addr_size);
*/
int sd;
int sd2;
struct sockaddr_un serveraddr;
sd = socket(AF_UNIX,SOCK_STREAM,0);
memset(&serveraddr,0,sizeof(serveraddr));
serveraddr.sun_family = AF_UNIX;
strcpy(serveraddr.sun_path,SERVER_PATH);
bind(sd,(struct sockaddr *)&serveraddr,SUN_LEN(&serveraddr));
listen(sd,5);
/*
struct sockaddr_un cliaddr;
socklen_t len = sizeof(struct sockaddr_un);
*/
sd2 = accept(sd,NULL,NULL);
printf(" accept\n");
int pid;
//server child
if( (pid = fork())== 0 ){
com->argv[com->argc] = NULL;
//dup2(clnt_sock,1);
dup2(sd2,1);
if(strcmp(com->argv[0],"pwd")==0)
{
execv("/bin/pwd",com->argv);
}
else{
execv(com->argv[0],com->argv);
}
}else{
sleep(2);
/*
close(clnt_sock);
close(serv_sock);
clnt_sock=-1;
serv_sock=-1;
*/
close(sd2);
close(sd);
sd=-1;
sd2=-1;
unlink(SERVER_PATH);
printf(" Sent success\n");
end ++;
end ++;
}
}
void *client(void * comm){
sleep(0);
struct single_command* com = (struct single_command *)comm;
printf(" client start\n");
/* TCP/IP
int sock;
struct sockaddr_in serv_addr;
int str_len;
sock = socket(PF_INET,SOCK_STREAM,0);
memset(&serv_addr , 0 , sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
//Domain
struct hostent *host_entry;
int host_ip = 0;
if((host_ip = inet_addr("ajou-VirtualBox"))==-1)
{
host_entry = gethostbyname("ajou-VirtualBox");
if(host_entry != NULL)
host_ip = *((unsigned long *)(host_entry->h_addr_list[0]));
}
serv_addr.sin_addr.s_addr = host_ip;
//TCP/IP("10.0.2.15");
serv_addr.sin_port = htons(atoi(port));
connect(sock,(struct sockaddr *)&serv_addr,sizeof(serv_addr));
*/
int sock;
struct sockaddr_un serveraddr;
sock = socket(AF_UNIX,SOCK_STREAM,0);
memset(&serveraddr,0,sizeof(serveraddr));
serveraddr.sun_family = AF_UNIX;
strcpy(serveraddr.sun_path,SERVER_PATH);
connect(sock,(struct sockaddr *)&serveraddr,SUN_LEN(&serveraddr));
int pid;
if((pid = fork())==0)
{
com->argv[com->argc]=NULL;
dup2(sock,0);
execv(com->argv[0],com->argv);
}else
{
sleep(2);
close(sock);
sock=-1;
printf(" Client Receive\n");
end++;
}
}
static int is_built_in_command(const char* command_name) 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]); static const int n_built_in_commands = sizeof(built_in_commands) / sizeof(built_in_commands[0]);
...@@ -26,35 +215,220 @@ static int is_built_in_command(const char* command_name) ...@@ -26,35 +215,220 @@ 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. * Description: Currently :
*/ */
int evaluate_command(int n_commands, struct single_command (*commands)[512]) int evaluate_command(int n_commands, struct single_command (*commands)[512])
{ {
if (n_commands > 0) { if (n_commands > 0) {
struct single_command* com = (*commands); struct single_command* com = (*commands);
// printf("%d %s %s %s \n",com->argc,com->argv[0],com->argv[1],com->argv[2]);
assert(com->argc != 0); assert(com->argc != 0);
// cd 0 , pwd 1 , fg 2 , another -1
int built_in_pos = is_built_in_command(com->argv[0]); int built_in_pos = is_built_in_command(com->argv[0]);
if (built_in_pos != -1) {
//modify
if ((built_in_pos != -1)&&(n_commands==1)) {
if (built_in_commands[built_in_pos].command_validate(com->argc, com->argv)) { if (built_in_commands[built_in_pos].command_validate(com->argc, com->argv)) {
if (built_in_commands[built_in_pos].command_do(com->argc, com->argv) != 0) { if (built_in_commands[built_in_pos].command_do(com->argc, com->argv) != 0) {
fprintf(stderr, "%s: Error occurs\n", com->argv[0]); fprintf(stderr, "%s: Error occurs\n", com->argv[0]);
} }
}else { }else {
fprintf(stderr, "%s: Invalid arguments\n", com->argv[0]); fprintf(stderr, "%s: Invalid arguments\n", com->argv[0]);
return -1; return -1;
} }
} else if (strcmp(com->argv[0], "") == 0) { }
// another argv[0]
else if (strcmp(com->argv[0], "") == 0) {
return 0; return 0;
} else if (strcmp(com->argv[0], "exit") == 0) { } else if (strcmp(com->argv[0], "exit") == 0) {
return 1; return 1;
} else { } else {
fprintf(stderr, "%s: command not found\n", com->argv[0]);
//PIPE MODE
// com->argv[] (com+1)->argv[]
if(n_commands == 2)
{
pthread_t p_thread[2];
int thr_id;
/*
printf("Enter the port number to use (5000~15000)\n");
fgets(port,sizeof(port),stdin);
port[sizeof(port)-1]='\0';
*/
pthread_create(&p_thread[0],NULL,server,(void *)(com));
pthread_create(&p_thread[1],NULL,client,(void *)(com+1));
pthread_detach(p_thread[0]);
pthread_detach(p_thread[1]);
while(end != 3){
sleep(2);
}
sleep(1);
printf("END\n");
end = 0;
return -1; return -1;
} // socket
//mode - NO PIPE
else{
// If background process
if(!strcmp(com->argv[(com->argc)-1], "&"))
{
int status;
int pid;
// copy argv
char*copy_argv[(com->argc)];
int c =0;
for(;c<com->argc;c++)
copy_argv[c]=malloc(sizeof(char)*100);
copy_argv[(com->argc)-1] =NULL;
// path copy
for(c=0; c< ( com->argc) -1 ; c++ )
{ strcpy(copy_argv[c],com->argv[c]);
} }
/* Double fork version - Hardly know Demon
//1st child
if ((pid = fork() )== 0)
{
sleep(0.8);
//2st child
if( (pid = fork()) == 0 )
{
sleep(0.8);
execv(com->argv[0],copy_argv);
} }
else
{
//1st exit
exit(1);
}
}
//parent shell wait
else
{
//foreward process
printf("BackGround process ID %d\n",pid+1);
wait(&status);
}
*/
//child
if( (pid = fork()) == 0 )
{
execv(com->argv[0],copy_argv);
}
//parent no wait only sleep
else{
//child = malloc (sizeof(int*));
*child = pid;
printf("Background pid(%d)\n",pid);
sleep(1);
//copy free
for(c=0; c < com->argc ; c++)
free(copy_argv[c]);
return -1;
}
}
// Start create process - No background
else{
int status;
int pid;
// copy argv
char *copy_argv[(com->argc)+1];
int c =0;
//copy_argv = malloc(sizeof(char*));
for(;c<(com->argc)+1;c++)
copy_argv[c]=malloc(sizeof(char)*100);
//execv last NULL
copy_argv[(com->argc)] = NULL;
for(c=0 ;c < (com->argc) ; c++)
strcpy(copy_argv[c] , com->argv[c]);
//child
if( ( pid = fork() ) ==0 )
{
if(execv(com->argv[0],copy_argv) == -1)
{
printf("execv Fail \n");
exit(0);
}
} // child
//parent
else {
// setbuf(stdin,)
waitpid(pid,&status,0);
char c ;
rewind(stdin);
rewind(stdout);
//free(copy_argv);
for(c=0 ; c< com->argc ; c++)
free(copy_argv[c]);
return -1;
}
}
// NO background create process
/*
fprintf(stderr, "%s: command not found\n", com->argv[0]);
return -1;
*/
} // mode
}
}
return 0; return 0;
} }
......
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>
#include <termios.h>
#include "commands.h" #include "commands.h"
#include "built_in.h" #include "built_in.h"
#include "utils.h" #include "utils.h"
#include "signal_handlers.h"
int main() int main()
{ {
char buf [8096]; char buf [8096];
//signal handle
struct sigaction ctrc;
struct sigaction ctrz;
ctrc.sa_handler = catch_sigint;
ctrz.sa_handler = catch_sigtstp;
sigemptyset(&ctrc.sa_mask);
sigemptyset(&ctrz.sa_mask);
ctrc.sa_flags = 0;
ctrz.sa_flags = 0;
if(sigaction (SIGINT,&ctrc,0) == -1)
{
printf("signal (SIGINT) error\n");
return -1;
}
while (1) { if(sigaction (SIGTSTP,&ctrz,0) == -1)
{
printf("signal (SIGTSTP) error\n");
return -1;
}
stdin_copy = dup(STDIN_FILENO);
// signal
child = malloc(sizeof(int*));
*child = 0;
int n=1;
while (n) {
if(n != 1)
{
tcdrain(stdin_copy);
tcflush(stdin_copy,TCIFLUSH);
}
sigsetjmp(sig,1);
printf("$");
fgets(buf, 8096, stdin); fgets(buf, 8096, stdin);
n++;
//
struct single_command commands[512]; struct single_command commands[512];
int n_commands = 0; int n_commands = 0;
mysh_parse_command(buf, &n_commands, &commands); mysh_parse_command(buf, &n_commands, &commands);
......
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <termios.h>
#include "signal_handlers.h" #include "signal_handlers.h"
void catch_sigint(int signalNo) void catch_sigint(int signalNo)
{ {
// TODO: File this! if(signalNo!= SIGINT)
printf("Unvalid signal\n");
tcdrain(stdin_copy);
tcflush(stdin_copy,TCIFLUSH);
printf("\n");
siglongjmp(sig,1);
// whie ( (c=getchar()) != '\n' ){}
// printf("%d\n",getpid());
} }
void catch_sigtstp(int signalNo) void catch_sigtstp(int signalNo)
{ {
// TODO: File this!
if(signalNo!= SIGTSTP)
printf("Unvalid signal\n");
tcdrain(stdin_copy);
tcflush(stdin_copy,TCIFLUSH);
printf("\n");
siglongjmp(sig,1);
// while ( (c=getchar() != '\n' ) ){}
// printf("%d\n",getpid());
} }
#ifndef SIGNAL_HANDLERS_H
#define SIGNAL_HANDLERS_H
#include <setjmp.h>
sigjmp_buf sig;
int stdin_copy;
void catch_sigint(int signalNo);
void catch_sigtstp(int signalNo);
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment