Modify programming problem 4 from Assignment 2. You will create a number of threads—for example, 100—and...

80.2K

Verified Solution

Question

Programming

Modify programming problem 4 from Assignment 2. You will createa number of threads—for example, 100—and each thread will request apid, sleep for a random period of time, and then release the pid.(Sleeping for a random period of time approximates the typical pidusage in which a pid is assigned to a new process, the processexecutes and then terminates, and the pid is released on theprocess's termination.) On UNIX and Linux systems, sleeping isaccomplished through the sleep() COMP2004 Assignment 3 &Assignment 4 Fall 2020 function, which is passed an integer valuerepresenting the number of seconds to sleep.

HERE IS MY CODE:

#include
#include
#include

#define MIN_PID 300
#define MAX_PID 5000

typedef struct node {
int pid;
struct node* next;
}Node;

int allocate_map(void);
int allocate_pid(void);
void release_pid(int pid);
int i;
Node *head;
/*
main function tests all three functions
*/
int main(){
allocate_map();
printf(\"allocated pid: %d\n\",allocate_pid());
printf(\"allocated pid: %d\n\",allocate_pid());
printf(\"allocated pid: %d\n\",allocate_pid());
printf(\"allocated pid: %d\n\",allocate_pid());
release_pid(301);
printf(\"allocated pid: %d\n\",allocate_pid());
allocate_map();
printf(\"allocated pid: %d\n\",allocate_pid());
printf(\"allocated pid: %d\n\",allocate_pid());
printf(\"allocated pid: %d\n\",allocate_pid());
printf(\"allocated pid: %d\n\",allocate_pid());
release_pid(305);
release_pid(30);

}

int allocate_map(void){
if(i>0){
return -1;
}
head = malloc(sizeof(Node));
Node *curr = head;
for(i=0;icurr->pid = 0;
curr->next = (i<(MAX_PID-MIN_PID)) ?malloc(sizeof(Node)):NULL;
curr = curr->next;
}

return 1;
}
int allocate_pid(void){
if(head->next == NULL){
return -1;
}

int i = MIN_PID +1 ;

Node *iter = head;
if(head->pid == 0){
head->pid = MIN_PID;
return head->pid;
}
while(iter ->next->pid !=0 && iiter = iter->next;
i++;
}
if(i

iter->next->pid = i;
return iter->next->pid;
}
else{return -1;}
}

void release_pid(int pid){
if(head->next == NULL && head->pid==0){
printf(\"Nothing to release\n\");
return;
}
else if(pidMAX_PID){
printf(\"invalid release\n\");
return;
}
if(head->pid==pid){
head->pid=0;
}
Node *iter = head;
while(iter->next !=NULL){
if(iter->next->pid==pid){
iter->next->pid=0;
return;
}
iter = iter->next;
}
printf(\"Nothing to release\n\");
}

Answer & Explanation Solved by verified expert
3.7 Ratings (653 Votes)
make sure you are running it on linux machine compile as gcc pidc o pid    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students