Question

Assume that there are 4 Processors and a total of 20 processes.

Assume that there are 4 Processors and a total of 20 processes. Those processes should be distributed across those Processors evenly(5 process for each Processor) and they should be run until they finish.

The important thing is not to run just a single process until the process finishes, but inside of each processor run the processes concurrently for 1 Unit Time(UT). This means that we run a process for 1 UT then move on with the next process and run that one for 1 UT and move on with the next process...

The Process_Ets integer array should consist of 20 proccesses. Each element of the processor structure array have 5 processes(proc_ETs) waiting to be completed and an integer number which shows the processor(proc_id) that process belongs to. You should form a loop so that you run those 5 processes that are assigned to a Processor, so you will be using 4 different threads(excluding main) for 4 different Processors. To sum up, after running a process for 1 UT then you should move on with the next process until all processes complete their Execution Times(ET).


Ekran Alıntısı.PNG


#define total_processes 20

#define processor_count 4

struct processor{

int proc_ETs[5];

int proc_id;

};

void* process (void *args){

}

int main(void){

}

Assigning Execution Time values :

for(int i = 0; i < 20; i++){

process_ETs[i] = (i % 4)+1; }


SAMPLE RUN:

resim_2021-05-01_224554.png

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <pthread.h>

#include <stdlib.h>

#include<semaphore.h>

#include <stdio.h>


#include <unistd.h>

#include <sys/types.h>

#include <sys/wait.h>

#include<time.h>




#define total_processes 20

#define processor_count 4


pthread_mutex_t mutex;


struct processor

{

    

    int proc_ETs[5];

    int proc_id;

};














void *process(void *P)

{   

    struct processor temp = *((struct processor*)P);

    

    for(int i=0;i<5;i++){ 

        

        

        int remain = temp.proc_ETs[i];

        while(remain>=0){

            // mutex lock on cout out so that any other thread can execute this part

            

            pthread_mutex_lock(&mutex);

            printf("Process %d from Processor %d has Remaining %d ET.\n", (i+1),(temp.proc_id),remain);

            

            pthread_mutex_unlock(&mutex);

            

            if(remain)

                sleep(1);

            remain--;

        }

        

        

        

        

        

        

    }

    printf("Processor %d has completed all assigned processes\n",(temp.proc_id) );

    

}



int main()

{   

    srand(time(0));

    struct processor P[processor_count];

    pthread_t pro[processor_count];

    

    pthread_mutex_init(&mutex, NULL);

    

    

    

    //assigning execution times to the processes

    for (int i=0;i< processor_count;i++)

        {

            P[i].proc_id = i+1;

            for (int j=0;j<5;j++)

                P[i].proc_ETs[j] = ((i*5+j)%4)+1;

            

        }

    

    

    

    

    for(int i = 0; i < processor_count; i++) 

        {

            pthread_create(&pro[i], NULL, process, (void *)&P[i]);

        }

    

    

    

    for(int i = 0; i < processor_count; i++) {

        pthread_join(pro[i], NULL);

    }

    

    

    pthread_mutex_destroy(&mutex);

    

    return 0;

    

}


answered by: codegates
Add a comment
Know the answer?
Add Answer to:
Assume that there are 4 Processors and a total of 20 processes.
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Design and implement a C Language program that measures the performance of given processors. There are...

    Design and implement a C Language program that measures the performance of given processors. There are several metrics that measure the performance of a processor. We will be using the following 3 measures: 1.CPI (clock cycles per instruction) = #clock cycles /#instructions 2.CPU execution time = #instructions x CPI x clock cycle time . cylce time = 1/CPU clock rate in hertz units 3.MIPS (mega instructions per second)= #instrucrions/ CPU X 1000000 Typically, processors’ performance is measured using a wide...

  • Please give a output Linux/Ubuntu and how to run it (compile) this program ,my assigment is below...

    Please give a output Linux/Ubuntu and how to run it (compile) this program ,my assigment is below : : Merge Sort algorithm using 2 processes a.) Define an integer array of 100 integers. Populate this array with random numbers. You can use int rand(void); function. Do not forget to initialize this function. You will sort the numbers in the array using merge-sort algorithm. In merge sort algorithm the half of the array will be sorted by one process and second...

  • Class: Assembly Language x 86 Processors I need help reviewing the following topics: 1.) Define a...

    Class: Assembly Language x 86 Processors I need help reviewing the following topics: 1.) Define a bit, byte, and a nibble. 2.) Define a program, a register, Cache Memory. 3.) What is the meaning of backward compatibility? 4.) List & define x89 processor Modes 5.) What is the processor's operation (Instruction Execution) Cycle ( I am most confused on this topic) 6.) List the processor's Basic registers.

  • How can I convert the following C code to MIPS Assembly? +++++++++++++++++++++++++++++++++ MIPS main program ++++++++++++++++++++++++++++++++...

    How can I convert the following C code to MIPS Assembly? +++++++++++++++++++++++++++++++++ MIPS main program ++++++++++++++++++++++++++++++++ .data # Defines variable section of an assembly routine. array: .word x, x, x, x, x, x, x, x, x, x # Define a variable named array as a word (integer) array # with 10 unsorted integer numbers of your own. # After your program has run, the integers in this array # should be sorted. .text # Defines the start of the code...

  • Please I am in a hurry, I need an answer asap. I have seen an answer previously regarding to this...

    Please I am in a hurry, I need an answer asap. I have seen an answer previously regarding to this question, however I found a lot of mistakes on it, where the guy declared a public class, which is not a thing in C language. Furthermore it is important to divide the question into two C files, one for part a and one for part b. hw2 Merge Sort algorithm using 2 processes a.) Define an integer array of 100...

  • #include <stdio.h> // Define other functions here to process the filled array: average, max, min, sort,...

    #include <stdio.h> // Define other functions here to process the filled array: average, max, min, sort, search // Define computeAvg here // Define findMax here // Define findMin here // Define selectionSort here ( copy from zyBooks 11.6.1 ) // Define binarySearch here int main(void) { // Declare variables FILE* inFile = NULL; // File pointer int singleNum; // Data value read from file int valuesRead; // Number of data values read in by fscanf int counter=0; // Counter of...

  • PLEASE HELP!!! C PROGRAMMING CODE!!! Please solve these functions using the prototypes provided. At the end...

    PLEASE HELP!!! C PROGRAMMING CODE!!! Please solve these functions using the prototypes provided. At the end please include a main function that tests the functions that will go into a separate driver file. Prototypes int R_get_int (void); int R_pow void R Jarvis int start); void R_fill_array(int arrayll, int len); void R_prt_array (int arrayl, int len) void R-copy-back (int from[], int to [], int len); int R_count_num (int num, int arrayll, int len) (int base, int ex) 3. Build and run...

  • Step 4: Write a Sum Function Since we can write, compile and run simple c files,...

    Step 4: Write a Sum Function Since we can write, compile and run simple c files, lets add a bit to make a program that will sum an entire array of integers. To do this we are going to simply overwrite the main.c file to include the new function. The sum function below is not complete and must be finished before the program will execute properly. %%file main.c #include <stdio.h> int sum(int array[], int arrayLength) {     int i =...

  • Submissions) Part A Type and run the Array class template discussed in lecture (Templates notes). Modify...

    Submissions) Part A Type and run the Array class template discussed in lecture (Templates notes). Modify the class by adding sort member function (refer to Algorithm Analysis notes for sorting algorithms) Sample usage: a. sort(); Add the needed code in main to test your function. template <class T> 1/you can use keyword typename instead of class class Array { private: T *ptr; int size; public: Array(T arr[], int s); void print(); template <class T> Array<T>:: Array (T arr[], int s)...

  • //bubble sort implementation// Fig 6.15 with simplified convention for the for loop. #define _CRT...

    //bubble sort implementation// Fig 6.15 with simplified convention for the for loop. #define _CRT_SECURE_NO_WARNINGS //for windows os only #include <stdio.h> #include <stdlib.h> // Fig. 6.15: fig06_15.c // Sorting an array's values into ascending order. #include <stdio.h> #define SIZE 10 // function main begins program execution int main(void) {    // initialize a    int a[SIZE] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 };    printf("Data items in original order");    // output original array    for (int i = 0;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT