Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9c17462

Browse files
committed
Exam pdf added
1 parent e37e35b commit 9c17462

File tree

17 files changed

+841
-0
lines changed

17 files changed

+841
-0
lines changed
294 KB
Binary file not shown.

‎기말고사/답안/10.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <unistd.h>
4+
#include <errno.h>
5+
#include <fcntl.h>
6+
7+
#define NAMESIZE 50
8+
#define DUMMY 0
9+
10+
struct employe{
11+
char name[NAMESIZE];
12+
int salary;
13+
int pid;
14+
};
15+
16+
int main(int argc, char *argv[])
17+
{
18+
struct flock lock;
19+
struct employe record;
20+
int fd, flags, length, pid;
21+
22+
if (argc < 2) {
23+
fprintf(stderr, "Usage :%s file \n",argv[0]);
24+
exit(1);
25+
}
26+
27+
if((fd = open(argv[1], O_RDWR)) < 0) {
28+
fprintf(stderr, "Open eror :%s file \n",argv[1]);
29+
exit(1);
30+
}
31+
32+
if ((flags = fcntl(fd, F_GETFL, DUMMY)) == -1) {
33+
fprintf(stderr, "fcntl F_GETFL error\n");
34+
exit(1);
35+
}
36+
37+
flags |= O_APPEND;
38+
39+
if (fcntl(fd, F_SETFL, flags) == -1) {
40+
fprintf(stderr, "fcntl F_SETFL error\n");
41+
exit(1);
42+
}
43+
44+
pid = getpid();
45+
46+
while (1) {
47+
printf("Enter employee name : ");
48+
scanf("%s", record.name);
49+
50+
if(record.name[0] == '.')
51+
break;
52+
53+
printf("Enter employee salary : ");
54+
scanf("%d", &record.salary);
55+
record.pid = pid;
56+
length = sizeof(record);
57+
58+
if (write(fd, (char *)&record, length) != length) {
59+
fprintf(stderr, "record write error\n");
60+
exit(1);
61+
}
62+
}
63+
64+
close(fd);
65+
exit(0);
66+
}
67+

‎기말고사/답안/11.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <unistd.h>
4+
#include <fcntl.h>
5+
6+
#define NAMESIZE 50
7+
8+
struct employee {
9+
char name[NAMESIZE];
10+
int salary;
11+
int pid;
12+
};
13+
14+
int main(int argc, char *argv[])
15+
{
16+
struct flock lock;
17+
struct employee record;
18+
int fd, recnum, pid;
19+
long position;
20+
21+
if ((fd = open(argv[1], O_RDWR)) == -1) {
22+
perror(argv[1]);
23+
exit(1);
24+
}
25+
26+
pid = getpid();
27+
while (1) {
28+
printf("\nEnter record number :");
29+
scanf("%d", &recnum);
30+
if (recnum < 0)
31+
break;
32+
position = recnum * sizeof(record);
33+
lock.l_type= F_WRLCK;
34+
lock.l_whence = 0;
35+
lock.l_start = position;
36+
lock.l_len = sizeof(record);
37+
if (fcntl(fd, F_SETLKW, &lock) == -1) {
38+
perror(argv[1]);
39+
exit(2);
40+
}
41+
lseek(fd, position, 0);
42+
if (read(fd, (char *)&record, sizeof(record)) == 0) {
43+
printf("record %d not found\n", recnum);
44+
lock.l_type = F_UNLCK;
45+
fcntl(fd, F_SETLK, &lock);
46+
continue;
47+
}
48+
49+
printf("Employee: %s, salary: %d\n", record.name, record.salary);
50+
record.pid = pid;
51+
printf("Enter new salaray: ");
52+
scanf("%d", &record.salary);
53+
lseek(fd, position, 0);
54+
write(fd, (char *)&record, sizeof(record));
55+
lock.l_type = F_UNLCK;
56+
fcntl(fd, F_SETLK, &lock);
57+
58+
}
59+
close(fd);
60+
exit(0);
61+
}

‎기말고사/답안/12.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
#include <stdlib.h>
4+
#include <unistd.h>
5+
#include <fcntl.h>
6+
#include <sys/mman.h>
7+
#include <sys/stat.h>
8+
9+
#define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
10+
11+
int main(int argc, char *argv[])
12+
{
13+
int fdin, fdout;
14+
void *src, *dst;
15+
struct stat statbuf;
16+
17+
if (argc != 3) {
18+
printf("usage : %s <fromfile> <tofile>\n", argv[0]);
19+
exit(1);
20+
}
21+
22+
if ((fdin = open(argv[1], O_RDONLY)) < 0) { /* fromfile 열기 */
23+
printf("can't open %s for reading\n", argv[1]);
24+
exit(1);
25+
}
26+
27+
if ((fdout = open(argv[2], O_RDWR | O_CREAT | O_TRUNC, FILE_MODE)) < 0) { /* tofile 열기 */
28+
printf("cannot creat %s for writing\n", argv[2]);
29+
exit(1);
30+
}
31+
32+
if (fstat(fdin, &statbuf) < 0) { /* 입력 파일의 크기 획득 */
33+
printf("fstat() error\n");
34+
exit(1);
35+
}
36+
37+
if (lseek(fdout, statbuf.st_size - 1, SEEK_SET) == -1) { /* 출력 파일의 크기 조정 */
38+
printf("lseek() error\n");
39+
exit(1);
40+
}
41+
42+
if (write(fdout, "", 1) != 1) { /* 쓰기 가능 여부 검사 */
43+
printf("write() error");
44+
exit(1);
45+
}
46+
47+
/* mmap: 메모리 맵 입출력, 특정 크기만큼의 메모리를 지정, 시작주소와 길이를 인자로 주며, 영역보호방식(PROT), 특성(FLAG)를 통해 메모리를 맵핑한다 */
48+
if ((src = mmap(0, statbuf.st_size, PROT_READ, MAP_SHARED, fdin, 0)) == MAP_FAILED) { /* ADDR, LENGTH, PROT, FLAG, FD, OFFSET */
49+
printf("mmap() error for input\n");
50+
exit(1);
51+
}
52+
53+
if ((dst = mmap(0, statbuf.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fdout, 0)) == MAP_FAILED) {
54+
printf("mmap() error for output\n");
55+
exit(1);
56+
}
57+
58+
memcpy(dst, src, statbuf.st_size); /* 파일 복사를 수행 */
59+
exit(0);
60+
}
61+
62+

‎기말고사/답안/13.c

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <unistd.h>
4+
#include <pthread.h>
5+
6+
void *ssu_thread1(void *arg);
7+
void *ssu_thread2(void *arg);
8+
9+
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
10+
pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER;
11+
pthread_cond_t cond1 = PTHREAD_COND_INITIALIZER;
12+
pthread_cond_t cond2 = PTHREAD_COND_INITIALIZER;
13+
14+
int count = 0;
15+
int input = 0;
16+
int t1 = 0, t2 = 0;
17+
18+
int main(void)
19+
{
20+
pthread_t tid1, tid2;
21+
int status;
22+
23+
if (pthread_create(&tid1, NULL, ssu_thread1, NULL) != 0) {
24+
fprintf(stderr, "pthread_create error\n");
25+
exit(1);
26+
}
27+
28+
if (pthread_create(&tid2, NULL, ssu_thread2, NULL) != 0) {
29+
fprintf(stderr, "pthread_create error\n");
30+
exit(1);
31+
}
32+
33+
while (1) {
34+
printf("2개 이상의 개수 입력: ");
35+
scanf("%d", &input);
36+
37+
if (input >= 2) {
38+
pthread_cond_signal(&cond1); // ssu_thread1을 깨움
39+
break;
40+
}
41+
}
42+
43+
pthread_join(tid1, (void*)status);
44+
pthread_join(tid2, (void*)status);
45+
46+
printf("complete\n");
47+
exit(0);
48+
}
49+
50+
void *ssu_thread1(void *arg) {
51+
while (1) {
52+
53+
pthread_mutex_lock(&mutex1); // 뮤텍스 락 설정
54+
55+
if (input < 2)
56+
pthread_cond_wait(&cond1, &mutex1); // 시그널 대기
57+
58+
if (input == count) {
59+
pthread_cond_signal(&cond2); // ssu_thread2에 시그널 전송
60+
break;
61+
}
62+
63+
if (count == 0) {
64+
t2++;
65+
count++;
66+
printf("Thread 1 : %d\n", t1);
67+
} else if (count % 2 == 0) {
68+
t1 += t2;
69+
count++;
70+
printf("Thread 1 : %d\n", t1);
71+
}
72+
73+
pthread_cond_signal(&cond2); // ssu_thread2에 시그널 전송
74+
pthread_cond_wait(&cond1, &mutex1); // 시그널 대기
75+
pthread_mutex_unlock(&mutex1); // 뮤텍스 락 해제
76+
}
77+
return NULL;
78+
}
79+
80+
void *ssu_thread2(void *arg) {
81+
while(1) {
82+
83+
pthread_mutex_lock(&mutex2); // 뮤텍스 락 설정
84+
85+
if (input < 2)
86+
pthread_cond_wait(&cond2, &mutex2); // 시그널 대기
87+
88+
if (input == count) {
89+
pthread_cond_signal(&cond1); // ssu_thread1에 시그널 전송
90+
break;
91+
}
92+
93+
if (count == 1) {
94+
count++;
95+
printf("Thread 2 : %d\n", t2);
96+
} else if (count % 2 == 1) {
97+
t2 += t1;
98+
count++;
99+
printf("Thread 2 : %d\n", t2);
100+
}
101+
102+
pthread_cond_signal(&cond1); // ssu_thread1에 시그널 전송
103+
pthread_cond_wait(&cond2, &mutex2); // 시그널 대기
104+
pthread_mutex_unlock(&mutex2); // 뮤텍스 락 해제
105+
}
106+
return NULL;
107+
}
108+
109+

‎기말고사/답안/16.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <unistd.h>
4+
#include <signal.h>
5+
#include <time.h>
6+
7+
void ssu_signal_handler(int signo);
8+
9+
int main(void)
10+
{
11+
struct sigaction sig_act;
12+
sigset_t blk_set;
13+
pid_t pid;
14+
15+
sigfillset(&blk_set); // 모든 시그널을 블록 시그널 셋에 추가
16+
sigdelset(&blk_set, SIGCHLD); // SIGCHLD를 블록 시그널 셋에서 제외
17+
18+
sigemptyset(&sig_act.sa_mask); // 시그널 셋 초기화
19+
sig_act.sa_flags = 0; // 시그널 플래그 초기화
20+
sig_act.sa_handler = ssu_signal_handler; // 시그널 핸들러 등록
21+
sigaction(SIGCHLD, &sig_act, NULL); // SIGCHLD 시그널에 대한 액션 변경
22+
23+
printf("before fork\n");
24+
if ((pid = fork()) < 0) {
25+
fprintf(stderr, "fork error\n");
26+
exit(1);
27+
} else if (pid != 0) {
28+
printf("after fork in parent, suspend...\n");
29+
sigsuspend(&blk_set);
30+
printf("after suspend\n");
31+
} else {
32+
printf("after fork in child, sleep...\n");
33+
sleep(3);
34+
}
35+
36+
exit(0);
37+
}
38+
39+
void ssu_signal_handler(int signo)
40+
{
41+
printf("in ssu_signal_handler() function\n");
42+
}
43+

‎기말고사/답안/17.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <unistd.h>
4+
#include <pthread.h>
5+
6+
void *ssu_thread1(void *arg);
7+
void *ssu_thread2(void *arg);
8+
9+
pthread_t glo_tid;
10+
11+
int main(void)
12+
{
13+
pthread_t loc_tid[2];
14+
int i;
15+
16+
// 스레드 생성
17+
pthread_create(&loc_tid[0], NULL, ssu_thread1, NULL);
18+
pthread_create(&loc_tid[1], NULL, ssu_thread2, NULL);
19+
20+
sleep(5);
21+
22+
for (i = 0; i < 2; i++)
23+
if (loc_tid[i] == glo_tid)
24+
printf("second thread assigns it's tid to global tid\n");
25+
26+
exit(0);
27+
}
28+
29+
void *ssu_thread1(void *arg)
30+
{
31+
printf("in ssu_thread1\n");
32+
return NULL;
33+
}
34+
35+
void *ssu_thread2(void *arg)
36+
{
37+
printf("in ssu_thread2\n");
38+
glo_tid = pthread_self();
39+
return NULL;
40+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /