































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The concepts of synchronization in computer systems, focusing on the producer-consumer and readers-writers problems, thread safety, races, and deadlocks. It also delves into the use of semaphores to schedule access to shared resources and the implementation of a shared buffer package using semaphores. Examples of thread-unsafe functions and deadlock avoidance strategies are provided.
Typology: Slides
1 / 39
This page cannot be seen from the preview
Don't miss anything!
































th
#include ācsapp.hā #define NITERS 5 void *producer(void *arg); void *consumer(void arg); struct { int buf; / shared var / sem_t full; / sems */ sem_t empty; } shared; int main() { pthread_t tid_producer; pthread_t tid_consumer; _/ Initialize the semaphores /_ Sem_init(&shared.empty, 0, 1); Sem_init(&shared.full, 0, 0); _/ Create threads and wait /_ Pthread_create(&tid_producer, NULL, producer, NULL); Pthread_create(&tid_consumer, NULL, consumer, NULL); Pthread_join(tid_producer, NULL); Pthread_join(tid_consumer, NULL); exit(0); }
/* Insert item onto the rear of shared buffer sp */ void sbuf_insert(sbuf_t sp, int item) { P(&sp->slots); / Wait for available slot / P(&sp->mutex); / Lock the buffer / sp->buf[(++sp->rear)%(sp->n)] = item; / Insert the item / V(&sp->mutex); / Unlock the buffer / V(&sp->items); / Announce available item */ }
/* Remove and return the first item from buffer sp */ int sbuf_remove(sbuf_t sp) { int item; P(&sp->items); / Wait for available item / P(&sp->mutex); / Lock the buffer / item = sp->buf[(++sp->front)%(sp->n)]; / Remove the item / V(&sp->mutex); / Unlock the buffer / V(&sp->slots); / Announce available slot */ return item; }
connec5ons Insert descriptors (^) Remove descriptors
Service client Service client
sbuf_t sbuf; /* Shared buffer of connected descriptors */ int main(int argc, char *argv) { int i, listenfd, connfd, port; socklen_t clientlen=sizeof(struct sockaddr_in); struct sockaddr_in clientaddr; pthread_t tid; port = atoi(argv[1]); sbuf_init(&sbuf, SBUFSIZE); listenfd = Open_listenfd(port); for (i = 0; i < NTHREADS; i++) / Create worker threads */ Pthread_create(&tid, NULL, thread, NULL); while (1) { connfd = Accept(listenfd, (SA ) &clientaddr, &clientlen); sbuf_insert(&sbuf, connfd); / Insert connfd in buffer */ } }
static int byte_cnt; /* Byte counter / static sem_t mutex; / and the mutex that protects it */ static void init_echo_cnt(void) { Sem_init(&mutex, 0, 1); byte_cnt = 0; }
void echo_cnt(int connfd) { int n; char buf[MAXLINE]; rio_t rio; static pthread_once_t once = PTHREAD_ONCE_INIT; Pthread_once(&once, init_echo_cnt); Rio_readinitb(&rio, connfd); while((n = Rio_readlineb(&rio, buf, MAXLINE)) != 0) { P(&mutex); byte_cnt += n; printf("thread %d received %d (%d total) bytes on fd %d\nā, (int) pthread_self(), n, byte_cnt, connfd); V(&mutex); Rio_writen(connfd, buf, n); } }