Question

In: Computer Science

The purpose of this project is to practice the pthread built in functions. The following c...

The purpose of this project is to practice the pthread built in functions.

The following c program is a simple program to make a matrix of integers and print it.

//File name: a.c

#include <stdio.h>

#include <time.h>

#include <stdlib.h>

int** a;

int main(){

time_t t;

int m, n, i, j;       //m is the numbers of rows and n is the number of columns.

printf("Enter the number of rows, and columns: ");

scanf("%d%d", &m, &n);

printf("%d, %d\n", m, n);

srand((unsigned) time(&t));

a=(int**) malloc(m*sizeof(int*));

for(j = 0; j < n; j++)

    a[j] = (int*) malloc(n * sizeof(int*));

for(i = 0; i < m; i++)

    for(j = 0; j < n; j++)

      a[i][j] = rand() % 1000;

for(i = 0; i < m; i++){

    for(j = 0; j < n; j++)

      printf("%d,", a[i][j]);

    printf("\n");

}

return 0;

}

Your project uses pthread built-in functions based on the following conditions:

1. The program reads from the console the number of rows and the number of columns (like the above program). Therefore, the matrix has m rows and n columns.

2. The program creates m threads.

3. Each thread assigns random numbers to one row of the matrix.

4. The function main, sorts each row.

5. Each thread displays its sorted row.

6. The function: main displays the entire matrix.

Answer:

Solutions

Expert Solution

To compile

gcc a.c -pthread

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include<pthread.h>
#include<unistd.h>
int** a;


// a structure for passing argument in thread
typedef struct {
    int row;
    int col;
}args;
int cmpfunc (const void * a, const void * b) {
   return ( *(int*)a - *(int*)b );
}
void* fun(void * arg){
    args* temp = (args *)arg;
    qsort(a[temp->row],temp->col,sizeof(int),cmpfunc);
    for(int i=0;i<temp->col;i++)printf("%d,",a[temp->row][i]);
    printf("\n");
    return NULL;

}


int main(){
    time_t t;
    int m, n, i, j;       //m is the numbers of rows and n is the number of columns.
    printf("Enter the number of rows, and columns: ");
    scanf("%d%d", &m, &n);
    printf("%d, %d\n", m, n);
    srand((unsigned) time(&t));
    a=(int**) malloc(m*sizeof(int*));
    for(j = 0; j < n; j++)
        a[j] = (int*) malloc(n * sizeof(int*));
    for(i = 0; i < m; i++)
        for(j = 0; j < n; j++)
          a[i][j] = rand() % 1000;
    pthread_t tid[m];
    for(int i=0;i<m;i++){
        args arg ;
        arg.row = i;
        arg.col = n;
        pthread_create(&tid[i],NULL,fun,(void *)&arg);
        usleep(1000);  // applied micro sleep so that thread returns correctly
    }
    for(int i=0;i<m;i++){
        pthread_join(tid[i],NULL);
    }
    printf("\n");
    printf("\n");
    printf("By main thread\n");
    printf("\n");
    for(i = 0; i < m; i++){
        for(j = 0; j < n; j++)
          printf("%d,", a[i][j]);
        printf("\n");
    }
    return 0;
}


Related Solutions

The purpose of this question is to practice the pthread built in functions. The following c...
The purpose of this question is to practice the pthread built in functions. The following c program is a simple program to make a matrix of integers and print it. //File name: a.c #include <stdio.h> #include <time.h> #include <stdlib.h> int** a; int main(){ time_t t; int m, n, i, j;       //m is the numbers of rows and n is the number of columns. printf("Enter the number of rows, and columns: "); scanf("%d%d", &m, &n); printf("%d, %d\n", m, n); srand((unsigned) time(&t));...
Project 1 - Arrays - Grader The purpose of this assignment is to practice dealing with...
Project 1 - Arrays - Grader The purpose of this assignment is to practice dealing with arrays and array parameters. Arrays are neither classes nor objects. As a result, they have their own way of being passed as a parameter and they also do not support the dot operator ( .), so you cannot determine how full they are. This results in some pain and suffering when coding with arrays. It is this awareness that I am trying to get...
Project Assignment The purpose of this assignment is for you to gain practice in applying the...
Project Assignment The purpose of this assignment is for you to gain practice in applying the concepts and techniques we learned in class. In order to complete the assignment, please do the following: 1. find or create a data set containing values of at least one interval or ratio variable for at least one-hundred cases (n >= 100); 1 2. provide basic descriptive statistics to summarize the central tendency and variability of the data; 3. provide at least one table...
The purpose of this C++ programming assignment is to practice using an array. This problem is...
The purpose of this C++ programming assignment is to practice using an array. This problem is selected from the online contest problem archive, which is used mostly by college students worldwide to challenge their programming ability and to prepare themselves for attending programming contests such as the prestige ACM International Collegiate Programming Contest. For your convenience, I copied the description of the problem below with my note on the I/O and a sample executable. Background The world-known gangster Vito Deadstone...
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,...
The goal of this project is to practice (Write a C Program) with a function that...
The goal of this project is to practice (Write a C Program) with a function that one of its parameter is a function.The prototype of this function is: void func ( float (*f)(float*, int), float* a, int length); This means the function: func has three parameters: float (*f)(float*, int): This parameter itself is a function: f that has two parameters and returns a floating-point number. In the body of the function: func we call the function: f with its arguments...
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...
Serial.flush() and Delay() are the two built-in functions. Place them in the following code to remove...
Serial.flush() and Delay() are the two built-in functions. Place them in the following code to remove the garbage data printing as discussed in class. Code: char data; void setup() { // put your setup code here, to run once: Serial.begin(9600); } void loop() { // put your main code here, to run repeatedly: while(Serial.available()==NULL){ data = Serial.read(); Serial.print("given character is: "); Serial.println(data); } Serial.end(); }
The Programming Language is C++ Objective: The purpose of this project is to expose you to:...
The Programming Language is C++ Objective: The purpose of this project is to expose you to: One-dimensional parallel arrays, input/output, Manipulating summation, maintenance of array elements. In addition, defining an array type and passing arrays and array elements to functions. Problem Specification: Using the structured chart below, write a program to keep records and print statistical analysis for a class of students. There are three quizzes for each student during the term. Each student is identified by a four-digit student...
For several decades, it was a common practice in Southern California for houses to be built...
For several decades, it was a common practice in Southern California for houses to be built with pools in the backyard (as any airplane flight which ends at a Southern California airport will reveal). Now, however, that practice may be changing, possibly because of the recent demand for landscaped homes, which experts believe help reduce pollution. A recent study examined a random sample of 161 houses built in Southern California between 1950 and 1985 and an independent, random sample of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT