LINUX multi-thread Learning (7)-implement "producer and consumer"

Source: Internet
Author: User

In the previous article, we used semaphores to achieve mutual exclusion between threads. This article uses the mutex synchronization mechanism of semaphores to implement a classic instance, that is, "producer and consumer ".

1. Briefly describe the problems of producers and consumers.

There is a buffer and two threads: producer and consumer. The producer places the product in the buffer zone, and the consumer removes the product from the buffer zone. When the buffer zone is full, the producer must wait. In addition, when the buffer zone is empty, the consumer must wait, and the buffer zone cannot operate both the producer and consumer.

# Include <stdio. h> <br/> # include <stdlib. h> <br/> # include <pthread. h> <br/> # include <semaphore. h> <br/> # include <errno. h> <br/> # include <fcntl. h> <br/> # include <string. h> </P> <p> # define FIFO "my_fifo" <br/> # define N 10 <br/> # define return_if_fail (p) if (P) = 0) {printf ("[% s]: func error! /N ", _ FUNC _); Return ;}</P> <p> typedef struct _ privinfo <br/>{< br/> sem_t avail; <br/> sem_t full; <br/> sem_t mutex; <br/> char buf_r [255]; <br/> time_t end_time; <br/> int FD; <br/>} privinfo; </P> <p> static void info_init (privinfo * thiz); <br/> static void info_destroy (privinfo * thiz ); <br/> static void * productor (privinfo * thiz); <br/> static void * Consumer (privinfo * thiz); </P> <p> int main (int Argc, char * argv []) <br/>{< br/> pthread_t pt_1 = 0; <br/> pthread_t pt_2 = 0; <br/> int ret = 0; <br/> privinfo * thiz = NULL; </P> <p> thiz = (privinfo *) malloc (sizeof (privinfo )); <br/> If (thiz = NULL) <br/> {<br/> printf ("[% s]: failed to malloc privinfo. /n ", _ FUNC _); <br/> return-1; <br/>}</P> <p> info_init (thiz ); </P> <p> ret = pthread_create (& pt_1, null, (void *) productor, thiz); <br /> If (Ret! = 0) <br/>{< br/> perror ("pthread_1_create:"); <br/>}</P> <p> ret = pthread_create (& pt_2, null, (void *) Consumer, thiz); <br/> If (Ret! = 0) <br/>{< br/> perror ("pthread_2_func:"); <br/>}</P> <p> pthread_join (pt_1, null ); <br/> pthread_join (pt_2, null); </P> <p> info_destroy (thiz); <br/> thiz = NULL; </P> <p> return 0; <br/>}</P> <p> static void info_init (privinfo * thiz) <br/>{< br/> return_if_fail (thiz! = NULL); </P> <p> thiz-> end_time = Time (null) + 10; </P> <p> sem_init (& thiz-> avail, 0, n); <br/> sem_init (& thiz-> full, 0, 0); <br/> sem_init (& thiz-> mutex, 0, 1 ); </P> <p> If (mkfifo (FIFO, o_creat | o_excl) & (errno! = Eexist) <br/>{< br/> printf ("cannot create FIFO server. /n "); <br/>}</P> <p> printf (" prepareing for reading bytes... /n "); </P> <p> memset (thiz-> buf_r, 0, sizeof (thiz-> buf_r )); </P> <p> thiz-> FD = open (FIFO, o_rdwr | o_nonblock, 0777); <br/> If (thiz-> FD =-1) <br/>{< br/> perror ("Open FIFO:"); <br/> info_destroy (thiz); <br/> exit (1 ); <br/>}</P> <p> return; <br/>}</P> <p> static void info _ Destroy (privinfo * thiz) <br/>{< br/> return_if_fail (thiz! = NULL); </P> <p> sem_destroy (& thiz-> avail); <br/> sem_destroy (& thiz-> full ); <br/> sem_destroy (& thiz-> mutex); </P> <p> free (thiz); <br/> thiz = NULL; </P> <p> return; <br/>}</P> <p> static void * productor (privinfo * thiz) <br/>{< br/> return_if_fail (thiz! = NULL); </P> <p> int ret = 0; </P> <p> while (Time (null) <thiz-> end_time) <br/>{< br/> sem_wait (& thiz-> avail); <br/> sem_wait (& thiz-> mutex ); </P> <p> If (ret = write (thiz-> FD, "hello", 5) =-1) <br/>{< br/> If (errno = eagain) <br/>{< br/> printf ("the FIFO has not been read, please try later again. /n "); <br/>}< br/> else <br/> {<br/> printf (" Write hello to the FIFO. /n "); <br/>}< br/>} </P> <p> sem_post (& thiz-> full); <br/> sem_post (& thiz-> mutex ); </P> <p> sleep (1); <br/>}</P> <p> return; <br/>}</P> <p> static void * Consumer (privinfo * thiz) <br/>{< br/> return_if_fail (thiz! = NULL); </P> <p> int ret = 0; </P> <p> while (Time (null) <thiz-> end_time) <br/>{< br/> sem_wait (& thiz-> full); <br/> sem_wait (& thiz-> mutex ); </P> <p> If (ret = read (thiz-> FD, thiz-> buf_r, 255)> 0) <br/>{</P> <p> printf ("read % s frm FIFO. /n ", thiz-> buf_r); <br/>}< br/> else <br/>{< br/> If (errno = eagain) <br/> {<br/> printf ("no data yet. /n "); <br/>}</P> <p> sem_post (& thiz-> avail ); <br/> sem_post (& thiz-> mutex); </P> <p> sleep (1); <br/>}</P> <p> return; <br/>}< br/>

From the classic example above, we have combined the multi-thread Simultaneous programming problem. The Learning thread has come to an end. In future programming applications, we hope to implement it in practice ~~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.