From eb6eedc04b958c0cf4ffaa662d26f74b8f0ff10e Mon Sep 17 00:00:00 2001
From: Sohyun Lee <sh119@ajou.ac.kr>
Date: Wed, 26 Jan 2022 15:55:46 +0900
Subject: [PATCH] lec14

---
 lec13/th3.c        |  6 ++++--
 lec14/prtest.c     | 12 ++++++++++++
 lec14/th4.c        | 35 +++++++++++++++++++++++++++++++++
 lec14/threadtest.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 99 insertions(+), 2 deletions(-)
 create mode 100644 lec14/prtest.c
 create mode 100644 lec14/th4.c
 create mode 100644 lec14/threadtest.c

diff --git a/lec13/th3.c b/lec13/th3.c
index 2ec2f10..ed1a8fa 100644
--- a/lec13/th3.c
+++ b/lec13/th3.c
@@ -7,14 +7,14 @@ int bbb = 0;
 void fn_s()
 {
     static int a = 0; 
-    printf("== %d %d ==\n",a++, bbb++);
+    printf("== %d %d ==",a++, bbb++);
 }
 
 
 void *run (void *arg)
 {
     printf("Hello world of POSXI threads.%d\n", (int) (0) );
-    for (int i = 0; i < 100; i++)
+    for (int i = 0; i < 10; i++)
 	{
 		sleep(1); 
 		fn_s(); 
@@ -26,6 +26,8 @@ void *run (void *arg)
 int main()
 {
 	int result1;
+   	 char bufff[10];
+   	 setvbuf(stdout,bufff, _IOFBF, 10);
 	run((void *) 0); 
 	sleep(10);
 
diff --git a/lec14/prtest.c b/lec14/prtest.c
new file mode 100644
index 0000000..ae535e9
--- /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("%hhd %hd %d %lld", &a, &b, &c, &d);
+	printf("%hhd %hd %d %lld\n", a,b,c,d);
+}
diff --git a/lec14/th4.c b/lec14/th4.c
new file mode 100644
index 0000000..d3d07dd
--- /dev/null
+++ b/lec14/th4.c
@@ -0,0 +1,35 @@
+#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..c0688ec
--- /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 < 1000; 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