Question

In: Computer Science

In JAVA Language Please! Programming Exercise 3.20 required you to design a PID manager that allocated...

In JAVA Language Please!

Programming Exercise 3.20 required you to design a PID manager that allocated a unique process
identifier to each process. Exercise 4.20 required you to modify your solution to Exercise 3.20 by writing
a program that created a number of threads that requested and released process identifiers. Now
modify your solution to Exercise 4.20 by ensuring that the data structure used to represent the
availability of process identifiers is safe from race conditions. Use Pthreads mutex locks.
Thank you in advance.

Solutions

Expert Solution

  

/*Programming Exercise 3.20 required you to design a PID manager that
allocated a unique process identifier to each process. Exercise 4.20
required you to modify your solution to Exercise 3.20 by writing a
program that created a number of threads that requested and released
process identifiers. Now modify your solution to Exercise 4.20 by
ensuring that the data structure used to represent the availability of
process identifiers is safe from race conditions. Use Pthreads mutex
locks
*/
#include <iostream>
#include <stdlib.h>
#include <pthread.h>
#include <windows.h>
using namespace std;
#define MIN_PID 300
#define MAX_PID 5000
int threadVar = 0;
pthread_mutex_t mutex;
struct pid_tab
{
int pid;
bool bitmap;
}pidArr[4700];
int allocate_map(void) //allocates bitmap values to the data structure
{
int i,j;
for(i = MIN_PID, j =0; i <= MAX_PID; i++, j++)
{
pidArr[j].pid = i;
pidArr[j].bitmap = 0;
}
if(i == MAX_PID && j == 4700)
return 1;
else
return -1;
}
int allocate_pid(void) //allocates a pid to the new process
{
for(int i = MIN_PID, j =0; i <= MAX_PID; i++, j++)
{
if(pidArr[j].bitmap == 0)
{
pidArr[j].pid = i;
pidArr[j].bitmap = 1;
return i;
break;
}
}
return -1;
}
void release_pid(int pid) //releases pid
{
for(int i = 0; i <= 4700; i++)
{
if(pidArr[i].pid == pid)
{
pidArr[i].bitmap = 0;
}
}
}
/* below function executes such that every thread only increments the threadVar by 1. Hence the output is numbers from 1 to 100 printed corresponding to each thread's execution.
The thread increments the value of threadVar by 1 and exits. Then the next thread increments by 1 again and exits. Every execution consists of a lock and unlock. */
void * threadCall(void* voidA) //function called by the created thread
{
int ret = allocate_pid(); //allocates a pid
while (threadVar < 100)
{
pthread_mutex_lock(&mutex); //mutex lock occurs
if (threadVar >= 100)
{
pthread_mutex_unlock(&mutex);
break;
}
threadVar++; //threadVar increments at least once
Sleep(100);
cout<<"\n "<<threadVar;
//cout<<"\n "<<pidArr[threadVar].pid;
pthread_mutex_unlock(&mutex); //mutex now unlocked
}
Sleep(5);
release_pid(ret); //pid released
}
int main()
{
int i =0;
pthread_t thread[100];
cout<<"\n 100 threads created. Every thread will print the value of variable 'threadVar' and increment it by 1 with a delay of 100ms each process execution";
Sleep(3000); //delay only so that the above can be read in output screen before execution of the rest of the code
for(i = 0; i < 100; i++)
{
pthread_mutex_init(&mutex, NULL);
pthread_create(&thread[i], NULL, threadCall, NULL);
threadCall(NULL);
}
for(i = 0; i < 100; i++)
{
pthread_join(thread[i], NULL);
pthread_mutex_destroy(&mutex);
}
return 0;
}

Note: Plzzz don' t give dislike.....Plzzz comment if u have any problem i will try to resolve it.......



Related Solutions

Programming language= In Java and Oracle Sql You are required to develop a simple HR application...
Programming language= In Java and Oracle Sql You are required to develop a simple HR application for a small accounting firm that wishes to keep track of all the employees at the firm; storing details about their salary, phone numbers and Date of Birth. The firm has many departments and there are 5 to 20 employees in each department. The department information includes department name, description and total number of employees in that department. The company also provides vehicles for...
PUT IN JAVA PROGRAMMING LANGUAGE The Rectangle class: Design a class named Rectangle to represent a...
PUT IN JAVA PROGRAMMING LANGUAGE The Rectangle class: Design a class named Rectangle to represent a rectangle. The class contains: • Two double data fields named width and height that specify the width and height of a rectangle. The default values are 1 for both width and height. • A no-arg (default) constructor that creates a default rectangle. • A constructor that creates a rectangle with the specified width and height. • A method named findArea() that finds the area...
THE QUESTION IS OF JAVA LANGUAGE. ANSWER IS REQUIRED IN THREE PARTS (THREE JAVA FILES). PLEASE...
THE QUESTION IS OF JAVA LANGUAGE. ANSWER IS REQUIRED IN THREE PARTS (THREE JAVA FILES). PLEASE DIFFERENTIATE FILES SO I CAN UNDERSTAND BETTER. NOTE - Submission in parts. Parts required - Dog Class Code, Dog Manager Class Code and the main code. Please differentiate all three in the answer. This Assignment is designed to take you through the process of creating basic classes, aggregation and manipulating arrays of objects. Scenario: A dog shelter would like a simple system to keep...
Java Programming language. Proof of concept class design based on the following ideas Look at your...
Java Programming language. Proof of concept class design based on the following ideas Look at your refrigerator and think about how you would model it as a class. Considerations include: A refrigerator is made by a company on a manufacturing date and has an overall size based on length, width, and height A refrigerator contains a number of shelves and drawers for storing dairy, meats, and vegetables A refrigerator also has storage areas on the door for things like bottled...
Please use the Java Programming language. This is the introductory course, chapter two. Please only use...
Please use the Java Programming language. This is the introductory course, chapter two. Please only use if/else if, else and while loop. We have not touch base with do and while do(I don't know if while do exist in Java). Create an application that converts number grades to letter grades. Console Welcome to the Letter Grade Converter Enter numerical grade: 90 Letter grade: A Continue? (y/n): y Enter numerical grade: 88 Letter grade: A Continue? (y/n): y Enter numerical grade:...
THIS QUESTION IS BASED UPON JAVA PROGRAMMING. Exercise 1 In this exercise, you will add a...
THIS QUESTION IS BASED UPON JAVA PROGRAMMING. Exercise 1 In this exercise, you will add a method swapNodes to SinglyLinkedList class. This method should swap two nodes node1 and node2 (and not just their contents) given references only to node1 and node2. The new method should check if node1 and node2 are the same nodes, etc. Write the main method to test the swapNodes method. Hint: You may need to traverse the list. Exercise 2 In this exercise, you will...
PROGRAMMING LANGUAGE : JAVA Problem specification. In this assignment, you will create a simulation for a...
PROGRAMMING LANGUAGE : JAVA Problem specification. In this assignment, you will create a simulation for a CPU scheduler. The number of CPU’s and the list of processes and their info will be read from a text file. The output, of your simulator will display the execution of the processes on the different available CPU’s. The simulator should also display: -   The given info of each process -   CPU utilization - The average wait time - Turnaround time for each process...
Programming Language: JAVA In this assignment you will be sorting an array of numbers using the...
Programming Language: JAVA In this assignment you will be sorting an array of numbers using the bubble sort algorithm. You must be able to sort both integers and doubles, and to do this you must overload a method. Bubble sort work by repeatedly going over the array, and when 2 numbers are found to be out of order, you swap those two numbers. This can be done by looping until there are no more swaps being made, or using a...
***Please answer the question using the JAVA programming language. Write a program that calculates mileage reimbursement...
***Please answer the question using the JAVA programming language. Write a program that calculates mileage reimbursement for a salesperson at a rate of $0.35 per mile. Your program should interact (ask the user to enter the data) with the user in this manner: MILEAGE REIMBURSEMENT CALCULATOR Enter beginning odometer reading > 13505.2 Enter ending odometer reading > 13810.6 You traveled 305.4 miles. At $0.35 per mile, your reimbursement is $106.89. ** Extra credit 6 points: Format the answer (2 points),...
Answer the following in Java programming language Create a Java Program that will import a file...
Answer the following in Java programming language Create a Java Program that will import a file of contacts (contacts.txt) into a database (Just their first name and 10-digit phone number). The database should use the following class to represent each entry: public class contact {    String firstName;    String phoneNum; } Furthermore, your program should have two classes. (1) DatabaseDirectory.java:    This class should declare ArrayList as a private datafield to store objects into the database (theDatabase) with the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT