Linux 编程简单示例代码
Linux进程管理
编辑a.c 文件
#include <stdio.h>
#include <unistd.h> int main()
{
printf( "Message aaaa\n" );
if ( fork() ) {
sleep();
printf( "Message bbbb\n" );
if ( fork() ) {
sleep();
printf( "Message cccc\n" );
}else{
sleep();
printf( "Message dddd\n" );
}
}else{
sleep();
printf( "Message eeee\n" );
if ( fork() ) {
sleep();
printf( "Message ffff\n" );
}else{
sleep();
printf( "Message gggg\n" );
}
}
return ;
}
编译 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[] = "Hello" ;
pthread_t id ; void *mystart(void *param)
{
int i;
for (i=; i<; i++) {
printf( "Thread %d [%s]\n", i, buffer );
sleep();
} return NULL ;
} int main()
{
int i;
pthread_create( &id, NULL, mystart, NULL ); for (i-; i<; i++) {
printf( "Main %d [%s]\n", i, buffer );
if ( i == ) {
strcpy( buffer, "-----" );
}
sleep();
} printf( "Hello,world.\n" );
return ;
}
编译运行
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[];
for(;;){
k = read(fd,b,);
if(k!=) break;
putchar(b[]);
}
} int main()
{
int pipefd[];
pipe(pipefd);
if(fork())
{
int i;
for(i=;i<;i++){
write(pipefd[],"hello\n",);
}
}else
{
readMessage(pipefd[]);
}
}
编译运行
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 ;
}
编写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,);
if(fd<)
{
printf("File cannot open!\n");
return ;
}
write(fd,"wjwwjwwjwwjwwjw",);
close(fd);
return ; }
运行 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[];
int fd = open("myfile.txt",O_RDONLY);
if(fd < ){
perror("cannot open file!");
return ;
}
k = read(fd,b,);
printf("%d\n",k);
b[k]=;
printf("%d\n",b);
close(fd);
return ;
}
运行 a.out
完成!
Linux 编程简单示例代码的更多相关文章
- JDBC简单示例代码
本文章教程中将演示如何创建一个简单的JDBC应用程序的示例. 这将显示如何打开数据库连接,执行SQL查询并显示结果. 这个示例代码中涉及所有步骤,一些步骤将在本教程的后续章节中进行说明. 创建JDBC ...
- Linux内核模块简单示例
1. Linux 内核的整体结构非常庞大,其包含的组件也非常多,使用这些组件的方法有两种: ① 直接编译进内核文件,即zImage或者bzImage(问题:占用内存过多) ② 动态添加 * 模块本身并 ...
- C#使用互斥量(Mutex)实现多进程并发操作时多进程间线程同步操作(进程同步)的简单示例代码及使用方法
本文主要是实现操作系统级别的多进程间线程同步(进程同步)的示例代码及测试结果.代码经过测试,可供参考,也可直接使用. 承接上一篇博客的业务场景[C#使用读写锁三行代码简单解决多线程并发写入文件时线程同 ...
- 基于OpenMP的C++并行编程简单示例
示例要求:在整数A和B之间找到符合条件的值X,使f(X)=C. 示例代码(需要在VS中开启OpenMP支持): #include<iostream> #include<time.h& ...
- Linux网络编程简单示例
linux 网络编程是通过socket(套接字)接口实现,Socket是一种文件描述符,socket起源于UNIX,在Unix一切皆文件哲学的思想下,socket是一种"打开—读/写—关闭& ...
- _CrtDumpMemoryLeaks报告程序中的内存泄露问题(简单示例代码)
// .h 文件 #pragma once class CConsoleDump { public: explicit CConsoleDump(LPCTSTR lpszWindowTitle = N ...
- 【java】网络socket编程简单示例
package 网络编程; import java.io.IOException; import java.io.PrintStream; import java.net.ServerSocket; ...
- Spring security oauth2 client_credentials认证 最简单示例代码
基于spring-boot-2.0.0 1,在pom.xml中添加: <!-- security --> <!-- https://mvnrepository.com/artifac ...
- C#判断数据类型的简单示例代码
; Console.WriteLine( "i is an int? {0}",i.GetType()==typeof(int)); Console.WriteLine( &quo ...
随机推荐
- UGUI实现摇杆
效果图
- LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏。
问题描述:VS2010 LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏. 解决办法: 修改嵌入清单选项为否,然后重新便于创建. 参考自:htt ...
- asp.net webapi 404/或无效控制器/或无效请求 截取处理统一输出格式
public static class PreRouteHandler { public static void HttpPreRoute(this HttpConfigura ...
- windows系统快捷键
1.我的键盘:windows键的开启,需要按住FN键+windows键. 2.windows键 + E,表示打开我的电脑. 3.windows键 + R,打开windows的命令行窗口. 4.wind ...
- WebStorage (1) 实例
实例代码 <p>本页面每5秒刷新一下,这是您第<mark></mark>次进入本页面!</p> <script> if (window.lo ...
- 扩展kmp 模板
算法可以参考http://wenku.baidu.com/view/8e9ebefb0242a8956bece4b3.html 百度文库 #include<iostream> #inclu ...
- 转自大神的KM想法
我第一次理解KM算法看到大神的讲解不胜感激这km挺神奇的接下来就见识一下这个大牛的吧 转自 http://blog.csdn.net/wuxinxiaohuangdou/article/details ...
- SpringMVC探究-----从HelloWorld开始
1.SpringMVC简介 Spring MVC框架是有一个MVC框架,通过实现Model-View-Controller模式来很好地将数据.业务与展现进行分离. 它的设计是围绕Dispatch ...
- 用 hashcat 破解 WIFI WPA2破解
首先用CDlinux系统进行抓包,CDlinux抓包我就不详细说明 到这里可以查看如何安装CDlinux http://jingyan.baidu.com/article/7f766daf5173a9 ...
- Linux基础命令---间歇执行命令watch
watch watch指令可以间歇性的执行程序,将输出结果以全屏的方式显示,默认是2s执行一次.watch将一直运行,直到被中断. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS ...