Skip to content

Commit c74c99f

Browse files
authored
Merge pull request asuan99#11 from asuan99/wonho
[Docs] process_and_thread (02/20)
2 parents 2846d9c + 18643df commit c74c99f

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed
122 KB
Loading
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# CS_Study_example
2+
3+
## 프로세스와 스레드의 차이점에 대해 설명해주세요
4+
5+
- 자료 조사
6+
7+
![Untitled](overview_process.png)
8+
9+
- 프로그램(program)
10+
- 컴퓨터가 실행하기 위한 프로그래밍 언어로 작성된 명령어들의 집합 또는 순서
11+
- 사람이 읽을 수 있는 형태의 프로그램 - source code
12+
- 이는 추가적인 컴퓨터 프로그램을 요구함
13+
- 컴퓨터가 각자의 기계어로만 실행할 수 있기때문
14+
- 이는 해당 언어의 컴파일러를 통해 executable 파일로 변환
15+
- 어셈블리 언어는 어셈블러를 통해 변환
16+
- 언어의 인터프리터를 통한 실행도 가능
17+
- 프로세스(process)
18+
- 하나 또는 그 이상의 스레드에 의해 실행되는 컴퓨터 프로그램
19+
- 이는 운영체제에 따라 다른 형태를 가짐
20+
- 프로그램 코드, 할당 자원, 물리/논리적 접근 권한, 데이터 구조, 제어, 관련 실행 활동으로 구성됨
21+
- 운영체제에 따라 여러 스레드로 구성되며, 이는 동시적으로 명령어를 실행할 수 있게 함
22+
- 스레드(thread)
23+
- 스레드는 프로그램을 실행할 수 있는 가장 작은 단위로 각각 스케쥴러에 의해 독립적으로 관리될 수 있다.
24+
- 한 프로세스에 의해 생성된 여러 스레드들은 서로 자원을 공유할 수 있으며, 동시 실행이 가능하다.
25+
- 스케쥴러(scheduler)
26+
- 스케쥴러는 작업을 실행하기 위해 자원의 할당과 회수를 진행하는 활동(스케쥴링)을 담당함.
27+
- 작업의 단위는 스레드, 프로세스, 데이터 플로우가 될 수 있음.
28+
- 이는 멀티 태스킹을 활용할 수 있도록 하며, 컴퓨팅 자원의 활용성을 높이기 위함이다.
29+
- 이 과정에서 우선순위가 높은 작업에 먼저 자원을 할당(preemption, 선점형)하거나, 기존의 작업과 다른 작업을 교체(Context Switch)를 진행
30+
- 스케쥴링 알고리즘
31+
- FIFO(First-In First-Out)
32+
- 먼저 온 작업 먼저 처리
33+
- EDF(Earliest Deadline First)
34+
- 데드라인이 가장 먼저인 작업에 우선순위
35+
- SJF(Shortest Job First)
36+
- 남은 시간이 가장 짧은 작업에 우선순위
37+
- FPPS(Fixed Priority Pre-emptive Scheduling)
38+
- 프로세스에 대해 고정된 우선순위 부여
39+
- 낮은 우선순위의 작업에 starvation 발생 가능
40+
- RR(Round-Robin)
41+
- 작업의 특성을 고려하지 않고 고정된 시간만큼만 작업을 할당
42+
- Multilevel queue
43+
- 작업의 성격을 그룹별로 나눌 수 있을 때 활용
44+
- foreground process(interactive) vs. background process(batch)
45+
- 답안
46+
47+
프로세스와 스레드는 모두 CPU가 처리할 수 있는 하나의 작업 단위가 될 수 있지만, 프로세스는 서로 간의 메모리 자원을 공유할 수 없지만, 스레드는 이를 공유할 수 있다는 점에서 차이점이 있습니다. 스레드의 이런 특징은 프로그래머 레벨에서 동시 프로그래밍, 멀티 프로그래밍을 가능하게 해줍니다.
48+
49+
- 코드 예시
50+
51+
전체 합(1~100)
52+
53+
싱글 프로그래밍
54+
55+
```cpp
56+
#include <iostream>
57+
#include <chrono>
58+
59+
using namespace std;
60+
using namespace chrono;
61+
62+
int sum=0;
63+
64+
void partSum(int start, int end)
65+
{
66+
for(int i =start; i <=end;i++)
67+
{
68+
sum+=i;
69+
//cout<<"partial : "<< sum <<"("<<i<<")"<<endl;
70+
}
71+
}
72+
73+
int main(){
74+
auto begin = steady_clock::now();
75+
76+
partSum(1,100);
77+
78+
auto end = steady_clock::now();
79+
80+
cout<<"sum : "<< sum << endl;
81+
cout<<"runtime : "<<duration_cast<nanoseconds>(end-begin).count()<<"ns"<<endl;
82+
return 0;
83+
}
84+
```
85+
86+
병렬 프로그래밍 (4-thread)
87+
88+
```cpp
89+
#include <iostream>
90+
#include <thread>
91+
#include <chrono>
92+
93+
using namespace std;
94+
using namespace chrono;
95+
int sum = 0;
96+
97+
void partSum(int start, int end)
98+
{
99+
for(int i =start; i <=end;i++)
100+
{
101+
sum+=i;
102+
//cout<<"partial : "<< sum <<"("<<i<<")"<<endl;
103+
}
104+
}
105+
106+
int main()
107+
{
108+
thread thread1(partSum,1,25);
109+
thread thread2(partSum,26,50);
110+
thread thread3(partSum,51,75);
111+
thread thread4(partSum,76,100);
112+
113+
auto begin = steady_clock::now();
114+
thread1.join();
115+
thread2.join();
116+
thread3.join();
117+
thread4.join();
118+
119+
auto end = steady_clock::now();
120+
cout<<"sum : "<< sum << endl;
121+
cout<<"runtime : "<<duration_cast<nanoseconds>(end-begin).count()<<"ns"<<endl;
122+
return 0;
123+
124+
}
125+
```
126+
127+
- Reference
128+
129+
Wikipedia ([Process](https://en.wikipedia.org/wiki/Process_(computing)), [Light-weight process](https://en.wikipedia.org/wiki/Light-weight_process), [Thread](https://en.wikipedia.org/wiki/Thread_(computing)))

0 commit comments

Comments
 (0)