Question

In: Computer Science

Please also write one of your own C programming with for loop which will be assigned...

Please also write one of your own C programming with for loop which will be assigned to multiple threads. Using dynamic scheduling.

Solutions

Expert Solution

The following code uses while loop to print 1 2 3 1 2 3 1 2 3 1 2 3 ...... infinitely using multi-threading. Thumbs up if you find the code helpful :)

CODE:

#include <stdio.h>
#include <pthread.h>
  
// Declaration of thread condition variables
pthread_cond_t cond1 = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond2 = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond3 = PTHREAD_COND_INITIALIZER;
  
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;

int done = 1;

void *f(void *n)
{
while(1) {
pthread_mutex_lock(&lock);

if (done != (int)*(int*)n) {
if ((int)*(int*)n == 1) {
pthread_cond_wait(&cond1, &lock);
} else if ((int)*(int*)n == 2) {
pthread_cond_wait(&cond2, &lock);
}
else {
pthread_cond_wait(&cond3, &lock);
}

}
printf("%d ", *(int*)n);
if (done == 3) {
done = 1;
pthread_cond_signal(&cond1);
}
else if(done == 1) {
done = 2;
pthread_cond_signal(&cond2);
} else if (done == 2) {
done = 3;
pthread_cond_signal(&cond3);
}
pthread_mutex_unlock(&lock);
}
return NULL;
}
int main()
{
pthread_t t1, t2, t3;
int n1 = 1, n2 = 2, n3 = 3;
  
// Creating 3 thread
pthread_create(&t1, NULL, f, (void *)&n1);
pthread_create(&t2, NULL, f, (void *)&n2);
pthread_create(&t3, NULL, f, (void *)&n3);
  
// infinite loop to avoid exit of program
for(;;);
return 0;
}

OUTPUT:


Related Solutions

Please use C programming to write the code to solve the following problem. Also, please use...
Please use C programming to write the code to solve the following problem. Also, please use the instructions, functions, syntax and any other required part of the problem. Thanks in advance. Use these functions below especially: void inputStringFromUser(char *prompt, char *s, int arraySize); void songNameDuplicate(char *songName); void songNameFound(char *songName); void songNameNotFound(char *songName); void songNameDeleted(char *songName); void artistFound(char *artist); void artistNotFound(char *artist); void printMusicLibraryEmpty(void); void printMusicLibraryTitle(void); const int MAX_LENGTH = 1024; You will write a program that maintains information about your...
Write in Python. come up with a programming task for which you will write your own...
Write in Python. come up with a programming task for which you will write your own program. It should be a purposeful and non-trivial program, bigger than any programming assignment. But it should not be something too ambitious that you may not end up finishing by the deadline. The program must not be something available on the Internet or in a book. The program must satisfy the following five requirements: It must define and appropriately use at least one class....
Loop Introduction Assignment Please write a program in c# Using the conditions below, write one program...
Loop Introduction Assignment Please write a program in c# Using the conditions below, write one program that calculates a person’s BMI. Your main() function will call functions 1, 2, and 3. Your program will contain three functions: Function #1: Will ask the user for their weight in pounds and their height in inches.   Your function will convert the weight and height into Body Mass Index (BMI). The formula for converting weight into BMI is as follows: BMI = Weight *...
Using C programming make one for loop Description For this problem you will be figuring out...
Using C programming make one for loop Description For this problem you will be figuring out if it is more beneficial to pay off your loans first before investing or if you should only make the minimum payments and invest the rest. Some things to pay attention to Interest rates given are annual interests rates but we will be assuming that interest is compounded monthly so the real rates to use will be 1/12 of what we are given We...
A)Write a C++ program using a while loop for Programming Exercise 5.1 on p. 193. Turn...
A)Write a C++ program using a while loop for Programming Exercise 5.1 on p. 193. Turn in a printout of the program and a printout of the results. Test the program for the two test cases in the book along with a third test case that includes 10 valid numbers (including some negative and some positive).
Also please add comments on the code and complete in C and also please use your...
Also please add comments on the code and complete in C and also please use your last name as key. The primary objective of this project is to increase your understanding of the fundamental implementation of Vigenere Cipher based program to encrypt any given message based on the Vignere algorithm. Your last name must be used as the cipher key. You also have to skip the space between the words, while replicating the key to cover the entire message. Test...
Write a program that uses loops (both the for-loop and the while loop). This assignment also...
Write a program that uses loops (both the for-loop and the while loop). This assignment also uses a simple array as a collection data structure (give you some exposure to the concept of data structure and the knowledge of array in Java). In this assignment, you are asked to construct an application that will store and retrieve data. The sequence of data retrieval relative to the input is Last In First Out (LIFO). Obviously, the best data structure that can...
All in C++ programming language 1. a.) convert for loop to while loop example b.) convert...
All in C++ programming language 1. a.) convert for loop to while loop example b.) convert while loop to for loop example 2.) pass one dimension array(and its size) to function example
Write in C programming using if and else statements only please!!! Write a program that plays...
Write in C programming using if and else statements only please!!! Write a program that plays the following card game: The user starts out with a pot of $100. At each hand of the game, the dealer and the player are dealt a random number between 1 and 52. The player wins $20 if his/her number is greater than the dealer's number; otherwise they lose $20.
CODE MUST BE IN C++ (please use for loop) write a program that loops a number...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number from 1 to 10 thousand and keeps updating a count variable (count variable starts at 0 ) according to these rules: n1 = 14 n2 = 54 n3 = 123 if the number is divisible by n1, increase count by 1 if the number is divisible by n2, increase count by 2 if the number is divisible by n3, increase count by 3 if...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT