code for 'deep technology of C' master
authoryu.dongliang <18588496441@163.com>
Wed, 16 Oct 2024 07:03:41 +0000 (15:03 +0800)
committeryu.dongliang <18588496441@163.com>
Wed, 16 Oct 2024 07:03:41 +0000 (15:03 +0800)
1.txt [new file with mode: 0644]
fopen.c [new file with mode: 0644]
global_var.c [new file with mode: 0644]
hanoi.c [new file with mode: 0644]
hello.c [new file with mode: 0644]
macro.c [new file with mode: 0644]
mutex.c [new file with mode: 0644]
qsort.c [new file with mode: 0644]

diff --git a/1.txt b/1.txt
new file mode 100644 (file)
index 0000000..242c034
--- /dev/null
+++ b/1.txt
@@ -0,0 +1 @@
+hello_world
diff --git a/fopen.c b/fopen.c
new file mode 100644 (file)
index 0000000..62be9a5
--- /dev/null
+++ b/fopen.c
@@ -0,0 +1,26 @@
+/*#include<stdio.h>
+#include<stdint.h>
+*/
+void*   fopen(const char* path, const char* mode);
+int     fclose(void* fp);
+int     fscanf(void* fp, const char* fmt, ...);
+
+int     printf(const char* fmt, ...);
+
+int main()
+{
+       void* fp = fopen("1.txt", "r");
+       if (!fp) {
+               printf("fp: %p\n", fp);
+               return -1;
+       }
+
+       char* s[64];
+
+       int n = fscanf(fp, "%s", s);
+
+       printf("s: %s, n: %d\n", s, n);
+
+       fclose(fp);
+       return 0;
+}
diff --git a/global_var.c b/global_var.c
new file mode 100644 (file)
index 0000000..303b7ed
--- /dev/null
@@ -0,0 +1,35 @@
+
+int printf(const char* fmt, ...);
+
+/* C 全局数据
+   1,函数都是全局的,在.text段
+
+   2, 字符串常量
+
+   3,全局变量
+
+   a 和 p 都在 .data段,可写
+----------------------
+   进程的内存排布
+
+   .text    代码段,     R E,可读可执行
+   .rodata  只读数据段, R,
+   .data    数据段,     R, W, 可读可写
+   -------------------
+   堆 heap
+   ------------------ brk,数据段末尾
+   (空白区)
+   ------------------ rsp, 栈顶
+   栈
+   ------------------
+ */
+
+int  a = 123;
+int* p = &a;
+
+int main()
+{
+       printf("a: %d, p: %p\n", a, p);
+       return 0;
+}
diff --git a/hanoi.c b/hanoi.c
new file mode 100644 (file)
index 0000000..50ae046
--- /dev/null
+++ b/hanoi.c
@@ -0,0 +1,22 @@
+// 汉诺塔,递归算法
+
+int printf(const char* fmt, ...);
+
+int count = 0;
+
+void hanoi(int n, char a, char b, char c)
+{
+       if (1 == n)
+               printf("count: %d, n: %d, %c->%c\n", count++, n, a, c);
+       else {
+               hanoi(n-1, a, c, b); // 递归
+               printf("count: %d, n: %d, %c->%c\n", count++, n, a, c);
+               hanoi(n-1, b, a, c); // 递归
+       }
+}
+
+int main()
+{
+       hanoi(5, 'A', 'B', 'C');
+       return 0;
+}
diff --git a/hello.c b/hello.c
new file mode 100644 (file)
index 0000000..9bf9444
--- /dev/null
+++ b/hello.c
@@ -0,0 +1,19 @@
+
+/* hello world 程序
+   1,对库函数的调用,printf()
+   2, 对常量字符串的寻址,"hello world\n"
+
+   常量字符串是只读的,放在.rodata段
+
+   3,分段是为了权限控制(读、写、运行),为了信息安全
+ */
+
+int printf(const char* fmt, ...);
+
+int main()
+{
+       printf("hello world\n");
+       return 0;
+}
diff --git a/macro.c b/macro.c
new file mode 100644 (file)
index 0000000..13ab9ba
--- /dev/null
+++ b/macro.c
@@ -0,0 +1,12 @@
+
+int printf(const char* fmt, ...);
+
+#define A(x) #x 
+
+int main()
+{
+       int z = 1;
+
+       printf("%s: %d\n", A(z), z);
+       return 0;
+}
diff --git a/mutex.c b/mutex.c
new file mode 100644 (file)
index 0000000..eae664f
--- /dev/null
+++ b/mutex.c
@@ -0,0 +1,61 @@
+#include<stdio.h>
+#include<unistd.h>
+#include<pthread.h>
+
+int xchg(int* mutex)
+{
+       int ret;
+
+       asm volatile(
+               "xchg %0, (%1)\n\t"
+               :"=r"(ret)
+               :"r"(mutex), "0"(1)
+               :
+       );
+
+       return ret;
+}
+
+void lock(int* mutex)
+{
+       while (1 == xchg(mutex))
+               sched_yield();
+}
+
+void unlock(int* mutex)
+{
+       *mutex = 0;
+}
+
+int g_mutex = 0;
+int g_count = 0;
+
+void* thread(void* arg)
+{
+       int i;
+       for (i = 0; i < 1000 * 1000; i++) {
+
+               lock(&g_mutex);
+               g_count++;
+               unlock(&g_mutex);
+       }
+       return 0;
+}
+
+int main()
+{
+       pthread_t tid;
+       pthread_create(&tid, NULL, thread, NULL);
+
+       int i;
+       for (i = 0; i < 1000 * 1000; i++) {
+               lock(&g_mutex);
+               g_count++;
+               unlock(&g_mutex);
+       }
+
+       pthread_join(tid, NULL);
+
+       printf("g_count: %d\n", g_count);
+       return 0;
+}
diff --git a/qsort.c b/qsort.c
new file mode 100644 (file)
index 0000000..1a45e31
--- /dev/null
+++ b/qsort.c
@@ -0,0 +1,43 @@
+int printf(const char* fmt, ...);
+
+int sort(int* a, int m, int n)
+{
+       if (m >= n)
+               return 0;
+
+       int i = m;
+       int j = n;
+       int t;
+
+       t = a[i];
+       while (i < j) {
+
+               while (i < j && t <= a[j])
+                       j--;
+               a[i]  = a[j];
+               a[j]  = t;
+
+               while (i < j && a[i] <= t)
+                       i++;
+               a[j]  = a[i];
+               a[i]  = t;
+       }
+
+       sort(a, m, i - 1);
+       sort(a, i + 1, n);
+       return 0;
+}
+
+int main()
+{
+       int a[20] = {1, 3, 5, 7, 9, 8, 4, 2, 6, 0,
+               11, 13, 15, 17, 19, 18, 16, 14, 12, 10};
+
+       sort(a, 0, 19);
+
+       int i;
+       for (i = 0; i < 20; i++)
+               printf("%d\n", a[i]);
+
+       return 0;
+}