1. Engineering
  2. Computer Science
  3. please change the following code to python form includeltstdiohgt includeltstdlibhgt...

Question: please change the following code to python form includeltstdiohgt includeltstdlibhgt...

Question details

Please change the following code to python form.

#include<stdio.h>
#include<stdlib.h>
#define MIN_PID 300
#define MAX_PID 5000
#define TRUE 1
#define FALSE 0
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=TRUE;
        for( i=1;i<MAX_PID-MIN_PID+1;i++){
      pId[i].PID=pId[i-1].PID+1;
          pId[i].isAvailable=TRUE;
        }   
        return 1;
}
int allocate_pid(){
   int i ;
   for( i=0;i<MAX_PID-MIN_PID+1;i++){
                if(pId[i].isAvailable==TRUE){
                        pId[i].isAvailable=FALSE;
                        return pId[i].PID;
                }
   }
   if(i==MAX_PID-MIN_PID+1)
        return -1;
}
void release_pid(int pid){
        pId[pid-MIN_PID].isAvailable=TRUE;
}


int main(){
  int pid;
  allocate_map();
  
  if((pid=allocate_pid())!=-1);
  printf("New Process Allocated Pid= %d \n",pid);
  if((pid=allocate_pid())!=-1);
  printf("New Process Allocated Pid= %d \n",pid);
  if((pid=allocate_pid())!=-1);
  printf("New Process Allocated Pid= %d \n",pid);
  if((pid=allocate_pid())!=-1);
  printf("New Process Allocated Pid= %d \n",pid);
  
  printf("Process %d now Releasing \n",pid);
  release_pid(pid);
  if((pid=allocate_pid())!=-1);
  printf("New Process Allocated Pid= %d \n",pid);
  return 0;
}
Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution