導航:首頁 > 網路營銷 > seminitsemwait

seminitsemwait

發布時間:2021-02-18 09:40:54

1、怎麼用c語言編程 實現創建原語、撤銷原語、阻塞原語和喚醒原語

下,應該差不多

一、如何建立線程
用到的頭文件
(a)pthread.h
(b)semaphore.h
(c) stdio.h
(d)string.h
定義線程標識
pthread_t
創建線程
pthread_create
對應了一個函數作為線程的程序段
注意的問題
要保證進程不結束(在創建線程後加死循環)
在線程中加入While(1)語句,也就是死循環,保證進程不結束。

二、控制線程並發的函數
sem_t:信號量的類型
sem_init:初始化信號量
sem_wait:相當於P操作
sem_post:相當於V操作

三、實現原形系統
父親、母親、兒子和女兒的題目:
桌上有一隻盤子,每次只能放入一隻水果。爸爸專放蘋果,媽媽專放橘子,一個兒子專等吃盤子中的橘子,一個女兒專等吃盤子中的蘋果。分別用P,V操作和管程實現

每個對應一個線程
pthread_t father; father進程
pthread_t mother; mother進程
pthread_t son; son進程
pthread_t daughter; daughter進程

盤子可以用一個變數表示
sem_t empty;

各線程不是只做一次,可以是無限或有限次循環
用While(1)控制各線程無限次循環

輸出每次是那個線程執行的信息
printf("%s\n",(char *)arg);通過參數arg輸出對應線程執行信息

編譯方法
gcc hex.c -lpthread
生成默認的可執行文件a.out
輸入./a.out命令運行
查看結果:程序連續運行顯示出
father input an apple.
daughter get an apple.
mother input an orange.
son get an orange.
mother input an orange.
son get an orange.
………………..
四、程序源代碼
#include <stdio.h>
#include<string.h>
#include <semaphore.h>
#include <pthread.h>
sem_t empty; //定義信號量
sem_t applefull;
sem_t orangefull;

void *procf(void *arg) //father線程
{
while(1){
sem_wait(&empty); //P操作
printf("%s\n",(char *)arg);
sem_post(&applefull); //V操作
sleep(7);
}

}
void *procm(void *arg) //mother線程
{
while(1){
sem_wait(&empty);
printf("%s\n",(char *)arg);
sem_post(&orangefull);
sleep(3);
}
}
void *procs(void *arg) //son線程
{
while(1){
sem_wait(&orangefull);
printf("%s\n",(char *)arg);
sem_post(&empty);
sleep(2);
}
}
void *procd(void *arg) //daughter線程
{
while(1){
sem_wait(&applefull);
printf("%s\n",(char *)arg);
sem_post(&empty);
sleep(5);
}

}

main()
{
pthread_t father; //定義線程
pthread_t mother;
pthread_t son;
pthread_t daughter;
sem_init(&empty, 0, 1); //信號量初始化
sem_init(&applefull, 0, 0);
sem_init(&orangefull, 0, 0);

pthread_create(&father,NULL,procf,"father input an apple."); //創建線程
pthread_create(&mother,NULL,procm,"mother input an orange.");
pthread_create(&daughter,NULL,procd,"daughter get an apple.");
pthread_create(&son,NULL,procs,"son get an orange.");

while(1){} //循環等待
}
另外,站長團上有產品團購,便宜有保證

2、如何使用Linux提供的信號量來實現進程的互斥和同步?

#include<stdio.h>
#include<pthread.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<semaphore.h>
#include<stdlib.h>
#define N 3
pthread_mutex_t mutex_w,mutex_r; // 定義讀寫互斥鎖
sem_t sem_w,sem_r; //定義讀寫信號量

int data[N];
int pos=0;
void *function_w(void *arg)
{
int w = *(int *)arg;
pos = w;
while(1)
{
usleep(100000);
sem_wait(&sem_w);//等待可寫的資源
pthread_mutex_lock(&mutex_w);//禁止別的線程寫此資源
data[pos] = w;
w++;
w++;
w++;
pos++;
pos=pos%N;
pthread_mutex_unlock(&mutex_w);//別的線程可寫此資源
sem_post(&sem_r);// 釋放一個讀資源
}
return (void *)0;
}
void *function_r(void *arg)
{
while(1)
{
sem_wait(&sem_r);//等待可讀的資源
pthread_mutex_lock(&mutex_r);//禁止別的線程讀此資源
printf("%d\n",data[(pos+N-1)%N]);
pthread_mutex_unlock(&mutex_r);//別的線程可讀此資源
sem_post(&sem_w);// 釋放一個寫資源
}
return (void *)0;
}
int main(int argc, char **argv)
{
pthread_t thread[2*N];

int i;

pthread_mutex_init(&mutex_w,NULL);
pthread_mutex_init(&mutex_r,NULL);
sem_init(&sem_w,0,N);
sem_init(&sem_r,0,0);

for(i=0;i<N;i++)
{
if ( pthread_create(&thread[i],NULL,function_w,(void *)&i) < 0)//創建寫線程
{
perror("pthread_create");
exit(-1);
}
}

for(i=N;i<2*N;i++)
{
if ( pthread_create(&thread[i],NULL,function_r,NULL) < 0)//創建讀線程
{
perror("pthread_create");
exit(-1);
}
}

sleep(1);

return(0);
}

3、關於linux下的多線程使用sem信號量的運行問題

不是信號量的問題
printf函數,是先寫到輸出緩沖,遇到\n時,或者緩沖區滿時,或者有強制輸出(fflush)時,才會將緩沖區里的內容輸出到屏幕上(標准輸出設備:stdout)。你的代碼裡面並沒有以上3個觸發條件的任意一種,所以printf的內存沒有實際輸出到屏幕上。
你只要在每個printf函數後面加上fflush(stdout);就可以了。

4、使用信號量發生內存錯誤

你就一個信號量,而且兩個線程都是先sem_wait,你的兩個線程都會卡在sem_wait(&sem1);
你可以把其中給一個線程的sem_wait跟sem_post換個位置試一下

5、Vc的build中為什麼會出現這個錯誤 error LNK2001: unresolved external symbol __imp__sem_wait

你用的什麼編譯器?我用Visual Studio2012都找不到兩個頭文件。

#include <pthread.h>
#include<semaphore.h>

這兩個貌似是Linux系統下的。

6、sem_wait的範例

<

與seminitsemwait相關的知識