You are on page 1of 21

Unit -7

Memory allocation functions

Malloc Calloc Realloc Free

Malloc()
Syntax void *malloc( size_t size );

Size: Bytes to allocate. Malloc returns a void pointer to the allocated space, Or NULL if there is insufficient memory available. To return a pointer to a type other than void, use a type cast on the return value.

Free()
Syntax void free( void *memblock ); Return Values None. PARAMETERS memblock
Previously allocated memory block to be freed.

Explaination
The free function deallocates a memory block, memblock, that was previously allocated by a call to malloc, or reallocated by a call to realloc.

The number of freed bytes is equivalent to the number of bytes requested when the block was allocated or reallocated.

Example free() and malloc()


/* MALLOC.C: This program allocates memory with * malloc, then frees the memory with free. */ #include <stdlib.h> /* For _MAX_PATH definition */ #include <stdio.h> #include <malloc.h> void main( void ) { char *string; /* Allocate space for a path name */ string = malloc( _MAX_PATH );

if( string == NULL ) printf( "Insufficient memory available\n" ); else { printf( "Memory space allocated for path name\n" ); free( string ); printf( "Memory freed\n" ); } }

CALLOC()
Allocates an array in memory with elements initialized to 0. SYNTAX void *calloc( size_t num, size_t size ); ARGUMENTS Num: Number of elements. Size: Length in bytes of each element.

EXPLAINATION
RETURN VALUES calloc returns a pointer to the allocated space. Example: long *buffer; buffer = (long *)calloc( 40, sizeof( long ) );

REALLOC
Reallocate memory blocks. SYNTAX: void *realloc( void* memblock, size_t size );
PARAMETERS Memblock: Pointer to previously allocated memory block. Size: New size in bytes.

RETURN VALUES realloc returns a void pointer to the reallocated (and possibly moved) memory block.

To get a pointer to a type other than void, use a type cast on the return value.

EXPLAINATION
The realloc function changes the size of an allocated memory block. If memblock is NULL, realloc behaves the same way as malloc and allocates a new block of size bytes.

FILE LOCKING
FLOCK LOCKF Fcntl

FILE AND RECORD LOCKING


File locking : locks an entire file Record locking: allows a process to lock a specified portion of file. Locks the region form starting byte offset in the file and number of bytes from that position.

Range blocking: As unix kernel doesn't support the concept of records. It is the range of the file that is locked. Lockf function is used to lock the region. Lockf function has the following sequence.

Lockf()
SYNTAX #include<unistd.h> int lockf( int fd, int function, long size); PARAMETERS where function has one of the following values F_ULOCK F_LOCK F_TLOCK F_TEST unlock a previously locked region. lock a region ( blocking ) . test and lock a region (non blocking ) . test a region to see if it is locked

BLOCKING AND NON-BLOCKING


Blocking: if a lock is set and the region is already locked by another process, the calling process is put to sleep until the region is available. (F_LOCK) Non blocking: if a process is started and it wants to assure that only a single copy of the it is running . (F_TLOCK) Test and lock the region.

Flock()
#include<sys/file.h> int flock(int fd, int operation); Operations LOCK_SH shared lock LOCK_EX exclusive lock LOCK_UN unlock LOCK_NB dontblockwhenlocking

Fcntl()
fcntl Record locking #include< sys/types.h> #include<unistd.h> #include<fcntl.h>

int fcntl( int filedes, int cmd, .../* struct flock *flockptr */); returns depends on cmd, if ok(F_GETLK, F_SETLK,F_SETLKW)

-1 on error.

Structure of flock struct flock { short l_type; off_t l_start; short l_whence; off_t l_len; pid_t l_pid; };

/* F_RDLCK, F_WRLCK, or F_UNLCK */ /* offset in bytes, relative to l_whence */ /* SEEK_SET, SEEK_CUR, or SEEK_END */ /* length in bytes; 0 means lock to EOF */ /* returned with F_GETLK */

The structure defines the type of lock desired: F_RDLCK( a shared lock ) F_WRLCK ( an exclusive lock ) F_UNLCK ( unlocking a region)

Examples
Example for NON-Blocking if( lockf(fd, F_TEST, size)==0) { rc= lockf( fd, F_LOCK, size); .. } If the size is Zero the record extends from the current offset to the end of the file.

ADVISORY LOCKING
Advisory locking means the operating system maintains the a correct knowledge of which files have been locked. But it doesn't prevents some process from writing to a file that is locked by another process.

A process can ignore the lock and can write to a file that is locked . If the process has adequate permissions.

MANDATORY LOCKING
Operating system checks every read and write requesttoverifythattheoperationdoesntinterfere with a lock held by a process.

You might also like