Linux系統編程


1. 什么是Linux系統編程

Linux系統編程也叫Linux下的高級編程;

學習Linux系統編程C語言是基礎,能夠在Linux系統下通過指令完成文件的創建、復制、剪切、刪除;文件夾的創建和刪除;軟件的下載和安裝就可以。

Linux系統編程主要是學習Linux系統調用的接口,學習這些接口的功能、參數和返回值,是比較簡單的。可以通過框架學習法來學習。(迅為的框架學習法)

 

2. Linux系統編程的基本框架

#include <stdio.h>
#include <stdlib.h>

int main(int argc,char *argv[])
{
    //argc:表示的是命令行中參數的個數。最小為1
    //argv:表示的是命令行中的參數。

    printf("argc = %d \n",argc);

    for(int i=0;i<argc;i++)
    {
        printf("argv[%d] = %s \n",i,argv[i]);
    }

    return 0;
}

 

 

3. 標准IO和文件IO

 

文件IO就是直接調用系統內核提供的系統調用函數,如open;

標准IO就是間接調用系統調用函數,是C庫函數,如fopen。

 

文件IO和標准IO的區別?

文件IO是直接調用系統內核提供的系統調用函數,頭文件是unistd.h;標准IO是間接調用系統函數,頭文件是stdio.h

文件IO是依賴於操作系統的;標准IO是不依賴於操作系統的。所以在任何操作系統下,使用標准IO,也就是C庫函數操作文件的方法都是相同的。

 

 

4. 文件IO之open函數

 

 

 

文件描述符

  對於文件IO來說,一切都是圍繞文件描述符進行的。在Linux系統中,所有打開的文件都有一個對應的文件描述符。

  文件描述符的本質是一個非負整數,當我們打開一個文件時,系統會給我們分配一個文件描述符。

  當我們對一個文件做讀寫操作的時候,我們使用open函數返回的這個文件描述符會標識該文件,並將其作為參數傳遞給read或write函數。 

  在posix.1應用程序里面,文件描述符0,1,2分別標識標准輸入,標准輸出和標准出錯。

 

open函數

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);

功能:打開文件,返回文件描述符。

open函數有兩個原型,參數不同。

第一個參數:表示要打開文件的路徑。

第二個參數:O_RDONLY,O_WRONLY,O_RDWR。O_CREAT等等。可以通過 | 組合使用

第三個參數:當第二個參數中有O_CREAT時,需要填寫第三個參數,表示訪問權限。文件實際的訪問權限:  mode & ~(umask)

返回值:正常返回打開文件的描述符;-1:打開錯誤

 

 

文件的訪問權限

文件的訪問權限可以由數字表示:
可執行 -- 1 可寫 -- 2 可讀 -- 4

如果我們表示文件可讀可寫,就是上述值的和,可讀可寫 -- 6。

 

 

open.c

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc,char *argv[])
{

    int fd;

    fd = open("a.c",O_CREAT|O_RDWR,0666);

    if(fd < 0){
        printf("open is error!\n");
    }

    printf("fd = %d \n",fd);

    close(fd);

    return 0;
}

 

umask 是 022 ,open第三個參數是 666 ,mode & ~(umask) = 644

 

 

 

5.文件IO之close函數

 

close函數

#include <unistd.h>

int close(int fd);

作用:關閉一個文件描述符

參數:文件描述符

返回值:0-成功   -1-失敗。

 

為什么要關閉文件描述符?

系統中的文件描述符是有上限的,一般為1024。關閉不用的文件描述符以讓內核可以重用。

 

6.文件IO之read函數

 

read函數

#include <unistd.h>

ssize_t read(int fd, void *buf, size_t count);

作用:讀出文件描述符fd所指向的文件中,count字節的數據到buf指向的緩沖區。

參數:  fd:文件描述符

     buf:讀出數據的緩沖區

     count:要讀出數據的字節長度

返回值:n - 實際讀取到數據的字節長度

    0 - 讀到文件末尾

    -1 - 讀取出錯 

 

 

read.c

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>


int main(int argc,char *argv[])
{
    int fd;
    char buf[32];
    ssize_t ret;

    fd = open("a.c",O_RDWR);
    if(fd < 0)
    {
        printf("open is error\n");
        return -1;
    }

    printf("fd is %d\n",fd);

    ret = read(fd,buf,32);
    if(ret < 0)
    {
        printf("read is error\n");
        return -2;
    }

    printf("buf is %s\n",buf);
    printf("ret is %d\n",ret);
    
    close(fd);

    return 0;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM