基础开发工具(中)

文章目录

    • [4. 自动化构建-make/Makefile](#4. 自动化构建-make/Makefile)
    • [5. Linux第一个系统程序−进度条](#5. Linux第一个系统程序−进度条)

4. 自动化构建-make/Makefile

c 复制代码
[gsm@VM-4-3-centos lesson7]$ touch proc.c
[gsm@VM-4-3-centos lesson7]$ ll
total 0
-rw-rw-r-- 1 gsm gsm 0 Oct 12 15:53 proc.c
[gsm@VM-4-3-centos lesson7]$ vim proc.c 
[gsm@VM-4-3-centos lesson7]$ ll
total 4
-rw-rw-r-- 1 gsm gsm 79 Oct 12 15:54 proc.c
[gsm@VM-4-3-centos lesson7]$ gcc -o proc proc.c
[gsm@VM-4-3-centos lesson7]$ ll
total 16
-rwxrwxr-x 1 gsm gsm 8360 Oct 12 15:55 proc
-rw-rw-r-- 1 gsm gsm   79 Oct 12 15:54 proc.c
[gsm@VM-4-3-centos lesson7]$ ./proc 
hello Linux!
[gsm@VM-4-3-centos lesson7]$ rm proc
[gsm@VM-4-3-centos lesson7]$ ll
total 4
-rw-rw-r-- 1 gsm gsm 79 Oct 12 15:54 proc.c
[gsm@VM-4-3-centos lesson7]$ touch Makefile
[gsm@VM-4-3-centos lesson7]$ ll
total 4
-rw-rw-r-- 1 gsm gsm  0 Oct 12 15:56 Makefile
-rw-rw-r-- 1 gsm gsm 79 Oct 12 15:54 proc.c
[gsm@VM-4-3-centos lesson7]$ vim Makefile 
[gsm@VM-4-3-centos lesson7]$ cat Makefile 
proc:proc.c
	gcc -o proc proc.c
[gsm@VM-4-3-centos lesson7]$ ll
total 8
-rw-rw-r-- 1 gsm gsm 32 Oct 12 15:58 Makefile
-rw-rw-r-- 1 gsm gsm 79 Oct 12 15:54 proc.c
[gsm@VM-4-3-centos lesson7]$ make
gcc -o proc proc.c
[gsm@VM-4-3-centos lesson7]$ ll
total 20
-rw-rw-r-- 1 gsm gsm   32 Oct 12 15:58 Makefile
-rwxrwxr-x 1 gsm gsm 8360 Oct 12 15:59 proc
-rw-rw-r-- 1 gsm gsm   79 Oct 12 15:54 proc.c
[gsm@VM-4-3-centos lesson7]$ ./proc 
hello Linux!
[gsm@VM-4-3-centos lesson7]$ rm proc
[gsm@VM-4-3-centos lesson7]$ ll
total 8
-rw-rw-r-- 1 gsm gsm 32 Oct 12 15:58 Makefile
-rw-rw-r-- 1 gsm gsm 79 Oct 12 15:54 proc.c
[gsm@VM-4-3-centos lesson7]$ vim Makefile 
[gsm@VM-4-3-centos lesson7]$ ll
total 8
-rw-rw-r-- 1 gsm gsm 64 Oct 12 16:14 Makefile
-rw-rw-r-- 1 gsm gsm 79 Oct 12 15:54 proc.c
[gsm@VM-4-3-centos lesson7]$ cat Makefile 
proc:proc.c
	gcc -o proc proc.c
.PHONY:clean
clean:
	rm -f proc
[gsm@VM-4-3-centos lesson7]$ make
gcc -o proc proc.c
[gsm@VM-4-3-centos lesson7]$ ./proc 
hello Linux!
[gsm@VM-4-3-centos lesson7]$ ll
total 20
-rw-rw-r-- 1 gsm gsm   64 Oct 12 16:14 Makefile
-rwxrwxr-x 1 gsm gsm 8360 Oct 12 16:15 proc
-rw-rw-r-- 1 gsm gsm   79 Oct 12 15:54 proc.c
[gsm@VM-4-3-centos lesson7]$ make clean
rm -f proc
[gsm@VM-4-3-centos lesson7]$ ll
total 8
-rw-rw-r-- 1 gsm gsm 64 Oct 12 16:14 Makefile
-rw-rw-r-- 1 gsm gsm 79 Oct 12 15:54 proc.c
[gsm@VM-4-3-centos lesson7]$ make proc
gcc -o proc proc.c
[gsm@VM-4-3-centos lesson7]$ ll
total 20
-rw-rw-r-- 1 gsm gsm   64 Oct 12 16:14 Makefile
-rwxrwxr-x 1 gsm gsm 8360 Oct 12 16:22 proc
-rw-rw-r-- 1 gsm gsm   79 Oct 12 15:54 proc.c
[gsm@VM-4-3-centos lesson7]$ make clean
rm -f proc
[gsm@VM-4-3-centos lesson7]$ ll
total 8
-rw-rw-r-- 1 gsm gsm 64 Oct 12 16:14 Makefile
-rw-rw-r-- 1 gsm gsm 79 Oct 12 15:54 proc.c
[gsm@VM-4-3-centos lesson7]$ vim Makefile 
[gsm@VM-4-3-centos lesson7]$ cat Makefile 
.PHONY:clean
clean:
	rm -f proc
proc:proc.c
	gcc -o proc proc.c
[gsm@VM-4-3-centos lesson7]$ make
rm -f proc
[gsm@VM-4-3-centos lesson7]$ make proc
gcc -o proc proc.c
[gsm@VM-4-3-centos lesson7]$ ll
total 20
-rw-rw-r-- 1 gsm gsm   64 Oct 12 16:24 Makefile
-rwxrwxr-x 1 gsm gsm 8360 Oct 12 16:24 proc
-rw-rw-r-- 1 gsm gsm   79 Oct 12 15:54 proc.c
[gsm@VM-4-3-centos lesson7]$ vim Makefile 
[gsm@VM-4-3-centos lesson7]$ vim Makefile 
[gsm@VM-4-3-centos lesson7]$ ll
total 20
-rw-rw-r-- 1 gsm gsm   94 Oct 12 16:27 Makefile
-rwxrwxr-x 1 gsm gsm 8360 Oct 12 16:24 proc
-rw-rw-r-- 1 gsm gsm   79 Oct 12 15:54 proc.c
[gsm@VM-4-3-centos lesson7]$ rm proc
[gsm@VM-4-3-centos lesson7]$ ll
total 8
-rw-rw-r-- 1 gsm gsm 94 Oct 12 16:27 Makefile
-rw-rw-r-- 1 gsm gsm 79 Oct 12 15:54 proc.c
[gsm@VM-4-3-centos lesson7]$ cat Makefile 
proc:proc.c
	echo "hahaha"
	#gcc -o proc proc.c
.PHONY:clean
clean:
	echo "hehe"
	#rm -f proc
[gsm@VM-4-3-centos lesson7]$ make
echo "hahaha"
hahaha
#gcc -o proc proc.c
[gsm@VM-4-3-centos lesson7]$ vim Makefile 
[gsm@VM-4-3-centos lesson7]$ cat Makefile 
proc:proc.c
	echo "hahaha"
.PHONY:clean
clean:
	echo "hehe"
	#rm -f proc
[gsm@VM-4-3-centos lesson7]$ make
echo "hahaha"
hahaha
[gsm@VM-4-3-centos lesson7]$ ll
total 8
-rw-rw-r-- 1 gsm gsm 73 Oct 12 16:29 Makefile
-rw-rw-r-- 1 gsm gsm 79 Oct 12 15:54 proc.c
[gsm@VM-4-3-centos lesson7]$ vim Makefile 
[gsm@VM-4-3-centos lesson7]$ make
echo "hahaha"
hahaha
echo "hahaha"
hahaha
echo "hahaha"
hahaha
echo "hahaha"
hahaha
[gsm@VM-4-3-centos lesson7]$ vim Makefile 
[gsm@VM-4-3-centos lesson7]$ cat Makefile 
proc:proc.c
	@echo "hahaha"
	@echo "hahaha"
	@echo "hahaha"
	@echo "hahaha"
.PHONY:clean
clean:
	echo "hehe"
	#rm -f proc
[gsm@VM-4-3-centos lesson7]$ make
hahaha
hahaha
hahaha
hahaha
[gsm@VM-4-3-centos lesson7]$ vim Makefile 
[gsm@VM-4-3-centos lesson7]$ cat Makefile 
proc:proc.c
	@echo "开始编译..."
	@gcc -o proc proc.c
	@echo "结束编译..."
.PHONY:clean
clean:
	@echo "清理完成..."
	@rm -f proc
[gsm@VM-4-3-centos lesson7]$ make
开始编译...
结束编译...
[gsm@VM-4-3-centos lesson7]$ vim proc.c
[gsm@VM-4-3-centos lesson7]$ cat proc.c
#include <stdio.h>

int main()
{
    printf("hello Linux!\n")
    return 0;
}
[gsm@VM-4-3-centos lesson7]$ make clean
清理完成...
[gsm@VM-4-3-centos lesson7]$ make
开始编译...
proc.c: In function 'main':
proc.c:6:5: error: expected ';' before 'return'
     return 0;
     ^
make: *** [proc] Error 1
[gsm@VM-4-3-centos lesson7]$ vim proc.c 
[gsm@VM-4-3-centos lesson7]$ ll
total 8
-rw-rw-r-- 1 gsm gsm 141 Oct 12 16:35 Makefile
-rw-rw-r-- 1 gsm gsm  79 Oct 12 16:39 proc.c
[gsm@VM-4-3-centos lesson7]$ make
开始编译...
结束编译...
[gsm@VM-4-3-centos lesson7]$ ll
total 20
-rw-rw-r-- 1 gsm gsm  141 Oct 12 16:35 Makefile
-rwxrwxr-x 1 gsm gsm 8360 Oct 12 16:39 proc
-rw-rw-r-- 1 gsm gsm   79 Oct 12 16:39 proc.c
[gsm@VM-4-3-centos lesson7]$ ./proc 
hello Linux!
[gsm@VM-4-3-centos lesson7]$ make clean
清理完成...
[gsm@VM-4-3-centos lesson7]$ ll
total 8
-rw-rw-r-- 1 gsm gsm 141 Oct 12 16:35 Makefile
-rw-rw-r-- 1 gsm gsm  79 Oct 12 16:39 proc.c
[gsm@VM-4-3-centos lesson7]$ cp Makefile backup1012
[gsm@VM-4-3-centos lesson7]$ ll
total 12
-rw-rw-r-- 1 gsm gsm 141 Oct 12 16:41 backup1012
-rw-rw-r-- 1 gsm gsm 141 Oct 12 16:35 Makefile
-rw-rw-r-- 1 gsm gsm  79 Oct 12 16:39 proc.c
[gsm@VM-4-3-centos lesson7]$ vim Makefile 
[gsm@VM-4-3-centos lesson7]$ cat Makefile 
proc:proc.c
	gcc -o proc proc.c
.PHONY:clean
clean:
	rm -f proc
[gsm@VM-4-3-centos lesson7]$ make
gcc -o proc proc.c
[gsm@VM-4-3-centos lesson7]$ ll
total 24
-rw-rw-r-- 1 gsm gsm  141 Oct 12 16:41 backup1012
-rw-rw-r-- 1 gsm gsm   64 Oct 12 16:42 Makefile
-rwxrwxr-x 1 gsm gsm 8360 Oct 12 16:42 proc
-rw-rw-r-- 1 gsm gsm   79 Oct 12 16:39 proc.c
[gsm@VM-4-3-centos lesson7]$ ./proc 
hello Linux!
[gsm@VM-4-3-centos lesson7]$ mv backup1012 ..
[gsm@VM-4-3-centos lesson7]$ ll
total 20
-rw-rw-r-- 1 gsm gsm   64 Oct 12 16:42 Makefile
-rwxrwxr-x 1 gsm gsm 8360 Oct 12 16:42 proc
-rw-rw-r-- 1 gsm gsm   79 Oct 12 16:39 proc.c
[gsm@VM-4-3-centos lesson7]$ make clean
rm -f proc
[gsm@VM-4-3-centos lesson7]$ ll
total 8
-rw-rw-r-- 1 gsm gsm 64 Oct 12 16:42 Makefile
-rw-rw-r-- 1 gsm gsm 79 Oct 12 16:39 proc.c
[gsm@VM-4-3-centos lesson7]$ make
gcc -o proc proc.c
[gsm@VM-4-3-centos lesson7]$ ./proc 
hello Linux!
[gsm@VM-4-3-centos lesson7]$ ll
total 20
-rw-rw-r-- 1 gsm gsm   64 Oct 12 16:48 Makefile
-rwxrwxr-x 1 gsm gsm 8360 Oct 12 16:49 proc
-rw-rw-r-- 1 gsm gsm   79 Oct 12 16:39 proc.c
[gsm@VM-4-3-centos lesson7]$ make
make: `proc' is up to date.
[gsm@VM-4-3-centos lesson7]$ vim Makefile 
[gsm@VM-4-3-centos lesson7]$ cat Makefile 
proc:proc.c
	gcc -o proc proc.c

#.PHONY:clean
clean:
	rm -f proc
[gsm@VM-4-3-centos lesson7]$ make clean
rm -f proc
[gsm@VM-4-3-centos lesson7]$ make
gcc -o proc proc.c
[gsm@VM-4-3-centos lesson7]$ make
make: `proc' is up to date.
[gsm@VM-4-3-centos lesson7]$ make
make: `proc' is up to date.
[gsm@VM-4-3-centos lesson7]$ make clean
rm -f proc
[gsm@VM-4-3-centos lesson7]$ make clean
rm -f proc
[gsm@VM-4-3-centos lesson7]$ make clean
rm -f proc
[gsm@VM-4-3-centos lesson7]$ vim Makefile 
[gsm@VM-4-3-centos lesson7]$ cat Makefile 
.PHONY:proc
proc:proc.c
	gcc -o proc proc.c

#.PHONY:clean
clean:
	rm -f proc
[gsm@VM-4-3-centos lesson7]$ make clean
rm -f proc
[gsm@VM-4-3-centos lesson7]$ make
gcc -o proc proc.c
[gsm@VM-4-3-centos lesson7]$ ./proc 
hello Linux!
[gsm@VM-4-3-centos lesson7]$ make
gcc -o proc proc.c
[gsm@VM-4-3-centos lesson7]$ make
gcc -o proc proc.c
[gsm@VM-4-3-centos lesson7]$ make
gcc -o proc proc.c
[gsm@VM-4-3-centos lesson7]$ ll
total 20
-rw-rw-r-- 1 gsm gsm   78 Oct 12 17:00 Makefile
-rwxrwxr-x 1 gsm gsm 8360 Oct 12 17:01 proc
-rw-rw-r-- 1 gsm gsm   79 Oct 12 16:39 proc.c
[gsm@VM-4-3-centos lesson7]$ make clean
rm -f proc
[gsm@VM-4-3-centos lesson7]$ ll
total 8
-rw-rw-r-- 1 gsm gsm 78 Oct 12 17:00 Makefile
-rw-rw-r-- 1 gsm gsm 79 Oct 12 16:39 proc.c
[gsm@VM-4-3-centos lesson7]$ vim Makefile 
[gsm@VM-4-3-centos lesson7]$ cat Makefile 
#.PHONY:proc
proc:proc.c
	gcc -o proc proc.c

#.PHONY:clean
clean:
	rm -f proc
[gsm@VM-4-3-centos lesson7]$ make
gcc -o proc proc.c
[gsm@VM-4-3-centos lesson7]$ ll
total 20
-rw-rw-r-- 1 gsm gsm   79 Oct 12 17:06 Makefile
-rwxrwxr-x 1 gsm gsm 8360 Oct 12 17:06 proc
-rw-rw-r-- 1 gsm gsm   79 Oct 12 16:39 proc.c
[gsm@VM-4-3-centos lesson7]$ ./proc 
hello Linux!
[gsm@VM-4-3-centos lesson7]$ make
make: `proc' is up to date.
[gsm@VM-4-3-centos lesson7]$ make
make: `proc' is up to date.
[gsm@VM-4-3-centos lesson7]$ vim proc.c
[gsm@VM-4-3-centos lesson7]$ cat proc.c
#include <stdio.h>

int main()
{
    printf("hello Linux!\n");
    printf("hello Linux!\n");
    printf("hello Linux!\n");
    printf("hello Linux!\n");
    return 0;
}
[gsm@VM-4-3-centos lesson7]$ make
gcc -o proc proc.c
[gsm@VM-4-3-centos lesson7]$ make
make: `proc' is up to date.
[gsm@VM-4-3-centos lesson7]$ ll
total 20
-rw-rw-r-- 1 gsm gsm   79 Oct 12 17:06 Makefile
-rwxrwxr-x 1 gsm gsm 8360 Oct 12 17:07 proc
-rw-rw-r-- 1 gsm gsm  169 Oct 12 17:07 proc.c
[gsm@VM-4-3-centos lesson7]$ stat proc.c
  File: 'proc.c'
  Size: 169       	Blocks: 8          IO Block: 4096   regular file
Device: fd01h/64769d	Inode: 1052393     Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1001/     gsm)   Gid: ( 1001/     gsm)
Access: 2025-10-12 17:07:11.741037892 +0800
Modify: 2025-10-12 17:07:11.739037901 +0800
Change: 2025-10-12 17:07:11.739037901 +0800
 Birth: -
[gsm@VM-4-3-centos lesson7]$ ll
total 20
-rw-rw-r-- 1 gsm gsm   79 Oct 12 17:06 Makefile
-rwxrwxr-x 1 gsm gsm 8360 Oct 12 17:07 proc
-rw-rw-r-- 1 gsm gsm  169 Oct 12 17:07 proc.c
[gsm@VM-4-3-centos lesson7]$ chmod o-r proc.c
[gsm@VM-4-3-centos lesson7]$ ll
total 20
-rw-rw-r-- 1 gsm gsm   79 Oct 12 17:06 Makefile
-rwxrwxr-x 1 gsm gsm 8360 Oct 12 17:07 proc
-rw-rw---- 1 gsm gsm  169 Oct 12 17:07 proc.c
[gsm@VM-4-3-centos lesson7]$ stat proc.c
  File: 'proc.c'
  Size: 169       	Blocks: 8          IO Block: 4096   regular file
Device: fd01h/64769d	Inode: 1052393     Links: 1
Access: (0660/-rw-rw----)  Uid: ( 1001/     gsm)   Gid: ( 1001/     gsm)
Access: 2025-10-12 17:07:11.741037892 +0800
Modify: 2025-10-12 17:07:11.739037901 +0800
Change: 2025-10-12 17:14:13.525097070 +0800
 Birth: -
[gsm@VM-4-3-centos lesson7]$ vim proc.c
[gsm@VM-4-3-centos lesson7]$ cat proc.c
#include <stdio.h>

int main()
{
    printf("hello Linux!\n");
    printf("hello Linux!\n");
    printf("hello Linux!\n");
    printf("hello Linux!\n");
    printf("hello Linux!\n");
    printf("hello Linux!\n");
    printf("hello Linux!\n");
    return 0;
}
[gsm@VM-4-3-centos lesson7]$ stat proc.c
  File: 'proc.c'
  Size: 259       	Blocks: 8          IO Block: 4096   regular file
Device: fd01h/64769d	Inode: 1052393     Links: 1
Access: (0660/-rw-rw----)  Uid: ( 1001/     gsm)   Gid: ( 1001/     gsm)
Access: 2025-10-12 17:15:26.229763190 +0800
Modify: 2025-10-12 17:15:26.226763204 +0800
Change: 2025-10-12 17:15:26.226763204 +0800
 Birth: -
[gsm@VM-4-3-centos lesson7]$ ll
total 20
-rw-rw-r-- 1 gsm gsm   79 Oct 12 17:06 Makefile
-rwxrwxr-x 1 gsm gsm 8360 Oct 12 17:07 proc
-rw-rw---- 1 gsm gsm  259 Oct 12 17:15 proc.c
[gsm@VM-4-3-centos lesson7]$ make
gcc -o proc proc.c
[gsm@VM-4-3-centos lesson7]$ make
make: `proc' is up to date.
[gsm@VM-4-3-centos lesson7]$ make
make: `proc' is up to date.
[gsm@VM-4-3-centos lesson7]$ stat proc.c
  File: 'proc.c'
  Size: 259       	Blocks: 8          IO Block: 4096   regular file
Device: fd01h/64769d	Inode: 1052393     Links: 1
Access: (0660/-rw-rw----)  Uid: ( 1001/     gsm)   Gid: ( 1001/     gsm)
Access: 2025-10-12 17:15:26.229763190 +0800
Modify: 2025-10-12 17:15:26.226763204 +0800
Change: 2025-10-12 17:15:26.226763204 +0800
 Birth: -
[gsm@VM-4-3-centos lesson7]$ stat proc
  File: 'proc'
  Size: 8360      	Blocks: 24         IO Block: 4096   regular file
Device: fd01h/64769d	Inode: 1052395     Links: 1
Access: (0775/-rwxrwxr-x)  Uid: ( 1001/     gsm)   Gid: ( 1001/     gsm)
Access: 2025-10-12 17:23:44.872478158 +0800
Modify: 2025-10-12 17:23:44.147481475 +0800
Change: 2025-10-12 17:23:44.147481475 +0800
 Birth: -
[gsm@VM-4-3-centos lesson7]$ make
make: `proc' is up to date.
[gsm@VM-4-3-centos lesson7]$ touch -m proc.c
[gsm@VM-4-3-centos lesson7]$ stat proc.c
  File: 'proc.c'
  Size: 259       	Blocks: 8          IO Block: 4096   regular file
Device: fd01h/64769d	Inode: 1052393     Links: 1
Access: (0660/-rw-rw----)  Uid: ( 1001/     gsm)   Gid: ( 1001/     gsm)
Access: 2025-10-12 17:25:46.871920321 +0800
Modify: 2025-10-12 17:25:45.957924498 +0800
Change: 2025-10-12 17:25:45.957924498 +0800
 Birth: -
[gsm@VM-4-3-centos lesson7]$ make
gcc -o proc proc.c
[gsm@VM-4-3-centos lesson7]$ stat proc
  File: 'proc'
  Size: 8360      	Blocks: 24         IO Block: 4096   regular file
Device: fd01h/64769d	Inode: 1052395     Links: 1
Access: (0775/-rwxrwxr-x)  Uid: ( 1001/     gsm)   Gid: ( 1001/     gsm)
Access: 2025-10-12 17:26:32.865710134 +0800
Modify: 2025-10-12 17:26:32.422712158 +0800
Change: 2025-10-12 17:26:32.422712158 +0800
 Birth: -
[gsm@VM-4-3-centos lesson7]$ make
make: `proc' is up to date.
[gsm@VM-4-3-centos lesson7]$ touch proc.c
[gsm@VM-4-3-centos lesson7]$ stat proc.c
  File: 'proc.c'
  Size: 259       	Blocks: 8          IO Block: 4096   regular file
Device: fd01h/64769d	Inode: 1052393     Links: 1
Access: (0660/-rw-rw----)  Uid: ( 1001/     gsm)   Gid: ( 1001/     gsm)
Access: 2025-10-12 17:27:22.865481713 +0800
Modify: 2025-10-12 17:27:21.185489386 +0800
Change: 2025-10-12 17:27:21.185489386 +0800
 Birth: -
[gsm@VM-4-3-centos lesson7]$ make
gcc -o proc proc.c
[gsm@VM-4-3-centos lesson7]$ ll
total 20
-rw-rw-r-- 1 gsm gsm   79 Oct 12 17:06 Makefile
-rwxrwxr-x 1 gsm gsm 8360 Oct 12 17:27 proc
-rw-rw---- 1 gsm gsm  259 Oct 12 17:27 proc.c
[gsm@VM-4-3-centos lesson7]$ vim Makefile 
[gsm@VM-4-3-centos lesson7]$ cat Makefile 
.PHONY:proc
proc:proc.c
	gcc -o proc proc.c

#.PHONY:clean
clean:
	rm -f proc
[gsm@VM-4-3-centos lesson7]$ make
gcc -o proc proc.c
[gsm@VM-4-3-centos lesson7]$ make
gcc -o proc proc.c
[gsm@VM-4-3-centos lesson7]$ make
gcc -o proc proc.c
[gsm@VM-4-3-centos lesson7]$ make clean
rm -f proc
[gsm@VM-4-3-centos lesson7]$ ll
total 8
-rw-rw-r-- 1 gsm gsm  78 Oct 12 17:30 Makefile
-rw-rw---- 1 gsm gsm 259 Oct 12 17:27 proc.c
[gsm@VM-4-3-centos lesson7]$ vim Makefile 
[gsm@VM-4-3-centos lesson7]$ cat Makefile 
proc:proc.c
	gcc -o proc proc.c

.PHONY:clean
clean:
	rm -f proc
[gsm@VM-4-3-centos lesson7]$ ll
total 8
-rw-rw-r-- 1 gsm gsm  65 Oct 12 17:56 Makefile
-rw-rw---- 1 gsm gsm 259 Oct 12 17:27 proc.c
[gsm@VM-4-3-centos lesson7]$ cp Makefile backup20251012_2
[gsm@VM-4-3-centos lesson7]$ ll
total 12
-rw-rw-r-- 1 gsm gsm  65 Oct 12 17:57 backup20251012_2
-rw-rw-r-- 1 gsm gsm  65 Oct 12 17:56 Makefile
-rw-rw---- 1 gsm gsm 259 Oct 12 17:27 proc.c
[gsm@VM-4-3-centos lesson7]$ mv backup20251012_2 ..
[gsm@VM-4-3-centos lesson7]$ ll
total 8
-rw-rw-r-- 1 gsm gsm  65 Oct 12 17:56 Makefile
-rw-rw---- 1 gsm gsm 259 Oct 12 17:27 proc.c
[gsm@VM-4-3-centos lesson7]$ vim Makefile 
[gsm@VM-4-3-centos lesson7]$ cat Makefile 
proc:proc.o
	gcc proc.o -o proc
proc.o:proc.s
	gcc -c proc.s -o proc.o
proc.s:proc.i
	gcc -S proc.i -o proc.s
proc.i:proc.c
	gcc -E proc.c -o proc.i


.PHONY:clean
clean:
	rm -f proc
[gsm@VM-4-3-centos lesson7]$ make
gcc -E proc.c -o proc.i
gcc -S proc.i -o proc.s
gcc -c proc.s -o proc.o
gcc proc.o -o proc
[gsm@VM-4-3-centos lesson7]$ ll
total 48
-rw-rw-r-- 1 gsm gsm   183 Oct 12 18:05 Makefile
-rwxrwxr-x 1 gsm gsm  8360 Oct 12 18:05 proc
-rw-rw---- 1 gsm gsm   259 Oct 12 17:27 proc.c
-rw-rw-r-- 1 gsm gsm 17057 Oct 12 18:05 proc.i
-rw-rw-r-- 1 gsm gsm  1848 Oct 12 18:05 proc.o
-rw-rw-r-- 1 gsm gsm   622 Oct 12 18:05 proc.s
[gsm@VM-4-3-centos lesson7]$ ./proc 
hello Linux!
hello Linux!
hello Linux!
hello Linux!
hello Linux!
hello Linux!
hello Linux!
[gsm@VM-4-3-centos lesson7]$ vim Makefile 
[gsm@VM-4-3-centos lesson7]$ cat Makefile 
proc:proc.o
	gcc proc.o -o proc
proc.o:proc.s
	gcc -c proc.s -o proc.o
proc.s:proc.i
	gcc -S proc.i -o proc.s
proc.i:proc.c
	gcc -E proc.c -o proc.i


.PHONY:clean
clean:
	rm -f proc.i proc.s proc.o proc
[gsm@VM-4-3-centos lesson7]$ make clean
rm -f proc.i proc.s proc.o proc
[gsm@VM-4-3-centos lesson7]$ ll
total 8
-rw-rw-r-- 1 gsm gsm 204 Oct 12 18:07 Makefile
-rw-rw---- 1 gsm gsm 259 Oct 12 17:27 proc.c
[gsm@VM-4-3-centos lesson7]$ vim Makefile 
[gsm@VM-4-3-centos lesson7]$ cat Makefile 
proc:proc.o
	gcc proc.o -o proc
proc.o:proc.c
	gcc -c proc.c


.PHONY:clean
clean:
	rm -f proc.o proc
[gsm@VM-4-3-centos lesson7]$ ll
total 8
-rw-rw-r-- 1 gsm gsm 102 Oct 12 18:17 Makefile
-rw-rw---- 1 gsm gsm 259 Oct 12 17:27 proc.c
[gsm@VM-4-3-centos lesson7]$ make
gcc -c proc.c
gcc proc.o -o proc
[gsm@VM-4-3-centos lesson7]$ ll
total 24
-rw-rw-r-- 1 gsm gsm  102 Oct 12 18:17 Makefile
-rwxrwxr-x 1 gsm gsm 8360 Oct 12 18:17 proc
-rw-rw---- 1 gsm gsm  259 Oct 12 17:27 proc.c
-rw-rw-r-- 1 gsm gsm 1848 Oct 12 18:17 proc.o
[gsm@VM-4-3-centos lesson7]$ make clean
rm -f proc.o proc
[gsm@VM-4-3-centos lesson7]$ ll
total 8
-rw-rw-r-- 1 gsm gsm 102 Oct 12 18:17 Makefile
-rw-rw---- 1 gsm gsm 259 Oct 12 17:27 proc.c
[gsm@VM-4-3-centos lesson7]$ vim Makefile 
[gsm@VM-4-3-centos lesson7]$ cat Makefile 
proc:proc.o
	gcc proc.o -o proc
%.o:%.c
	gcc -c $<


.PHONY:clean
clean:
	rm -f proc.o proc
[gsm@VM-4-3-centos lesson7]$ make
gcc -c proc.c
gcc proc.o -o proc
[gsm@VM-4-3-centos lesson7]$ ll
total 24
-rw-rw-r-- 1 gsm gsm   92 Oct 12 18:20 Makefile
-rwxrwxr-x 1 gsm gsm 8360 Oct 12 18:22 proc
-rw-rw---- 1 gsm gsm  259 Oct 12 17:27 proc.c
-rw-rw-r-- 1 gsm gsm 1848 Oct 12 18:22 proc.o
[gsm@VM-4-3-centos lesson7]$ make clean
rm -f proc.o proc
[gsm@VM-4-3-centos lesson7]$ ll
total 8
-rw-rw-r-- 1 gsm gsm  92 Oct 12 18:20 Makefile
-rw-rw---- 1 gsm gsm 259 Oct 12 17:27 proc.c
[gsm@VM-4-3-centos lesson7]$ vim Makefile 
[gsm@VM-4-3-centos lesson7]$ cat Makefile 
bin=proc
src=proc.o

$(bin):$(src)
	gcc $^ -o $@
%.o:%.c
	gcc -c $<


.PHONY:clean
clean:
	rm -f proc.o $(bin)
[gsm@VM-4-3-centos lesson7]$ make
gcc -c proc.c
gcc proc.o -o proc
[gsm@VM-4-3-centos lesson7]$ ll
total 24
-rw-rw-r-- 1 gsm gsm  111 Oct 12 19:47 Makefile
-rwxrwxr-x 1 gsm gsm 8360 Oct 12 19:47 proc
-rw-rw---- 1 gsm gsm  259 Oct 12 17:27 proc.c
-rw-rw-r-- 1 gsm gsm 1848 Oct 12 19:47 proc.o
[gsm@VM-4-3-centos lesson7]$ ./proc 
hello Linux!
hello Linux!
hello Linux!
hello Linux!
hello Linux!
hello Linux!
hello Linux!
[gsm@VM-4-3-centos lesson7]$ make clean
rm -f proc.o proc
[gsm@VM-4-3-centos lesson7]$ ll
total 8
-rw-rw-r-- 1 gsm gsm 111 Oct 12 19:47 Makefile
-rw-rw---- 1 gsm gsm 259 Oct 12 17:27 proc.c
[gsm@VM-4-3-centos lesson7]$ cat Makefile 
bin=hello
src=proc.o

$(bin):$(src)
	gcc $^ -o $@
%.o:%.c
	gcc -c $<


.PHONY:clean
clean:
	rm -f proc.o $(bin)
[gsm@VM-4-3-centos lesson7]$ make
gcc -c proc.c
gcc proc.o -o hello
[gsm@VM-4-3-centos lesson7]$ ll
total 24
-rwxrwxr-x 1 gsm gsm 8360 Oct 12 19:48 hello
-rw-rw-r-- 1 gsm gsm  112 Oct 12 19:48 Makefile
-rw-rw---- 1 gsm gsm  259 Oct 12 17:27 proc.c
-rw-rw-r-- 1 gsm gsm 1848 Oct 12 19:48 proc.o
[gsm@VM-4-3-centos lesson7]$ ./hello 
hello Linux!
hello Linux!
hello Linux!
hello Linux!
hello Linux!
hello Linux!
hello Linux!
[gsm@VM-4-3-centos lesson7]$ make clean
rm -f proc.o hello
[gsm@VM-4-3-centos lesson7]$ ll
total 8
-rw-rw-r-- 1 gsm gsm 112 Oct 12 19:48 Makefile
-rw-rw---- 1 gsm gsm 259 Oct 12 17:27 proc.c
[gsm@VM-4-3-centos lesson7]$ cp Makefile backup20251012_2
[gsm@VM-4-3-centos lesson7]$ ll
total 12
-rw-rw-r-- 1 gsm gsm 112 Oct 12 19:50 backup20251012_2
-rw-rw-r-- 1 gsm gsm 112 Oct 12 19:48 Makefile
-rw-rw---- 1 gsm gsm 259 Oct 12 17:27 proc.c
[gsm@VM-4-3-centos lesson7]$ mv backup20251012_2 ..
[gsm@VM-4-3-centos lesson7]$ ll
total 8
-rw-rw-r-- 1 gsm gsm 112 Oct 12 19:48 Makefile
-rw-rw---- 1 gsm gsm 259 Oct 12 17:27 proc.c
[gsm@VM-4-3-centos lesson7]$ vim Makefile 
[gsm@VM-4-3-centos lesson7]$ cat Makefile 
bin=proc
src=proc.c

$(bin):$(src)
	gcc $^ -o $@

.PHONY:clean
clean:
	rm -f $(bin)
[gsm@VM-4-3-centos lesson7]$ make
gcc proc.c -o proc
[gsm@VM-4-3-centos lesson7]$ ll
total 20
-rw-rw-r-- 1 gsm gsm   84 Oct 12 20:06 Makefile
-rwxrwxr-x 1 gsm gsm 8360 Oct 12 20:07 proc
-rw-rw---- 1 gsm gsm  259 Oct 12 17:27 proc.c
[gsm@VM-4-3-centos lesson7]$ make clean
rm -f proc
[gsm@VM-4-3-centos lesson7]$ ll
total 8
-rw-rw-r-- 1 gsm gsm  84 Oct 12 20:06 Makefile
-rw-rw---- 1 gsm gsm 259 Oct 12 17:27 proc.c
[gsm@VM-4-3-centos lesson7]$ touch code.c
[gsm@VM-4-3-centos lesson7]$ ll
total 8
-rw-rw-r-- 1 gsm gsm   0 Oct 12 20:13 code.c
-rw-rw-r-- 1 gsm gsm  84 Oct 12 20:06 Makefile
-rw-rw---- 1 gsm gsm 259 Oct 12 17:27 proc.c
[gsm@VM-4-3-centos lesson7]$ cat proc.c > code.c 
[gsm@VM-4-3-centos lesson7]$ vim code.c 
[gsm@VM-4-3-centos lesson7]$ cat proc.c 
#include <stdio.h>

int main()
{
    printf("hello Linux!\n");
    printf("hello Linux!\n");
    printf("hello Linux!\n");
    printf("hello Linux!\n");
    printf("hello Linux!\n");
    printf("hello Linux!\n");
    printf("hello Linux!\n");
    return 0;
}
[gsm@VM-4-3-centos lesson7]$ cat code.c 
#include <stdio.h>

int main()
{
    printf("hello code!\n");
    printf("hello code!\n");
    printf("hello code!\n");
    printf("hello code!\n");
    printf("hello code!\n");
    printf("hello code!\n");
    return 0;
}
[gsm@VM-4-3-centos lesson7]$ ll
total 12
-rw-rw-r-- 1 gsm gsm 223 Oct 12 20:15 code.c
-rw-rw-r-- 1 gsm gsm  84 Oct 12 20:06 Makefile
-rw-rw---- 1 gsm gsm 259 Oct 12 17:27 proc.c
[gsm@VM-4-3-centos lesson7]$ vim Makefile 
[gsm@VM-4-3-centos lesson7]$ cat Makefile 
bin1=proc
src1=proc.c

bin2=code
src2=code.c

.PHONY:all
all:$(bin1) $(bin2)

$(bin1):$(src1)
	gcc $^ -o $@

$(bin2):$(src2)
	gcc $^ -o $@


.PHONY:clean
clean:
	rm -f $(bin1) $(bin2)
[gsm@VM-4-3-centos lesson7]$ make
gcc proc.c -o proc
gcc code.c -o code
[gsm@VM-4-3-centos lesson7]$ ll
total 36
-rwxrwxr-x 1 gsm gsm 8360 Oct 12 20:28 code
-rw-rw-r-- 1 gsm gsm  223 Oct 12 20:15 code.c
-rw-rw-r-- 1 gsm gsm  184 Oct 12 20:28 Makefile
-rwxrwxr-x 1 gsm gsm 8360 Oct 12 20:28 proc
-rw-rw---- 1 gsm gsm  259 Oct 12 17:27 proc.c
[gsm@VM-4-3-centos lesson7]$ ./proc 
hello Linux!
hello Linux!
hello Linux!
hello Linux!
hello Linux!
hello Linux!
hello Linux!
[gsm@VM-4-3-centos lesson7]$ ./code 
hello code!
hello code!
hello code!
hello code!
hello code!
hello code!
[gsm@VM-4-3-centos lesson7]$ make clean
rm -f proc code
[gsm@VM-4-3-centos lesson7]$ ll
total 12
-rw-rw-r-- 1 gsm gsm 223 Oct 12 20:15 code.c
-rw-rw-r-- 1 gsm gsm 184 Oct 12 20:28 Makefile
-rw-rw---- 1 gsm gsm 259 Oct 12 17:27 proc.c

5. Linux第一个系统程序−进度条

c 复制代码
[gsm@VM-4-3-centos lesson7]$ ll
total 12
-rw-rw-r-- 1 gsm gsm 223 Oct 12 20:15 code.c
-rw-rw-r-- 1 gsm gsm 184 Oct 12 20:28 Makefile
-rw-rw---- 1 gsm gsm 259 Oct 12 17:27 proc.c
[gsm@VM-4-3-centos lesson7]$ vim proc.c 
[gsm@VM-4-3-centos lesson7]$ cat proc.c 
#include <stdio.h>
#include <unistd.h>

int main()
{
    printf("hello Linux!\n");

    sleep(2);
    return 0;
}
[gsm@VM-4-3-centos lesson7]$ man 3 sleep
[gsm@VM-4-3-centos lesson7]$ vim Makefile 
[gsm@VM-4-3-centos lesson7]$ cat Makefile 
bin1=proc
src1=proc.c

#bin2=code
#src2=code.c

#.PHONY:all
#all:$(bin1) $(bin2)

$(bin1):$(src1)
	gcc $^ -o $@

#$(bin2):$(src2)
#	gcc $^ -o $@


.PHONY:clean
clean:
	rm -f $(bin1)
	#rm -f $(bin1) $(bin2)
[gsm@VM-4-3-centos lesson7]$ make
gcc proc.c -o proc
[gsm@VM-4-3-centos lesson7]$ ./proc 
hello Linux!
[gsm@VM-4-3-centos lesson7]$ vim proc.c
[gsm@VM-4-3-centos lesson7]$ cat proc.c
#include <stdio.h>
#include <unistd.h>

int main()
{
    printf("hello Linux!");

    sleep(2);
    return 0;
}
[gsm@VM-4-3-centos lesson7]$ make
gcc proc.c -o proc
[gsm@VM-4-3-centos lesson7]$ ./proc 
hello Linux![gsm@VM-4-3-centos lesson7]$ man fflush
[gsm@VM-4-3-centos lesson7]$ man stdin
[gsm@VM-4-3-centos lesson7]$ ll
total 24
-rw-rw-r-- 1 gsm gsm  223 Oct 12 20:15 code.c
-rw-rw-r-- 1 gsm gsm  206 Oct 12 21:01 Makefile
-rwxrwxr-x 1 gsm gsm 8408 Oct 12 21:03 proc
-rw-rw---- 1 gsm gsm  112 Oct 12 21:03 proc.c
[gsm@VM-4-3-centos lesson7]$ vim proc.c
[gsm@VM-4-3-centos lesson7]$ cat proc.c
#include <stdio.h>
#include <unistd.h>

int main()
{
    printf("hello Linux!");
    fflush(stdout);
    sleep(2);
    return 0;
}
[gsm@VM-4-3-centos lesson7]$ make
gcc proc.c -o proc
[gsm@VM-4-3-centos lesson7]$ ./proc 
hello Linux![gsm@VM-4-3-centos lesson7]$ make clean
rm -f proc
#rm -f proc 
[gsm@VM-4-3-centos lesson7]$ ll
total 12
-rw-rw-r-- 1 gsm gsm 223 Oct 12 20:15 code.c
-rw-rw-r-- 1 gsm gsm 206 Oct 12 21:01 Makefile
-rw-rw---- 1 gsm gsm 131 Oct 12 21:29 proc.c
c 复制代码
[gsm@VM-4-3-centos lesson7]$ mkdir count
[gsm@VM-4-3-centos lesson7]$ ll
total 16
-rw-rw-r-- 1 gsm gsm  223 Oct 12 20:15 code.c
drwxrwxr-x 2 gsm gsm 4096 Oct 12 21:32 count
-rw-rw-r-- 1 gsm gsm  206 Oct 12 21:01 Makefile
-rw-rw---- 1 gsm gsm  131 Oct 12 21:29 proc.c
[gsm@VM-4-3-centos lesson7]$ cd count/
[gsm@VM-4-3-centos count]$ ll
total 0
[gsm@VM-4-3-centos count]$ touch Count.c
[gsm@VM-4-3-centos count]$ ll
total 0
-rw-rw-r-- 1 gsm gsm 0 Oct 12 21:33 Count.c
[gsm@VM-4-3-centos count]$ ls > Makefile
[gsm@VM-4-3-centos count]$ ll
total 4
-rw-rw-r-- 1 gsm gsm  0 Oct 12 21:33 Count.c
-rw-rw-r-- 1 gsm gsm 17 Oct 12 21:33 Makefile
[gsm@VM-4-3-centos count]$ vim Makefile 
[gsm@VM-4-3-centos count]$ ll
total 4
-rw-rw-r-- 1 gsm gsm  0 Oct 12 21:33 Count.c
-rw-rw-r-- 1 gsm gsm 61 Oct 12 21:36 Makefile
[gsm@VM-4-3-centos count]$ cat Makefile 
Count:Count.c
	gcc -o $@ $^
.PHONY:clean
clean:
	rm -f Count
[gsm@VM-4-3-centos count]$ vim Count.c 
[gsm@VM-4-3-centos count]$ cat Count.c 
#include <stdio.h>
#include <unistd.h>

int main()
{
    int count = 9;
    
    while (count >= 0)
    {
        printf("%d\n", count);
        count--;
        sleep(1);
    }
    
    return 0;
}
[gsm@VM-4-3-centos count]$ make
gcc -o Count Count.c
[gsm@VM-4-3-centos count]$ ll
total 20
-rwxrwxr-x 1 gsm gsm 8408 Oct 12 21:41 Count
-rw-rw-r-- 1 gsm gsm  199 Oct 12 21:40 Count.c
-rw-rw-r-- 1 gsm gsm   61 Oct 12 21:36 Makefile
[gsm@VM-4-3-centos count]$ ./Count 
9
8
7
6
5
4
3
2
1
0
[gsm@VM-4-3-centos count]$ make clean
rm -f Count
[gsm@VM-4-3-centos count]$ ll
total 8
-rw-rw-r-- 1 gsm gsm 199 Oct 12 21:40 Count.c
-rw-rw-r-- 1 gsm gsm  61 Oct 12 21:36 Makefile
[gsm@VM-4-3-centos count]$ vim Count.c 
[gsm@VM-4-3-centos count]$ cat Count.c 
#include <stdio.h>
#include <unistd.h>

int main()
{
    int count = 9;
    
    while (count >= 0)
    {
        printf("%d\r", count);// \r回车,但是没有换行,也就没有刷新
        fflush(stdout);
        count--;
        sleep(1);
    }

    printf("\r\n");
    
    return 0;
}
[gsm@VM-4-3-centos count]$ make
gcc -o Count Count.c
[gsm@VM-4-3-centos count]$ ./Count 
0
[gsm@VM-4-3-centos count]$ make clean
rm -f Count
[gsm@VM-4-3-centos count]$ vim Count.c 
[gsm@VM-4-3-centos count]$ cat Count.c 
#include <stdio.h>
#include <unistd.h>

int main()
{
    int count = 10;
    
    while (count >= 0)
    {
        printf("%-2d\r", count);// \r回车,但是没有换行,也就没有刷新
        fflush(stdout);
        count--;
        sleep(1);
    }

    printf("\r\n");
    
    return 0;
}
[gsm@VM-4-3-centos count]$ make
gcc -o Count Count.c
[gsm@VM-4-3-centos count]$ ./Count 
0 
[gsm@VM-4-3-centos count]$ make clean
rm -f Count
[gsm@VM-4-3-centos count]$ ll
total 8
-rw-rw-r-- 1 gsm gsm 300 Oct 12 22:17 Count.c
-rw-rw-r-- 1 gsm gsm  61 Oct 12 21:36 Makefile
c 复制代码
[gsm@VM-4-3-centos lesson7]$ ll
total 16
-rw-rw-r-- 1 gsm gsm  223 Oct 12 20:15 code.c
drwxrwxr-x 2 gsm gsm 4096 Oct 12 22:17 count
-rw-rw-r-- 1 gsm gsm  206 Oct 12 21:01 Makefile
-rw-rw---- 1 gsm gsm  131 Oct 12 21:29 proc.c
[gsm@VM-4-3-centos lesson7]$ mkdir processbar
[gsm@VM-4-3-centos lesson7]$ ll
total 20
-rw-rw-r-- 1 gsm gsm  223 Oct 12 20:15 code.c
drwxrwxr-x 2 gsm gsm 4096 Oct 12 22:17 count
-rw-rw-r-- 1 gsm gsm  206 Oct 12 21:01 Makefile
-rw-rw---- 1 gsm gsm  131 Oct 12 21:29 proc.c
drwxrwxr-x 2 gsm gsm 4096 Oct 12 22:21 processbar
[gsm@VM-4-3-centos lesson7]$ cd processbar/
[gsm@VM-4-3-centos processbar]$ ll
total 0
[gsm@VM-4-3-centos processbar]$ pwd
/home/gsm/112/lesson7/processbar
[gsm@VM-4-3-centos processbar]$ touch process.c
[gsm@VM-4-3-centos processbar]$ touch process.h
[gsm@VM-4-3-centos processbar]$ touch main.c
[gsm@VM-4-3-centos processbar]$ ll
total 0
-rw-rw-r-- 1 gsm gsm 0 Oct 12 22:36 main.c
-rw-rw-r-- 1 gsm gsm 0 Oct 12 22:36 process.c
-rw-rw-r-- 1 gsm gsm 0 Oct 12 22:36 process.h
[gsm@VM-4-3-centos processbar]$ cp ../count/Makefile .
[gsm@VM-4-3-centos processbar]$ ll
total 4
-rw-rw-r-- 1 gsm gsm  0 Oct 12 22:36 main.c
-rw-rw-r-- 1 gsm gsm 61 Oct 12 22:37 Makefile
-rw-rw-r-- 1 gsm gsm  0 Oct 12 22:36 process.c
-rw-rw-r-- 1 gsm gsm  0 Oct 12 22:36 process.h
[gsm@VM-4-3-centos processbar]$ vim Makefile 
[gsm@VM-4-3-centos processbar]$ cat Makefile 
process:main.c process.c
	gcc -o $@ $^
.PHONY:clean
clean:
	rm -f process
[gsm@VM-4-3-centos processbar]$ vim process.h
[gsm@VM-4-3-centos processbar]$ vim main.c 
[gsm@VM-4-3-centos processbar]$ cat process.h
#pragma once

//version

void Process();
[gsm@VM-4-3-centos processbar]$ cat process.c
#include "process.h"
#include <stdio.h>

void Process()
{
    printf("hello Process\n");
}
[gsm@VM-4-3-centos processbar]$ cat main.c 
#include <stdio.h>
#include "process.h"

int main()
{
    Process();
    printf("hello process\n");

    return 0;
}
[gsm@VM-4-3-centos processbar]$ make
gcc -o process main.c process.c
[gsm@VM-4-3-centos processbar]$ ll
total 28
-rw-rw-r-- 1 gsm gsm  117 Oct 12 22:49 main.c
-rw-rw-r-- 1 gsm gsm   74 Oct 12 22:39 Makefile
-rwxrwxr-x 1 gsm gsm 8424 Oct 12 22:52 process
-rw-rw-r-- 1 gsm gsm   91 Oct 12 22:47 process.c
-rw-rw-r-- 1 gsm gsm   41 Oct 12 22:47 process.h
[gsm@VM-4-3-centos processbar]$ ./process 
hello Process
hello process
[gsm@VM-4-3-centos processbar]$ make clean
rm -f process
[gsm@VM-4-3-centos processbar]$ ll
total 16
-rw-rw-r-- 1 gsm gsm 117 Oct 12 22:49 main.c
-rw-rw-r-- 1 gsm gsm  74 Oct 12 22:39 Makefile
-rw-rw-r-- 1 gsm gsm  91 Oct 12 22:47 process.c
-rw-rw-r-- 1 gsm gsm  41 Oct 12 22:47 process.h
[gsm@VM-4-3-centos processbar]$ man 3 usleep
[gsm@VM-4-3-centos processbar]$ vim main.c 
[gsm@VM-4-3-centos processbar]$ vim process.c
[gsm@VM-4-3-centos processbar]$ cat process.h
#pragma once

//version

void Process();
[gsm@VM-4-3-centos processbar]$ cat process.c
#include "process.h"
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#define NUM 101
#define STYLE '#'

// version 1
void Process()
{
    const char* lable = "|/-\\";
    int len = strlen(lable);
    char bar[NUM];
    memset(bar, '\0', sizeof(bar));
    int cnt = 0;

    while (cnt <= 100)
    {
        printf("[%-100s][%d%%][%c]\r", bar, cnt, lable[cnt % len]);
        fflush(stdout);
        bar[cnt] = STYLE;
        cnt++;

        usleep(50000);
    }

    printf("\r\n");
}
[gsm@VM-4-3-centos processbar]$ cat main.c 
#include <stdio.h>
#include "process.h"

int main()
{
    Process();

    return 0;
}
[gsm@VM-4-3-centos processbar]$ make
gcc -o process main.c process.c
[gsm@VM-4-3-centos processbar]$ ./process 
[####################################################################################################][100%][|]
[gsm@VM-4-3-centos processbar]$ make clean
rm -f process
[gsm@VM-4-3-centos processbar]$ ll
total 16
-rw-rw-r-- 1 gsm gsm  86 Oct 13 15:50 main.c
-rw-rw-r-- 1 gsm gsm  74 Oct 12 22:39 Makefile
-rw-rw-r-- 1 gsm gsm 496 Oct 13 16:41 process.c
-rw-rw-r-- 1 gsm gsm  41 Oct 12 22:47 process.h
[gsm@VM-4-3-centos processbar]$ vim process.c
[gsm@VM-4-3-centos processbar]$ cat process.h
#pragma once

//version

void Process();
[gsm@VM-4-3-centos processbar]$ cat process.c
#include "process.h"
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#define NUM 101
#define STYLE '='

// version 1
void Process()
{
    const char* lable = "|/-\\";
    int len = strlen(lable);
    char bar[NUM];
    memset(bar, '\0', sizeof(bar));
    int cnt = 0;

    while (cnt <= 100)
    {
        printf("[%-100s][%d%%][%c]\r", bar, cnt, lable[cnt % len]);
        fflush(stdout);
        bar[cnt] = STYLE;
        cnt++;

        if (cnt + 1 == NUM)
        {
            printf("[%-100s][%d%%][%c]\r", bar, cnt, lable[cnt % len]);
            break;
        }

        bar[cnt] = '>';

        usleep(50000);
    }

    printf("\r\n");
}
[gsm@VM-4-3-centos processbar]$ cat main.c 
#include <stdio.h>
#include "process.h"

int main()
{
    Process();

    return 0;
}
[gsm@VM-4-3-centos processbar]$ make
gcc -o process main.c process.c
[gsm@VM-4-3-centos processbar]$ ./process 
[====================================================================================================][100%][|]
[gsm@VM-4-3-centos processbar]$ make clean
rm -f process
[gsm@VM-4-3-centos processbar]$ ll
total 16
-rw-rw-r-- 1 gsm gsm  86 Oct 13 15:50 main.c
-rw-rw-r-- 1 gsm gsm  74 Oct 12 22:39 Makefile
-rw-rw-r-- 1 gsm gsm 661 Oct 13 17:33 process.c
-rw-rw-r-- 1 gsm gsm  41 Oct 12 22:47 process.h
[gsm@VM-4-3-centos processbar]$ cp process.c process-backup20251013.c
[gsm@VM-4-3-centos processbar]$ ll
total 20
-rw-rw-r-- 1 gsm gsm  86 Oct 13 15:50 main.c
-rw-rw-r-- 1 gsm gsm  74 Oct 12 22:39 Makefile
-rw-rw-r-- 1 gsm gsm 661 Oct 13 17:34 process-backup20251013.c
-rw-rw-r-- 1 gsm gsm 661 Oct 13 17:33 process.c
-rw-rw-r-- 1 gsm gsm  41 Oct 12 22:47 process.h
[gsm@VM-4-3-centos processbar]$ vim process.c
[gsm@VM-4-3-centos processbar]$ cat process.h
#pragma once

//version

void Process();
[gsm@VM-4-3-centos processbar]$ cat process.c
#include "process.h"
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#define NUM 101
#define STYLE1 '>'
#define STYLE2 '='

// version 1
void Process()
{
    const char* lable = "|/-\\";
    int len = strlen(lable);
    char bar[NUM];
    memset(bar, '\0', sizeof(bar));
    int cnt = 0;

    while (cnt < 100)
    {
        bar[cnt] = STYLE1;
        printf("[%-100s][%d%%][%c]\r", bar, cnt + 1, lable[(cnt + 1) % len]);
        fflush(stdout);
        usleep(100000);
        bar[cnt] = STYLE2;
        printf("[%-100s][%d%%][%c]\r", bar, cnt + 1, lable[(cnt + 1) % len]);
        fflush(stdout);
        cnt++;

//        if (cnt + 1 == NUM)
//        {
//            printf("[%-100s][%d%%][%c]\r", bar, cnt, lable[cnt % len]);
//            break;
//        }
//
//        bar[cnt] = '>';
//
        usleep(50000);
    }

    printf("\r\n");
}
[gsm@VM-4-3-centos processbar]$ cat main.c 
#include <stdio.h>
#include "process.h"

int main()
{
    Process();

    return 0;
}
[gsm@VM-4-3-centos processbar]$ make
gcc -o process main.c process.c
[gsm@VM-4-3-centos processbar]$ ./process 
[====================================================================================================][100%][|]
[gsm@VM-4-3-centos processbar]$ make clean
rm -f process
[gsm@VM-4-3-centos processbar]$ ll
total 20
-rw-rw-r-- 1 gsm gsm  86 Oct 13 15:50 main.c
-rw-rw-r-- 1 gsm gsm  74 Oct 12 22:39 Makefile
-rw-rw-r-- 1 gsm gsm 661 Oct 13 17:34 process-backup20251013.c
-rw-rw-r-- 1 gsm gsm 860 Oct 13 17:54 process.c
-rw-rw-r-- 1 gsm gsm  41 Oct 12 22:47 process.h
[gsm@VM-4-3-centos processbar]$ cp process.c process-backup20251013_02.c  
[gsm@VM-4-3-centos processbar]$ ll
total 24
-rw-rw-r-- 1 gsm gsm  86 Oct 13 15:50 main.c
-rw-rw-r-- 1 gsm gsm  74 Oct 12 22:39 Makefile
-rw-rw-r-- 1 gsm gsm 860 Oct 13 17:56 process-backup20251013_02.c
-rw-rw-r-- 1 gsm gsm 661 Oct 13 17:34 process-backup20251013.c
-rw-rw-r-- 1 gsm gsm 860 Oct 13 17:54 process.c
-rw-rw-r-- 1 gsm gsm  41 Oct 12 22:47 process.h
c 复制代码
[gsm@VM-4-3-centos processbar]$ man srand
[gsm@VM-4-3-centos processbar]$ man 3 time
[gsm@VM-4-3-centos processbar]$ vim main.c
[gsm@VM-4-3-centos processbar]$ cat main.c 
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <stdlib.h>
#include "process.h"

const int base = 20;
double total = 2048.0; // 2048MB
double once = 0.1;

void download()
{
    double current = 0.0;

    while (current < total)
    {
        // 模拟下载行为
        int r = rand() % base + 1;
        double speed = r * once;
        current += speed;

        if (current >= total)
        {
            current = total;
        }

        usleep(10000);
        printf("test:%.1lf/%.1lf\r", current, total);
        fflush(stdout);
    }

    printf("\n");
}

int main()
{
    srand(time(NULL));
    download();

    return 0;
}
[gsm@VM-4-3-centos processbar]$ make
gcc -o process main.c process.c
[gsm@VM-4-3-centos processbar]$ ./process 
test:2048.0/2048.0
[gsm@VM-4-3-centos processbar]$ make clean
rm -f process
[gsm@VM-4-3-centos processbar]$ ll
total 24
-rw-rw-r-- 1 gsm gsm 657 Oct 13 19:24 main.c
-rw-rw-r-- 1 gsm gsm  74 Oct 12 22:39 Makefile
-rw-rw-r-- 1 gsm gsm 860 Oct 13 17:56 process-backup20251013_02.c
-rw-rw-r-- 1 gsm gsm 661 Oct 13 17:34 process-backup20251013.c
-rw-rw-r-- 1 gsm gsm 495 Oct 13 19:02 process.c
-rw-rw-r-- 1 gsm gsm  41 Oct 12 22:47 process.h
[gsm@VM-4-3-centos processbar]$ vim main.c 
[gsm@VM-4-3-centos processbar]$ vim process.c
[gsm@VM-4-3-centos processbar]$ vim process.h
[gsm@VM-4-3-centos processbar]$ cat process.h
#pragma once

//version

//void Process();
//void Process(double total, double current);
void FlushProcess(double total, double current);
[gsm@VM-4-3-centos processbar]$ cat process.c
#include "process.h"
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#define NUM 101
#define STYLE '='
#define POINT '.'
#define SPACE ' '

const int pnum = 6;

// version 2:真实的进度条,应该根据具体的比如下载的量,来动态刷新进度
void FlushProcess(double total, double current)
{
    // 1. 更新当前进度的百分比
    double rate = (current / total) * 100;
    //printf("test:%.1lf%%\r", rate);
    //fflush(stdout);
    
    // 2. 更新进度条主体
    char bar[NUM]; // 我们认为,1% 更新一个等号
    memset(bar, '\0', sizeof(bar));

    for (int i = 0; i < (int)rate; i++)
    {
        bar[i] = STYLE;
    }

    // 3. 更新旋转光标或者是其他风格
    static int num = 0;
    char points[pnum + 1];
    memset(points, '\0', sizeof(points));

    for (int i = 0; i < pnum; i++)
    {
        if (i <= num)
        {
            points[i] = POINT;
        }
        else
        {
            points[i] = SPACE;
        }
    }

    num++;
    num %= pnum;

    // 4. test && printf
    printf("[%-100s][%.1lf%%]%s\r", bar, rate, points);
    fflush(stdout);
}

// version 1
//void Process()
//{
//    const char* lable = "|/-\\";
//    int len = strlen(lable);
//    char bar[NUM];
//    memset(bar, '\0', sizeof(bar));
//    int cnt = 0;
//
//    while (cnt <= 100)
//    {
//        printf("[%-100s][%d%%][%c]\r", bar, cnt, lable[cnt % len]);
//        fflush(stdout);
//        bar[cnt] = STYLE;
//        cnt++;
//        usleep(50000);
//    }
//
//    printf("\r\n");
//}
[gsm@VM-4-3-centos processbar]$ cat main.c
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <stdlib.h>
#include "process.h"

typedef void (*flush_t)(double total, double current);// 这是一个刷新的函数指针类型

const int base = 100;
double total = 2048.0; // 2048MB
double once = 0.1;

// 进度条的调用方式
void download(flush_t f)
{
    double current = 0.0;

    while (current < total)
    {
        // 模拟下载行为
        int r = rand() % base + 1;
        double speed = r * once;
        current += speed;

        if (current >= total)
        {
            current = total;
        }

        usleep(10000);
        
        // 更新出了本次新的下载量
        // 根据真实的应用场景,进行动态刷新
        //Process(total, 1.0);
        f(total, current);
        //printf("test:%.1lf/%.1lf\r", current, total);
        //fflush(stdout);
    }

    printf("\n");
}

int main()
{
    srand(time(NULL));
    download(FlushProcess);
    download(FlushProcess);
    download(FlushProcess);
    download(FlushProcess);
    download(FlushProcess);
    download(FlushProcess);
    download(FlushProcess);

    return 0;
}
[gsm@VM-4-3-centos processbar]$ make
gcc -std=gnu99 -o process main.c process.c
[gsm@VM-4-3-centos processbar]$ ./process 
[====================================================================================================][100.0%]..... 
[====================================================================================================][100.0%]......
[====================================================================================================][100.0%]....  
[====================================================================================================][100.0%]..... 
[====================================================================================================][100.0%].     
[====================================================================================================][100.0%]..    
[====================================================================================================][100.0%].     
[gsm@VM-4-3-centos processbar]$ make clean
rm -f process
[gsm@VM-4-3-centos processbar]$ ll
total 24
-rw-rw-r-- 1 gsm gsm 1149 Oct 14 12:15 main.c
-rw-rw-r-- 1 gsm gsm   85 Oct 14 00:52 Makefile
-rw-rw-r-- 1 gsm gsm  860 Oct 13 17:56 process-backup20251013_02.c
-rw-rw-r-- 1 gsm gsm  661 Oct 13 17:34 process-backup20251013.c
-rw-rw-r-- 1 gsm gsm 1556 Oct 14 12:15 process.c
-rw-rw-r-- 1 gsm gsm  138 Oct 14 12:15 process.h
相关推荐
艾莉丝努力练剑3 小时前
【C++模版进阶】如何理解非类型模版参数、特化与分离编译?
linux·开发语言·数据结构·c++·stl
wdfk_prog3 小时前
[Linux]学习笔记系列 -- [kernel][irq]softirq
linux·笔记·学习
迎風吹頭髮3 小时前
Linux服务器编程实践60-双向管道:socketpair函数的实现与应用场景
linux·运维·服务器
试试勇气4 小时前
Linux学习笔记(九)--Linux进程终止与进程等待
linux·笔记·学习
wheeldown4 小时前
【Linux】Linux 进程信号核心拆解:pending/block/handler 三张表 + signal/alarm 实战
linux·运维·服务器
运维老司机4 小时前
ThinkPad 安装 Ubuntu 系统教程
linux·运维·ubuntu
云飞云共享云桌面5 小时前
替代传统电脑的共享云服务器如何实现1拖8SolidWorks设计办公
linux·运维·服务器·网络·电脑·制造
添砖java‘’10 小时前
vim高效编辑:从入门到精通
linux·编辑器·操作系统·vim
tryCbest11 小时前
CentOS部署Docker容器
linux·docker·centos