国产亚洲精品福利在线无卡一,国产精久久一区二区三区,亚洲精品无码国模,精品久久久久久无码专区不卡

當(dāng)前位置: 首頁 > news >正文

公裝網(wǎng)站怎么做seo專業(yè)培訓(xùn)學(xué)費(fèi)多少錢

公裝網(wǎng)站怎么做,seo專業(yè)培訓(xùn)學(xué)費(fèi)多少錢,wordpress動(dòng)態(tài)sidebar,wordpress后臺(tái)打開真慢在 Linux 系統(tǒng)中,進(jìn)程和線程是兩種重要的并發(fā)執(zhí)行單元。本文將詳細(xì)介紹它們的區(qū)別、使用場(chǎng)景、以及多線程編程中的關(guān)鍵API和示例代碼。 進(jìn)程與線程的區(qū)別 進(jìn)程 進(jìn)程是程序運(yùn)行的一個(gè)實(shí)例,承擔(dān)分配系統(tǒng)資源的基本單位。每個(gè)進(jìn)程都有獨(dú)立的地址空間&…

在 Linux 系統(tǒng)中,進(jìn)程和線程是兩種重要的并發(fā)執(zhí)行單元。本文將詳細(xì)介紹它們的區(qū)別、使用場(chǎng)景、以及多線程編程中的關(guān)鍵API和示例代碼。

進(jìn)程與線程的區(qū)別

進(jìn)程

  • 進(jìn)程是程序運(yùn)行的一個(gè)實(shí)例,承擔(dān)分配系統(tǒng)資源的基本單位。
  • 每個(gè)進(jìn)程都有獨(dú)立的地址空間,一個(gè)進(jìn)程崩潰不會(huì)影響其他進(jìn)程。
  • 進(jìn)程的創(chuàng)建和切換消耗較多資源。

線程

  • 線程是進(jìn)程中的一個(gè)執(zhí)行路徑,是CPU調(diào)度的基本單位。
  • 線程共享進(jìn)程的地址空間,但每個(gè)線程有自己的堆棧和局部變量。
  • 線程的創(chuàng)建和切換開銷較小。
  • 如果一個(gè)線程崩潰,會(huì)導(dǎo)致整個(gè)進(jìn)程崩潰。

使用線程的理由

  1. 節(jié)省資源:創(chuàng)建進(jìn)程需要分配獨(dú)立的地址空間,建立多個(gè)數(shù)據(jù)表來維護(hù)其代碼段、數(shù)據(jù)段和堆棧段,這種方式十分昂貴。線程共享同一進(jìn)程的地址空間和大部分?jǐn)?shù)據(jù),啟動(dòng)一個(gè)線程比啟動(dòng)一個(gè)進(jìn)程要快很多。一個(gè)進(jìn)程的開銷大約是一個(gè)線程的30倍。
  2. 方便的通信機(jī)制:不同進(jìn)程之間的數(shù)據(jù)傳遞需要通過通信機(jī)制,如管道、信號(hào)等,這種方式耗時(shí)且復(fù)雜。而線程共享進(jìn)程的數(shù)據(jù)空間,數(shù)據(jù)共享非常方便和快捷,但需要注意數(shù)據(jù)同步的問題。

多線程開發(fā)及API

多線程開發(fā)主要包含三點(diǎn):線程、互斥鎖、條件變量。以下是具體的操作和API介紹:

線程操作

線程的創(chuàng)建
#include <pthread.h>
int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void *), void *restrict arg);
// 返回:若成功返回0,否則返回錯(cuò)誤編號(hào)
線程的獲取和比較
#include <pthread.h>
pthread_t pthread_self(void);
線程的等待
#include <pthread.h>
int pthread_join(pthread_t thread, void **rval_ptr);
// 參數(shù):
// pthread_t thread:等待的線程
// void **rval:線程退出狀態(tài)的收回,NULL表示不收回
線程的退出
#include <pthread.h>
int pthread_exit(void *rval_ptr);

線程的創(chuàng)建、退出、等待示例

#include <stdio.h>
#include <pthread.h>void *func1(void *arg)
{static int ret = 10;printf("t1:%ld thread is created\n", (unsigned long)pthread_self());printf("t1: parameter is %d\n", *((int *)arg));pthread_exit((void*)&ret); // 線程退出
}int main()
{int ret;int param = 100;int *pret = NULL;pthread_t t1;ret = pthread_create(&t1, NULL, func1, (void*)&param); // 創(chuàng)建線程if(ret == 0){printf("main: create t1 success\n");}printf("main: %ld\n", (unsigned long)pthread_self()); // 獲取線程IDpthread_join(t1, (void**)&pret); // 等待線程退出printf("main: t1 quit with %d\n", *pret);return 0;
}

傳入一個(gè)結(jié)構(gòu)體的線程創(chuàng)建示例

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>struct data
{int a;char *s;
};void *func1(void *arg)
{static char *x = "t1 run out";struct data *temp;temp = (struct data*)arg;printf("t1:%ld pthread is created\n", (unsigned long)pthread_self());printf("t1: %d\n", temp->a);printf("t1: %s\n", temp->s);pthread_exit((void*)x);
}int main()
{int ret;pthread_t t1;char *pret = NULL;struct data *p = (struct data*)malloc(sizeof(struct data));p->a = 1;p->s = "xiancheng";ret = pthread_create(&t1, NULL, func1, (void*)p);if(ret == 0){printf("main: create t1 success\n");}printf("main: %ld\n", (unsigned long)pthread_self());pthread_join(t1, (void**)&pret);printf("main: t1 quit with %s\n", pret);free(p);return 0;
}

線程共享空間驗(yàn)證示例

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>int g_data = 10;void *func1(void *arg)
{printf("t1:%ld thread is created\n", (unsigned long)pthread_self());printf("t1: parameter is %d\n", *((int*)arg));while(1){printf("%d\n", g_data++);sleep(1);if(g_data == 3){pthread_exit(NULL);}}
}void *func2(void *arg)
{printf("t2:%ld thread is created\n", (unsigned long)pthread_self());printf("t2: parameter is %d\n", *((int*)arg));while(1){printf("%d\n", g_data++);sleep(1);}
}int main()
{int ret;int param = 100;pthread_t t1;pthread_t t2;ret = pthread_create(&t1, NULL, func1, (void*)&param);if(ret == 0){printf("main: create t1 success\n");}ret = pthread_create(&t2, NULL, func2, (void*)&param);if(ret == 0){printf("main: create t2 success\n");}printf("main: %ld\n", (unsigned long)pthread_self());while(1){printf("%d\n", g_data++);sleep(1);} pthread_join(t1, NULL);pthread_join(t2, NULL);return 0;
}

互斥鎖(Mutex)

互斥鎖API
創(chuàng)建及銷毀互斥鎖
#include <pthread.h>
int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr);
int pthread_mutex_destroy(pthread_mutex_t *restrict mutex);
加鎖及解鎖
#include <pthread.h>
int pthread_mutex_lock(pthread_mutex_t *restrict mutex);
int pthread_mutex_trylock(pthread_mutex_t *restrict mutex);
int pthread_mutex_unlock(pthread_mutex_t *restrict mutex);

使用互斥鎖的示例

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>int g_data = 0;
pthread_mutex_t mutex; // 定義鎖void *func1(void *arg)
{pthread_mutex_lock(&mutex); // 加鎖for(int i = 0; i < 5; i++){printf("t1: %ld thread is created\n", (unsigned long)pthread_self());printf("t1: parameter is %d\n", *((int*)arg));sleep(1);}pthread_mutex_unlock(&mutex); // 解鎖
}void *func2(void *arg)
{pthread_mutex_lock(&mutex);for(int i = 0; i < 5; i++){printf("t2: %ld thread is created\n", (unsigned long)pthread_self());printf("t2: parameter is %d\n", *((int*)arg));sleep(1);}pthread_mutex_unlock(&mutex);
}void *func3(void *arg)
{pthread_mutex_lock(&mutex);for(int i = 0; i < 5; i++){printf("t3: %ld thread is created\n", (unsigned long)pthread_self());printf("t3: parameter is %d\n", *((int*)arg));sleep(1);}pthread_mutex_unlock(&mutex);
}int main()
{int ret;int param = 100;pthread_t t1;pthread_t t2;pthread_t t3;pthread_mutex_init(&mutex, NULL); // 初始化鎖ret = pthread_create(&t1, NULL, func1, (void*)&param);if(ret == 0){printf("main: create t1 success\n");}ret = pthread_create(&t2, NULL, func2, (void*)&param);if(ret == 0){printf("main: create t2 success\n");}ret = pthread_create(&t3, NULL, func3, (void*)&param);if(ret == 0){printf("main: create t3 success\n");}printf("main: %ld\n", (unsigned long)pthread_self());pthread_join(t1, NULL);pthread_join(t2, NULL);pthread_join(t3, NULL);pthread_mutex_destroy(&mutex); // 收回鎖return 0;
}

互斥鎖限制共享資源的訪問示例

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>int g_data = 0;
pthread_mutex_t mutex;void *func1(void *arg)
{printf("t1: %ld pthread is created\n", (unsigned long)pthread_self());printf("t1: parameter is %d\n", *((int*)arg));while(1){pthread_mutex_lock(&mutex);printf("%d\n", g_data++);sleep(1);if(g_data == 3){pthread_mutex_unlock(&mutex);pthread_exit(NULL);}}
}void *func2(void *arg)
{printf("t2: %ld pthread is created\n", (unsigned long)pthread_self());printf("t2: parameter is %d\n", *((int*)arg));while(1){printf("%d\n", g_data);pthread_mutex_lock(&mutex);g_data++;pthread_mutex_unlock(&mutex);sleep(1); }
}int main()
{int ret;int param = 100;pthread_t t1;pthread_t t2;pthread_mutex_init(&mutex, NULL);ret = pthread_create(&t1, NULL, func1, (void*)&param);if(ret == 0){printf("main: create t1 success\n");}ret = pthread_create(&t2, NULL, func2, (void*)&param);if(ret == 0){printf("main: create t2 success\n");}printf("main: %ld\n", (unsigned long)pthread_self());pthread_join(t1, NULL);pthread_join(t2, NULL);pthread_mutex_destroy(&mutex);return 0;
}

死鎖示例

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>pthread_mutex_t mutex;
pthread_mutex_t mutex2;void *func1(void *arg)
{pthread_mutex_lock(&mutex);sleep(1);pthread_mutex_lock(&mutex2);for(int i = 0; i < 5; i++){printf("t1: %ld\n", (unsigned long)pthread_self());printf("t1: parameter is %d\n", *((int *)arg));sleep(1);}pthread_mutex_unlock(&mutex);pthread_mutex_unlock(&mutex2);
}void *func2(void *arg)
{pthread_mutex_lock(&mutex2);sleep(1);pthread_mutex_lock(&mutex);for(int i = 0; i < 5; i++){printf("t2: %ld\n", (unsigned long)pthread_self());printf("t2: parameter is %d\n", *((int *)arg));sleep(1);}pthread_mutex_unlock(&mutex2);pthread_mutex_unlock(&mutex);
}void *func3(void *arg)
{pthread_mutex_lock(&mutex);for(int i = 0; i < 5; i++){printf("t3: %ld\n", (unsigned long)pthread_self());printf("t3: parameter is %d\n", *((int *)arg));sleep(1);}pthread_mutex_unlock(&mutex);
}int main()
{int ret;int param = 100;pthread_t t1;pthread_t t2;pthread_t t3;pthread_mutex_init(&mutex, NULL);pthread_mutex_init(&mutex2, NULL);ret = pthread_create(&t1, NULL, func1, (void*)&param);if(ret == 0){printf("main: create t1 success\n");}ret = pthread_create(&t2, NULL, func2, (void*)&param);if(ret == 0){printf("main: create t2 success\n");}ret = pthread_create(&t3, NULL, func3, (void*)&param);if(ret == 0){printf("main: create t3 success\n");}printf("main: %ld\n", (unsigned long)pthread_self());pthread_join(t1, NULL);pthread_join(t2, NULL);pthread_join(t3, NULL);pthread_mutex_destroy(&mutex);pthread_mutex_destroy(&mutex2);return 0;
}

條件變量實(shí)現(xiàn)線程同步

條件變量API
創(chuàng)建及銷毀條件變量
#include <pthread.h>
int pthread_cond_init(pthread_cond_t *restrict cond, const pthread_condattr_t *restrict attr);
int pthread_cond_destroy(pthread_cond_t cond);
等待
#include <pthread.h>
int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex);
int pthread_cond_timedwait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex, const struct timespec *restrict timeout);
觸發(fā)
#include <pthread.h>
int pthread_cond_signal(pthread_cond_t *restrict cond);
int pthread_cond_broadcast(pthread_cond_t cond);

使用條件變量的示例

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>int g_data = 0;
pthread_mutex_t mutex;
pthread_cond_t cond;void *func1(void *arg)
{static int cnt = 10;printf("t1: %ld pthread is created\n", (unsigned long)pthread_self());printf("t1: parameter is %d\n", *((int*)arg));while(1){pthread_cond_wait(&cond, &mutex); // 等待if(g_data == 3){printf("t1 run=========================\n");}printf("t1: %d\n", g_data);g_data = 0;sleep(1);if(cnt++ == 10){exit(1);}}
}void *func2(void *arg)
{printf("t2: %ld pthread is created\n", (unsigned long)pthread_self());printf("t2: parameter is %d\n", *((int*)arg));while(1){printf("t2: %d\n", g_data);pthread_mutex_lock(&mutex);printf("%d\n", g_data++);if(g_data == 3){pthread_cond_signal(&cond); // 觸發(fā)}pthread_mutex_unlock(&mutex);sleep(1);}
}int main()
{int ret;int param = 100;pthread_t t1;pthread_t t2;pthread_cond_init(&cond, NULL);pthread_mutex_init(&mutex, NULL);ret = pthread_create(&t1, NULL, func1, (void *)&param);if(ret == 0){printf("main: create t1 success\n");}ret = pthread_create(&t2, NULL, func2, (void *)&param);if(ret == 0){printf("main: create t2 success\n");}pthread_join(t1, NULL);pthread_join(t2, NULL);pthread_mutex_destroy(&mutex);pthread_cond_destroy(&cond);return 0;
}

通過上述示例和API的講解,本文詳細(xì)介紹了Linux下進(jìn)程與線程的區(qū)別、多線程開發(fā)的基本操作以及常見問題和解決方案。希望能夠幫助大家更好地理解和使用多線程編程。

http://m.aloenet.com.cn/news/44071.html

相關(guān)文章:

  • 領(lǐng)卷網(wǎng)站怎么做的seo關(guān)鍵詞排名優(yōu)化教程
  • 優(yōu)秀北京網(wǎng)站建設(shè)武漢百度推廣多少錢
  • 114啦建站程序軍事最新消息
  • 外貿(mào)網(wǎng)站建設(shè)方法百度知道入口
  • vue做公司網(wǎng)站天津網(wǎng)站優(yōu)化公司
  • 茶葉網(wǎng)站建設(shè)公司做網(wǎng)站seo推廣公司
  • 怎么用服務(wù)器ip做網(wǎng)站谷歌官方網(wǎng)站首頁
  • 花錢做推廣廣告哪個(gè)網(wǎng)站好網(wǎng)絡(luò)營(yíng)銷的發(fā)展現(xiàn)狀及趨勢(shì)
  • 怎么樣建設(shè)一個(gè)電影網(wǎng)站友情鏈接網(wǎng)自動(dòng)收錄
  • 廣州市建設(shè)交易中心網(wǎng)站首頁深圳網(wǎng)絡(luò)推廣專員
  • 做短視頻的網(wǎng)站網(wǎng)址怎么申請(qǐng)注冊(cè)
  • 網(wǎng)站備案主體撤銷西安網(wǎng)站建設(shè)優(yōu)化
  • 愛眼護(hù)眼ppt模板免費(fèi)下載 素材鶴壁seo
  • 湖北網(wǎng)站建設(shè)的釋義搜索網(wǎng)站有哪幾個(gè)
  • 網(wǎng)站建設(shè)學(xué)習(xí)廣告公司主要做什么
  • 網(wǎng)站可以做電信增值百度的首頁
  • 什么是小手機(jī)型網(wǎng)站網(wǎng)銷是做什么的
  • 蘇州做網(wǎng)站最好公司軟文是什么東西
  • 政府部門網(wǎng)站建設(shè)負(fù)責(zé)部門百度一下首頁網(wǎng)址百度
  • 云主機(jī)網(wǎng)站配置網(wǎng)頁設(shè)計(jì)需要學(xué)什么軟件
  • 做a 免費(fèi)網(wǎng)站如何制作一個(gè)網(wǎng)址
  • 南昌企業(yè)網(wǎng)站設(shè)計(jì)建設(shè)制作百度風(fēng)云榜
  • 深圳開發(fā)網(wǎng)站建設(shè)搜索引擎推廣的基本方法
  • 松江專業(yè)做網(wǎng)站公司谷歌關(guān)鍵詞搜索排名
  • 什么APP可以做網(wǎng)站網(wǎng)絡(luò)推廣發(fā)帖網(wǎng)站
  • 布吉做棋牌網(wǎng)站建設(shè)好的在線crm系統(tǒng)
  • wordpress自定義頁seo代碼優(yōu)化包括哪些
  • nodejs做網(wǎng)站能保護(hù)源代碼嗎廊坊seo排名霸屏
  • 做js鏈接的網(wǎng)站要加證書嗎seo具體優(yōu)化流程
  • 免費(fèi)電子版?zhèn)€人簡(jiǎn)歷可編輯李江seo