EmbLogic's Blog

structure padding

Most processors require specific memory alignment on variables certain types. Normally the minimum alignment is the size of the basic type in question, fo instance this is common

 

char variables can be byte aligned and appear at any byte boundary

 

short (2 byte) variables must be 2 byte aligned, they can appear at any even byte boundary. This means that 0×10004567 is not a valid location for a short variable but 0×10004566 is.

 

long (4 byte) variables must be 4 byte aligned, they can only appear at byte boundarys that are a multiple of 4 bytes. This means that 0×10004566 is not a valid location for a long variable but 0×10004568 is.

 

Structure padding occurs because the members of the structure must appear at the correect byte boundary, to achieve this the compiler puts in padding bytes (or bits if bit fields are in use) so that the structure members appear in the correct location. Additionally the size of the structure must be such that in an array of the structures all the structures are correctly aligned in memory so there may be padding bytes at the end of the structure too

 

struct example {

char c1;

short s1;

char c2;

long l1;

char c3;

}

 

In this structure, assuming the alignment scheme I have previously stated then

 

c1 can appear at any byte boundary, however s1 must appear at a 2 byte boundary so there is a padding byte between c1 and s1.

 

c2 can then appear in the available memory location, however l1 must be at a 4 byte boundary so there are 3 padding bytes between c2 and l1

 

c3 then appear in the available memory location, however because the structure contains a long member the structure must be 4 byte aligned and must be a multiple of 4 bytes in size. Therefore there are 3 padding bytes at the end of the structure. it would appear in memory in this order

 

c1

padding byte

s1 byte 1

s1 byte 2

c2

padding byte

padding byte

padding byte

l1 byte 1

l1 byte 2

l1 byte 3

l1 byte 4

c3

padding byte

padding byte

padding byte

 

The structure would be 16 bytes long.

 

re-written like this

 

struct example {

long l1;

short s1;

char c1;

char c2;

char c3;

}

 

Then l1 appears at the correct byte alignment, s1 will be correctly aligned so no need for padding between l1 and s1. c1, c2, c3 can appear at any location. The structure must be a multiple of 4 bytes in size since it contains a long so 3 padding bytes appear after c3

 

It appears in memory in the order

 

l1 byte 1

l1 byte 2

l1 byte 3

l1 byte 4

s1 byte 1

s1 byte 2

c1

c2

c3

padding byte

padding byte

padding byte

 

and is only 12 bytes long.

Posted in Uncategorized | Leave a comment

To print master array

#include<stdio.h>
#include<string.h>
#include<fcntl.h>
int main()
{
int i,j,fd,len,maslen=1;
char arr[100]={0};
char masterarr[25]={0};
fd=open(“add.c”,O_RDONLY);
i=0;
while(read(fd,&arr[i],1))
i++;
len=strlen(arr);
for(i=0;i<len;i++)
{
for(j=0;j<maslen;j++)
{
if(j+1==maslen)
{
masterarr[j]=arr[i];
maslen++;
break;
}

else
{
if(masterarr[j]==arr[i])
break;

}

}

}
printf(“masterarr=%s\n”,masterarr);
return 0;
}

Posted in Uncategorized | Leave a comment

how union differ from the structure!!!

#include<stdio.h>

union abc
{
char a;
int b;
int c;

}z;
int main()
{
printf(“size of union %d\n”,sizeof(union abc));//the size of the union is the maximum size of the data type.
z.a=4;
printf(“the value for a is %d\n”,z.a);
z.b=5;
printf(“***********************after assign the value again**********************\n”);
printf(“the value for b is %d\n”,z.b);//as their is only 1 block of memory is allocated so the previous value will be overwrite.
printf(“the value for a is %d\n”,z.a);
return 0;
}

Posted in Uncategorized | Leave a comment

Different ways to make and use the structures in C language.

#include<stdio.h>
2
3
4         struct
5         {
6                 char a;
7                 int b;
8                 int c;
9         }d;//as d is the variable of the structure and this is the structure without its name that’s why we can’t make the further     variable in the main i.e struct<struct name> z is not possible;
10
11         struct abc//name of the structure
12         {
13                 char p;
14                 int q;
15                 int r;
16         }s;//variable of the structure abc and we can make the further variables as struct abc z

18         typedef struct
19         {
20                 char p;
21                 int q;
22                 int r;
23         }a;//its the not the variable its the name of the structure which is typeddef as a new datatype is “a”
24
25 int main()
26 {/************print the same the i.e the size of the structure****************/
27         printf(“size of the first structure is %d\n”,sizeof(d));
28         printf(“size of the second structure is %d\n”,sizeof(struct abc));
29         printf(“size of the third structure is %d\n”,sizeof(a));
30 }

Posted in Uncategorized | Leave a comment

Comparison

Comparison between different processors currently in use.

Posted in Uncategorized | Leave a comment

Use of fopen(), fgetc() and fputc().

#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
int main(int argc,char *argv[])
{
FILE *fd1, *fd2;
char ch;int i;
fd1=fopen(argv[1],”r”);//the read file is to be read from command line arguments.(as this is the high level programming because in this we are using the fopen instead of open as fopen is the function which works on pointer and the return type of this function is address of the buffer. The syntax and the declaration will be check using the man fopen command).
fd2=fopen(“file2″,”w”);//another file into which we have to write

//for(;ch != EOF;)//we can use this also instead of do while loop.
do
{
printf(“%d\n”,i);

ch=fgetc(fd1);
fputc(ch,fd2);
i++;
}while(ch!=EOF);

fclose(fd1);
fclose(fd2);
return 0;
}

Posted in Uncategorized | Leave a comment

package installation from cd

how to change yum configuration??

we want gcc package installation from cd using yum.

Posted in Uncategorized | Leave a comment

ftp configuration

check if package is already present or not by using

rpm -q vsftpd    (as the rpm package for ftp is vsftpd)

if not present then

yum install vsftpd

yum y install

after that service vsftpd start

 

 

Posted in Uncategorized | Leave a comment

FIFO

A FIFO is similar to a pipe. FIFO (First In First Out) is a one-way flow of data. FIFO’s have a name i.e. FIFO is a named pipe. This is the main difference between pipes and FIFOs.

To create FIFO,we need  two header files:

#include <sys/types.h>
#include <sys/stat.h>
and  there are two ways to create FIFO:

int mkfifo(const char *pathname, mode_t mode);

and other way is to use mknod(const char *filename,mode_t mode|S_IFIFO,(dev_t)0);

There is no need to pass the file descriptor is FIFO which is the limitation of pipes. To read and write the data we have to open the FIFO using O_RDONLY to read the data and O_WRONLY to write the data through FIFO. To avoid block on read or block on write,we can use O_NONBLOCK.

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

pipe

DISCRIPTION->
Pipe-> it is a unidirectional channel that is used in inter process commmunication ,in pipe two file discriptor is used these are fd(0)=read & fd(1)=write

RETURN VALUE->
on success zero is returned,if error -1 is returned

 

Posted in Uncategorized | Leave a comment

memory management concept

Memory Management

The memory management subsystem is one of the most important parts of the operating system. Since the early days of computing, there has been a need for more memory than exists physically in a system. Strategies have been developed to overcome this limitation and the most successful of these is virtual memory. Virtual memory makes the system appear to have more memory than is physically present by sharing it among competing processes as they need it.

Virtual memory does more than just make your computer’s memory go farther. The memory management subsystem provides:

 

Large Address Spaces
The operating system makes the system appear as if it has a larger amount of memory than it actually has. The virtual memory can be many times larger than the physical memory in the system. 

Protection
Each process in the system has its own virtual address space. These virtual address spaces are completely separate from each other and so a process running one application cannot affect another. Also, the hardware virtual memory mechanisms allow areas of memory to be protected against writing. This protects code and data from being overwritten by rogue applications. 

Memory Mapping
Memory mapping is used to map image and data files into a process’ address space. In memory mapping, the contents of a file are linked directly into the virtual address space of a process. 

Fair Physical Memory Allocation
The memory management subsystem allows each running process in the system a fair share of the physical memory of the system. 

Shared Virtual Memory
Although virtual memory allows processes to have separate (virtual) address spaces, there are times when you need processes to share memory. For example there could be several processes in the system running the bash command shell. Rather than have several copies of bash, one in each process’s virtual address space, it is better to have only one copy in physical memory and all of the processes running bash share it. Dynamic libraries are another common example of executing code shared between several processes.Shared memory can also be used as an Inter Process Communication (IPC) mechanism, with two or more processes exchanging information via memory common to all of them. Linux supports the Unix System V shared memory IPC.

Virtual Memory

 

An Abstract Model of Virtual Memory

 

s

Figure: Abstract model of Virtual to Physical address mapping

 

Before considering the methods that Linux uses to support virtual memory it is useful to consider an abstract model that is not cluttered by too much detail.

As the processor executes a program it reads an instruction from memory and decodes it. In decoding the instruction, the processor may need to fetch or store the contents of a location in memory. The processor then executes the instruction and moves on to the next instruction in the program. In this way the processor is always accessing memory either to fetch instructions or to fetch and store data.

In a virtual memory system all of these addresses are virtual addresses and not physical addresses. These virtual addresses are converted into physical addresses by the processor based on information held in a set of tables maintained by the operating system.

To make this translation easier, virtual and physical memory are divided into handy sized chunks called pages. These pages are all the same size. They need not be but if they were not, the system would be very hard to administer. Linux on Alpha AXP systems uses 8 Kbyte pages and on Intel x86 systems it uses 4 Kbyte pages. Each of these pages is given a unique number: the page frame number (PFN).

In this paged model, a virtual address is composed of two parts: an offset and a virtual page frame number. If the page size is 4 Kbytes, bits 1-10 of the virtual address contain the offset and bits 12 and above are the virtual page frame number. The processor extracts the virtual page frame number and offset from a virtual address every time it encounters one. Then it matches the virtual page frame number to a physical page and uses the offset to specify how far to go into the page. The processor uses page tables to match the virtual page frame number to the physical page.

The figure above shows the virtual address spaces of two processes, process X and process Y, each with their own page tables. These page tables map each process’ virtual pages into physical pages in memory. This shows that process X’s virtual page frame number 0 is mapped into memory in physical page frame number 1 and that process Y’s virtual page frame number 1 is mapped into physical page frame number 4. Each entry in the page table contains the following information:

 

  • Valid flag. This indicates if this page table entry (PTE) is valid,
  • The physical page frame number that this entry describes
  • Access control information. This describes how the page may be used. Can it be written to? Does it contain executable code?

The page table is accessed using the virtual page frame number as an offset. Virtual page frame 5 would be the 6th element of the table (0 is the first element).

To translate a virtual address into a physical one, the processor must first work out the virtual address’ page frame number and the offset within that virtual page. By making the page size a power of 2 this can be easily done by masking and shifting. Looking again at the figures and assuming a page size of 0×2000 bytes (which is decimal 8192) and an address of 0×2194 in process Y’s virtual address space then the processor would translate that address into offset 0×194 into virtual page frame number 1.

The processor uses the virtual page frame number as an index into the process’ page table to retrieve its page table entry. If the page table entry at that offset is valid, the processor takes the physical page frame number from this entry. If the entry is invalid, the process has accessed a non-existent area of its virtual memory. In this case, the processor cannot resolve the address and must pass control to the operating system so that it can fix things up.

Just how the processor notifies the operating system that the correct process has attempted to access a virtual address for which there is no valid translation is specific to the processor. However the processor delivers it, this is known as a page fault and the operating system is notified of the faulting virtual address and the reason for the page fault.

For a valid page table entry, the processor takes that physical page frame number and multiplies it by the page size to get the address of the base of the page in physical memory. Finally, the processor adds in the offset to the instruction or data that it needs.

Using the above example again, process Y’s virtual page frame number 1 is mapped to physical page frame number 4 which starts at0×8000 (4 x 0×2000). Adding in the 0×194 byte offset gives us a final physical address of 0×8194.

By mapping virtual to physical addresses this way, the virtual memory can be mapped into the system’s physical pages in any order. In the figure above, process X’s virtual page frame number 0 is mapped to physical page frame number 1, whereas virtual page frame number 7 is mapped to physical page frame number 0 although it is higher in virtual memory than virtual page frame number 0. This demonstrates an interesting byproduct of virtual memory; the pages of virtual memory do not have to be present in physical memory in any particular order.

 

Shared Virtual Memory

Virtual memory makes it easy for several processes to share memory. All memory access are made via page tables and each process has its own separate page table. For two processes sharing a physical page of memory, its physical page frame number must appear in a page table entry in both of their page tables.

The figure above shows two processes that each share physical page frame number 4. For process X this is virtual page frame number 4 whereas for process Y this is virtual page frame number 6. This illustrates an interesting point about sharing pages: the shared physical page does not have to exist at the same place in virtual memory for any or all of the processes sharing it.

 

Physical and Virtual Addressing Modes

It does not make much sense for the operating system itself to run in virtual memory. This would be a nightmare situation where the operating system must maintain page tables for itself. Most multi-purpose processors support the notion of a physical address mode as well as a virtual address mode. Physical addressing mode requires no page tables and the processor does not attempt to perform any address translations in this mode. The Linux kernel is linked to run in physical address space.

The Alpha AXP processor does not have a special physical addressing mode. Instead, it divides up the memory space into several areas and designates two of them as physically mapped addresses. This kernel address space is known as KSEG address space and it encompasses all addresses upwards from 0xfffffc0000000000. In order to execute from code linked in KSEG (by definition, kernel code) or access data there, the code must be executing in kernel mode. The Linux kernel on Alpha is linked to execute from address0xfffffc0000310000.

 

Access Control

The page table entries also contain access control information. As the processor is already using the page table entry to map a process’ virtual address to a physical one, it can easily use the access control information to check that the process is not accessing memory in a way that it should not.

There are many reasons why you would want to restrict access to areas of memory. Some memory, such as that containing executable code, is naturally read only memory; the operating system should not allow a process to write data over its executable code. By contrast, pages containing data can be written to, but attempts to execute that memory as instructions should fail. Most processors have at least two modes of execution: kernel and user. This adds a level of security to your operating system. Because it is the core of the operating system and therefore can do most anything, kernel code is only run when the CPU is in kernel mode. You would not want kernel code executed by a user or kernel data structures to be accessible except when the processor is running in kernel mode.

1. What is the difference between Swapping and Paging?

 

Swapping: Whole process is moved from the swap device to the main memory for execution. Process size must be less than or equal to the available main memory. It is easier to implementation and overhead to the system. Swapping systems does not handle the memory more flexibly as compared to the paging systems.

Paging: Only the required memory pages are moved to main memory from the swap device for execution. Process size does not matter. Gives the concept of the virtual memory. It provides greater flexibility in mapping the virtual address space into the physical memory of the machine. Allows more number of processes to fit in the main memory simultaneously. Allows the greater process size than the available physical memory. Demand paging systems handle the memory more flexibly.

 3.What is the main goal of the Memory Management?

 

  1. It decides which process should reside in the main memory,
  2. Manages the parts of the virtual address space of a process which is non-core resident,
  3. Monitors the available main memory and periodically write the processes into the swap device to provide more processes fit in the main memory simultaneously.

 

4. What is a Map?

 

A Map is an Array, which contains the addresses of the free space in the swap device that are allocatable resources, and the number of the resource units available there.

Address Units
1     10,000

This allows First-Fit allocation of contiguous blocks of a resource. Initially the Map contains one entry – address (block offset from the starting of the swap area) and the total number of resources.

Kernel treats each unit of Map as a group of disk blocks. On the allocation and freeing of the resources Kernel updates the Map for accurate information.

5. What is a Region?

A Region is a continuous area of a process’s address space (such as text, data and stack). The kernel in a “Region Table” that is local to the process maintains region. Regions are sharable among the process.

 What is Fork swap?

“fork()” is a system call to create a child process. When the parent process calls “fork()” system call, the child process is created and if there is short of memory then the child process is sent to the read-to-run state in the swap device, and return to the user state without swapping the parent process. When the memory will be available the child process will be swapped into the main memory.

Posted in Linux Internals and System Programming | Leave a comment

Write a C Programme that does not terminate when Cntrl+C is pressed

Write a C program that doesn’t terminate when Ctrl+C is pressed. It prints a message “Cannot be terminated using Ctrl+c” and continues execution.

We can use signal handling in C for this. When Ctrl+C is pressed, SIGINT signal is generated, we can catch this signal and run our defined signal handler. C standard defines following 6 signals in signal.h header file.

SIGABRT – abnormal termination.
SIGFPE – floating point exception.
SIGILL – invalid instruction.
SIGINT – interactive attention request sent to the program.
SIGSEGV – invalid memory access.
SIGTERM – termination request sent to the program.

Additional signals are specified Unix and Unix-like operating systems (such as Linux) defines more than 15 additional signals. See http://en.wikipedia.org/wiki/Unix_signal#POSIX_signals
The standard C library function signal() can be used to set up a handler for any of the above signals.
/* A C program that does not terminate when Ctrl+C is pressed */
#include
#include

/* Signal Handler for SIGINT */
void sigintHandler(int sig_num)
{
/* Reset handler to catch SIGINT next time.
Refer http://en.cppreference.com/w/c/program/signal */
signal(SIGINT, sigintHandler);
printf(“\n Cannot be terminated using Ctrl+C \n”);
fflush(stdout);
}

int main ()
{
/* Set the SIGINT (Ctrl-C) signal handler to sigintHandler
Refer http://en.cppreference.com/w/c/program/signal */
signal(SIGINT, sigintHandler);

/* Infinite loop */
while(1)
{
}
return 0;
}

Ouput: When Ctrl+C was pressed two times

Cannot be terminated using Ctrl+C

Cannot be terminated using Ctrl+C

Posted in Uncategorized | Leave a comment

USB DEVICE CONTROL THROUGH NTERNET

In this project I have controlled a USB device through internet using a web browser. The device could be used to control multiple device connected to the USB device.

The project involved configuring Apache server(alongwith SElinux configuration for apache),writing CGI applications, interfacing the CGI application with the device( which involved the linux IPC mechanism also).

video attached to this post shows how the device is controlled through a web browser on a tablet phone while the USB device was attached to a laptop.

 

Posted in Uncategorized | Leave a comment

ipc prjct with one process

1
2 RCS file: server.c,v
3 Working file: server.c
4 head: 1.1
5 branch:
6 locks: strict
7         root: 1.1
8 access list:
9 symbolic names:
10 keyword substitution: kv
11 total revisions: 1;     selected revisions: 1
12 description:
13 this is server
14 —————————-
15 revision 1.1    locked by: root;
16 date: 2014/03/19 07:21:04;  author: root;  state: Exp;
17 Initial revision
18 =============================================================================

Posted in Uncategorized | Leave a comment

Making the IPC by using the pipe in the sync way.

RCS file: server.c,v
Working file: server.c
head:
branch:
locks: strict
access list:
symbolic names:
keyword substitution: kv
total revisions: 0
description:
this is the server getiing the message from the requesting client and deliver the message to the procedural client.
making this with hel[ of the pipe in simultenously way.
=============================================================================
RCS file: client1.c,v
Working file: client1.c
head:
branch:
locks: strict
access list:
symbolic names:
keyword substitution: kv
total revisions: 0
description:
making the client 1 and this send the signal to the server.
after sending the signal to the server 1 the output is receive by this.
=============================================================================
RCS file: client2.c,v
Working file: client2.c
head:
branch:
locks: strict
access list:
symbolic names:
keyword substitution: kv
total revisions: 0
description:
making tyhe client2 and this send the signal to the server for the operation multiplier.
=============================================================================
RCS file: adder.c,v
Working file: adder.c
head:
branch:
locks: strict
access list:
symbolic names:
keyword substitution: kv
total revisions: 0
description:
this is the procedural client
perform the adding operation.
=============================================================================
RCS file: product.c,v
Working file: product.c
head:
branch:
locks: strict
access list:
symbolic names:
keyword substitution: kv
total revisions: 0
description:
this is the adder for the serce.
performing the multiplication operation.
=============================================================================

Posted in Uncategorized | Leave a comment