linux 線程學習初步01

  1. 線程的概念
    在這里插入圖片描述
    進程與線程內核實現 通過函數clone實現的
ps -Lf pid
  1. Linux內核線程實現原理
    在這里插入圖片描述
    同一個進程下的線程,共享該進程的內存區, 但是只有stack區域不共享。

  2. 線程共享資源
    a.文件描述符表
    b.每種信號的處理方式
    c.當前工作目錄
    d.用戶id和組id

  3. 線程非共享資源
    a.線程id
    b.處理器現場和棧指針(內核棧)
    c.獨立的棧空間(用戶空間棧)
    d.errno變量
    e.信號屏蔽字
    f.調度優先級

  4. 在主線程里面執行return, 相當于整個進程退出了

  5. 小技巧
    set -o vi 相當于把當前shell,弄成了 vi 編輯器模式

7.創建一個線程
man pthread_create

 #include <pthread.h>int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);Compile and link with -pthread.

在這里插入圖片描述

#include <pthread.h>
#include <unistd.h>
#include <stdio.h>void* func(void *arg) {printf("I am a common thread, pid is %d, tid is %ld\n", getpid(), pthread_self());pthread_exit(NULL);
}int main() {pthread_t tid;pthread_create(&tid, NULL, func, NULL);printf("I am a man thread, pid is %d,create tid is %ld\n", getpid(), tid);printf("I am a man thread, pid is %d, tid is %ld\n", getpid(), pthread_self());pthread_exit(NULL);return 0;
}經測試,主線程使用pthread_exit函數,可以等待子線程的退出。

線程退出函數:
在這里插入圖片描述
8.線程回收函數:

int pthread_join(pthread_t thread, void **retval);
參數介紹:thread: 表示要回收的線程(創建線程時候傳出的第一個參數)retval:要回收的線程的退出信息

線程回收也是阻塞等待回收
代碼案例:

#include <pthread.h>
#include <unistd.h>
#include <stdio.h>void* func(void *arg) {printf("I am a common thread, pid is %d, tid is %ld\n", getpid(), pthread_self());// pthread_exit((void *)100);return (void*)(100);
}int main() {pthread_t tid;pthread_create(&tid, NULL, func, NULL);printf("I am a man thread, pid is %d,create tid is %ld\n", getpid(), tid);printf("I am a man thread, pid is %d, tid is %ld\n", getpid(), pthread_self());void * ret;pthread_join((tid), &ret);printf("join tid return value is %d\n", (int)ret);return 0;
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/382501.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/382501.shtml
英文地址,請注明出處:http://en.pswp.cn/news/382501.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

python3字符串處理,高效切片

高級技巧&#xff1a;切片&#xff0c;迭代&#xff0c;列表&#xff0c;生成器 切片 L [Hello, World, !]print("-------1.一個一個取-------") print(L[0]) print(L[1]) print(L[2])print("-------2.開辟一個新列表把內容存進去-------") r [] for i…

linux線程學習初步02

殺死線程的函數 int pthread_cancel(pthread_t thread); 參數介紹&#xff1a;需要輸入的tid 返回值&#xff1a;識別返回 errno成功返回 0 被殺死的線程&#xff0c;退出狀態值為一個 #define PTHREAD_CANCELED((void *)-1)代碼案例&#xff1a; #include <stdio.h> #…

python的文件基本操作和文件指針

讀寫模式的基本操作 https://www.cnblogs.com/c-x-m/articles/7756498.html r,w,a r只讀模式【默認模式&#xff0c;文件必須存在&#xff0c;不存在則拋出異常】w只寫模式【不可讀&#xff1b;不存在則創建&#xff1b;存在則清空內容】a之追加寫模式【不可讀&#xff1b;不…

python3 將unicode轉中文

decrypted_str.encode(utf-8).decode(unicode_escape)

HTTP菜鳥教程速查手冊

HTTP協議&#xff08;HyperText Transfer Protocol&#xff0c;超文本傳輸協議&#xff09;是因特網上應用最為廣泛的一種網絡傳輸協議&#xff0c;所有的WWW文件都必須遵守這個標準。 HTTP是一個基于TCP/IP通信協議來傳遞數據&#xff08;HTML 文件, 圖片文件, 查詢結果等&am…

mysql學習筆記01-創建數據庫

創建數據庫&#xff1a; 校驗規則&#xff1a;是指表的排序規則和查詢時候的規則 utf8_general_ci 支持中文&#xff0c; 且不區分大小寫 utf8_bin 支持中文&#xff0c; 區分大小寫 比如&#xff1a; create database db3 character set utf8 collate utf8_general_ci; &…

python的Web編程

首先看一下效果 完整代碼 import socket from multiprocessing import ProcessHTML_ROOT_DIR ""def handle_client(client_socket):request_data client_socket.recv(1024)print("request data:", request_data)response_start_line "HTTP/1.0 20…

mysql 學習筆記 02創建表

表結構的創建 比如&#xff1a; create table userinfo (id int unsigned comment id號name varchar(60) comment 用戶名password char(32),birthday date ) character set utf8 engine MyISAM;comment 表示注釋的意思 不同的存儲引擎&#xff0c;創建的表的文件不一樣

mysql 學習筆記03 常用數據類型

數值類型&#xff1a; a. 整數類型&#xff1a; 注意事項&#xff1a; 舉例&#xff1a;某個整型字段 &#xff0c;不為空&#xff0c;且有默認值 create table test (age int unisigned not null default 1);zerofill的使用 b. bit類型的使用 c.小數類型 小數類型占用…

VMware的虛擬機連不上網

1.如果你發現在VMware下運行的虛擬機無法連接網絡&#xff0c;那下面的方法也許可以幫 到你。&#xff08;前提是你的物理網絡是通的&#xff09; 第一步&#xff1a;在VMware界面下 單擊“編輯“→”虛擬網絡編輯器” 第二步&#xff1a;單擊”更改設置” 獲取權限&#xff…

python三國演義人物出場統計

完整代碼 開源代碼 統計三國演義人物高頻次數 #!/usr/bin/env python # codingutf-8 #e10.4CalThreeKingdoms.py import jieba excludes {"來到","人馬","領兵","將軍","卻說","荊州","二人","…

mysql 學習筆記03修改表以及其他操作

首先創建一張表 在現有表的結構上增加字段 alter table users add image varchar(100) not null defalut comment 圖片路徑;修改某個字段的長度 alter table users modify job vachar(60) not null comment 工作;刪除某個字段 刪除sex這個字段 alter table users drop se…

統計哈姆雷特文本中高頻詞的個數

統計哈姆雷特文本中高頻詞的個數 三國演義人物出場統計 開源代碼 講解視頻 kouubuntu:~/python$ cat ClaHamlet.py #!/usr/bin/env python # codingutf-8#e10.1CalHamlet.py def getText():txt open("hamlet.txt", "r").read()txt txt.lower()for ch…

mysql 學習筆記04 insert與update語句

1.插入數據 注意事項&#xff1a; 字符和日期類型&#xff0c; 要用 單引號 括起來 insert into (), (), () 例如&#xff1a; insert into goods values(1, abc, 2.2), (2, def, 3.3);這種形式添加多條記錄 insert 語句&#xff0c;如果沒有指定字段名&#xff0c;則values …

PyCharm怎么關閉端口,解決端口占用問題

在進行web開發遇到這個問題&#xff01;

mysql 筆記05 select語句以及條件語句的使用

select語句 過濾重復語句&#xff08;distinct&#xff09; 舉例&#xff1a; 查詢學生的總分 select name, math English China as 總分 from students;在姓趙的學生總分基礎上&#xff0c; 增加60%&#xff0c; select name, round((math English China) * 1.6, 2) as …

python3 與 Django 連接數據庫:Error loading MySQLdb module: No module named 'MySQLdb'

解決方法&#xff1a;在 init.py 文件中添加以下代碼即可。 import pymysql pymysql.install_as_MySQLdb()

mysql 學習筆記05 統計函數的相關使用

合計函數count&#xff0c; 統計多少條記錄 統計共有多少學生 select count(*) from students;查詢數學成績大于等于90的學生數量 select count(*) from students where math > 90;查詢總分超過235分的學生的數量 select count(*) from students where (English math Ch…

Unknown column '' in 'field list'

Unknown column ‘’ in ‘field list’ 解決辦法 正確寫法&#xff1a;cursor.execute("update book set name%s where id%d" % (name, int(id))) 錯誤寫法&#xff1a;cursor.execute("update book set name%s where id%d" % (name, int(id)))你要獲取字…