Lab 4 Directions
Write a C
#include <stdio.h>
#include <sys/time.h>
/* Return 1 if 'i'th bit of 'n' is 1; 0 otherwise */
#define EXTRACT_BIT(n,i) ((n&(1<<i))?1:0)
int check_circuit (int z) {
int v[16]; /* Each element is a bit of z */
int i;
for (i = 0; i < 16; i++) v[i] = EXTRACT_BIT(z,i);
if ((v[0] || v[1]) && (!v[1] || !v[3]) && (v[2] || v[3])
&& (!v[3] || !v[4]) && (v[4] || !v[5])
&& (v[5] || !v[6]) && (v[5] || v[6])
&& (v[6] || !v[15]) && (v[7] || !v[8])
&& (!v[7] || !v[13]) && (v[8] || v[9])
&& (v[8] || !v[9]) && (!v[9] || !v[10])
&& (v[9] || v[11]) && (v[10] || v[11])
&& (v[12] || v[13]) && (v[13] || !v[14])
&& (v[14] || v[15])) {
printf ("%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d%d\n",
v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7],v[8],v[9],
v[10],v[11],v[12],v[13],v[14],v[15]);
return 1;
} else return 0;
}
int main (int argc, char *argv[])
{
int count, i;
count = 0;
for (i = 0; i < 65536; i++)
count += check_circuit (i);
printf ("There are %d solutions\n", count);
return 0;
}
If a thread finds a combination that satisfies the circuit, it should print out the combination (like in the given sequential version), along with the thread id (a number between 0 and 5 (p-1)). In the end, the main thread should print out the total number of combinations that satisfy this circuit (like in the given sequential program). Mutex should be used to update the total by each thread. An example output of the program is shown below:
% threadcircuit
0) 0110111110011001
0) 1110111111011001
2) 1010111110011001
1) 1110111110011001
1) 1010111111011001
1) 0110111110111001
0) 1010111110111001
2) 0110111111011001
2) 1110111110111001
There are 9 solutions
The source file should have your name & Panther ID, a description and it should have the affirmation of originality included in a comment at the top.
Code should be nicely indented and commented.
Create a simple Makefile to compile your program into an executable called threadcircuit.
Take a screenshot of the screen showing the output on ocelot. Name it FirstNameLastName.png or .jpg.
Submit your source file, the screenshot and your Makefile in one zip file called FirstnameLastnameL4.zip to the assignment Submission Link in Canvas.
Trending nowThis is a popular solution!
Step by stepSolved in 4 steps
- Consider the following program: const int n=10; int s; //shared variable between two processes (threads) void P1( ) { int i=0; for (i=0;iarrow_forwardCan you help me with this code because i don't know what to do with this code, this code has to be in C. question that I need help with:You need to use the pthread for matrix multiplication. Each threadfrom the threadpool should be responsible for computing only a partof the multiplication (partial product as shown in the above picture –all Ti(S) are called a partical product). Your main thread should splitthe matrices accordingly and create the partial data arrays that areneeded to compute each Ti. You must create a unique task with thedata and submit it to the job queue. You can compute the partialproducts concurrently as long as you have threads available in thethreadpool. You have to remove the task the from queue and submitto a thread in the threadpool. You should define the number ofthreads to be 5 and keep it dynamic so that we can test the samecode with a higher or lower number of threads as needed. When allthe partial products are computed all the threads in the…arrow_forwardPlease can you help me with the code that I have contributed, as I played a role in its development. Please ensure that only my code is utilized. This code should be written in C, and I have provided a portion of it below. Question that I need help with: You need to use the pthread for matrix multiplication. Each thread from the threadpool should be responsible for computing only a partof the multiplication (partial product as shown in the above picture –all Ti(S) are called a partical product). Your main thread should splitthe matrices accordingly and create the partial data arrays that areneeded to compute each Ti. You must create a unique task with thedata and submit it to the job queue. You can compute the partialproducts concurrently as long as you have threads available in thethreadpool. You have to remove the task the from queue and submitto a thread in the threadpool. You should define the number ofthreads to be 5 and keep it dynamic so that we can test the samecode with a…arrow_forwardComplete the following code. The goal is to implement the producer-consumer problem. You are expected to extend the provided C code to synchronize the thread operations consumer() and producer() such that an underflow and overflow of the queue is prevented. You are not allowed to change the code for implementing the queue operations, that is the code between lines 25 and 126 as shown in the screenshot. You must complete the missing parts as shown in the screenshot as well as complete the missing codes of producer and consumer. #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <time.h> #include <pthread.h> #include <semaphore.h> #include <errno.h> #include <fcntl.h> #define MAX_LENGTH_CAP 100 #define INIT -127 #define UNDERFLOW (0x80 + 0x02) #define OVERFLOW 0x80 + 0x01 #define BADPTR (0x80 + 0x03) #define CONSUMER_TERMINATION_PROBABILITY 40 #define PRODUCER_TERMINATION_PROBABILITY 30 // ============= LOCKED…arrow_forwardUSE SIMPLE PYTHON CODE TO COMPLETE Basic version with two levels of threads (master and slaves) One master thread aggregates and sums the result of n slave-threads where each slavethread sums a different range of values in an array of 1000 random integers (please program to generate 1000 random integers to populate the array). The number of slave-threads is a parameter which the user can change. For example, if the user chooses 4 slave threads, each slave thread will sum 1000/4 = 250 numbers. If the user chooses 3 slave threads, the first two may each sum 333 numbers and the third slave threadsums the rest 334 numbers. 2) Advanced version with more than two levels of threadsThe master thread creates two slave-threads where each slave-thread is responsible to sum half segment of the array. Each slave thread will fork/spawn two new slave-threads where each new slave-threadsums half of the array segment received by its parent. Each slave thread will return the subtotal to its parent…arrow_forwardComplete the following code. The goal is to implement the producer-consumer problem. You are expected to extend the provided C code to synchronize the thread operations consumer() and producer() such that an underflow and overflow of the queue is prevented. You are not allowed to change the code for implementing the queue operations, that is the code between lines 25 and 126 as shown in the Figure below. You must complete the missing parts between lines 226-261 as shown in the screenshot.arrow_forwardModify this threading example to use, exclusively, multiprocessing, instead of threading. import threadingimport time class BankAccount(): def __init__(self, name, balance): self.name = name self.balance = balance def __str__(self): return self.name # These accounts are our shared resourcesaccount1 = BankAccount("account1", 100)account2 = BankAccount("account2", 0) class BankTransferThread(threading.Thread): def __init__(self, sender, receiver, amount): threading.Thread.__init__(self) self.sender = sender self.receiver = receiver self.amount = amount def run(self): sender_initial_balance = self.sender.balance sender_initial_balance -= self.amount # Inserting delay to allow switch between threads time.sleep(0.001) self.sender.balance = sender_initial_balance receiver_initial_balance = self.receiver.balance receiver_initial_balance += self.amount # Inserting delay to allow switch between threads time.sleep(0.001)…arrow_forwardMulti-tasking can not be achieved with a single processor machine. True False 2. Async/Await is best for network bound operations while multi-threading and parallel programming is best for CPU-bound operations. True False 3. The following is a characteristic of an async method:The name of an async method, by convention, ends with an "Async" suffix. True False 4. Using asynchronous code for network bound operations can speed up the time needed to contact the server and get the data back. True False 5. Asynchronous programming has been there for a long time but has tremendously been improved by the simplified approach of async programming in C# 5 through the introduction of: The Task class True Falsearrow_forwardCould you kindly utilize solely my code as I contributed to its development? I emphasize, please utilize only my code.I attached my code below label my code. The question that I need help with: You need to use the pthread for matrix multiplication. Each threadfrom the threadpool should be responsible for computing only a partof the multiplication (partial product as shown in the above picture –all Ti(S) are called a partical product). Your main thread should splitthe matrices accordingly and create the partial data arrays that areneeded to compute each Ti. You must create a unique task with thedata and submit it to the job queue. You can compute the partialproducts concurrently as long as you have threads available in thethreadpool. You have to remove the task the from queue and submitto a thread in the threadpool. You should define the number ofthreads to be 5 and keep it dynamic so that we can test the samecode with a higher or lower number of threads as needed. When allthe…arrow_forwardSubject Name: Advanced Object-Oriented Programming 1. Complete the below given code (1) class NyThread Thread t: Runnable ( MyThread (String n, int pl t (2) (3) : / creates teh Thread t with nane n : // changen the priority to p Syster.out.printin ("Thread "+n+* created with priority "+p): t. (4) : // exexcute the thread t. I // The only abatarct aethed in Runnable public for (int 1=10: 1>=1: 1--) { Syaten.out.prántin ("Thread *t.getName ()+"\e i - "a) : (5) try { (6) :/ ma ke the Thead t eleep to 2 seconds } catch (Exception e) Syater.out.printin ("Exception in Child"): System.out.printin ("Thread "+t. getName ()+" 1s Exiting"): public statie void main (Stringt) arga) { MyThread t1, 12; ti - new MyThread ("R1", 7): t2 = new MyThread ("R1", 3): try ( :// 2 atatements to aake the Threads ti a v2 exit at same time (8) ) catoh (Exception e) ( Syster.out.printin("Exception in main"): System.out.printin ("Thread Main is Exiting"): 1. 2. 3. 4. 6. 7. 8. 5.arrow_forwardJAVA You are requested to implement a “message buffer and response connector” class using Javaprogramming language. The following figure depicts a message buffer and response connectorclass, followed by detailed operations specifications. The message buffer and response connectorclass has three operations, send(), receive(), and reply().Your program should create two separate threads for testing your implementation, a producerthread, and a consumer thread. The producer thread sends a message having a structure (string,integer) – e.g., (add, 3) or (multiply, 7) - to a consumer thread via a message buffer and responseconnector. The consumer thread encapsulates a SimpleCalculation class that has two operations,add() and “multiply(). You should implement the SimpleCalculation class as well. When theconsumer thread receives a message from the connector, it extracts the message and then callsone of the operations on the SimpleCalculation class, depending on the message. For example,the…arrow_forwardarrow_back_iosarrow_forward_ios
- Computer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Concepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningPrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY