1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <stdio.h>
#include <unistd.h>
#include <sched.h>
int main(void)
{
    struct sched_param param;
    int i, j;
 
    sched_getparam(0&param);
 
    printf("\nBefore set\n");
    printf(" Param.priority = %d\n", param.sched_priority);
    printf(" Sched policy = %d\n", sched_getscheduler(0));
    
   
/*
    for(i=0;i<1000000;i++)
        for(j=0;j<100000;j++);
*/
    param.sched_priority =10;
    sched_setscheduler(0, SCHED_FIFO, &param);
    sched_getparam(0&param);
    
    printf(" \nFIFO set\n");
    printf(" Param.priority = %d\n", param.sched_priority);
    printf(" Sched policy = %d\n", sched_getscheduler(0));
    
 /* 
    for(i=0;i<1000000;i++)
        for(j=0;j<10000;j++);
*/
 
    param.sched_priority =20;
    sched_setscheduler(0, SCHED_RR, &param);
    sched_getparam(0&param);
    
    printf(" \nRR set\n");
    printf(" Param.priority = %d\n", param.sched_priority);
    printf(" Sched policy = %d\n", sched_getscheduler(0));
 
 
    /*
    for(i=0;i<100000;i++)
        for(j=0;j<10000;j++);
*/
    return 0;
 
 
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs

 

 

 

 

 

Chapter10

운영체제 관련 실습

shell.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdbool.h>
#include <sys/wait.h>
 
struct COMMAND{
    char* name;
    char* desc;
    bool (*func)(int argc,char* argv[]);
};
 
bool cmd_cd(int argc,char* argv[]);
bool cmd_exit(int argc,char* argv[]);
bool cmd_help(int argc,char* argv[]);
 
struct COMMAND builtin_cmds[]={
    {"cd","change directory",cmd_cd},
    {"exit","exit this shell", cmd_exit},
    {"quit","quit this shell",cmd_exit},
    {"help","show this help",cmd_help},
    {"?","show this help",cmd_help}
};
 
bool cmd_cd(int argc,char* argv[])
{
    if(argc==1)
        chdir(getenv("HOME"));
    else if(argc==2){
        if(chdir(argv[1]))
            printf("No directory\n");
    }else
        printf("USAGE: cd [dir]\n");
    return true;
}
 
bool cmd_exit(int argc,char* argv[])
{
    return false;
}
 
bool cmd_help(int argc,char* argv[])
{
    int i;
    for(i=0;i<sizeof(builtin_cmds)/sizeof(struct COMMAND);i++)
    {
        if(argc==1||strcmp(builtin_cmds[i].name,argv[1])==0)
            printf("%-10s:%s\n",builtin_cmds[i].name,builtin_cmds[i].desc);
    }
}
 
int tokenize(char* buf,char* delims,char* tokens[],int maxTokens)
{
    int token_count = 0;
    char* token;
    token=strtok(buf,delims);
    while(token!=NULL && token_count<maxTokens){
        tokens[token_count]=token;
        token_count++;
        token=strtok(NULL,delims);
    }
    tokens[token_count]=NULL;
    return token_count;
}
 
bool run(char* line)
{
    char delims[]="\r\n\t";
    char* tokens[128];
    int token_count;
    int i;
    int status;
    pid_t child;
    token_count=tokenize(line,delims,tokens,sizeof(tokens)/sizeof(char*));
    if(token_count==0)return true;
    for(i=0;i<sizeof(builtin_cmds)/sizeof(struct COMMAND);i++){
        if(strcmp(builtin_cmds[i].name,tokens[0])==0)
            return builtin_cmds[i].func(token_count,tokens);
    }
    child=fork();
    if(child==0){
        execvp(tokens[0],tokens);
        printf("No such file\n");
        _exit(0);
    }else if(child<0){
        printf("Failed to fork()!");
        _exit(0);
    }else
        wait(&status);
    return true;
}
 
int main(void)
{   
    char line[1024];
    while(-1){
        printf("%s $",get_current_dir_name());
        fgets(line,sizeof(line)-1,stdin);
        if(run(line)==false)
            break;
 
    }
return 0;
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs

 

 

 

스케줄러와 시뮬레이팅 환경

1부터 시작하여 1씩 숫자를 증가시켜 화면에 출력하고 1초간 쉬는 태스크1과, 510부터 시작하여 10씩 숫자를 증가시켜 화면에 출력하고 1초간 쉬는 태스크2, 1001부터 시작하여 1씩 숫자를 증가시켜 화면에 출력하고 2초간 쉬는 태스크3이 라운드 로빈 정책에 의해 스케줄링 되어 CPU를 나누어 사용한다.

scheduler.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#ifndef    SCHEDULER_H
#define    SCHEDULER_H
 
#define    THREAD_STACKSIZE    1024
// task의 상태
typedef enum {
    TASK_READY    = 0,
    TASK_RUN,
    TASK_YIELD,
    TASK_SLEEP,
    TASK_KILL
} TaskStatus;
 
// 각 task를 위한 구조체
typedef struct task_info_tag {
    unsigned long stack[THREAD_STACKSIZE];
    unsigned long sp;
    int task_id;
 
    TaskStatus status;
 
    struct task_info_tag *next;
    struct task_info_tag *prev;
*TaskInfo;
 
 
typedef void (*TaskFunc)(void *context);
 
TaskInfo thread_create(TaskFunc callback, void *context);
 
void thread_init(void);
void thread_wait(void);
void thread_uninit(void);
void thread_switch(int);
void thread_kill(void);
 
#endif    // SCHEDULER_H
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs

 

 

scheduler.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <setjmp.h>
#include <malloc.h>
#include <memory.h>
#include <sys/time.h>
#include "scheduler.h"
 
// task switching시 저장되어야 하는 정보
struct frame {
    unsigned long flags;
    unsigned long ebp;
    unsigned long edi;
    unsigned long esi;
    unsigned long edx;
    unsigned long ecx;
    unsigned long ebx;
    unsigned long eax;
    unsigned long retaddr;
    unsigned long retaddr2;
    unsigned long data;
};
 
typedef struct sch_handle_tag {
    int child_task;
 
    TaskInfo running_task;
    TaskInfo root_task;
} SchHandle;
 
// global schedule handler
SchHandle gh_sch;
 
// task data struct
TaskInfo task_get_runningtask();
void task_insert(TaskInfo taskinfo);
void task_delete(TaskInfo taskinfo);
void task_next();
void scheduler();
void parent_task();
 
// thread_create : task를 생성하는 함수로 taskinfo 구조체를 할당하고 구성한다
TaskInfo thread_create(TaskFunc callback, void *context)
{
    TaskInfo taskinfo;
    // task를 위한 공간 할당
    taskinfo = malloc(sizeof(*taskinfo));
    memset(taskinfo, 0x00sizeof(*taskinfo));
 
    {
        struct frame *= (struct frame *)&taskinfo->stack[
            THREAD_STACKSIZE - sizeof(struct frame)/4];
        // taskinfo로할당된 공간 중 stack부분 뒤쪽에 frame을 위한 공간으
        // 로 할당 이어 task가 수행되면서 stack공간을 활용
        int i;
        for(i = 0; i < THREAD_STACKSIZE; ++i) // stack overfllow check
            taskinfo->stack[i] = i;
        memset(f, 0sizeof(struct frame));
        f->retaddr = (unsigned long)callback;
        f->retaddr2 = (unsigned long)thread_kill;
        f->data = (unsigned long)context;
        taskinfo->sp = (unsigned long)&f->flags;
        f->ebp = (unsigned long)&f->eax;
    }
    // task 생성에 따라 gh_sch에 child task가 늘었을을 표시
    gh_sch.child_task++;
    // gh_sch.child_task 값으로 task_id 할당
    taskinfo->task_id = gh_sch.child_task;
    // task 생성시 TASK_READY로 상태를 설정함
    taskinfo->status = TASK_READY;
    // taskinfo구조체들의 linkedlist에 새 thread의 taskinfo 구조체를 삽입
    task_insert(taskinfo);
 
    return taskinfo;
}
 
/* thread_init : 초기화 함수로 main 함수가 처음에 호출하여
 * global scheduler handeler를 초기화 하고, parent_task를 생성한다
 */
void thread_init()
{
    gh_sch.root_task = NULL;
    gh_sch.running_task = NULL;
 
    gh_sch.child_task = 0;
    
    thread_create(parent_task, NULL);
}
 
/*
 * thread_switch : 수행중이던 task가 다른 대기중인 task에게 cpu사용을 양보하게
 * 하는 함수로, 현재 cpu레지스터의 값이 수행중이던 task의 stack부분에 차례차례
 * 저장되게 되며, 다음에 수행될 것으로 선택된 task의 taskinfo의 stack정보가 레지
 * 스터로 올려진다.
 */
static unsigned long spsave, sptmp;
void thread_switch(int a)
{
    asm(    "push %%rax\n \t"
        "push %%rbx\n \t"
        "push %%rcx\n \t"
        "push %%rdx\n \t"
        "push %%rsi\n \t"
        "push %%rdi\n \t"
        "push %%rbp\n \t"
        "push %%rbp\n \t"
        "mov  %%rsp, %0"
        : "=r" (spsave)
       );
 
    gh_sch.running_task->sp = spsave;
 
    scheduler();
    sptmp = gh_sch.running_task->sp;
 
    asm(    "mov %0, %%rsp\n \t"
        "pop %%rbp\n \t"
        "pop %%rbp\n \t"
        "pop %%rdi\n \t"
        "pop %%rsi\n \t"
        "pop %%rdx\n \t"
        "pop %%rcx\n \t"
        "pop %%rbx\n \t"
        "pop %%rax\n \t"
        ::"r" (sptmp)
       );
}
 
// 다음 수행될 task를 선택하는 함수
void scheduler(void)
{
    TaskInfo task;
    // gh_sch의 running_task가 가르키고 있는 taskinfo 받음
    task = task_get_runningtask();
    
    switch( task->status ) {
        // task 상태가 TASK_RUN이나 TASK_SLEEP이면 선택됨
        case TASK_RUN:
        case TASK_SLEEP:
            break;
        // task상태가 TASK_KILL이면 delete하고, swiching함수 다시 호출
        case TASK_KILL:
            task_delete(task);
            scheduler();
            break;
        // task상태가 TASK_YIELD이면 상태를 TASK_RUN으로 바꾸고 선택됨
        case TASK_YIELD:
            task->status = TASK_RUN;
            break;
        // task상태가 TASK_READY이면 상태를 TASK_RUN으로 바꾸고 선택됨
        case TASK_READY:
            task->status = TASK_RUN;
            break;
    }
    // gh_sch의 running_task를 linkedlist의 다음 task로 설정
    task_next();
}
 
void thread_wait(void)
{
    parent_task(NULL);
}
 
// task 상태를 TASK_KILL로 설정 후, thread_yield
void thread_kill(void)
{
    TaskInfo    task;
    task = task_get_runningtask();
    task->status = TASK_KILL;
    thread_switch(0);
}
 
void thread_uninit(void)
{
    return;
}
 
// child thread가 더이상 없을때까지 thread_switch
void parent_task(void *context)
{
    // signal 처리를 위한 정보를 위한 구조체
    struct sigaction act;
    sigset_t masksets;
    pid_t pid;
 
    // signal set 초기화
    sigemptyset( &masksets );
    // signal handler로 thread_switch() 등록
    act.sa_handler = thread_switch;
    act.sa_mask = masksets;
    act.sa_flags = SA_NODEFER;
 
    // signal 수신 때 취할 action 설정
    sigaction( SIGUSR1, &act, NULL );
 
    if( ( pid = fork() ) == 0 ) {
        while(1) {
            sleep(1);
            kill( getppid(), SIGUSR1 );
        }
    } else {
        while(1) {
            // child_task가 1개 남았을 때, 즉,parent_task만 남았을때
            if( gh_sch.child_task == 1 ) {
                kill( pid, SIGINT );
                break;
            }
        }
    }
}
 
// linkedlist에 새로운 taskinfo 삽입
void task_insert(TaskInfo taskinfo)
{
    if( gh_sch.root_task == NULL ){
        gh_sch.root_task = taskinfo;
        gh_sch.running_task = taskinfo;
    } else {
        TaskInfo temp = gh_sch.root_task;
        while( temp->next != NULL )
            temp = temp->next;
        temp->next = taskinfo;
        taskinfo->prev = temp;
    }
}
 
// linkedlist에 gh_sch.running_task가 가르키고 있는 task 리턴
TaskInfo task_get_runningtask(void)
{
    return gh_sch.running_task;
}
 
// linkedlist에서 gh_sch.running_task가 가르키고 있는 task의 다음 task리턴
void task_next(void)
{
    TaskInfo temp;
    temp = gh_sch.running_task;
    // gh_sch.running_task가 NULL이 아니면
    if( temp->next != NULL )
        gh_sch.running_task = temp->next;
    // gh_sch.running_task가 NULL이면, parent task를 가르킴
    else 
        gh_sch.running_task = gh_sch.root_task;
}
 
// linkedlist에서 task를 지움
void task_delete(TaskInfo taskinfo)
{
    TaskInfo temp = taskinfo->prev;
    if( gh_sch.root_task == taskinfo ) {
        gh_sch.root_task = NULL;
        gh_sch.running_task = NULL;
        gh_sch.child_task = 0;
    } else {
        temp->next = taskinfo->next;
 
        if( taskinfo == gh_sch.running_task ) {
            if( temp->next != NULL ){
                (taskinfo->next)->prev = temp;
                gh_sch.running_task = temp->next;
            } else {
                gh_sch.running_task = temp;
            }
        }
        gh_sch.child_task--;
    }
    free(taskinfo);
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs

 

 

main.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <unistd.h>
#include <stdio.h>
#include "scheduler.h"
 
// 스케줄링 대상이 되는 태스트
void test_func_one(void *context)
{
    int i = 0;
    while1 ){
        i++;
        printf("TASK 1 : %5d\n", i);
        sleep(1);
        if( i == 15 )
            break;
    }
}
 
void test_func_two(void *context)
{
    int i = 500;
    while1 ){
        i += 10;
        printf("\t\t\tTASK 2 : %3d\n", i);
        sleep(1);
        if( i == 600 )
            break;
    }
}
 
 
void test_func_three(void *context)
{
    int i = 1000;
    while1 ){
        i++;
        printf("\t\t\t\t\t\tTASK 3 : %4d\n", i);
 
        sleep(1);
        sleep(1);
 
        if( i == 1005 )
            break;
    }
}
 
// my_scheduler의 main 함수
int main(void)
{
    thread_init();
 
    thread_create(test_func_one, NULL);
    thread_create(test_func_two, NULL);
    thread_create(test_func_three, NULL);
 
    thread_wait();
 
    return 0;
}
 
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs

 

Makefile

1
2
3
4
5
6
7
all:
    gcc -g -c scheduler.c
    gcc -g -c main.c
    gcc -g scheduler.o main.o -o scheduler
clean:
    rm *.o scheduler
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs

 

 

 

 

 

+ Recent posts