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

Case Analysis of C language concurrent programming Model

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "C language concurrent programming model case analysis". In daily operation, I believe that many people have doubts about C language concurrent programming model case analysis. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "C language concurrent programming model case analysis". Next, please follow the editor to study!

1. Output in the specified order

We execute two threads: foo1 and foo2

Foo1: printing step1, step3

Foo2: printing step2

Please use concurrency to output in the order of 1 2 3

Answer: first of all, the execution order of the two threads is unpredictable. We must make sure that the step2 is printed before the step1 is printed, so we need to block the step2. The way to achieve this is to initialize the sem to 0, and only after printing the step1 (and then unlocking the V operation) can the step2 be executed.

By the same token, the lock blocking the step2 is not unlocked until the step3 is printed. You can understand it by looking at the code implementation.

# include "csapp.c" sem_t step1_done, step2_done;void* foo1 () {printf ("test1 is done\ n"); V (& step1_done); / / step1 is completed, then the foo2 block will be unblocked P (& step2_done); / / Test whether the step2 execution is complete, printf ("test3 is done\ n"); return NULL } void* foo2 () {P (& step1_done); printf ("test2 is done\ n"); V (& step2_done); / / step2 has been executed, unlock the lock return NULL;} int main () {pthread_t tid1, tid2; Sem_init (& step1_done, 0,0) of the print step / / the second parameter is 0: between threads, and the third parameter initializes with zero Sem_init (& step2_done, 0,0); Pthread_create (& tid1, NULL, foo1, NULL); Pthread_create (& tid2, NULL, foo2, NULL); / / ensures that the main thread exits after the thread has finished execution, otherwise the thread cannot execute Pthread_join (tid1, NULL). Pthread_join (tid2, NULL); exit (0);} 2, producer-consumer model

The main thing is to deal with semaphores in production and consumption functions.

Error example:

Void sbuf_insert (subf_t* sp, int item) {sem_wait (& sp- > mutex); sem_wait (& sp- > slots); / / put the project into buf sp- > buf [(+ + sp- > rear)% (sp- > n)] = item; sem_post (& sp- > items); sem_post (& sp- > mutex);} void sbuf_remove (sbuf_t* sp) {sem_wait (& sp- > mutex) Sem_wait (& sp- > items); / / do works sem_post (& sp- > slots); sem_post (& sp- > mutex);}

If we get the mutex first, it may cause a deadlock.

Suppose the buf is full now, and the producer gets the mutex, but he is block because he has no free time.

At this time, the consumer is also block because he can't get the mutex.

Other producers also do not have mutexes by block.

Solution:

It's relatively simple. Just change the order. It is equivalent to that we producers and consumers make it clear which grid I want to control and then take the mutex????.

# include # include # include typedef struct sbuf {memory opened on the int * buf; / * heap to store * / int n; / * cap of the buf*/ int front; / / the first item int rear; / / the last item sem_t mutex; / / acquire the lock sem_t slots of the critical section / / number of empty slots sem_t items; / / number of empty slots produced} subf_t;void sbuf_init (subf_t* sp, int n) {sp- > n = n; sp- > buf = static_cast (calloc (n, sizeof (int); sp- > front = 0; sp- > rear = 0; sem_init (& sp- > mutex, 0,1) Sem_init (& sp- > slots, 0, n); sem_init (& sp- > items, 0,0);} void sbuf_deinit (subf_t*sp) {free (sp- > buf);} void sbuf_insert (subf_t*sp, int item) {/ / first judge the semaphore slots, your producer likes sem_wait (& sp- > slots); sem_wait (& sp- > mutex) / / put items into buf sp- > buf [(+ + sp- > rear)% (sp- > n)] = item; / / CSAPP mentioned that the unlocking order is generally sem_post (& sp- > mutex); sem_post (& sp- > items);} int sbuf_remove (subf_t* sp) {int item; sem_wait (& sp- > items) / / which lattice product do I like sem_wait (& sp- > mutex); item = sp- > buf [(+ + sp- > front)% (sp- > n)]; sem_post (& sp- > mutex); sem_post (& sp- > slots); return item;} 3, read-write lock

The first kind of reader and writer problem (reader first)

The reader will not be asked to wait unless the current permission is the writer's.

In other words, the reader will not wait because there is a writer.

Achieve:

Semaphore: W maintains access to critical section, and mutex maintains access to the shared variable readcnt (the number of readers currently in the critical section)

Whenever the writer enters the critical area, he locks w and unlocks it when he leaves. It ensures that there can be at most one writer in the critical area at any time.

Only when the first reader locks W and the last one is released, then as long as there is one reader, any other reader can enter without barrier, which will also lead to the writer's hunger.

Int readcnt = 0semidet, mutex = 1, w = 1 × void reader () {while (1) {P (& mutex); readcnt++; if (readcnt = = 1) / / the first incoming reader P (& w); / / locked, the writer cannot write V (& mutex) / / unlock the protection lock for readcnt / * work in the critical area * / P (& mutex); readcnt--; if (readcnt = = 0) V (& w) / / Last reader, unlock the blocking writer's lock V (& mutex); / / unlock the readcnt protection lock}} void writer () {while (1) {P (& w) / * critical section work * / V (& w);} at this point, the study on "case analysis of C language concurrent programming model" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Development

Wechat

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

12
Report