- Unix
- Gcc
gcc -o shellfinal shellfinal.c
- exit
- cd
- pwd
- env
- export
- set
- unset
- history
- background process (&)
To start simple shell, run the command below:
./shellfinal
- For internal command pwd, cd, set, unset, export
$ pwd
$ cd
$ export
$ export MYVAR="abcd"
$ set MYVAR="abcd"
$ unset MYVAR
$ sleep 30 &
$ cat log.txt &
- For external command
$ ls
$ ls -l
$ ps
$ cat README.md
- Execute history
$ !!
- Single redirection
$ ls -l > output
$ grep README < output
- Single pipe
$ cat output | grep README
$ cat > file.txt
abc
hsds
bbcb
asdas
pofas
$ cat file.txt | sort
-
Creating the child process and executing the command in the child
- pid_t fork(void);
- Call once, return twice
- Concurrent excution
- Duplicate but separate address spaces
- Shared files
- pid_t waitpid(pid_t pid, int *startup, int options); -> background process
- pid_t wait(int *startus); Calling wait(&status) is equivalent to calling waitpid(-1, &status, 0).
Problem internal command pwd, cd
- Implement internal command: pwd, cd, exit, export, set, unset
- pid_t fork(void);
-
Providing a history feature
- Save command in log.txt
- !! excute the last command in log.txt
-
Adding support of input and output redirection
- int creat(char *filename, mode_t mode);
- int open (const char* Path, int flags [, int mode ]);
- int dup2(int oldfd, int newfd);
-
Allowing the parent and child processes to communicate via a pipe
- int pipe(int fds[2]);
- fds[0] for read
- fds[1] for write
- Backup file description STDIN and STDOUT
- Open pipe and exec
- int pipe(int fds[2]);
- 1712689 Huỳnh Ngọc Quân
- 1712759 Phạm Minh Thắng
- 1712698 Võ Văn Quân
Ho Chi Minh University of Science