電子產(chǎn)業(yè)一站式賦能平臺

PCB聯(lián)盟網(wǎng)

搜索
查看: 17|回復(fù): 0
收起左側(cè)

Linux應(yīng)用編程基本概念

[復(fù)制鏈接]

180

主題

180

帖子

920

積分

二級會員

Rank: 2

積分
920
跳轉(zhuǎn)到指定樓層
樓主
發(fā)表于 2024-9-7 16:15:00 | 只看該作者 |只看大圖 回帖獎勵 |倒序?yàn)g覽 |閱讀模式




Linux應(yīng)用編程涉及到在Linux環(huán)境下開發(fā)和運(yùn)行應(yīng)用程序的一系列概念。以下是一些涵蓋Linux應(yīng)用編程的基本概念:
1. 系統(tǒng)調(diào)用系統(tǒng)調(diào)用是用戶空間程序與內(nèi)核之間進(jìn)行通信的方式。它提供了一組接口,允許應(yīng)用程序請求內(nèi)核執(zhí)行特權(quán)操作。在Linux中,系統(tǒng)調(diào)用的例子包括fork(創(chuàng)建新進(jìn)程)、read(讀取文件)、write(寫入文件)等。開發(fā)者通常通過系統(tǒng)調(diào)用接口來訪問操作系統(tǒng)提供的功能。
#include
int main() {
    char buffer[256];
    read(STDIN_FILENO, buffer, sizeof(buffer));
    write(STDOUT_FILENO, buffer, sizeof(buffer));
    return 0;
}
2. 進(jìn)程在Linux中,進(jìn)程是正在運(yùn)行的程序的實(shí)例。每個進(jìn)程都有獨(dú)立的內(nèi)存空間、文件描述符和執(zhí)行上下文。fork系統(tǒng)調(diào)用用于創(chuàng)建新進(jìn)程。exec系列系統(tǒng)調(diào)用用于在進(jìn)程中執(zhí)行新程序。
#include
#include
int main() {
    pid_t child_pid = fork();
    if (child_pid == 0) {
        // 子進(jìn)程執(zhí)行的代碼
        execl("/bin/ls", "ls", NULL);
    } else {
        // 等待子進(jìn)程結(jié)束
        waitpid(child_pid, NULL, 0);
    }
    return 0;
}
3. 文件描述符文件描述符是一個整數(shù),用于標(biāo)識一個打開的文件、套接字或其他I/O資源。標(biāo)準(zhǔn)輸入、標(biāo)準(zhǔn)輸出和標(biāo)準(zhǔn)錯誤的文件描述符分別是0、1和2。文件描述符的操作包括讀、寫、關(guān)閉等。
#include
#include
int main() {
    int fd = open("example.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
    write(fd, "Hello, Linux!", 13);
    close(fd);
    return 0;
}
4. 線程Linux支持多線程編程。線程是一個輕量級的執(zhí)行單元,可以與同一進(jìn)程的其他線程共享內(nèi)存空間。線程可以通過pthread庫創(chuàng)建和管理。
#include
#include
void* threadFunction(void* arg) {
    std::cout "Hello from thread!" std::endl;
    return NULL;
}
int main() {
    pthread_t thread;
    pthread_create(&thread, NULL, threadFunction, NULL);
    pthread_join(thread, NULL);
    return 0;
}
5. 進(jìn)程間通信(IPC)進(jìn)程間通信是指不同進(jìn)程之間進(jìn)行數(shù)據(jù)交換的機(jī)制。Linux提供多種IPC機(jī)制,包括管道、消息隊(duì)列、共享內(nèi)存和信號等。這些機(jī)制允許進(jìn)程之間進(jìn)行數(shù)據(jù)共享和通信。
6. 信號信號是一種在軟件層次上處理異步事件的機(jī)制。它允許進(jìn)程在運(yùn)行時接收通知,例如用戶按下Ctrl+C終止進(jìn)程。signal函數(shù)和kill命令用于處理和發(fā)送信號。
#include
#include
void signalHandler(int signum) {
    std::cout "Received signal: " std::endl;
}
int main() {
    signal(SIGINT, signalHandler);  // 注冊信號處理函數(shù)
    while (1) {
        // 程序執(zhí)行主循環(huán)
    }
    return 0;
}
7. 動態(tài)鏈接庫Linux支持動態(tài)鏈接庫(共享庫)的概念,允許程序在運(yùn)行時動態(tài)加載和卸載共享庫。這有助于減小可執(zhí)行文件的大小,共享代碼,提高代碼的可重用性。
#include
#include
int main() {
    void* handle = dlopen("libexample.so", RTLD_NOW);
    if (handle) {
        typedef void (*ExampleFunction)();
        ExampleFunction function = (ExampleFunction)dlsym(handle, "exampleFunction");
        if (function) {
            function();
        }
        dlclose(handle);
    }
    return 0;
}
8. 文件系統(tǒng)操作Linux應(yīng)用編程涉及對文件系統(tǒng)的各種操作,例如創(chuàng)建、讀取、寫入、刪除文件,以及目錄操作。系統(tǒng)調(diào)用和標(biāo)準(zhǔn)C庫提供了相關(guān)的函數(shù),例如open、read、write、unlink等。
這些概念構(gòu)成了Linux應(yīng)用程序開發(fā)的基礎(chǔ),開發(fā)者可以通過這些機(jī)制實(shí)現(xiàn)復(fù)雜的應(yīng)用程序和系統(tǒng)工具。掌握這些概念對于在Linux環(huán)境下進(jìn)行應(yīng)用編程至關(guān)重要。
9. Socket 編程Socket 編程是 Linux 應(yīng)用程序中常用的一種網(wǎng)絡(luò)編程方式。通過使用套接字(Socket),可以實(shí)現(xiàn)進(jìn)程間的通信和網(wǎng)絡(luò)通信。常見的 Socket 編程包括創(chuàng)建套接字、綁定地址、監(jiān)聽連接、接受連接、發(fā)送和接收數(shù)據(jù)等操作。
#include
#include
#include
#include
int main() {
    // 創(chuàng)建套接字
    int serverSocket = socket(AF_INET, SOCK_STREAM, 0);
    // 綁定地址
    sockaddr_in serverAddress;
    serverAddress.sin_family = AF_INET;
    serverAddress.sin_port = htons(8080);
    serverAddress.sin_addr.s_addr = INADDR_ANY;
    bind(serverSocket, (struct sockaddr*)&serverAddress, sizeof(serverAddress));
    // 監(jiān)聽連接
    listen(serverSocket, 5);
    // 接受連接
    int clientSocket = accept(serverSocket, NULL, NULL);
    // 發(fā)送和接收數(shù)據(jù)
    char buffer[256];
    read(clientSocket, buffer, sizeof(buffer));
    std::cout "Received: " std::endl;
    write(clientSocket, "Hello from server!", 18);
    // 關(guān)閉套接字
    close(clientSocket);
    close(serverSocket);
    return 0;
}
10. 多路復(fù)用(select 和 epoll)多路復(fù)用是一種提高 I/O 操作效率的機(jī)制,它允許一個進(jìn)程同時監(jiān)視多個文件描述符。在 Linux 中,select 和 epoll 是常用的多路復(fù)用機(jī)制。它們可以用于處理多個套接字的并發(fā)事件,提高網(wǎng)絡(luò)應(yīng)用程序的性能。
// 使用 select 示例
#include
#include
int main() {
    fd_set readfds;
    FD_ZERO(&readfds);
    FD_SET(STDIN_FILENO, &readfds);
    struct timeval timeout;
    timeout.tv_sec = 5;
    timeout.tv_usec = 0;
    int result = select(STDIN_FILENO + 1, &readfds, NULL, NULL, &timeout);
    if (result > 0 && FD_ISSET(STDIN_FILENO, &readfds)) {
        std::cout "Data is available to read from stdin." std::endl;
    } else if (result == 0) {
        std::cout "Timeout occurred." std::endl;
    } else {
        std::cerr "Error in select." std::endl;
    }
    return 0;
}
11. 內(nèi)存映射(mmap)內(nèi)存映射是將文件的一部分直接映射到進(jìn)程的地址空間,使得文件可以像內(nèi)存一樣被訪問。mmap 是 Linux 提供的用于內(nèi)存映射的系統(tǒng)調(diào)用。
#include
#include
#include
#include
int main() {
    int fileDescriptor = open("example.txt", O_RDWR);
    off_t fileSize = lseek(fileDescriptor, 0, SEEK_END);
    void* mappedMemory = mmap(NULL, fileSize, PROT_READ | PROT_WRITE, MAP_SHARED, fileDescriptor, 0);
    close(fileDescriptor);
    // 對映射的內(nèi)存進(jìn)行讀寫操作
    char* data = static_castchar*>(mappedMemory);
    data[0] = 'H';
    data[1] = 'i';
    // 解除內(nèi)存映射
    munmap(mappedMemory, fileSize);
    return 0;
}
12. 定時器Linux 提供了多種定時器機(jī)制,允許應(yīng)用程序執(zhí)行定時任務(wù)。setitimer 是其中之一,它允許設(shè)置定時器來在指定的時間間隔內(nèi)定期觸發(fā)信號。
#include
#include
#include
void timerHandler(int signum) {
    std::cout "Timer expired! Signal number: " std::endl;
}
int main() {
    struct itimerval timer;
    timer.it_value.tv_sec = 2;
    timer.it_value.tv_usec = 0;
    timer.it_interval.tv_sec = 1;
    timer.it_interval.tv_usec = 0;
    signal(SIGALRM, timerHandler);
    setitimer(ITIMER_REAL, &timer, NULL);
    while (1) {
        // 主循環(huán)
    }
    return 0;
}
這些概念覆蓋了 Linux 應(yīng)用編程的多個方面,包括文件 I/O、網(wǎng)絡(luò)編程、進(jìn)程控制、多路復(fù)用、內(nèi)存映射、定時器等。深入了解這些概念將幫助開發(fā)者編寫高效且功能強(qiáng)大的 Linux 應(yīng)用程序。
end

一口Linux

關(guān)注,回復(fù)【1024】海量Linux資料贈送
精彩文章合集
文章推薦
?【專輯】ARM?【專輯】粉絲問答?【專輯】所有原創(chuàng)?【專輯】linux入門?【專輯】計算機(jī)網(wǎng)絡(luò)?【專輯】Linux驅(qū)動?【干貨】嵌入式驅(qū)動工程師學(xué)習(xí)路線?【干貨】Linux嵌入式所有知識點(diǎn)-思維導(dǎo)圖
回復(fù)

使用道具 舉報

發(fā)表回復(fù)

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則


聯(lián)系客服 關(guān)注微信 下載APP 返回頂部 返回列表