In: Computer Science
how to convert Sudo Code to C language of these two functions
MsgEnv * request_msg_env( ) {
search for free memory block in the queue of the free blocks(shown in the data structure section );
if (no memory block is available)
{
Block invoking process(Process_switching();
}
else
{
update the data structure;
return a pointer to the memory block;
}
int release_msg_env( MsgEnv * msg_env_ptr ) :
{ if (memory block pointer is not valid)
return ErrorCode;
Add memory block to the pool;
Update datastructure;
Return Sucess code
}
these are the data stucures used in the above functions
// data strucure of message envelops
typedef struct MsgEnv{ struct MsgEnv *Msg_env_ptr;
char body[128];
int time_delay,sender_pid, Target_pid;
int Message_type ; }MsgEnvi;
// queue of message envelops
typedef struct MsgEnvq {
MsgEnvi *head;
MsgEnvi *tail;
MsgEnvi* (*dequeue)();
int (*enqueue)(MsgEnvi *env);
struct MsgEnvq *next;
};
The question have given you the pseudo code. So first step is to understand as ti what it states.
I assume you have the basic knowledge about how a queue works. So according to the given pseudo code we can say,
first function is traversal and searching in the queue
second function is just releasing the memory block that is deleting(dequeue) from a queue
No description about the size of queue is given. So I am taking it as a constant Size_Q in my code.
// data strucure of message envelops
typedef struct MsgEnv{ struct MsgEnv *Msg_env_ptr;
char body[128];
int time_delay,sender_pid, Target_pid;
int Message_type ; }MsgEnvi;
// queue of message envelops
typedef struct MsgEnvq {
MsgEnvi *head;
MsgEnvi *tail;
MsgEnvi* (*dequeue)();
int (*enqueue)(MsgEnvi *env);
struct MsgEnvq *next;
};
#define size_q 5
MsgEnv *search_queue(Msgenv *s){
int count=0;
MsgEnv *temp = s->head;
for(temp=s->head;temp!=NULL;temp=temp->NULL){
count++;
}
if(count == 5){
Block invoking process(Process_switching();
temp = NULL;
}
else{
temp = s->tail->next;
update the data structure;
s->tail = temp;
temp->tail = s->tail;
}
return temp;
}
int release_msg_env(MsgEnv * msg_env_ptr){
if(msg_env_ptr == NULL)
return 0; // refering 0 as error code as int has to be returned. You amy include your ErrorCode function as well which will be returning its own integer value which will finally be returned by this return statement.
else{
msg_env_ptr->next = NULL
}
}