Linux操作系统总结

第一章基本命令

1.ls -a:显示所有文件

ls -l:以长格式显示文件

-rw-r--r-- 1 root root 字节数 最近一次修改的日期 文件名

文件类型(-:普通文件;d:目录文件)+所有者权限+所有者所在组权限+其他用户权限

1:文件的连接数+所有者登录名+所有者的组的名称

2.touch 创建文件;

mkdir -m(设置权限)/-v(创建新目录返回信息)创建目录;

rmdir 删除目录;

mv 原文件名/目录名 目标文件名/目录名;mv 源文件及地址 目标地址

cp 源文件 目标文件(文件复制)

3.grep -n(文件匹配的行号)  -v(文件不包含的行号) 文件名

find  路径 -name 按文件名 -user 按文件所有者 文件名

wc -c字节数 -m字符数 -l行数 -L最大行数 文件名

4.gzip -d 解压缩 文件名.gz

bzip2 -d -k 压缩源文件并保留 文件名.bz2

tar -cxvzjf 文件名

-c压缩 -x解压 -v细节 -f 必选

-z gzip 属性 :tar -cf 文件名.tar.gz

-j bzip2属性 :tar -cf 文件名.tar.bz2

5.chmod  1 2 4读写执行权限 文件名/目录名

cd 切换目录 /根目录 .当前 ..父目录 ~home目录=/home

su 切换使用者

Date 、kill

6.vi vim gedit 编辑器

第二章shell编程

脚本编程加:#!bin/bash

编程顺序:终端->vi/vim 文件名

         终端->bash 文件名

1.各类括号
  1. 命令替换:$(命令)=’命令’;//系统变量
  2. 算数替换:$((算术表达式))
  3. test命令:test 表达式=[ 表达式 ](返回true/false)

其中,字符串比较:str1 = str2、str1 != str2;

算数比较:exp1 -eq (==) exp2、-ne(!=)、-gt(>)、-ge(>=)、-lt(<)、-le(<=)

若使用((算数表达式))则不上述需要算数比较的转义字符,直接使用==、<=等

2.命令

(1)自定义变量:$变量名

变量输入 read 变量名

(2)let命令:let 表达式

expr 命令:x=expr  $sum + $t,因为是命令所以x=’’用单引号

$((算术表达式))

(3)’’单引号:强引用 ;echo  this 直接输出

“”双引号:弱引用;echo  this is  $str会将str替换后输出

  1. bash脚本中echo输出

$0:程序名称;$@:所有参数;$#:参数个数;$1-$9:参数;$*:所有参数组成的字符串

3.基本语句
(1)if语句

A.if [  判断语句 ] / if((语句))

then

fi

  1. if 表达式                C.if 表达式

then then

  else elif

  fi   then

else

fi

(2)控制体

1).for/while/util

do

done

其中,for两种写法: for 变量 in 参数列表 /for((i=0;i<$num;i++))

2).case 变量 in

0)表达式;;

1)表达式;;

*)     ;;

esac

4.数组与函数

(1)声明 declare -a数组 -i整数 -f函数 -x全局变量

(2)数组赋值 arr=(1 2 3) /arr=([0]=1 [1]=2 [2]=3) /arr[0]=1,arr[1]=2...

${#arr[*]}/${#arr[@]}:数组元素个数;

${#arr[index]}数组元素大小

${arr[index]}数组元素

(3)删除 unset arr[index]删除元素 unset arr删除数组

(4)arr1=”${#arr[*]}” 将arr所有元素打包成一个元素赋给arr1

arr1=”${#arr[@]}”arr元素挨个赋值给arr1

(5)函数: 函数名(){} ;函数名+空括号

  • 第三章C编程

1.编译过程:预处理-编译-汇编-链接(例:hello.c文件)

预处理:gcc  hello.c  -o  hello.i  -E

编译:gcc  hello.i -o hello.s  -S

汇编:gcc  hello.s  -o  hello.o  -c

链接:gcc  hello.o  -o  hello

一步到位:gcc  hello.c  -o  hello  

2.make工程管理器

(1)作用:C编程包含许多头文件和源文件,如果对其中一个文件进行修改而其他文件没有变动却全部都需要重新编译,既耗时又增加了编译的工作量,而make工程管理器利用时间戳只将修改的文件进行编译后将其所有文件重新链接即可进行程序的执行,减轻了编译的工作量减少耗时

(2)make命令默认执行 makefile Makefile或GUNmakefile文件

若自己指定其文件,则:make -f 指定文件名

(3)makefile文件编写,其中变量表示为:${变量名}

目标文件名: 依赖文件列表 (:后必须有空格)

<tab>命令列表

描述:makefile文件有多行规则时,第一行规则的目标文件作为最终目标文件,从第一行开始,make先根据目标文件的依赖文件列表查看下方规则是否有以该依赖文件为目标的规则,如果有则不断递归执行规则,如果没有则比较时间戳,若依赖文件时间戳比目标文件时间戳新那么按照这个规则重新构建目标文件。

  • 第四章系统函数使用

1.数学函数

加头文件#include<math.h>  pow(x,y)=x**y

#include<stdlib.h> srand((int)time(0))生成种子 rand()%x [0,x-1]

2.时间函数 gettimeofday

头文件:#include<sys/time.h> #include<unistd.h>

其余时间函数是C库函数里的,所以头文件#include<time.h>

3.time、ctime、localtime、asctime、gettimeofday函数使用
(1)time_t timer;                     

    time_t timer;                                  time_t timer;

   time(&timer);//获取时间戳               time(&timer);

   struct tm* date=localtime(&timer);         char* str=ctime(&timer);

char * str=asctime(date);                  printf(“%s\n”,str);

printf(“%s\n”,str);

(2)struct timeval tv1,tv2;

struct timezone tz;

gettimeofday(&tv1,&tz/NULL);

程序段.......

gettimeofday(&tv2,&tz/NULL);

int usec=(tv2.tv_sec-tv1.tv_sec)*pow(10,6)+(tv2.tv_usec-tv1.tv_usec);

printf(“程序花费%d毫秒\n”,usec);

  • 第五章文件操作

  1. 不带缓存的文件操作(socket、管道等都是)

#include<sys/stat.h>

#include<sys/types.h>

#include<unistd.h>

#include<fnctl.h>

(1)创建 int fd=creat(const char*文件路径,mode_t mode)

(2)打开 int fd=open(const char*文件路径,int flags)

      open(const char*文件路径,int flags,mode_t mode)

        1)fd是文件描述符,创建或打开失败fd=-1;

        2)mode是创建文件的权限 0777(r=4 w=2 x=1)

        3)open中flags是访问权限:

O_RDONLY只读、O_WRONLY、O_RDWR读写

O_CREAT创建文件,用mode设置权限;

O_EXCL与O_CREAT一起用,如果文件存在打开失败

(3)读 int status=read(fd,cost void*buf,sizeof(buf))

(4)写 int status=write(fd,写入字符首地址,sizeof())

(5)关闭 int status=close(fd)

失败,status=-1

2.带缓存的文件操作

#include<stdio.h>

(1)打开 FILE *fp=fopen(const char*文件路径,mode)  失败fp=NULL

mode:

 r 只读 r+ 读写 文件必须存在

w 只写 w+读写 不存在会创建,存在先清空再写

a 只写 a+读写  不存在会创建,存在追加写

(2)关闭 fclose(fp)  其中:void*相当于任意类型

读写操作

        1)读int status=fread(void* buf,sizeof(char),1024,fp);存入buf

        2)写int status=fwrite(const void* buf,sizeof(char),1024,fp);buf写入

失败了 status=EOF(-1),成功status=成功写入/读取的字符串数目

        3)字符 int status=fgetc(fp);

        int status=fputc(char* c,fp);

        4)字符串 int status=fgets(char* buf,1024,fp);存入buf

        int  status=fputs(const char* buf,fp);buf存入

        失败 status=EOF(-1)

  • 第六章进程创建与守护进程

1.进程创建

(1)创建 pid_t pid=fork();

pid==0 子进程 pid>0父进程 pid<0失败

(2)子进程号 pid_t pid=getpid();

父进程号 pid_t ppid=getppid();

(3)睡眠 sleep(秒);

(4)等待 子进程状态存入status,成功pid=子进程号,失败返回-1

pid_t pid=wait(int *status);

pid_t pid=waitpid(pid,*status,0);等待特定进程

总过程

#include<stdio.h>

#include<stdlib.h>

#include<sys/types.h>

#include<unistd.h>

#include<wait.h>

int main()

{

pid_t pid=fork();

If(pid<0)

{ perror(“创建进程失败\n”);exit(1);}

else if(pid==0)

{ printf(“子进程pid=%d\n”,getpid()); }

else

{ pid_t wpid=waitpid(pid,NULL,0);

printf(“父进等待子进程:%d\n”,wpid);

}sleep(1);}

2.exec函数族

l:list,以列表参数

v:vector矢量,参数地址,以指针形式

e:环境变量,指定环境变量,在该环境变量下执行

p:路径自动搜索

  1. execl(路径,”参数1”,”参数2”,”参数3”,...,NULL)
  2. execv(路径,参数地址);
  3. execle(路径,”参数1”,”参数2”,”参数3”,...,NULL,环境参数地址)
  4. execve(路径,参数地址,环境参数地址)
  5. execlp(文件名,”参数1”,”参数2”,”参数3”,...,NULL)
  6. execvp(文件名,参数地址,环境参数地址)

调用失败返回-1,成功只执行不返回

3.守护进程创建步骤
  1. 创建子进程,关闭父进程
  2. 利用setsid()函数,使进程独立于当前会话、进程组、终端
  3. 变更目录,chdir(“/”) / chdir(“/tmp”),使其脱离当前父进程的目录系统
  4. 变更文件掩码umask(0),脱离父进程文件系统
  5. 关闭文件描述符,灵活操作父进程已打开文件

for(int i=0;i<NOFILE/MAXFILE;i++)

close(i);

守护进程创建

#include<stdio.h>

#include<stdlib.h>

#include<sys/stat.h>

#include<unistd.h>

#include<sys/types.h>

#include<signal.h>

#include<sya/param.h>

int main()

{

pid_t dm,child;

dm=fork();

if(fork<0)

{ perror(“创建失败\n”);exit(1); }

else if(fork()>0)

exit(0);//关闭父进程

setsid();

chdir(“/”);

umask(0);

for(int i=0;i<NOFILE;i++)

close(i);

child=fork();

If(child==0)

{ printf(“守护进程创建的进程号为:%d\n”,child);}

return 0;}

第七章 进程通信

1.信号函数

#include<signal.h>

#include<sys/types.h>

(1)signal(系统信号量编号,函数/SIG_IGN忽略/SIG_DFL恢复)

(2)kill(进程号,信号) 给特定进程发送信号

(3)raise(信号) 给本进程发送信号

SIG_INT=按下<Ctrl+C>进程终止信号;SIG_QUIT=<Ctrl+\>

管道皆为不带缓存的文件操作,所以用open、read、write来写

2.匿名管道通信

只适用于相关进程,即父子进程间通信

(1)创建管道

int pipe_fd[2];//文件描述符

pipe(pipe_fd[2])

(2)创建子进程

pid_t pid=fork();

(3)

pipe[1] 写端 pipe[0]读端

父进程写先关闭读端,子进程读先关闭写端

close(pipe_fd[0]);        close(pipe_fd[1])

(4)写/读完关闭写/读端

Close(pipe_fd[1])         close(pipe_fd[0])

总写:

#include<>stdio.h>

#include<stdlib.h>

#include<sys/types.h>

#include<unistd.h>

#include<string.h>

#include<wait.h>

int main()
{   int pipe_fd[2];

pid_t pid=fork();

Int n;

Char* str1,str2;

If(pipe(pipe_fd)<0)

{  perror(“管道创建失败\n”);exit(0);}

If(pid<0)

{  perror(“创建失败\n”);exit(1);}

Elseif(pid==0)//子进程

{   close(pipe_fd[1]);

     If( (n = read(pipe_fd[0],str1,1024) ) > 0)

       Printf(“子进程读:%s\n”,str1);

     Close(pipe_fd[0]);

}else{

     Close(pipe_fd[0])

      Scanf(“%s”,str2);

      If( (n= write(pipe_fd[1],str2,strlen(str2)) )!=-1 )

         Printf(“父进程写:%s\n”,str2);

          Close(pipe_fd[1]);

}

}

3.命名管道

#include<sys/types.h>

#include<sys/stat.h>

#include<sys/select.h>

#include<unistd.h>

(1)创建管道mkfifo(管道名,权限)

Mkfifo(“fifo1”,0644);

Mkfifo(“fifo2”,0644);

(2)打开管道open(文件描述符,访问权限)

Int wr=open(“fifo1”,O_WRONLY);//只写方式打开

Int re=open(“fifo2”,O_RDONLY);//只读方式打开

(3)用select进行非阻塞通信(可不用,但会一直等待)

Select(文件描述符范围,可读文件描述符,可写文件描述符,NULL,等待时间)

Struct timval timer={5,0};//5s

Struct fd_set  fd_re,fd_wr;//文件描述符集合

FD_ZEROS(&fd_re);//清空描述符集

FD_ZEROS(&fd_wr);

FD_SET(re,fd_re);//将文件描述符re加入fd_re描述符集合

FD_SET(wr,fd_wr);

Int i=select(re+1,&fd_re,NULL,NULL,timer)

Int j=select(wr+1,NULL,&fd_wr,NULL,timer)

 i/j=0:超时/无操作,i/j>0:有操作;i/j<0:错误

(4)读写操作,read/write

(5)关闭管道close(文件描述符)

Close(re);

Close(wr);

第八章线程

加头文件#include<pthread.h>

终端编译时:gcc 文件名.c  -lpthred.h  -o  文件名

1.线程

(1)创建 pthread_t  pt;

pthread_create(&pt,NULL,线程执行函数,函数参数);

函数类型 void*  函数名(void * arg)

(2)线程号等待终止

pthread_join(pt,NULL);//等待

pthread_exit(NULL);//终止

pthread_t pt=pthread_self();//pt为进程号

2.同步互斥
(1)互斥锁

初始化:pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;

        pthread_init(&mutex,NULL);

上锁解锁:pthread_mutex_lock(&mutex);

Pthread_mutex_unlock(&mutex);

销毁:pthread_mutex_destroy(&mutex);

(2)条件变量

作用:线程间交流通信,与互斥锁搭配使用

初始化:pthread_cond_t  cond=PTHREAD_COND_INITIALIZER;

        Pthread_cond_init(&cond,NULL);

发送信号:pthread_cond_signal(&cond);

等待信号:pthread_cond_wait(&cond,&mutex);

pthread_mutex_t mutex;

线程中执行顺序:p1: pthread_mutex_lock(&mutex);

  ....

   Pthread_mutex_unlock(&mutex);

Pthread_cond_signal(&cond);

P2: pthread_cond_wait(&cond,&mutex);

,...

Pthread_mutex_unlock(&mutex);

(3)信号量

用于控制共享资源访问

加头文件#include<semaphore.h>

初始化:sem_t  sem;

         sem_init(&sem,0,资源可访问的线程数量);//0表示只在该进程共享

V操作(释放资源):sem_post(&sem);

P操作(资源数-1):sem_wait(&sem);

销毁:sem_destroy(&sem);

  • 36
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值