Question

In: Computer Science

Modify HW#1. C++ use,Write a multithreaded program that tests your solution to HW#1. You will create...

Modify HW#1. C++ use,Write a multithreaded program that tests your solution to HW#1. You will create several threads – for example, 100 – and each thread will request a pid, sleep for a random period of time, and then release the pid. (Sleeping for a random period approximates the typical pid usage in which a pid is assigned to a new process, the process executes and terminates, and the pid is released on the process’ termination).On UNIX and Linux systems, sleeping is accomplished through the sleep() function, which is passed an integer value representing the number of seconds to sleep.

Solutions

Expert Solution

#include <stdio.h>

#include <stdlib.h>

#include <limits.h>

#define MIN_PID 300

#define MAX_PID 5000

#define CB CHAR_BIT

int sz = MAX_PID - MIN_PID + 1;

unsigned char *unsignedChar;

int allocate_map();

int allocate_pid();

void release_pid(int pid);

int main()  

{

   int map = allocate_map();

   if (map == 1) {

       printf("\nBitmap Data Structure initialized.\n");

       int id = 0, i = 0;

   //create 100 processes

       while (i < 100) {

           int val = allocate_pid();

           printf("\nProcess %d: pid = %d", i+1, val);

           i++;

       }

   //release a few processes

   release_pid(303); printf("\nProcess 303 released.");

   release_pid(308); printf("\nProcess 308 released.");

   release_pid(309); printf("\nProcess 309 released.");

   //allocate a few more processes after this release

       int val = allocate_pid(); printf("\nProcess %d : pid = %d", ++i, val); //should be 303

   val = allocate_pid(); printf("\nProcess %d : pid = %d\n", ++i, val); //should be 308

   }

   else printf("\nFailed to initialize data structure.\n");

}

/* Creates and initializes a bitmap data structure for representing pids;

returns —1 for unsuccessful, 1 for successful */

int allocate_map() {

   unsignedChar = (unsigned char*)malloc((sz+CB-1)/CB * sizeof(char));

   if (unsignedChar) return 1;

   return -1;

}

/* Allocates and returns a pid; returns -1

if it is unable to allocate a pid (all pids are in use) */

int allocate_pid() {

   int i = 0;

   int pid = unsignedChar[i/CB] & (1 << (i & (CB-1)));

   while (pid != 0) {

       i++;

       pid = unsignedChar[i/CB] & (1 << (i & (CB-1)));

       }

   if (i+MIN_PID > MAX_PID) return -1;

   unsignedChar[i/CB] |= 1 << (i & (CB-1));

   return i+MIN_PID;

}

/* Releases a pid given a pid parameter*/

void release_pid(int pid) {

   if (pid < 300) {

       printf("\nInvalid PID: It should lie between 300 and 3000.");

       return;

   }

   int i = pid - MIN_PID;

   unsignedChar[i/CB] &= ~(1 << (i & (CB-1)));

}

Related Solutions

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,...
Write a C++ program where you implement a synchronized multithreaded version of HAPPY with four threads....
Write a C++ program where you implement a synchronized multithreaded version of HAPPY with four threads. The program will take in an array from 1 to n (n = 50) and will be passed to four different threads: If the current number is divisible by 2, then print HAP If the current number is divisible by 5, then print PY If the current number is divisible by both 2 and 5, then print HAPPY If the number is neither divisible...
Write a program in C++ Write three new functions, mean() and standard_deviation(). Modify your code to...
Write a program in C++ Write three new functions, mean() and standard_deviation(). Modify your code to call these functions instead of the inline code in your main(). In addition, add error checking for the user input. If the user inputs an incorrect value prompt the user to re-enter the number. The flow of the code should look something like: /** Calculate the mean of a vector of floating point numbers **/ float mean(vector& values) /** Calculate the standard deviation from...
Need to create Mortgage Calculator Program In C++ Modify your mortgage to include two functions. A...
Need to create Mortgage Calculator Program In C++ Modify your mortgage to include two functions. A function to get principal, rate, and term. A function to calculate monthly payment. Test your program by getting the data from the user by using the first function and calculate monthly payment by calling the other function. Add version control and write proper comments with a few blank lines & indentations in order to improve your programming style.
Write a multithreaded program in C using the pthread library and dynamic memory(malloc) that multiplies two...
Write a multithreaded program in 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 result must be stored in...
Write a program to create a tree randomly. You can use C++ programming language. The input...
Write a program to create a tree randomly. You can use C++ programming language. The input is the number of vertices in the tree, and the output is an adjacent list of the tree. (Managed to complete this assignment with a binary tree. But, was told I needed a general tree instead)
C++ HW Aim of the assignment is to write classes. Create a class called Student. This...
C++ HW Aim of the assignment is to write classes. Create a class called Student. This class should contain information of a single student. last name, first name, credits, gpa, date of birth, matriculation date, ** you need accessor and mutator functions. You need a constructor that initializes a student by accepting all parameters. You need a default constructor that initializes everything to default values. write the entire program.
C/ C++ Preferably 1. Write a simple program where you create an array of single byte...
C/ C++ Preferably 1. Write a simple program where you create an array of single byte characters. Make the array 100 bytes long. In C this would be an array of char. Use pointers and casting to put INTEGER (4 byte) and CHARACTER (1 byte) data into the array and pull it out. YES, an integer can be put into and retrieved from a character array. It's all binary under the hood. In some languages this is very easy (C/C++)...
Write a multithreaded program that calculates various statistical values for a list of numbers. This program...
Write a multithreaded program that calculates various statistical values for a list of numbers. This program will be passed a series of numbers on the command line and will then create three separate worker threads. One thread will determine the average of the numbers, the second will determine the maximum value, and the third will determine the minimum value. For example, suppose your program is passed the integers 90 81 78 95 79 72 85 The program will report The...
1) Here is a program in c++ that calculates a speeding ticket, modify the program to...
1) Here is a program in c++ that calculates a speeding ticket, modify the program to double to cost of the ticket in a construction zone. // This project will calculate a speeding ticket between 0 to 150 mph. // 1. Ask for speed. // 2. Input speed of vehicle // 3. Calculate ticket cost (50$ if over 50mph with an additional 5$ for every mph over). // 4. Display cost of ticket. #include<iostream> using namespace std; int main() {...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT