In: Computer Science
Modify programming problem 4 from Assignment 2. You will create a number of 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 of time approximates the typical pid usage in which a pid is assigned to a new process, the process executes and then terminates, and the pid is released on the process's termination.) On UNIX and Linux systems, sleeping is accomplished through the sleep() COMP2004 Assignment 3 & Assignment 4 Fall 2020 function, which is passed an integer value representing the number of seconds to sleep.
HERE IS MY CODE:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#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;i<MAX_PID-MIN_PID;i++){
curr->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 && i<MAX_PID){
iter = iter->next;
i++;
}
if(i<MAX_PID ){
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(pid<MIN_PID || pid>MAX_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");
}
*****make sure you are running it on linux machine compile as >gcc pid.c -o pid -lpthread run as >./pid *****/ #include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<time.h> #include<pthread.h> #include<semaphore.h> #define MIN_PID 300 #define MAX_PID 5000 #define NUM_THREADS 100 #define YES 0 #define NO 1 sem_t SEM; struct PidTable{ int PID; int isAvailable; }*pId; int allocate_map(){ int i; pId=(struct PidTable *)calloc((MAX_PID-MIN_PID+1),sizeof(struct PidTable)); if(pId==NULL) return -1; pId[0].PID=MIN_PID; pId[0].isAvailable=YES; for( i=1;i<MAX_PID-MIN_PID+1;i++){ pId[i].PID=pId[i-1].PID+1; pId[i].isAvailable=YES; } return 1; } int allocate_pid(){ int i ; for( i=0;i<MAX_PID-MIN_PID+1;i++){ if(pId[i].isAvailable==YES){ pId[i].isAvailable=NO; return pId[i].PID; } } return -1; } void release_pid(int pid){ pId[pid-MIN_PID].isAvailable=YES; } void *processStart(void *id){ //long tid=(long)id; int pid,executionTime; sem_wait(&SEM); pid=allocate_pid(); usleep(10000); sem_post(&SEM); if(pid!=-1){ //printf("Thread %ld runnning....\n",tid); printf("New Process Allocated Pid= %d \n",pid); executionTime=rand()%10; sleep(executionTime); printf("Process %d releasing pid \n",pid); release_pid(pid); } pthread_exit(NULL); } int main(){ allocate_map(); srand(time(NULL)); void *status;int i; int ret=0;pthread_t thread[100]; sem_init(&SEM,0,1); for(i=0;i<NUM_THREADS;i++){ ret=pthread_create(&thread[i],NULL,processStart,(void *)(i+1)); if(ret){printf("Error creating thread\n");exit(1);} } for(i=0; i<NUM_THREADS; i++) { ret = pthread_join(thread[i], &status); if (ret) { printf("ERROR; return code from pthread_join() is %d\n", ret); exit(-1); } } return 0; } OUTPUT: