電子書筆記(二)

2022-07-27 18:12:14 字數 2592 閱讀 9472

1. 非阻塞方式獲得輸入

scanf函式是以阻塞方式獲得輸入,遇到回車鍵時輸入才結束,scanf函式才執行完成。支援多方法多輸入的電子書希望以非阻塞形式獲得輸入。網上搜尋stdin nonblock就能得到函式:這種方式不用按回車鍵就可以輸入完成。

1

void nonblock(int

state) 2

15else

if (state==nb_disable)

1620

//set the terminal attributes.

21 tcsetattr(stdin_fileno, tcsanow, &ttystate);

2223 }

初始化就執行if語句:

struct

termios ttystate;

//get the terminal state

tcgetattr(stdin_fileno, &ttystate);

//turn off canonical mode

ttystate.c_lflag &= ~icanon;

ttystate.c_cc[vmin] = 1;//

有乙個資料時就立刻返回

//set the terminal attributes.

tcsetattr(stdin_fileno, tcsanow, &ttystate);

退出非阻塞標準輸入,執行if else 語句:

struct

termios ttystate;

tcgetattr(stdin_fileno, &ttystate);

ttystate.c_lflag |=icanon;

tcsetattr(stdin_fileno, tcsanow, &ttystate);

初始化之後判斷有沒有輸入(這是使用select監聽):

struct

timeval tv;

fd_set fds;

tv.tv_sec = 0

;tv.tv_usec = 0

;fd_zero(&fds);

fd_set(stdin_fileno, &fds); //

stdin_fileno is 0

select(stdin_fileno+1, &fds, null, null, &tv);

return fd_isset(stdin_fileno, &fds);

return 為1則有輸入,進行操作,比如c = fgetc(stdin);

2. select函式

函式原型:int select(int nfds, fd_set *readfds, fd_set *writefds,

fd_set *exceptfds, struct timeval *timeout);

引數1:最大檔案控制代碼+1

引數2,3,4,5:檔案可讀/可寫/異常或者超時,函式返回

在linux終端輸入man select有詳細用法,裡面有example,可以參考

3. 檢視cpu佔用率

輸入telnetd -l /bin/sh

新建乙個sessions,host為開發板ip,相當於又開啟了乙個開發板的終端使用。

在新的終端輸入top就能檢視cpu佔用率了。

4. 多執行緒的使用

4.1 建立執行緒

pthread_create函式

4.2 取消執行緒

使用pthread_exit或者pthread_cancel,pthread_exit無法知道執行緒終止是否成功,而pthread_cancel取消執行緒成功返回0,失敗返回負值。

4.3 執行緒的使用

建立多個執行緒之後,要定義互斥鎖以及訊號量:

static pthread_mutex_t mutex  =pthread_mutex_initializer;

static pthread_cond_t condvar = pthread_cond_initializer;

每個執行緒要獲得cpu的控制權就要獲取互斥鎖:

pthread_mutex_lock(&mutex);  //

獲取互斥鎖

然後對臨界資料進行操作,操作完之後發條件變數喚醒主線程

pthread_cond_signal(&condvar);

獲取互斥鎖用完之後記得釋放

pthread_mutex_unlock(&mutex);

主線程也要上鎖,等待子執行緒的條件變數喚醒它,最後也要釋放鎖

pthread_mutex_lock(&mutex);  //

獲取互斥鎖

pthread_cond_wait(&condvar,&mutex);

這裡就是主線程根據子執行緒反饋之後進行的操作。

pthread_mutex_unlock(&mutex);

5. make 出錯

雖然包含了標頭檔案,但會出現pthread_create未定義的錯誤。需要在makefile中新增pthread庫,-lpthread。

電子書收藏

以下是我蒐集的電子書備份。1.網路硬體 完整版 日 三輪賢一 著,盛榮 譯 人民郵電出版社 2015年8月第1版 2.python學習手冊 第4版 mark lutz 著 李軍 劉紅偉 等譯 機械工業出版社 2011年4月第1版 3.android軟體安全與逆向分析 豐生強著 人民郵電出版社 201...

豆瓣電子書

import requests import urllib.request from bs4 import beautifulsoup import csv 目標資料 書名作者 日期 評分評價人數 headers key key ascii urllib.request.quote key book...

開源電子書

語言相關類 讀書筆記及其它 測試相關 智慧型系統git 簡易指南 猴子都能懂的git入門 git 參考手冊 pro git pro git 中文版 整理在gitbook上 git magic gotgithub git權威指南 git community book 中文版 mercurial 使用教...