EmbLogic's Blog

Memory allocation technique

Hi ,

Lets discuss about memory allocation techniques we use either in user space program or kernel space program .

1. Malloc :-

void *malloc(size_t size);
The malloc() function allocates size bytes and returns a pointer to the
allocated memory.  The memory is not initialized.  If size is  0,  then
malloc()  returns either NULL, or a unique pointer value that can later
be successfully passed to free().

2. Kmalloc

void *kmalloc(size_t size , flage);

Kmalloc allocates memory from kernel space and returns a pointer to the allocated memory .kmalloc allocated physically contiguous memory . if kernel doesn’t find contiguous memory of required size then this call fails .

3. vmalloc

void * vmalloc(size_t size);

vmalloc allocates virtually contiguous memory not necessary to be physically contiguous .vmalloc is a slower operation (slower because it makes an extra table to map the contiguous virtual memory to non-contiguous physical memory) . vmalloc is used when we need a large memory from kernel space .

Thank you ……

 

Posted in Uncategorized | Leave a comment

Switch Case

Rules for using Switch Case in C Programming :

1. Case Label must be unique.

2.Case Label must ends with colon.

3.Case Label must have constants/ constant expression.

4.Case Label must be of Integral Type(Integer, Character).

5.Case Label should not be ‘ Floating Point Number ‘.

6.Switch case should have at most one default Label.

7.Default Label is optional.

8.Default can be placed anywhere in the switch.

9.Break statement takes control out of the switch.

10.Two or more cases may share one break statement.

11.Nesting(switch within switch) is allowed.

12.Relational operators are not allowed in switch statement.

13.Macro Identifier are allowed as Switch Case Label.

14.Constant Variable is allowed in switch case statement.

15.Empty switch case is allowed.

Posted in Data Structures with C | Leave a comment

Passing a local variable in thread function

if we simply pass the address of local variable to a thread function then when that function ends then address of that variable lost(if variable is automatic) and we try to access that address or variable the it doesn’t give correct result .

I’ve resolved this problem by this way:
If i take a local variable pointer and malloc it and then pass it. when we malloc then it falls in heap segment and the it exist for the whole lifetime of program.

Please comment if any issue.

Posted in Data Structures with C, Project 03: Client Server Communication using Linux and IPC | Leave a comment

Introduction to Block Driver

Overview :

  •  Block Drivers are act as conduit between core memory and secondary storage.
  • There is a virtual memory layer between core memory and secondary storage.
  • Block Layer could be seen as a part of virtual memory subsystem.
  • Block driver provides access to devices that transfer randomly accessible data.
  • Block Driver controls the block layer, makes decision about collecting bytes into group of bytes, handle interaction between virtual layer and block layer, block layer and block devices.
  • Block Driver collect chunks of bytes into blocks.
  • As almost every block device consist of file system also. Therefore block device driver must take care of file system.

 

Posted in Block Driver | Leave a comment

Getting started with pointers

-> Pointers in c is variable which points the value stored in another location
-> It stores the logical addresses of the variable in which data is stored
->Size of a pointer depends on architecture of a workstation i.e
4bytes for 32 bit and 8bytes for 64 bit architecture
-> Representation of pointers
int *ptr;
//above expression points to the integer value stored in some location of memory.
-> Example
int *ptr;
int var=5;
ptr=&var;
printf(“Location stored in ptr is %p \n”,ptr); // ( it will print the the logical address of of varible var )

printf(“address of var is %x “,&var); // it will print the address of var

NOTE: check both the values and see the result …….
thank you

Posted in Uncategorized | Leave a comment

character driver

Hi!

character driver

steps of character driver writing -

1->create structure for device

2->Allocate memory

3->Initialise device

4->Insert device using modprobe /insmod command

5->Release device

 

 

3

.

 

Posted in Character Driver | Tagged | Leave a comment

How to estimate memory alocated under Double Pointer

If we use single malloc for a double pointer then it simply means that the memory is allocated to the for the external Pointer

i.e, malloc(**ptr) -> Memory is allocated only for external pointer of  *(*ptr) which is allocated in form of array. But to provide (*ptr) a memory location , we have to malloc() it again as follows:

malloc(*ptr) ->Now memory is allocated for *ptr .

To  check the amount of memory allocated under **ptr , just print using %d format specifier and as an argument to printf call sizeof(**ptr) .

i.e,

printf(“The size under **ptr is :%d \n”,sizeof(**ptr));

 

Posted in Uncategorized | Leave a comment

msg queues

There are different types of INTER PROCESSING COMMUNICATION techniques :–

1)Pipes, 2)FIFO, 3) shared memory, 4)message queue

Message queue is a technique through which we can send & receive data.

In message queues data is send with data type along with data, as the data type of each message is different there is less synchronization problem.

Posted in Uncategorized | Leave a comment

How to Write a Writer Routine.

In the char driver We write the Write Routine.  This write routine is mapped in struct file operations . We use copy_from_user macro. this macro has three arguments. first is void* to second is const  void* __user form and last argument is how many char  have to write.

first malloc the sqset then malloc sqset->data and at last create qsetarray. Then malloc qsetarry member one by one and write the data by using copy_from_user macro.

 

 

 

 

Posted in Character Driver | Leave a comment

Linklist

In c linkllist is very important topic …

Linklist have node which is connected one an other.linklist contain information(info) part and other part(Next) is address of other node.The type of info part is whatever you want like int,char,float,etc,but Next par is pointer type because it contain the address of next node.

We can insert node at beginning of linklist,end of linklist and after nth node of linklist.We can also insert node using key element of linklist.

I have give memory to a node using Malloc().

First of all i have take a structure pointer in this pointer i have stored address of first node,after that i have started inserting node at beginning ,end ,after nth node.

I have also delete node at begining,end and nth node using free().I have also sort the linklist…

Posted in Data Structures with C | Leave a comment

Changing the behavior of Pipe using funtion- ‘fcntl()’

The pipe() system call is used to create a pipe which returns two file-descriptors; one for read and one for write. If pipe is empty, then reader waits until data is available to read from the pipe which is termed as Block-On-Read.

But we can change this behaviour by using fcntl() funtion as follows;

fcntl(rfd, F_SETFL, O_NONBLOCK);

Here,

rfd =Read File Descriptor given by pipe() call,

F_SETFL = It tells fcntl() what to do with rfd,

O_NONBLOCK = Set the read flags associated with rfd so as to perfrom Non-Block Read.

Here’s also another way which is ‘pipe2()’ system call. Which quite easy :-P

 

Posted in Project 03: Client Server Communication using Linux and IPC | Leave a comment

Shared Memory

Shared memory allows two or more processes to share the same region (segment) of physical memory”

“One processes simply write data to the shared memory region, which will available to all other processes attached to that region & any of the attached process can read that data”

“Shared memory is the fastest IPC mechanism, because in this no kernel intervention is required”

“Kernel creates a memory region in user space using shmget() call, which will be available in a single page or in multiple pages”

“Then kernel add that shared memory (pages) to the page tables( to virtual address sapce) of all the processes, those want to share that page using shmat() call

“The attached shared memory segment can be detached using shmdt() call in the calling process”

Posted in Project 03: Client Server Communication using Linux and IPC, Uncategorized | Leave a comment

Pipes

Behaviour of pipes
*pipes provide unidirectional data channel (half duplex ),provides a read and write file descriptors
*the communication channel provided by a pipe transfers data in the form of byte stream
*if a process attempts to read from empty pipe provided both the file descriptors are open we will observe a block on read
*if a process atempts to write on a full pipe , with both descriptors open then we will observe a block on write
*if writing side has been closed and there exists some data in the pipe then reader could read till EOF
*if all the read descriptors are closed, then a attempt to write will cause a SIGPIPE signal to the calling process
*pipes can be made non blocking types using O_NONBLOCK in the flag

Posted in Uncategorized | Leave a comment

initialising character driver

Simple Algorithm to initialise a Scull

1->Register device

2->Give memory to scull device

3->Set this memory to NULL using memset

4->After this initialise device

5->Get device status

->>Use Insmod to add to kernel

->>Use rmmod to remove from kernel

 

Posted in Uncategorized | Leave a comment

How to change the thread attributes ?

We can change the thread attributes by modifying the pthread_attr_t structure.

Generally we give NULL in pthread_create , so it takes default attributes while creating a thread , but we can modify this by giving a pointer pointing to pthread_attr_t structure instead of giving null i:e;

pthread_create(pthread_t thid, pthread_attr_t *attr, void*(*start_routine)(void *), void *arg);

 

Posted in Uncategorized | Leave a comment