Can someone look at this code and tell me what it does and add comments within t
ID: 3888995 • Letter: C
Question
Can someone look at this code and tell me what it does and add comments within the code to explain methods#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; } } }
Can someone look at this code and tell me what it does and add comments within the code to explain methods
#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; } } }
#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; } } } #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; } } }
Explanation / Answer
#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;
/*
pidArr is array of structure pid_tab containing pid(Process ID) & BitMap.
*/
struct pid_tab
{
int pid;
bool bitmap;
}pidArr[4700];
/*
Function allocate bitmap values to the pidArr
Loop sets pidArr with pid as process id upto max available.
*/
int allocate_map(void)
{
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;
}
/*
Function allocate process id , it loops through all process ids and check if bitmap is available.
If yes, allocate bitmap to 1 and assign process id.
*/
int allocate_pid(void)
{
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;
}
/*
Release proce id, set bitmap to 0
*/
void release_pid(int pid)
{
for(int i = 0; i <= 4700; i++)
{
if(pidArr[i].pid == pid)
{
pidArr[i].bitmap = 0;
}
}
}