Skip to content

Commit

Permalink
3rd learning
Browse files Browse the repository at this point in the history
  • Loading branch information
redtree0 committed Nov 23, 2018
1 parent 79b3267 commit 1fe50ae
Show file tree
Hide file tree
Showing 17 changed files with 190 additions and 18 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#Learning-C
# Learning-C

Shell
* Shell
* Process
161 changes: 161 additions & 0 deletions process/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
# Process
[Ref] fork and exec <http://channelofchaos.tistory.com/55>

[Ref] System Call과 SubRoutine의 차이 <http://channelofchaos.tistory.com/37?category=382257>

[Ref] Differences between fork and exec <https://stackoverflow.com/questions/1653340/differences-between-fork-and-exec>

[Ref] System call <http://duksoo.tistory.com/entry/System-call-등록-순서>

[Ref] Subroutine <a name="subroutine_wiki"></a> <https://en.wikipedia.org/wiki/Subroutine>

[Ref] What is the difference between system call and library call? <https://stackoverflow.com/questions/29816791/what-is-the-difference-between-system-call-and-library-call>

## Subroutine vs System Call
쉽게 말하면 subroutine과 system call의 차이는 접근영역이 다르다.
subroutine이 system call을 호출하고 system call이 수행한 결과를 subroutine에 보내준다.
![Alt text](./assets/eco.png "unix eco")

## Subroutine
[Subroutine](#subroutine_wiki)은 각 언어별 function, procedure, routine, method, subprogram 이렇게도 불린다.

함수 호출 방식 중 Call by Value, Call by Reference뿐만 아니라 wiki에 Call by result, Call by value-result, Call by name, Call by constant value 등의 기법이 있다.

## System Call
system call은 커널을 통해 h/w 자원을 제어한다. 이 문서는 process control 관점에서 설명할 것이다.
### 1. File I/O
### 2. Process Control
* fork()
* wait()
* exec : execcl(), execlp(), execv(), execvp()

### 3. InterProcess Communication

## fork vs exec

![Alt text](./assets/fork_exec.png "fork exec")


fork와 exec를 OS 관점에서 설명할 것이다.

![Alt text](./assets/unix_process.png "unix process")


### fork
c언어로 다음과 같은 fork 예제를 실행해보자.

```c
int cpid = fork( );

if (cpid = = 0)
{

//child code

exit(0);

}

//parent code

wait(cpid);

// end
```
#### 1. 프로그램 실행
pid는 다음과 같다.
| pid|ppid| cpid|description|
|--- |---| --- | ---|
|25 | 1 | 0 |main process |
![Alt text](./assets/fork_step01.png "fork step01")
#### 2. fork 실행
fork로 process가 복제되고 child는 다른 pid가 할당된다.
| pid |ppid| cpid| description |
|---- |--- | --- | ------------------------ |
| 25 | 1 | 26 | main process (parent process) |
| 26 | 25 | 0 | forked process (child process)|
![Alt text](./assets/fork_step02.png "fork step02")
#### 3. fork pid 반환
fork의 반환 pid 값은 그 프로세스의 child pid이기 때문에 어느 프로세스이냐에 따라 다르다. 변수명을 cpid로 정의한 이유도 그것 때문
cpid == 0일때, child process
cpid > 0일때, parent process
cpid < 0일때, error
| pid|ppid| cpid|description|
|--- |---| --- | ---|
|25 | 1 | 26 |main process |
|26 | 25 | 0 |forked process |
![Alt text](./assets/fork_step03.png "fork step03")
#### 4. parent process와 child process 간 실행영역 분기
![Alt text](./assets/fork_step04.png "fork step04")
#### 5. wait
wait 함수로 child process가 종료되기를 대기하고 있다.
wait(cpid)로 pid 26번 프로세스가 종료될 때까지 대기하는 상태이다.
![Alt text](./assets/fork_step05.png "fork step05")
#### 6. child process 종료
![Alt text](./assets/fork_step06.png "fork step06")
### exec
c언어로 다음과 같은 exec 예제를 실행해보자.
```c
int cpid = fork( );
if (cpid = = 0)
{
//child code
exec(foo);
exit(0);
}
//parent code
wait(cpid);
// end
```

시나리오가 fork 1 ~ 4 설명과 동일하다.
fork 4번부터 실행한다.

#### 1. parent process와 child process 간 실행영역 분기

![Alt text](./assets/exec_step01.png "exec_step01")

#### 2. exec 수행
exec는 사실 함수가 아니라 exec family 함수를 표현하는 문구이다.
exec
![Alt text](./assets/exec_step02.png "exec_step02")

#### 3. exec 호출 후 kernel
exec 호출 후 kernel은 child process의 text, data, stack 등의 자원을 비우라를 요청을 하게 된다.
![Alt text](./assets/exec_step03.png "exec_step03")

#### 4. child process 자원 해제
foo 자원 적재를 위해 child process의 자원을 전부 할당 해제한다.
![Alt text](./assets/exec_step04.png "exec_step04")

#### 5. foo 실행
pid 26에 foo 자원 적재 후, foo 명렁 수행한다.
![Alt text](./assets/exec_step05.png "exec_step05")


Binary file added process/assets/eco.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added process/assets/exec_step01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added process/assets/exec_step02.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added process/assets/exec_step03.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added process/assets/exec_step04.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added process/assets/exec_step05.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added process/assets/fork_exec.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added process/assets/fork_step01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added process/assets/fork_step02.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added process/assets/fork_step03.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added process/assets/fork_step04.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added process/assets/fork_step05.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added process/assets/fork_step06.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added process/assets/unix_process.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 26 additions & 16 deletions shell/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@

[Ref] Cplusplus <a name="cpp"></a> <http://www.cplusplus.com>

[Ref] fork and exec <http://channelofchaos.tistory.com/55>

[Ref] System call <http://duksoo.tistory.com/entry/System-call-등록-순서>

[Ref] Subroutine <https://en.wikipedia.org/wiki/Subroutine>

[Ref] What is the difference between system call and library call? <https://stackoverflow.com/questions/29816791/what-is-the-difference-between-system-call-and-library-call>
<!--
[Link text](#some-id)-->
## Basic lifetime of a shell
Expand Down Expand Up @@ -184,10 +177,10 @@ int lsh_launch(char **args)
pid_t pid, wpid; // ①
int status;

pid = fork(); //
pid = fork(); // child process id
if (pid == 0) {
// Child process
if (execvp(args[0], args) == -1) { //
if (execvp(args[0], args) == -1) { //
perror("lsh");
}
exit(EXIT_FAILURE);
Expand All @@ -196,19 +189,36 @@ int lsh_launch(char **args)
perror("lsh");
} else {
// Parent process
do { // ④
wpid = waitpid(pid, &status, WUNTRACED);
do {
wpid = waitpid(pid, &status, WUNTRACED); // ③
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
}

return 1;
}
```
Subroutine vs System call
### ① pid_t
### ② process control
### ③ execvp
### ④ waitpid
fork와 exec 에 관한 내용은 process 디렉토리에 따로 정리 해 놓았다.
### ① pid_t
```c
#define pid_t int
```
pid_t 는 int 타입이다.

### ② execvp
```c
int execvp(const char *file, char *const argv[]);
```
return 값이 없음, -1이다.
return 값이 없는 것은 정상 실행 됬을때는 이미 다른 프로그램이다.
return -1, 명령 실행 실패
### ③ waitpid
```c
pid_t waitpid(pid_t pid, int *status, int options);
```
waitpid 함수는 인수로 주어진 pid 번호의 자식프로세스가 종료되거나, 시그널 함수를 호출하는 신호가 전달될때까지 waitpid 호출한 영역에서 일시 중지 된다.


## Shell Builtins

Expand Down

0 comments on commit 1fe50ae

Please sign in to comment.