Posts

Showing posts from August, 2012

Memory allocation/deallocation routines malloc() and free()

Image
According to my declaration, allocate the 50000 block of array. If memory block is available on the array, the program allocates a block from the array according to specify size.    Here I used the array data structure to implement the heap. In this code taken large contiguous buffer which used to allocate all the spaces associate with the NewMalloc() function. There doesn't allocate the same region of memory to two different NewMalloc() . Initially all 50000 bytes are free in this array. NewMalloc()              It takes one integer argument which represents the size and return a pointer to a contiguous region of that many bytes.              Now call to NewMalloc(60) . There we expect to allocate the first 60 bytes and return a pointer to the caller. Actually in the array allocates 68-bytes for that region. Because I maintain a meta data that keeps track of what space is allocated and what space is free. In meta data I hold the size of the block and availability of t

simple shell in C

This shell is called mosh (My Own Shell). When invoke, it should be able to read a command typed on its prompt ($) and execute it in a new process. It should also have a shell variable similar to PATH in bash. Shell variables are set as follows: PATH=/usr/bin:/bin The executable for the command is searched in the directories in the path and the first one found should be executed. mosh should also be able to handle pipes (not more than one pipe) similar to bash. Implementation in C #include <stdio.h> #include <unistd.h> #include <string.h> #include <stdlib.h> #include <sys/types.h> #define INPUT_SIZE 512 #define BUF_SIZE 10 char** split_String(char *input_ptr,char *buffer[BUF_SIZE],int *n_args); /** *Main function of the shell */ int main(){     char input[INPUT_SIZE];     char *buffer[BUF_SIZE];     char *pipe_forw[BUF_SIZE];     char *ext_array[BUF_SIZE];     char **buf;     char *input_ptr;     int status_1,status_2;     in