Question

In: Computer Science

(using C in a Linux environment and the POSIX pthreads library) Write a program named Sort.c...

(using C in a Linux environment and the POSIX pthreads library)

Write a program named Sort.c that accepts a list of numbers from the user. There are two threads: Main thread waits for the numbers to be entered by the user and for the second thread to finish sorting. It then asks user whether there are more lists or not. It will do this until the user indicates there are no more lists to be entered, at which point it will wait for the second thread to be finished sorting and then exits the program. Sorting and display thread waits for the user to be finished inputting the numbers from the list, and then will sort and print the numbers. It will do this each time the user enters the number (depending on if the main thread is told to continue accepting more numbers from the user).

*Edit* I'm not sure how much more clear it can be, as last "Expert" could not answer.

A C-program with two threads:

First thread asks user to enter 5 numbers and stores them in an array.

Second thread sorts the 5 numbers (the first thread at this point is "waiting" for the second thread to finish) and then prints the 5 numbers in order from least to greatest. Then it asks the user if they have more numbers to enter.

If yes, thread one is ran (asking the user for the 5 numbers), saves the 5 numbers in an array. Then the first thread again waits while the second thread sorts and prints the numbers in order. This loop continues until the user no longer wishes to enter a list of numbers.

Solutions

Expert Solution

#include <stdio.h>
#include <pthread.h>

void *sort(void *args) {
int *nums=(int*)args;
int i,j;
for(i=0;i<5;i++) {
int minIndex=i;
for(j=i+1;j<5;j++) {
if(nums[j]<nums[minIndex]) {
minIndex=j;
}
}
int temp=nums[i];
nums[i]=nums[minIndex];
nums[minIndex]=temp;
}
}

int main() {
int nums[5];
int choice=1;
int i;
do {
printf("Enter 5 numbers to sort: ");
for(i=0;i<5;i++) {
scanf("%d", &nums[i]);
}
pthread_t thread_id;
pthread_create(&thread_id, NULL, sort, nums);
pthread_join(thread_id, NULL);
printf("Sorted numbers are: ");
for(i=0;i<5;i++) {
printf("%d ", nums[i]);
}
printf("\n");
printf("Do you wish to enter more numbers? Enter 1 for yes, 0 for no: ");
scanf("%d", &choice);
} while(choice==1);
return 0;
}


Related Solutions

please write in c using linux or unix Write a program that will simulate non -...
please write in c using linux or unix Write a program that will simulate non - preemptive process scheduling algorithm: First Come – First Serve Your program should input the information necessary for the calculation of average turnaround time including: Time required for a job execution; Arrival time; The output of the program should include: starting and terminating time for each job, turnaround time for each job, average turnaround time. Step 1: generate the input data (totally 10 jobs) and...
Please write in C using linux or unix. Write a program that will simulate non -...
Please write in C using linux or unix. Write a program that will simulate non - preemptive process scheduling algorithm: First Come – First Serve Your program should input the information necessary for the calculation of average turnaround time including: Time required for a job execution; Arrival time; The output of the program should include: starting and terminating time for each job, turnaround time for each job, average turnaround time. Step 1: generate the input data (totally 10 jobs) and...
Use any C program without using PTHREADS calculate time taken to execute program. Write same program...
Use any C program without using PTHREADS calculate time taken to execute program. Write same program of question 1 using PTHREADS. Calculate time taken to execute program.(1 mark) Identify data races in above program and explain why this situation occurred with an example (1 mark) Rewrite the code to avoid data races should use any of the THREE techniques.(1.5 marks) please I need the c code.. critical section mutex solution semaphore functions Barriers Read-Write Locks Run program using 1, 2,...
Write a shell program named HELLO2(this should be done in linux, using bash) Consider files in...
Write a shell program named HELLO2(this should be done in linux, using bash) Consider files in the current directory whose names end in 't' or 'z'. For such files only, your program should list certain LINES of the file(s). These lines are those that have at least 4 x's somewhere in the line. Note that the x's may be upper- or lower-case, and may be separated by other characters; so the following 3 lines DO match: XXxX and more things...
Write a Pthreads program 'Dining-Philosophers.c' to implement the classical 'Dining-Philosophers'problem by using Pthreads mutex locks and...
Write a Pthreads program 'Dining-Philosophers.c' to implement the classical 'Dining-Philosophers'problem by using Pthreads mutex locks and condition variables. To implement the program, please followthe hints below. (a) The detailed 'Dining-Philosophers' problem statement: Refer to Page 71 ~ Page 72 in 'Lecture-08.pptx'    (b) The introduction to Pthreads mutex locks and condition variables: Refer to Page 17 ~ Page 39 in'Lecture-08.pptx'    (c) Creating five philosophers, each of which will run as a separate thread. Philosophers alternatebetween thinking and eating: The activities...
Programming Language Required: C Write a multithreaded program in C (not c++) using the pthread library...
Programming Language Required: C Write a multithreaded program in C (not c++) using the pthread library and dynamic memory(malloc) that multiplies two matrices together. The numbers in the matrices must be read in from a text file. The program should also check if the two matrices are capable of being multiplied together. The amount of threads used has to be dynamic. The user should be able to choose how many threads they wish to use using the command line. Finally,...
Matrix Multiplication with Threads - C/C++ In this assignment you will use the Pthreads library to...
Matrix Multiplication with Threads - C/C++ In this assignment you will use the Pthreads library to write a program that multiplies two square arrays and compare the difference between the imperative and parallel implementations of this algorithm. Use the matrix mulltiplication algorithm. Write a program that contains three functions: (1) A function that has an integer as a parameter and returns a pointer to square array of integers (i.e. both dimensions should be equal). The function should allocate storage from...
using C# Write a program named RectangleArea tocalculate the area of a rectangle with a...
using C# Write a program named RectangleArea to calculate the area of a rectangle with a length of 15.8 and a width of 7.9. The program should have two classes, one is theRectangleArea holding the Main(), and the other one is Rectangle. The Rectangleclass has three members: a property of length, a property of width, and a method with a name of CalArea() to calculate the area. You can invoke the auto-provided constructor or write a self-defined constructor to initialize...
Write a C++ program (using the pthread library) that accepts a phrase of unspecified length on...
Write a C++ program (using the pthread library) that accepts a phrase of unspecified length on the command line. For example: prompt$: ./vowcon Operating Systems Class at CSUN The main() in this program should read the phrase from the terminal. This phrase can be read into a global variable. This phrase or its parts can be directly accessed from the main() and from the threads. The main() has to create two threads running functions (vow and con). The main() can...
USING C++ PLEASE Write a program named Lab11C that calculates the average of a group of...
USING C++ PLEASE Write a program named Lab11C that calculates the average of a group of test scores where the lowest score is dropped. It should use the following functions a) void getScores should have 4 double reference parameters (one for each test score) getScores should do the following: - read 4 test scores from the text file Lab11C.txt (Important: declare your ifstream infile and open the file inside this function, not in the main function) - store them in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT