1. 程式人生 > >linux下生產者與消費者問題程式碼,以及編譯c程式碼時error:undefined reference to sem_wait 解決方法之一

linux下生產者與消費者問題程式碼,以及編譯c程式碼時error:undefined reference to sem_wait 解決方法之一

//本文的詳細講解內容請大家下載word文件:http://download.csdn.net/detail/chenqiai0/4611801
#include <stdio.h>
#include <pthread.h>//執行緒
#include <semaphore.h>//因為用到了sem_init等
#include <unistd.h>
#include <stdlib.h>
#include <time.h>

int buffer[10]={0,0,0,0,0,0,0,0,0,0};
int i=0;
int j=0;
//訊號量的資料型別為結構sem_t,它本質上是一個長整型的數
sem_t full;//訊號量 
sem_t empty;
pthread_mutex_t mutex;//pthread_mutex_t是一個結構,是互斥鎖


void producer(void)
{ 
  int m;
  while(i<10)
  {
   pthread_mutex_lock(&mutex);//宣告開始用互斥鎖上鎖
   buffer[i]=i+1;
   i=i+1;
   printf("\nthere are 10 blocks in the buffer:0,1,2,3,4,5,6,7,8,9\n");
   printf("\nbuffer:");
   for(m=0;m<10;m++)
      printf("%3d",buffer[m]);
   printf("\nthe number added by the producer is:");
   printf("%d", buffer[i]);
   printf("\npointer is %d",i);
   sem_post(&full);//   int sem_post(sem_t * sem);------sem_post函式的作用是給訊號量的值加上一個“1”,它是一個“原子操作"即同時對同一個訊號量做加“1”操作的兩個執行緒是不會衝突的
   pthread_mutex_unlock(&mutex);//宣告開始用互斥鎖解鎖
  }
}

void consumer(void)
{ 
  int m;
  while(j<10)
  {
   int x;
   pthread_mutex_lock(&mutex);
   sem_wait(&full);// sem_wait函式也是一個原子操作,它的作用是從訊號量的值減去一個“1”,但它永遠會先等待該訊號量為一個非零值才開始做減法。
   if(buffer[j]==0)
     break;
   x=buffer[j];
   buffer[j]=0;
   j=j+1;
   printf("\nbuffer:");
   for(m=0;m<10;m++)
     printf("%3d",buffer[m]);
   printf("\n %d is removed form the buffer by consumer",x);
   printf("\nThe present pointer is %d",j);
   pthread_mutex_unlock(&mutex);
   sem_post(&empty);
  }
}


int main()
{
  pthread_t thread1,thread2,thread3,thread4;
  sem_init(&full,0,0);//int sem_init(sem_t *sem, int pshared, unsigned int value)---------sem_init() 初始化一個定位在 sem 的匿名訊號量。value 引數指定訊號量的初始值。 pshared 引數指明訊號量是由程序內執行緒共享,還是由程序之間共享。如果 pshared 的值為 0,那麼訊號量將被程序內的執行緒共享,並且應該放置在所有執行緒都可見的地址上
  sem_init(&empty,0,10);
  pthread_create(&thread1,NULL,(void *)producer,NULL);//函式pthread_create用來建立一個執行緒
  pthread_create(&thread2,NULL,(void *)consumer,NULL);//函式pthread_join用來等待一個執行緒的結束
  pthread_join(thread1,NULL);
  pthread_join(thread2,NULL);
}

當我們在終端輸入:gcc -o producer_consumer.out producer_consumer.c

就會出現錯誤:

producer_consumer.c:(.text+0xc9): undefined reference to `sem_post'
/tmp/ccvFyPLL.o: In function `consumer':
producer_consumer.c:(.text+0x108): undefined reference to `sem_wait'
producer_consumer.c:(.text+0x1c6): undefined reference to `sem_post'
/tmp/ccvFyPLL.o: In function `main':
producer_consumer.c:(.text+0x1fe): undefined reference to `sem_init'
producer_consumer.c:(.text+0x21a): undefined reference to `sem_init'
producer_consumer.c:(.text+0x23f): undefined reference to `pthread_create'
producer_consumer.c:(.text+0x264): undefined reference to `pthread_create'
producer_consumer.c:(.text+0x278): undefined reference to `pthread_join'
producer_consumer.c:(.text+0x28c): undefined reference to `pthread_join'

這個是因為pthread並非Linux系統的預設庫,編譯時注意加上-lpthread引數,以呼叫連結庫

我們再一次在終端輸入:gcc -o producer_consumer.out producer_consumer.c -lpthread

此時編譯正確

執行:在終端輸入:./producer_consumer.out

輸出為:

there are 10 blocks in the buffer:0,1,2,3,4,5,6,7,8,9

buffer:  1  0  0  0  0  0  0  0  0  0
the number added by the producer is:0
pointer is 1
there are 10 blocks in the buffer:0,1,2,3,4,5,6,7,8,9

buffer:  1  2  0  0  0  0  0  0  0  0
the number added by the producer is:0
pointer is 2
there are 10 blocks in the buffer:0,1,2,3,4,5,6,7,8,9

buffer:  1  2  3  0  0  0  0  0  0  0
the number added by the producer is:0
pointer is 3
there are 10 blocks in the buffer:0,1,2,3,4,5,6,7,8,9

buffer:  1  2  3  4  0  0  0  0  0  0
the number added by the producer is:0
pointer is 4
there are 10 blocks in the buffer:0,1,2,3,4,5,6,7,8,9

buffer:  1  2  3  4  5  0  0  0  0  0
the number added by the producer is:0
pointer is 5
there are 10 blocks in the buffer:0,1,2,3,4,5,6,7,8,9

buffer:  1  2  3  4  5  6  0  0  0  0
the number added by the producer is:0
pointer is 6
there are 10 blocks in the buffer:0,1,2,3,4,5,6,7,8,9

buffer:  1  2  3  4  5  6  7  0  0  0
the number added by the producer is:0
pointer is 7
there are 10 blocks in the buffer:0,1,2,3,4,5,6,7,8,9

buffer:  1  2  3  4  5  6  7  8  0  0
the number added by the producer is:0
pointer is 8
there are 10 blocks in the buffer:0,1,2,3,4,5,6,7,8,9

buffer:  1  2  3  4  5  6  7  8  9  0
the number added by the producer is:0
pointer is 9
there are 10 blocks in the buffer:0,1,2,3,4,5,6,7,8,9

buffer:  1  2  3  4  5  6  7  8  9 10
the number added by the producer is:10
pointer is 10
buffer:  0  2  3  4  5  6  7  8  9 10
 1 is removed form the buffer by consumer
The present pointer is 1
buffer:  0  0  3  4  5  6  7  8  9 10
 2 is removed form the buffer by consumer
The present pointer is 2
buffer:  0  0  0  4  5  6  7  8  9 10
 3 is removed form the buffer by consumer
The present pointer is 3
buffer:  0  0  0  0  5  6  7  8  9 10
 4 is removed form the buffer by consumer
The present pointer is 4
buffer:  0  0  0  0  0  6  7  8  9 10
 5 is removed form the buffer by consumer
The present pointer is 5
buffer:  0  0  0  0  0  0  7  8  9 10
 6 is removed form the buffer by consumer
The present pointer is 6
buffer:  0  0  0  0  0  0  0  8  9 10
 7 is removed form the buffer by consumer
The present pointer is 7
buffer:  0  0  0  0  0  0  0  0  9 10
 8 is removed form the buffer by consumer
The present pointer is 8
buffer:  0  0  0  0  0  0  0  0  0 10
 9 is removed form the buffer by consumer
The present pointer is 9
buffer:  0  0  0  0  0  0  0  0  0  0
 10 is removed form the buffer by consumer

相關推薦

linux生產者消費者問題程式碼以及編譯c程式碼errorundefined reference to sem_wait 解決方法之一

//本文的詳細講解內容請大家下載word文件:http://download.csdn.net/detail/chenqiai0/4611801 #include <stdio.h> #include <pthread.h>//執行緒 #includ

CC++混合程式設計問題.C++呼叫.C檔案出現undefined reference to `xxxxx()'

本人使用qt的介面和opengl功能模擬演算法,該演算法需要在嵌入式中執行,因此需要用純C編寫,此時涉及到在C++寫的qt介面檔案中呼叫C檔案的庫函式,原先以為只需在C++中把C的.h檔案包含進去就可以正常使用,後來發現,使用這種方法時,在程式碼編寫時,C++檔案裡可以直接連

kafka學習(1)linux的安裝和啟動以及Java示例程式碼

1. 安裝 1.1 下載kafka並解壓 wget http://mirror.bit.edu.cn/apache/kafka/0.11.0.0/kafka_2.11-0.11.0.0.tgz tar -zxvf kafka_2.11-0.11.0.0.tgz 1.2 修改

Linux生產者消費者模型

1. 概念   有一個或多個生產者生產某種型別的資料,並放在緩衝區裡(生產者),有一個消費者從緩衝區中取資料,每次取一項(消費者)。系統保證任何時候只有一個主題可以訪問快取區。所以當生產滿時,生產者不會再生產資料;當緩衝區為空時,消費者不會從中移走資料。 接下

Linux 編譯報錯 -- undefined reference to 問題解決方法 (具體'pthread_create'和opencv看文章最後兩條)

最近在Linux下程式設計發現一個詭異的現象,就是在連結一個靜態庫的時候總是報錯,類似下面這樣的錯誤: (.text+0x13): undefined reference to `func'     關於undefined reference這樣的問題,大家其實經常會遇到

Linux 編譯報錯 -- undefined reference to 問題解決方法 (具體'pthread_create'和opencv看文章最後兩條)

最近在Linux下程式設計發現一個詭異的現象,就是在連結一個靜態庫的時候總是報錯,類似下面這樣的錯誤: (.text+0x13): undefined reference to `func'     關於undefined reference這樣的問題,大家其實經常會遇

Linux阻塞非阻塞同步非同步的關係及IO模型

一、阻塞與非阻塞,同步與非同步的關係 1、同步   同步,就是在發出一個功能呼叫時,在沒有得到結果之前,該呼叫就不返回。也就是說事情必須一件一件地做,等前一件做完了才能做下一件事。 2、非同步   非同步,就是在發出一個功能呼叫時,呼叫者不會立刻得到結果。實際處理這個呼叫的部

linux 檢視Tomcat的狀態以及開啟停止服務命令

1.首先進入你的tomcat 的bin目錄下 cd /你的安裝目錄/tomcat/bin 檢視服務啟動情況 ps -ef|grep java 此條命令具體含義 ps:將某個程序顯示出來 -A  顯示所有程式。 -e  此引數的效果和指定"A"引數相同。 -f  顯

LinuxQT及OpenCV搭建以及交叉編譯OpenCV

由於word圖片很多,不方便上傳,所以改成圖片 為了方便大家複製貼上,文件已經上傳(不需要積分),下載地址: http://download.csdn.net/detail/bizer_csdn/9560001 注意交叉編譯OpenCV時候,選不同版本可能需要改原始碼,

學習筆記- 自己寫的關於生產者消費者模式還有定時任務的demo。

為了加深對生產者消費者模式的理解,特意寫了這個demo,裡面還包含了一個自己寫的定時任務。程式碼下載地址:http://download.csdn.net/detail/li_yan_fei/9811572 是個maven專案,只用了spring框架。 學到的內容有3個

有關linuxredis overcommit_memory的問題以及導致的Cannot allocate memory問題

背景 公司的redis有時background save db不成功,通過log發現下面的告警,很可能由它引起的: [13223] 17 Mar 13:18:02.207 # WARNING overcommit_memory is set to 0! B

linux開發解決cocos2d-x中編譯出現的一個小問題, undefined reference to symbol &#39;pthread_create@@GLIBC_2.2.5&#39;

water span x86 code bject data- ace 技術分享 inux 解決cocos2d-x中編譯出現的一個小問題 對於cocos2d-x 2.×中編譯中,若頭文件裏引入了#include "cocos-ext.h",在進行C++編譯的時候會遇到例

ubuntu make 編譯錯誤undefined reference to cv::line

在編譯一個專案時候出現 “undefined reference to cv::line” 的錯誤,是在連結其中一個靜態庫的時候出現的錯誤,錯誤如下: /home/build/lib/libapriltags.a(TagDetection.cc.o): In function `AprilTag

linux路徑分隔符'/'windows的分隔符'\'以及java項目web項目讀取項目的路徑

oid ont resource light ros ren -i microsoft 配置文件 1, linux下的文件分隔符是‘/‘, windows下的文件分隔符為‘\‘。但是‘\‘這個符號是轉義符。如果需要在console輸出‘\‘這個符號的,你需要輸入‘\\‘。另

廣播模式生產者消費者fanout模式

生成 ack word 需要 bin 隊列 highlight time host 生產者 #coding=utf-8 import pika import sys connection = pika.BlockingConnection(pika.ConnectionP

生產者消費者 程式碼實現 java

        首先,我利用忙測試寫出了第一次版本的程式碼       1 package How; 2 //自寫程式碼 缺陷 無法完全實現pv操作執行緒處於忙測試狀態 3 public class bin_1_1 4 { 5

華為標準註釋文件以及程式碼規範

註釋 為什麼要寫註釋呢?為什麼要寫文件呢? 也許有人會這樣問。但是我只想說如果你還在這樣問,那麼你不僅不是一個優秀的程式設計師,應該說你是不是程式設計師都應該受到質疑。 先說一下注釋的重要性: 在公司的開發中,我們要明白程式不是寫給自己看的,也不是所有的程式碼都是自己寫的,我們不僅需要看

linux【centos】nginx自動原始碼編譯安裝指令碼以及通過service 啟動/停止/過載 nginx的服務指令碼

 第一:指令碼為nginx原始碼編譯安裝的指令碼,可以幫助使用者自動建立系統使用者“nginx”;可以使用者自己指定安裝路徑,配置檔案路徑,執行檔案路徑等; 第二:可以配置第二個指令碼使用,通過service服務來管理nginx。 [[email protect

程式碼編寫生產者消費者模式思路

##   **目前有個任務是建立大量的資料同時向kafka裡寫入,於是之前開了大量的執行緒建立資料並寫入,發現kafka並不能連線那麼多執行緒,後來就用到生產者與消費者模式,大量的執行緒生產資料放入佇列中,然後只開幾個執行緒從佇列中獲取並寫入kafka.**      

使用Condition物件實現執行緒同步模擬生產者消費者問題。

使用列表模擬物品池,生產者往裡放置東西,而消費者從池中獲取物品。物品池滿時生產者等待,空時消費者等待。假設物品池裡面能夠容納5個元素,每個元素都是1-1000之間的整數。請編寫程式碼實現並檢視執行結果。 import threading from random import randin