From 082a48fe6e316df7766507d1e04f721fb10b07c1 Mon Sep 17 00:00:00 2001 From: KimNayoung <skdud1201@ajou.ac.kr> Date: Wed, 26 Jan 2022 16:00:21 +0900 Subject: [PATCH] lec14 --- lec12/Makefile.2 | 8 ++++++++ lec13/forktest.c | 17 ++++++++++++++++ lec13/lserr.txt | 1 + lec13/lsout.txt | 1 + lec13/lsoutierr.txt | 2 ++ lec13/output.txt | 1 + lec13/test.c | 7 +++++++ lec13/th3.c | 29 +++++++++++++++++++++++++++ lec13/thread2.c | 37 ++++++++++++++++++++++++++++++++++ lec13/threadtest.c | 42 +++++++++++++++++++++++++++++++++++++++ lec14/prtest.c | 12 ++++++++++++ lec14/th4.c | 34 ++++++++++++++++++++++++++++++++ lec14/threadtest.c | 48 +++++++++++++++++++++++++++++++++++++++++++++ 13 files changed, 239 insertions(+) create mode 100644 lec12/Makefile.2 create mode 100644 lec13/forktest.c create mode 100644 lec13/lserr.txt create mode 100644 lec13/lsout.txt create mode 100644 lec13/lsoutierr.txt create mode 100644 lec13/output.txt create mode 100644 lec13/test.c create mode 100644 lec13/th3.c create mode 100644 lec13/thread2.c create mode 100644 lec13/threadtest.c create mode 100644 lec14/prtest.c create mode 100644 lec14/th4.c create mode 100644 lec14/threadtest.c diff --git a/lec12/Makefile.2 b/lec12/Makefile.2 new file mode 100644 index 0000000..9dbc9c0 --- /dev/null +++ b/lec12/Makefile.2 @@ -0,0 +1,8 @@ +main: main.o fx_s15_16.o + cc main.o fx_s15_16.o -o main +main.o: main.c fx_head.h fx_s15_16.h + cc -c main.c +fx_s15_16.o: fx_head.h fx_s15_16.c + cc -c fx_s15_16.c +clean: + rm main *.o *.out diff --git a/lec13/forktest.c b/lec13/forktest.c new file mode 100644 index 0000000..9223066 --- /dev/null +++ b/lec13/forktest.c @@ -0,0 +1,17 @@ +#include <stdio.h> +#include <sys/types.h> +#include <unistd.h> + +int main() +{ + int a = 0; + pid_t pid ,pid2; + pid = fork(); + pid2 = fork(); + for (int i = 0; i < 100; i++) + { + sleep(1); + if (pid == 0) + printf("PID %d : PID2 %d : A=%d : i=%d : \n", pid, pid2, a++, i); + } +} diff --git a/lec13/lserr.txt b/lec13/lserr.txt new file mode 100644 index 0000000..06488f2 --- /dev/null +++ b/lec13/lserr.txt @@ -0,0 +1 @@ +ls: cannot access 'zzz': No such file or directory diff --git a/lec13/lsout.txt b/lec13/lsout.txt new file mode 100644 index 0000000..f386eaf --- /dev/null +++ b/lec13/lsout.txt @@ -0,0 +1 @@ +-rw-r--r-- 1 pcc004 pcc 127 1월 25 15:22 test.c diff --git a/lec13/lsoutierr.txt b/lec13/lsoutierr.txt new file mode 100644 index 0000000..ec19ad5 --- /dev/null +++ b/lec13/lsoutierr.txt @@ -0,0 +1,2 @@ +ls: cannot access 'zzz': No such file or directory +-rw-r--r-- 1 pcc004 pcc 127 1월 25 15:22 test.c diff --git a/lec13/output.txt b/lec13/output.txt new file mode 100644 index 0000000..ce01362 --- /dev/null +++ b/lec13/output.txt @@ -0,0 +1 @@ +hello diff --git a/lec13/test.c b/lec13/test.c new file mode 100644 index 0000000..f0c7b0e --- /dev/null +++ b/lec13/test.c @@ -0,0 +1,7 @@ +#include <stdio.h> +#include <stdlib.h> + +int main(){ + printf("System Call: %d\n", system("ls -li test.c zzz")); + return(100); +} diff --git a/lec13/th3.c b/lec13/th3.c new file mode 100644 index 0000000..c47f256 --- /dev/null +++ b/lec13/th3.c @@ -0,0 +1,29 @@ +#include <stdio.h> +#include <sys/types.h> +#include <unistd.h> +int bbb = 0; +void fn_s() +{ + char bufff[10]; + static int a = 0; + setvbuf(stdout, bufff, _IOFBF, 10); + printf("== %d %d ==",a++, bbb++); +} + +void *run (void *arg) +{ + printf("Hello world of POSIX threads.%d\n", (int) (0) ); + for (int i = 0; i < 10; i++) + { + sleep(1); + fn_s(); + } + return 0; +} + +int main() +{ + int result1; + run((void *) 0); + printf("Thread return %d at the end\n", result1); +} diff --git a/lec13/thread2.c b/lec13/thread2.c new file mode 100644 index 0000000..d9761b6 --- /dev/null +++ b/lec13/thread2.c @@ -0,0 +1,37 @@ +#include <pthread.h> +#include <stdio.h> +#include <sys/types.h> +#include <unistd.h> + + +int bbb = 0; + +void fn_s() +{ + static int a = 0; + printf("== %d %d ==",a++, bbb++); +} + + +void *run (void *arg) +{ + printf("Hello world of POSXI threads.%d\n", (int) pthread_self() ); + for (int i = 0; i < 100; i++) + { + usleep(100); + fn_s(); + } + return 0; + +} + +int main() +{ + pthread_t thread1; + int result1; + pthread_create(&thread1, NULL, run, NULL ); + run((void *) 0); + pthread_join(thread1, (void **) &result1); + printf("Thread return %d at the end\n", result1); +} + diff --git a/lec13/threadtest.c b/lec13/threadtest.c new file mode 100644 index 0000000..3e8026c --- /dev/null +++ b/lec13/threadtest.c @@ -0,0 +1,42 @@ +#include <pthread.h> +#include <stdio.h> +#include <sys/types.h> +#include <unistd.h> + +int bbb = 0; + +void fn_s() +{ + static int a = 0; + printf("== %d %d ==",a++, bbb++); +} + + +void *run (void *arg) +{ + printf("Hello world of POSXI threads.%d\n", (int) pthread_self() ); + for (int i = 0; i < 100; i++) + { + sleep(1); + fn_s(); + } + return 0; + +} + +int main() +{ + pthread_t thread1; + // pthread_t thread2; + // pthread_t thread3; + int result1; + // int result2; + pthread_create(&thread1, NULL, run, NULL ); + // pthread_create(&thread2, NULL, run, NULL ); + // pthread_create(&thread3, NULL, run, NULL ); + pthread_join(thread1, &result1); + // pthread_join(thread2, &result2); + printf("Thread return %d at the end\n", result1); + // printf("Thread return %d at the end\n", result2); +} + diff --git a/lec14/prtest.c b/lec14/prtest.c new file mode 100644 index 0000000..9188cf9 --- /dev/null +++ b/lec14/prtest.c @@ -0,0 +1,12 @@ +#include <stdio.h> + +int main() +{ + char a; + short b; + int c; + long long d; + + scanf("%d %d %d %d", &a, &b, &c, &d); + printf("%d %d %d %d\n", a, b, c, d); +} diff --git a/lec14/th4.c b/lec14/th4.c new file mode 100644 index 0000000..6f3fa9c --- /dev/null +++ b/lec14/th4.c @@ -0,0 +1,34 @@ +#include <stdio.h> +#include <sys/types.h> +#include <unistd.h> + +int bbb = 0; + +void fn_s() +{ + static int a = 0; + printf("== %d %d ==",a++, bbb++); + fflush(stdout); +} + + +void *run (void *arg) +{ + printf("Hello world of POSIX threads.%d\n", (int) (0) ); + for (int i = 0; i < 10; i++) + { + sleep(1); + fn_s(); + } + return 0; + +} + +int main() +{ + int result1; + char bufff[10]; + setvbuf(stdout,bufff, _IONBF, 10); + run((void *) 0); + printf("Thread return %d at the end\n", result1); +} diff --git a/lec14/threadtest.c b/lec14/threadtest.c new file mode 100644 index 0000000..4308c85 --- /dev/null +++ b/lec14/threadtest.c @@ -0,0 +1,48 @@ +#include <stdio.h> +#include <sys/types.h> +#include <unistd.h> +#include <stdlib.h> +#include <pthread.h> + +int bbb = 0; + +void fn_s() +{ + static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; + static int a = 0; + printf("<= %d %d =>",a, bbb); + pthread_mutex_lock(&mutex); + a++; bbb++; + pthread_mutex_unlock(&mutex); +} + + +void *run (void *arg) +{ + printf("Hello world of POSXI threads.%d\n", (int) pthread_self() ); + for (int i = 0; i < 1000000; i++) + { + //usleep(10000); + fn_s(); + } + return 0; + +} + +int main() +{ + pthread_t thread1; + pthread_t thread2; + pthread_t thread3; + int result1, result2, result3; + + pthread_create(&thread1, NULL, run, NULL ); + pthread_create(&thread2, NULL, run, NULL ); + pthread_create(&thread3, NULL, run, NULL ); + run((void *) 0); + pthread_join(thread1, (void **) &result1); + pthread_join(thread2, (void **) &result2); + pthread_join(thread3, (void **) &result3); + printf("\nThread return %d at the end\n", result1); +} + -- GitLab