Question

In: Computer Science

Task 2 [10 pts] Implementing Synchronization of Chat Client Threads Using Semaphores In c language In...

Task 2 [10 pts] Implementing Synchronization of Chat Client Threads Using Semaphores

In c language

In this task, a character buffer should be created (e.g., char buf[500]) in the server which is shared among threads that are created to handle communication with clients. Specifically, when the server receives a message (a string) from a client, the server should (1) store the message in the buffer, (2) broadcast the message to all connected clients, and (3) clear the buffer. Make sure that this process (1)~(3) should be done in a synchronized manner. In other words, when the server is accessing the shared buffer to broadcast the message in the buffer, other threads should wait until the current thread that is accessing the shared buffer finishes this process using a semaphore.

In evaluating your work, we will look at your source code to see if semaphores are used appropriately to synchronize the threads of the server.

Solutions

Expert Solution

Considering Following Semaphores:
Counting Semaphores - 'Full' = 0 (number of filled slots of buffer)
'Empty' = 500 (number of empty slots of buffer)
Binary Semaphore - 'S' = 1

Consider Following Variable Declarations:
int in = 0; <-- Stores the index value where the next incoming message have to be stores in buffer
int out = 0; <-- Stores the index value from where the message have to be broadcasted to other clients.

Following shall be the pseudo code to understand the use of semaphores in the given scenario

Client Function:
client(string msg) {
down(empty); or wait(empty);
down(S); or wait(S);

// Critical Section starts
buff[in] = msg;
in = (in + 1) % 500;

// Critical Section ends
  
up(S); or signal(S);
up(full); or signal(full);
}


Server Function:
server () {
down(full); or wait(full);
down(S); or wait(S);

// Critical Section starts
msg_to_broadcast = buff[in];
out = (out + 1) % 500;

// Critical Section ends
  
up(S); or signal(S);
up(empty); or signal(empty);
}


Related Solutions

The project will study the coordination of multiple threads using semaphores. The design should consist of...
The project will study the coordination of multiple threads using semaphores. The design should consist of two things: (1) a list of every semaphore, its purpose, and its initial value, and (2) pseudocode for each function. Code Your code should be nicely formatted with plenty of comments. The code should be easy to read, properly indented, employ good naming standards, good structure, and should correctly implement the design. Your code should match your pseudocode. Project Language/Platform This project must target...
Task 4 [10 pts] Broadcasting Messages to All Clients (Shared Variable Among Threads) Modify the server...
Task 4 [10 pts] Broadcasting Messages to All Clients (Shared Variable Among Threads) Modify the server code so that when the server sends a message, that message is sent to all connected clients #include<stdio.h> #include<string.h> //strlen #include<stdlib.h> //strlen #include<sys/socket.h> #include<arpa/inet.h> //inet_addr #include<unistd.h> //write #include<pthread.h> //for threading , link with lpthread #define MAX 80 #define PORT 6543 #define SA struct sockaddr // Function designed for chat between client and server. void join( pthread_t *thread_id) { if(!pthread_join( *thread_id , NULL)) printf("thread %ld...
what will be the code in C programming for the client and server chat application for...
what will be the code in C programming for the client and server chat application for the below issue :- write the C Programming code that able client have a unique ID to be known by the server
Develop a program using Threads in C/C++ to estimate the value of PI using the Monte...
Develop a program using Threads in C/C++ to estimate the value of PI using the Monte Carlo method use: C/C++ #include srand((unsigned)(myid)); x = ((double)rand()) / ((double)RAND_MAX); y = ((double)rand()) / ((double)RAND_MAX); Your program will allow the user to specify the number of threads (range 1 to 10) and the total number of data points (range 10 to 1,000,000) used for the Monte Carlo simulation on the command line. Note, DO NOT assume the number of data points is always...
Develop a program using Threads in C/C++ to estimate the value of PI using the Monte...
Develop a program using Threads in C/C++ to estimate the value of PI using the Monte Carlo method use: C/C++ #include srand((unsigned)(myid)); x = ((double)rand()) / ((double)RAND_MAX); y = ((double)rand()) / ((double)RAND_MAX); Your program will allow the user to specify the number of threads (range 1 to 10) and the total number of data points (range 10 to 1,000,000) used for the Monte Carlo simulation on the command line. Note, DO NOT assume the number of data points is always...
IN C LANGUAGE: Write a multi-threaded Linux program that synchronizes it's threads to write to a...
IN C LANGUAGE: Write a multi-threaded Linux program that synchronizes it's threads to write to a file without the file becoming corrupted. To do this, your program will create three threads which write strings to the same file. Each thread will randomly write a selection of strings to the file at random intervals. When finished, the file will contain all the strings written correctly to the file. You may use mutexes, semaphores, or a monitor your write on your own....
(20 pts) Using your programming language of choice (from C++, Java, or Python) , also drawing...
(20 pts) Using your programming language of choice (from C++, Java, or Python) , also drawing on your experience from program 1, read an integer, n from keyboard (standard input). This integer represents the number of integers under consideration. After reading that initial integer in, read n integers in, and print the minimum and maximum of all the integers that are read in. Example: Input Output 7 1 5 3 6 9 22 2 Min: 1 Max: 22 C++ preferred
Using C Language Write a program segment that computes 1 + 2 + 3 + ......
Using C Language Write a program segment that computes 1 + 2 + 3 + ... + ( n - 1) + n , where n is a data value. Follow the loop body with an if statement that compares this value to (n * (n + 1)) / 2 and displays a message that indicates whether the values are the same or different. Please give me code to just copy and paste
In C programming: Using mutexes, semaphores, and spinlocks, you can sequence memory operations to prevent race...
In C programming: Using mutexes, semaphores, and spinlocks, you can sequence memory operations to prevent race conditions. It might make some sense to just chuck spinlocks out the window and use mutexes. Explain why mutexes would tend to relieve you of some of the problems inherent in spinlocks. Explain a situation in which those benefits might not be worth your time and you’d go with the spinlock anyway.
Answer all definitions. a) [2 pts] Environmental economics b) [2 pts] performance-based standard c) [2 pts]...
Answer all definitions. a) [2 pts] Environmental economics b) [2 pts] performance-based standard c) [2 pts] hedonic price method d) [2 pts] Market Failures e) [2 pts] contingent valuation method
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT