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 ...
随机推荐
- JAVA生成六位随机数
public static String getSixNum() { String str = "0123456789"; StringBuilder sb = new Strin ...
- IdentityServer4:Endpoint
Endpoint的概念在IdentityServer里其实就是一些资源操作的url地址:如同Restful API里面的Endpoint是概念: 那么可以通过你自己的授权服务端得到相对应的地址与信息: ...
- redhat6.5 linux 安装mysql5.6.27
1.yum安装mysql(root身份),适用于红帽6.5 yum install mysql-server mysql-devel mysql -y 如没有配置yum,请参见博客:http://ww ...
- ida信息获取函数
idc. http://www.cnblogs.com/fply/p/8503929.html 获取ida可执行文件路径 GetIdaDirectory() print GetIdaDirect ...
- echarts实现全国地图
1.首先我没有按需引入echarts,我是全局引入的,所以说在node_modules中有 这个china,你只需要在你的页面引入即可 但是按需引入echarts 的 项目中node_modules中 ...
- Flex中如何利用FocusManager类的setFocus函数设置TextInput的焦点的例子
参考:https://blog.csdn.net/liruizhuang/article/details/5876455 <?xml version="1.0" encodi ...
- [提权] 脏牛漏洞 Dirty COW CVE-2016-5195 2.6.22 < 3.9 (x86/x64)
/* * (un)comment correct payload first (x86 or x64)! * * $ gcc cowroot.c -o cowroot -pthread * $ ./c ...
- css抖动动画
button{ animation: beat 6s ease 0s infinite normal; } @keyframes beat { 0% { transform: translateY(0 ...
- jq文件上传及下载
一.使用jquery.form.js上传文件 jquery.form.js获取地址:https://pan.baidu.com/s/1nSdfkCt25Rc5cHMFJRVcUQ 提取码: sbmt ...
- SQL Server物化视图学习笔记
一. 基本知识 摘抄自http://www.cnblogs.com/kissdodog/p/3385161.html SQL Server索引 - 索引(物化)视图 <第九篇> 索引视 ...