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 ...
随机推荐
- EasyUI扩展——自定义列排序匹配字段
一些特殊情况下希望实现:单击某些列,但是排序要按照自定义指定另外的列排序 easyui扩展: 如果不写sort属性则按照默认该列的field排序 $.fn.datagrid.defaults.onBe ...
- JavaScript setInterval(定时/延时调用函数)
setInterval是一个实现定时调用的函数,可按照指定的周期(以毫秒计)来调用函数或计算表达式.setInterval方法会不停地调用函数,直到 clearInterval被调用或窗口被关闭. 由 ...
- Unity中HideInInspector和SerializeField以及Serializable
首先,Unity会自动为Public变量做序列化,序列化的意思是说再次读取Unity时序列化的变量是有值的,不需要你再次去赋值,因为它已经被保存下来. 然后是,什么样的值会被显示在面板上? 已经被序列 ...
- Bug 5323844-IMPDP无法导入远程数据库同义词的同义词
参见MOS文档: Bug 5323844 - SYNONYM for a SYNONYM in remote database not imported using IMPDP (文档 ID 5323 ...
- spring mvc配置datasource数据源的三种方式
2.使用org.apache.commons.dbcp.BasicDataSource 说明:这是一种推荐说明的数据源配置方式,它真正使用了连接池技术 <bean id="dataSo ...
- shell基础:预定义变量
比如&& ||用的就是$?,用于计算机的识别
- Unity shader学习之Grab Pass实现玻璃效果
GrabPass可将当前屏幕的图像绘制在一张纹理中,可用来实现玻璃效果. 转载请注明出处:http://www.cnblogs.com/jietian331/p/7201324.html shader ...
- steam Depot 生成与应用脚本
Depot 生成脚本 首先为您上传的每个 Depot 创建一个 Depot 生成脚本.从复制 depot_build_1001.vdf 脚本开始,并按将上传的 Depot ID 命名. Depot 生 ...
- Spark学习之路 (十)SparkCore的调优之Shuffle调优
摘抄自https://tech.meituan.com/spark-tuning-pro.html 一.概述 大多数Spark作业的性能主要就是消耗在了shuffle环节,因为该环节包含了大量的磁盘I ...
- gene Ontology (基因本体论)
gene ontology为了查找某个研究领域的相关信息,生物学家往往要花费大量的时间,更糟糕的是,不同的生物学数据库可能会使用不同的术语,好比是一些方言一样,这让信息查找更加麻烦,尤其是使得机器查找 ...