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

using 2 semaphores and 1 mutex: (solve in simple c++ language for linux terminal and add...
using 2 semaphores and 1 mutex: (solve in simple c++ language for linux terminal and add comments please) The barber shop has one barber (a thread), one barber chair, and n chairs for waiting customers (semaphore), if any, to sit on. If there are no customers (each customer is a thread) present, the barber sits down in the barber chair and falls asleep. When a customer arrives, he has to wake up the sleeping barber. If additional customers arrive while...
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
Task 2: Random Number Generator (10 Pts) Random numbers are usually computed using a formula from...
Task 2: Random Number Generator (10 Pts) Random numbers are usually computed using a formula from discrete mathematics. This type of random number generator is called the linear congruential generator and it is described by the following formula Xn+1=(aXn+c)modm where a, c and m are parameters of the generator, with Xn being the previous random number, and Xn+1 being the next random number. Variable X0 is referred to as the seed. Note: this is integer arithmetic, not floating-point arithmetic. Term...
TASK: Using stack functions, write a program in C++ language that acts as a simple calculator,...
TASK: Using stack functions, write a program in C++ language that acts as a simple calculator, reading an infix algebraic expression with numbers and simple operations: +, -, *, / , (, and ). The program converts an infix expression into an equivalent postfix expression, and then evaluates the postfix expression, and then prints the result if input expression is correct otherwise prints error messages. Your program must interact with the user until the user quits.    REQUIREMENTS: - Your...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT