Contiguous Memory Allocation
Contiguous Memory Allocation
Logical Address is generated by CPU while a program is running. The logical address is virtual address
as it does not exist physically, therefore, it is also known as Virtual Address. This address is used as a
reference to access the physical memory location by CPU. The term Logical Address Space is used for the
set of all logical addresses generated by a program’s perspective. The hardware device called Memory-
Management Unit is used for mapping logical address to its corresponding physical address.
Physical Address identifies a physical location of required data in a memory. The user never directly
deals with the physical address but can access by its corresponding logical address. The user program
generates the logical address and thinks that the program is running in this logical address but the program
needs physical memory for its execution, therefore, the logical address must be mapped to the physical
address by MMU before they are used. The term Physical Address Space is used for all physical
addresses corresponding to the logical addresses in a Logical address space.
faster.
5. A table is maintained by the operating system that maintains the list of available and occupied partition in the
memory space.
6. Swapping and compaction are the two contiguous memory allocation scheme.
4. The process is divided into several blocks and placed in different parts of the memory according to the availability of
memory space.
5. A table is maintained for each process which carries the base addresses of each block that has been acquired by a
process in memory.
creat
This system call is used to create new regular files.
The prototype of creat is
#include <sys/types.h>
#include<unistd.h>
int creat(const char *pathname, mode_t mode);
Returns: file descriptor opened for write-only if OK, -1 on error.
The first argument pathname specifies name of the file to be created.
The second argument mode_t, specifies permission of a file to be accessed by owner group and others.
The creat function can be implemented using open function as:
#define creat(path_name, mode)
open (pathname, O_WRONLY | O_CREAT | O_TRUNC, mode);
read
The read function fetches a fixed size of block of data from a file referenced by a given file descriptor.
The prototype of read function is:
#include<sys/types.h>
#include<unistd.h>
size_t read(int fdesc, void *buf, size_t nbyte);
If successful, read returns the number of bytes actually read.
If unsuccessful, read returns –1.
The first argument is an integer, fdesc that refers to an opened file.
The second argument, buf is the address of a buffer holding any data read.
The third argument specifies how many bytes of data are to be read from the file.
The size_t data type is defined in the <sys/types.h> header and should be the same as unsigned int.
Write ():
The write system call is used to write data into a file.
The write function puts data to a file in the form of fixed block size referred by a given file descriptor.
The prototype of write is
#include<sys/types.h>
#include<unistd.h>
ssize_t write(int fdesc, const void *buf, size_t size);
If successful, write returns the number of bytes actually written.
If unsuccessful, write returns –1.
The first argument, fdesc is an integer that refers to an opened file.
The second argument, buf is the address of a buffer that contains data to be written.
The third argument, size specifies how many bytes of data are in the buf argument.
The return value is usually equal to the number of bytes of data successfully written to a file. (size value)
close
The close system call is used to terminate the connection to a file from a process.
The prototype of the close is
#include<unistd.h>
int close(int fdesc);
If successful, close returns 0.
If unsuccessful, close returns –1.
The argument fdesc refers to an opened file.
Close function frees the unused file descriptors so that they can be reused to reference other files. This is
important because a process may open up to OPEN_MAX files at any time and the close function allows a
process to reuse file descriptors to access more than OPEN_MAX files in the course of its execution.
The close function de-allocates system resources like file table entry and memory buffer allocated to hold the
read/write.
fcntl
The fcntl function helps a user to query or set flags and the close-on-exec flag of any file descriptor.
The prototype of fcntl is
#include<fcntl.h>
int fcntl(int fdesc, int cmd, …);
The first argument is the file descriptor.
The second argument cmd specifies what operation has to be performed.
The third argument is dependent on the actual cmd value.
The possible cmd values are defined in <fcntl.h> header.
cmd value Use
F_GETFL Returns the access control flags of a file descriptor fdesc
F_SETFL Sets or clears access control flags that are specified in the third argument to
fcntl. The allowed access control flags are O_APPEND & O_NONBLOCK
F_GETFD Returns the close-on-exec flag of a file referenced by fdesc. If a return value is
zero, the flag is off; otherwise on.
F_SETFD Sets or clears the close-on-exec flag of a fdesc. The third argument to fcntl is
an integer value, which is 0 to clear the flag, or 1 to set the flag
F_DUPFD Duplicates file descriptor fdesc with another file descriptor. The third
argument to fcntl is an integer value which specifies that the duplicated file
descriptor must be greater than or equal to that value. The return value of
fcntl is the duplicated file descriptor
The fcntl function is useful in changing the access control flag of a file descriptor.
For example: after a file is opened for blocking read-write access and the process needs to change the access
to non-blocking and in write-append mode, it can call:
#include <sys/types.h>
#include <sys/dir.h>
#include <sys/param.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <dirent.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
#include <locale.h>
#include <langinfo.h>
#include <stdint.h>
#include <fcntl.h>
char pathname[MAXPATHLEN];
static int
one (const struct dirent *unused)
{
return 1;
}
int main()
{
int count,i;
struct direct **files;
struct stat statbuf;
char datestring[256];
struct passwd pwent;
struct passwd *pwentp;
struct group grp;
struct group *grpt;
struct tm time;
char buf[1024];
if(!getcwd(pathname, sizeof(pathname)))
die("Error getting pathnamen");
if(count > 0)
{
printf("total %d\n",count);
localtime_r(&statbuf.st_mtime, &time);
/* Get localized date string. */
strftime(datestring, sizeof(datestring), "%F %T", &time);
free (files[i]);
}
free(files);
}
}
C program to implement file
#include <stdio.h>
#include <sys/types.h>
Locking:#include <sys/stat.h>
#include <fcntl.h>
int main()
{
char fname[]="test.txt";
struct flock f;
int fd;
f.l_type = F_WRLCK;
f.l_start = 0;
f.l_whence = SEEK_SET;
f.l_len = 0;