Linux 编程简单示例代码
Linux进程管理
编辑a.c 文件
#include <stdio.h> #include <unistd.h> int main() { printf( "Message aaaa\n" ); if ( fork() ) { sleep(4); printf( "Message bbbb\n" ); if ( fork() ) { sleep(2); printf( "Message cccc\n" ); }else{ sleep(1); printf( "Message dddd\n" ); } }else{ sleep(1); printf( "Message eeee\n" ); if ( fork() ) { sleep(2); printf( "Message ffff\n" ); }else{ sleep(6); printf( "Message gggg\n" ); } } return 0; }
编译 a.c 文件
运行 a.out
./a.out
Linux信号处理
编辑 a.c 文件
编译 a.c 文件
gcc a.c
运行 a.out 文件
./a.out
Linux多线程
Lin编辑 a.c
#include <stdio.h> #include <string.h> #include <unistd.h> #include <pthread.h> char buffer[100] = "Hello" ; pthread_t id ; void *mystart(void *param) { int i; for (i=0; i<10; i++) { printf( "Thread %d [%s]\n", i, buffer ); sleep(1); } return NULL ; } int main() { int i; pthread_create( &id, NULL, mystart, NULL ); for (i-0; i<15; i++) { printf( "Main %d [%s]\n", i, buffer ); if ( i == 5 ) { strcpy( buffer, "-----" ); } sleep(1); } printf( "Hello,world.\n" ); return 0; }
编译运行
Linux 管道
#include <stdio.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <stdlib.h> void readMessage(int fd) { int k; char b[8]; for(;;){ k = read(fd,b,1); if(k!=1) break; putchar(b[0]); } } int main() { int pipefd[2]; pipe(pipefd); if(fork()) { int i; for(i=0;i<10;i++){ write(pipefd[1],"hello\n",6); } }else { readMessage(pipefd[0]); } }
编译运行
Linux makefile文件
编写 add.c show.c a.c 三个文件
// add.c文件: #include <stdio.h> void add(int *a,int *b) { scanf("%d %d",a,b); } // show.c 文件: #include <stdio.h> void show(int c) { printf("%d\n",c); } // a.c 文件: #include <stdio.h> void add(int *a,int *b); void show(int c); int main() { int a,b,c; add(&a,&b); c=a+b; show(c); return 0; }
编写makefile文件
myapp : a.o show.o add.o gcc a.o show.o add.o -o myapp a.o : a.c gcc -c a.c show.o : show.c gcc -c show.c add.o : add.c gcc -c add.c
运行 makefile 文件
make
运行 myapp
./myapp
基本I/O文件操作
编写w.c写文件:
#include <stdio.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> int main() { int fd = open("myfile.txt",O_WRONLY|O_CREAT,0644); if(fd<0) { printf("File cannot open!\n"); return 1; } write(fd,"wjwwjwwjwwjwwjw",15); close(fd); return 0; }
运行 w.c 文件
编写读文件r.c
#include <stdio.h> #include <sys/stat.h> #include <unistd.h> #include <fcntl.h> #include <sys/types.h> int main() { int k; char b[1000]; int fd = open("myfile.txt",O_RDONLY); if(fd < 0){ perror("cannot open file!"); return 1; } k = read(fd,b,1000); printf("%d\n",k); b[k]=0; printf("%d\n",b); close(fd); return 0; }
运行 a.out
完成!
【版权声明】本博文著作权归作者所有,任何形式的转载都请联系作者获取授权并注明出处!
【重要说明】博文仅作为本人的学习记录,论点和观点仅代表个人而不代表技术的真理,目的是自我学习和有幸成为可以向他人分享的经验,因此有错误会虚心接受改正,但不代表此刻博文无误!
【博客园地址】叫我+V : http://www.cnblogs.com/wjw1014
【CSDN地址】叫我+V : https://wjw1014.blog.csdn.net/
【Gitee地址】叫我+V :https://gitee.com/wjw1014
【重要说明】博文仅作为本人的学习记录,论点和观点仅代表个人而不代表技术的真理,目的是自我学习和有幸成为可以向他人分享的经验,因此有错误会虚心接受改正,但不代表此刻博文无误!
【博客园地址】叫我+V : http://www.cnblogs.com/wjw1014
【CSDN地址】叫我+V : https://wjw1014.blog.csdn.net/
【Gitee地址】叫我+V :https://gitee.com/wjw1014
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!