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 编程简单示例代码的更多相关文章

  1. JDBC简单示例代码

    本文章教程中将演示如何创建一个简单的JDBC应用程序的示例. 这将显示如何打开数据库连接,执行SQL查询并显示结果. 这个示例代码中涉及所有步骤,一些步骤将在本教程的后续章节中进行说明. 创建JDBC ...

  2. Linux内核模块简单示例

    1. Linux 内核的整体结构非常庞大,其包含的组件也非常多,使用这些组件的方法有两种: ① 直接编译进内核文件,即zImage或者bzImage(问题:占用内存过多) ② 动态添加 * 模块本身并 ...

  3. C#使用互斥量(Mutex)实现多进程并发操作时多进程间线程同步操作(进程同步)的简单示例代码及使用方法

    本文主要是实现操作系统级别的多进程间线程同步(进程同步)的示例代码及测试结果.代码经过测试,可供参考,也可直接使用. 承接上一篇博客的业务场景[C#使用读写锁三行代码简单解决多线程并发写入文件时线程同 ...

  4. 基于OpenMP的C++并行编程简单示例

    示例要求:在整数A和B之间找到符合条件的值X,使f(X)=C. 示例代码(需要在VS中开启OpenMP支持): #include<iostream> #include<time.h& ...

  5. Linux网络编程简单示例

    linux 网络编程是通过socket(套接字)接口实现,Socket是一种文件描述符,socket起源于UNIX,在Unix一切皆文件哲学的思想下,socket是一种"打开—读/写—关闭& ...

  6. _CrtDumpMemoryLeaks报告程序中的内存泄露问题(简单示例代码)

    // .h 文件 #pragma once class CConsoleDump { public: explicit CConsoleDump(LPCTSTR lpszWindowTitle = N ...

  7. 【java】网络socket编程简单示例

    package 网络编程; import java.io.IOException; import java.io.PrintStream; import java.net.ServerSocket; ...

  8. Spring security oauth2 client_credentials认证 最简单示例代码

    基于spring-boot-2.0.0 1,在pom.xml中添加: <!-- security --> <!-- https://mvnrepository.com/artifac ...

  9. C#判断数据类型的简单示例代码

    ; Console.WriteLine( "i is an int? {0}",i.GetType()==typeof(int)); Console.WriteLine( &quo ...

随机推荐

  1. php函数addslashes()使用方法详解

    实例 在每个双引号(")前添加反斜杠: <?php $str = addslashes('Shanghai is the "biggest" city in Chi ...

  2. WebSocket.之.基础入门-断开连接处理

    ebSocket.之.基础入门-断开连接处理 在<WebSocket.之.基础入门-后端响应消息>的代码基础之上,继续更新代码.代码只改动了:TestSocket.java 和 index ...

  3. 点击地面时,若鼠标点击的偶数次使得Cube向点击点移动,并且点击奇数次Cube变色

    using System.Collections; using System.Collections.Generic; using UnityEngine; public class ray10 : ...

  4. 软工网络15团队作业4——Alpha阶段敏捷冲刺1.0

    软工网络15团队作业4--Alpha阶段敏捷冲刺1.0 1. 各个成员在 Alpha 阶段认领的任务,以及整个项目预期的任务量(使用整数表示,与项目预估的总工作小时数一致.比如项目A预估需120小时才 ...

  5. memory consistency

    目前的计算机系统中,都是shared memory结构,提供统一的控制接口给软件, shared memory结构中,为了memory correctness,可以将问题分为:memory consi ...

  6. QString和char*互转

    1. QString转为char * // QString转QByteArray QByteArray sr = strQ.toLocal8Bit(); int len = sr.length(); ...

  7. MyEclipse 黑色主题 jsp 页面 js背景色修改

    Windows-->Preferences-->Myeclipse-->Files and Editors-->JavaScript-->Editor-->Synt ...

  8. Oracle10g 连接 sqlserver hsodbc dblink 方式 非透明网关

    Oracle10g 连接 sqlserver hsodbc dblink 方式 非透明网关 那个上传图片太麻烦了,发布到百度文库了 http://wenku.baidu.com/view/b38ae8 ...

  9. Vue系列之 => 组件切换

    组件切换方式一 <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  10. html5-css选择器

    /*/**{color: red}p{color: green}#div1{background: blue;padding-top: 15px;}.kk{background: blue;borde ...