From 1aed3ad6d1e11aa0c110efea403ea269ad019de3 Mon Sep 17 00:00:00 2001 From: Ji Hun Kim <dunois@ajou.ac.kr> Date: Wed, 26 Jan 2022 15:54:12 +0900 Subject: [PATCH] Added lec14 --- lec13/threadtest.c | 3 ++- lec14/mutextest.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++ lec14/prtest.c | 11 +++++++++++ lec14/th4.c | 34 ++++++++++++++++++++++++++++++++ lec14/threadtest.c | 45 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 lec14/mutextest.c create mode 100644 lec14/prtest.c create mode 100644 lec14/th4.c create mode 100644 lec14/threadtest.c diff --git a/lec13/threadtest.c b/lec13/threadtest.c index 8b37461..7b7fb7f 100644 --- a/lec13/threadtest.c +++ b/lec13/threadtest.c @@ -8,7 +8,8 @@ int bbb = 0; void fn_s() { static int a = 0; - printf("== %d %d ==\n",a++, bbb++); + setvbuf(stdout, _IOFBF, NULL, 10); + printf("== %.2d %.2d ==",a++, bbb++); } diff --git a/lec14/mutextest.c b/lec14/mutextest.c new file mode 100644 index 0000000..4308c85 --- /dev/null +++ b/lec14/mutextest.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); +} + diff --git a/lec14/prtest.c b/lec14/prtest.c new file mode 100644 index 0000000..297d341 --- /dev/null +++ b/lec14/prtest.c @@ -0,0 +1,11 @@ +#include <stdio.h> + +int main() +{ + char a; + short b; + int c; + long long d; + fscanf(stdin, "%d %d %d %d", &a, &b, &c, &d); + fprintf(stdout, "%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..13c319a --- /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..5bafc58 --- /dev/null +++ b/lec14/threadtest.c @@ -0,0 +1,45 @@ +#include <pthread.h> +#include <stdio.h> +#include <unistd.h> +#include <stdlib.h> + +int bbb = 0; + +void fn_s() +{ + static int a = 0; + printf("<= %.4d %.4d =>",a++, bbb++); + fflush(stdout); +} + + +void *run (void *arg) +{ + printf("Hello world of POSIX threads.%d\n", (int) pthread_self() ); + for (int i = 0; i < 100; i++) + { + usleep(10000); + 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 ); + printf("\nJUST AFTER THE CREATE\n"); + run((void *) 0); + pthread_create(&thread2, NULL, run, NULL ); + pthread_create(&thread3, NULL, run, NULL ); + pthread_join(thread1, (void **) &result1); + pthread_join(thread2, (void **) &result2); + printf("\nThread return %d at the end\n", result1); + printf("Thread return %d at the end\n", result2); +} + -- GitLab