Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Application example of unix mutex and semaphore

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

# include#include # define SIZE 3#define STR_SIZE 20pthread_mutex_t mutex; / / Global variables can be seen in sem_t read_sem for main and thread functions, and write_sem; / / global variables can be seen for main and thread functions as struct _ buf_ {char* buf [SIZE]; int front, rear;} buffer / / cyclic queue structure void thread (void) {while (1) {if (0! = sem_wait (& read_sem)) {perror ("sem_wait_0"); exit (1);} if (0! = pthread_mutex_lock (& mutex)) {perror ("pthread_mutex_lock_0"); exit (1);} fputs ("output characters:\ n", stdout); fputs (buffer.buf [buffer.front], stdout); buffer.front = (buffer.front + 1)% SIZE If (0! = pthread_mutex_unlock (& mutex)) {perror ("pthread_mutex_lock_0"); exit (1);} if (0! = sem_post (& write_sem)) {perror ("sem_wait_0"); exit (1);} sleep (10);}} int main (void) {/ / initialize the circular queue structure int iscape bufferbuffer.front = buffer.rear = 0x bufferbuffer.I

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report