Question

Please change the following code to python form. #include<stdio.h> #include<stdlib.h> #define MIN_PID 300 #define MAX_PID 5000...

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;
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1
Basically the Memory Management in python involves a private heap which contains data members and data structure.Python Memory Manager Manages the Private heap internally.To avoid memory corruption we should not operate 

Python converted code of c program

import sys
MIN_PID = 300
MAX_PID = 5000
TRUE = 1
FALSE =0

#Python Doesn't Contains Structure so we Can Use Class Instead of Structure
#Here I defined class called PIDTable Which Consists of 2 data Memebers/Variables
class PidTable:
    PID=0
    isAvailable=0

# Method for allocating the Process Availablity status and Process Id
def allocate_map(pidlist):
    print (sys.getsizeof(pid))
    if(pidlist is None):
        return -1
    pidlist[0].PID = MIN_PID;
    pidlist[0].isAvailable = TRUE;
    print(pidlist[0].PID)
    print(len(pidlist))
    for i in range(1,len(pidlist)):
        print (i)
        pidlist[i].PID=pidlist[i-1].PID+1
        pidlist[i].isAvailable = TRUE
    return 1;

# Method to get the currently allocated Process from a pool of available Process and change its Available status to false Since the current Process in been allocted for a process
def allocate_pid(pidlist):
    for i in range(0, len(pidlist)):
        if (pidlist[i].isAvailable == TRUE):
            pidlist[i].isAvailable=FALSE;
            print (pidlist[i].PID)
            return pidlist[i].PID;
    if (i == MAX_PID - MIN_PID + 1):
        return -1;

# Method to release the currently allocated Process and change its Available status to true
def release_pid(pid,pidlist):
    pidlist[pid-MIN_PID].isAvailable=TRUE;


num = MAX_PID - MIN_PID
pid = 0;
# PidList is a list of Class Object or we can call it as Array of Object
PidList = []
for i in range(num):
    PidList.insert(i,PidTable())
num=allocate_map(PidList)
if(num!=-1):
    pid=allocate_pid(PidList)
    if(pid!=-1):
        print("New Process Allocated Pid = ",pid);

    pid=allocate_pid(PidList)
    if(pid!=-1):
        print("New Process Allocated Pid = ",pid);

    pid=allocate_pid(PidList)
    if(pid!=-1):
        print("New Process Allocated Pid = ",pid);

    pid = allocate_pid(PidList)
    if (pid != -1):
        print("New Process Allocated Pid = ", pid);

    print("Process %d now Releasing = ", pid)
    release_pid(pid,PidList)

    pid=allocate_pid(PidList)
    if(pid!=-1):
        print("New Process Allocated Pid =",pid)
Add a comment
Know the answer?
Add Answer to:
Please change the following code to python form. #include<stdio.h> #include<stdlib.h> #define MIN_PID 300 #define MAX_PID 5000...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • PLease explain output of these two programs: 1. #include <stdio.h> typedef struct { char *name; int...

    PLease explain output of these two programs: 1. #include <stdio.h> typedef struct { char *name; int x, y; int h, w; } box; typedef struct { unsigned int baud : 5; unsigned int div2 : 1; unsigned int use_external_clock : 1; } flags; int main(int argc, char** argv){ printf("The size of box is %d bytes\n", sizeof(box)); printf("The size of flags is %d bytes\n", sizeof(flags)); return 0; } 2. #include <stdio.h> #include <string.h> /* define simple structure */ struct { unsigned...

  • ​what is the output? Consider the following program: #include <stdio.h> #include <stdlib.h> #define size 3 void...

    ​what is the output? Consider the following program: #include <stdio.h> #include <stdlib.h> #define size 3 void func(int **a) {int tmp; for (int i = 0; i < size; i++) {for (int j = i; j < size, j++) {tmp = *(*(a+i)+j); *(*(a+i)+j) = *(*(a+j)+i); *(*(a+j)+i) = tmp;}}} int main() {int **arr = malloc(sizeof(int*) * size); for(int i = 0; i < size; i++) {arr[i] = malloc(sizeof(int) * size); for (int j = 0; j < size, j++) arr[i][j] = 2*i...

  • Deleting multiples of a given integer from a linked list: #include <stdio.h> #include <stdlib.h> #include <assert.h>...

    Deleting multiples of a given integer from a linked list: #include <stdio.h> #include <stdlib.h> #include <assert.h> #define MAX 10000 typedef struct node_tag { int v; // data struct node_tag * next; // A pointer to this type of struct } node; // Define a type. Easier to use. node * create_node(int v) { node * p = malloc(sizeof(node)); // Allocate memory assert(p != NULL); // you can be nicer // Set the value in the node. p->v = v; p->next...

  • Debug to print output "2 3 5 6 8 11": #include <stdio.h> #include <stdlib.h> int main()...

    Debug to print output "2 3 5 6 8 11": #include <stdio.h> #include <stdlib.h> int main() {    int i, j, n; int A[] = {2, 3, 5, 5, 5, 6, 8, 11, 11, 11};    n = sizeof(A) / sizeof(int);    for (i = 0; i < n - 1; i++){        if (A[i] != A[i + 1]) {            for (j = 1; j < n - 1; j++) {                A[j]...

  • ​what's wrong with my code?????? #include <stdio.h> #include <stdlib.h> #include <string.h>                         &

    ​what's wrong with my code?????? #include <stdio.h> #include <stdlib.h> #include <string.h>                            #define MAXBINS 99 #define MAXCORS 26 #define MAXCUS 100 #define MAXPUR 10 /* datatype for a list of orders ----------------- */ typedef struct { int bin_order_number; char bin_order_char; }bin_order; typedef struct { int orderNo; bin_order orderbin; }order; typedef struct { int cusnumber; int n;   /* number of the orders what the txt include */ order oo[MAXPUR+1]; }customer; typedef struct{ int n; /*number of the customers */ customer cc[MAXCUS+1];...

  • Question 1 Consider the following program fragment that defines a self-referential class: #include <stdlib.h> #include <stdio.h>...

    Question 1 Consider the following program fragment that defines a self-referential class: #include <stdlib.h> #include <stdio.h> struct node_int; typedef struct node int *node; struct node_int void *data; node next; typedef struct list_int (node first;} *list; void main(int argc, char *argv[]) list shopping, t; node n; int x=25; shopping=(list) malloc(sizeof(struct list_int)); n= (node) malloc(sizeof(struct node_int)); n->data=shopping; n->next=NULL; shopping->first=n; t=(list) (shopping->first->data); // ***** t->first=NULL; // ***** n->data=&x; printf("%d\n", *((int *) (shopping->first->data))); a What will be displayed on the screen if the above...

  • Finish function to complete code. #include <stdio.h> #include <stdlib.h> #include<string.h> #define Max_Size 20 void push(char S[],...

    Finish function to complete code. #include <stdio.h> #include <stdlib.h> #include<string.h> #define Max_Size 20 void push(char S[], int *p_top, char value); char pop(char S[], int *p_top); void printCurrentStack(char S[], int *p_top); int validation(char infix[], char S[], int *p_top); char *infix2postfix(char infix[], char postfix[], char S[], int *p_top); int precedence(char symbol); int main() { // int choice; int top1=0; //top for S1 stack int top2=0; //top for S2 stack int *p_top1=&top1; int *p_top2=&top2; char infix[]="(2+3)*(4-3)"; //Stores infix string int n=strlen(infix); //length of...

  • Explain the code and analyze the performance of algorithm #include<stdio.h> #include<string.h> #define NUM 1...

    Explain the code and analyze the performance of algorithm #include<stdio.h> #include<string.h> #define NUM 100 #define maxint 10000 void dijkstra(int n,int v,int dist[],int prev[],int c[][NUM]) {    int i,j;    bool s[NUM];    for(i=1; i<=n; i++)    {        dist[i] = c[v][i];        s[i] = false;        if (dist[i]>maxint) prev[i] = 0;        else prev[i] = v;    }    dist[v] = 0;    s[v] = true;    for(i=1; i<n; i++)    {        int tmp = maxint;        int u = v;        for(j=1; j<=n; j++)            if(!(s[j]) && (dist[j]<tmp))            {                u = j;                tmp = dist[j];           ...

  • Would u help me fixing this CODE With Debugging Code with GDB #include <stdio.h> #include <stdlib.h>...

    Would u help me fixing this CODE With Debugging Code with GDB #include <stdio.h> #include <stdlib.h> #define SIZE (10) typedef struct _debugLab { int i; char c; } debugLab; // Prototypes void PrintUsage(char *); void DebugOption1(void); void DebugOption2(void); int main(int argc, char **argv) { int option = 0; if (argc == 1) { PrintUsage(argv[0]); exit(0); } option = atoi(argv[1]); if (option == 1) { DebugOption1(); } else if (option == 2) { DebugOption2(); } else { PrintUsage(argv[0]); exit(0); } }...

  • 1. Select all that are true for the following code. #include <stdio.h> #include <stdlib.h> void range(int...

    1. Select all that are true for the following code. #include <stdio.h> #include <stdlib.h> void range(int start, int end, int *results) { int length = end - start; results = malloc(sizeof(int) * length); for (int i=start; i<end; i++) { *results = i; results++; } } void printArray(int *arr, int length) { for (int i=0; i<length; i++) { printf("%d ", arr[i]); } } int main() { int *x; int s = 2; int e = 11; range(s, e, x); printArray(x, e...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT