中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當前位置: 首頁 > news >正文

公裝網站怎么做seo專業(yè)培訓學費多少錢

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

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

進程與線程的區(qū)別

進程

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

線程

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

使用線程的理由

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

多線程開發(fā)及API

多線程開發(fā)主要包含三點:線程、互斥鎖、條件變量。以下是具體的操作和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,否則返回錯誤編號
線程的獲取和比較
#include <pthread.h>
pthread_t pthread_self(void);
線程的等待
#include <pthread.h>
int pthread_join(pthread_t thread, void **rval_ptr);
// 參數:
// 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;
}

傳入一個結構體的線程創(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;
}

線程共享空間驗證示例

#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;
}

條件變量實現線程同步

條件變量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的講解,本文詳細介紹了Linux下進程與線程的區(qū)別、多線程開發(fā)的基本操作以及常見問題和解決方案。希望能夠幫助大家更好地理解和使用多線程編程。

http://www.risenshineclean.com/news/44071.html

相關文章:

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