EmbLogic's Blog

c program using array.

>> this is a program to find the average of array elements.
>> .
initial revision: 1.1
done
[root@localhost priya]# rlog avg.c,v

RCS file: avg.c,v
Working file: avg.c
head: 1.1
branch:
locks: strict
access list:
symbolic names:
keyword substitution: kv
total revisions: 1; selected revisions: 1
description:
this is a program to find the average of array elements.
—————————-
revision 1.1
date: 2014/03/06 11:21:12; author: root; state: Exp;
Initial revision
=============================================================================

Posted in Uncategorized | Leave a comment

program of pointer.

vim,v ci: vim: No such file or directory
ptr5.c,v > this is the program of pointer in which we analyse the code
>> .
initial revision: 1.1
done
[root@localhost priya]# rlog vim ptr5.c,v
rlog: RCS/vim,v: No such file or directory

RCS file: ptr5.c,v
Working file: ptr5.c
head: 1.1
branch:
locks: strict
access list:
symbolic names:
keyword substitution: kv
total revisions: 1; selected revisions: 1
description:
this is the program of pointer in which we analyse the code
—————————-
revision 1.1
date: 2014/03/08 10:17:21; author: root; state: Exp;
Initial revision

Posted in Uncategorized | Leave a comment

program of merging elements of two arrays and sorting them in ascending order.

RCS file: merge.c,v
Working file: merge.c
head: 1.1
branch:
locks: strict
access list:
symbolic names:
keyword substitution: kv
total revisions: 1; selected revisions: 1
description:
This is the program of array in which i have inserted elements of two array and then sorted them in ascending order of array.
—————————-
revision 1.1
date: 2014/03/08 10:28:04; author: root; state: Exp;
Initial revision

Posted in Uncategorized | Leave a comment

program of function.

head 1.1;
access;
symbols;
locks; strict;
comment @ * @;

1.1
date 2014.03.11.09.41.42; author root; state Exp;
branches;
next ;

desc
@this is the program in which we have added the distance in feet and inces using function.
@

1.1
log
@Initial revision
@
text
@#include
float input(float* ,float*, float*,float *);
float add(float*,float*,float*,float*,float*,float*);
void display( float*,float*,float*,float*,float*,float*);
int main()
{
float f1,f2,f3;
float i1,i2,i3;
input(&f1,&f2,&i1,&i2);
// printf(“enter the val;ue of i and f are %f %f %f %f ” ,f1,f2,,i1,i2);
add(&i1,&i2,&f1,&f2,&i3,&f3);
display (&i1,&i2,&f1,&f2,&f3,&i3);
return 0;
}
float input (float *a, float *b ,float *c, float *d)
{
printf(“enter the value of i1,i2,f1,f2″);
scanf(” %f %f %f %f”,a,b,c,d);
}
float add(float *x,float *y,float *z,float *p,float *q, float *s)
{
*s=*x+*y;
*q=*z+*p;
if(*s>=12)
{
*q=*q+1;
*s=*s-12;
}
}
void display(float*x,float*y,float*z,float*p,float*q,float*s)
{
printf(“\n(%f-%f)+(%f-%f)=(%f-%f)”,*z,*x,*p,*y,*q,*s);
}

@

Posted in Uncategorized | Leave a comment

head 1.1;
access;
symbols;
locks; strict;
comment @ * @;

1.1
date 2014.03.11.10.27.11; author root; state Exp;
branches;
next ;

desc
@this is the program of function in which we have to enter two string.
@

1.1
log
@Initial revision
@
text
@#include
#include
#include
int input(char* ,char*);
//int findpos(int *);
int main()
{
char *p1,*p2;
input (p1,p2);
return 0;
}
int input(char *x, char *y)
{
x=(char *)malloc(20);
y=(char *)malloc(20);
printf(“enter the first string\n”);
gets(x);
printf(“enter the second string”);
gets(y);
return 0;
}
//int findpos (char *a,char *b, char *c)

@

Posted in Uncategorized | Leave a comment

program of creating file using function.

head 1.1;
access;
symbols;
locks; strict;
comment @ * @;

1.1
date 2014.03.12.14.09.08; author root; state Exp;
branches;
next ;

desc
@this is the program in which i have created a file and then used write command using function.
@

1.1
log
@Initial revision
@
text
@#include
#include
#include
int writefile(int);
int openfile();
int main()
{
int fd,a,ret;
char *buff;
buff=(char*)malloc(10);
fd=openfile();
printf(“fd is %d\n”,fd);
a=writefile(fd);
printf(“no of bytes in file is %d”,a);
lseek(fd,0,SEEK_SET);
ret=read(fd,buff,5);
printf(“\n no of bytes is %d and string is %s”,ret,buff);
return 0;
}
int openfile()
{
int fd;
fd=open(“file”,O_CREAT|O_RDWR,0666);
if(fd<0)
{
perror("file open");
exit(-1);
}
return fd;
}
int writefile(int fd)
{
int ret;
const char *a="priya";
ret=write(fd,a,5);
return ret;
}

Posted in Uncategorized | Leave a comment

project -mdc.

head 1.1;
access;
symbols;
locks; strict;
comment @ * @;

1.1
date 2014.03.14.09.00.57; author root; state Exp;
branches;
next ;

desc
@this is the program of multiple data compression in which i have ope and read the file.
in this i have read the file character wise.
@

1.1
log
@Initial revision
@
text
@#include”header.h”
int main(int argc, char *argv[])
{
int fd,ndc,i,ret;
char ch;
if(argc !=2)
{
printf(“insufficient arguments!!”);
goto OUT;
}
fd=openfile(argv[1]);
do
{
ret=read(fd,&ch,1);
if(ret<0)
{
perror("read");
close(fd);
goto OUT;
}
if(ch!=10)
{
masterarray[ndc]=ch;
ndc++;
}
}
while(ch!=10);
for(i=0;i<ndc;i++)
printf("read %d char ;ch=%c\n",ret,masterarray[i]);
return 0;
OUT:
return -1;
}
int openfile(char *ipfile)
{
int fd;
fd=open(ipfile,O_RDONLY);
if(fd<0)
{
perror("open");
goto OUT;
}
printf("fd= %d\n",fd);
return fd;
OUT:
return -1;
}

Posted in Uncategorized | Leave a comment

Work on FIFO.

RCS file: 1.c,v
Working file: 1.c
head: 1.1
branch:
locks: strict
root: 1.1
access list:
symbolic names:
keyword substitution: kv
total revisions: 1;    selected revisions: 1
description:
make the fifo
do the write operation on fifo.
—————————-
revision 1.1    locked by: root;
date: 2014/03/14 07:27:06;  author: root;  state: Exp;
Initial revision
=============================================================================
RCS file: 2.c,v
Working file: 2.c
head: 1.1
branch:
locks: strict
access list:
symbolic names:
keyword substitution: kv
total revisions: 1;    selected revisions: 1
description:
here we read from the FIFO
—————————-
revision 1.1
date: 2014/03/14 07:30:29;  author: root;  state: Exp;
Initial revision
=============================================================================

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

C library function – atoi()

The library function atoi converts string argument to integer just like sscanf.
This function returns the converted integer number as an int value. If no valid conversion could be performed, it returns zero.

Posted in Uncategorized | Leave a comment

Client server communication using linux and IPC having two client

RCS file: server.c,v
Working file: server.c
head: 1.1
branch:
locks: strict
ankit: 1.1
access list:
symbolic names:
keyword substitution: kv
total revisions: 1;    selected revisions: 1
description:
make the pipe for the client 1
make the fork in the child process for the client 1
client1 is write and server is to be read what ever be write on  client1
—————————-
revision 1.1    locked by: ankit;
date: 2014/03/11 16:51:17;  author: ankit;  state: Exp;
Initial revision
======

RCS file: client1.c,v
Working file: client1.c
head: 1.11
branch:
locks: strict
root: 1.11
access list:
symbolic names:
keyword substitution: kv
total revisions: 11;    selected revisions: 11
description:
make the client1
and perform the write operation here.

—————————-
revision 1.11   locked by: root;
date: 2014/03/13 07:31:58;  author: root;  state: Exp;  lines: +1 -1
*** empty log message ***
—————————-
revision 1.10
date: 2014/03/13 07:22:50;  author: root;  state: Exp;  lines: +1 -1
*** empty log message ***
—————————-
revision 1.9
date: 2014/03/13 07:21:10;  author: root;  state: Exp;  lines: +1 -1
*** empty log message ***
—————————-
revision 1.8
date: 2014/03/13 07:19:58;  author: root;  state: Exp;  lines: +1 -1
*** empty log message ***
—————————-
revision 1.7
date: 2014/03/13 07:17:30;  author: root;  state: Exp;  lines: +1 -0
“logfile” 59L, 1892C

revision 1.6
date: 2014/03/13 07:12:17;  author: root;  state: Exp;  lines: +1 -1
*** empty log message ***
—————————-
revision 1.5
date: 2014/03/13 07:10:37;  author: root;  state: Exp;  lines: +1 -1
*** empty log message ***
—————————-
revision 1.4
date: 2014/03/13 07:03:04;  author: root;  state: Exp;  lines: +1 -1
*** empty log message ***
—————————-
revision 1.3
date: 2014/03/13 07:01:30;  author: root;  state: Exp;  lines: +2 -2
*** empty log message ***
—————————-
revision 1.2
date: 2014/03/13 05:39:57;  author: root;  state: Exp;  lines: +1 -1
make the client and this will pass one operator and two operand to the server
—————————-
revision 1.1
date: 2014/03/11 16:54:32;  author: root;  state: Exp;
Initial revision
=============================================================================
RCS file: client2.c,v
Working file: client2.c
head: 1.1
branch:
locks: strict
root: 1.1
access list:
symbolic names:
keyword substitution: kv
total revisions: 1;    selected revisions: 1
description:
the client 2 is formed and this client request to do the multipicatation to the server.
—————————-
revision 1.1    locked by: root;
date: 2014/03/13 08:51:25;  author: root;  state: Exp;
Initial revision
=============================================================================

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

static and shared libraries

When a C program is compiled, the compiler generates object code. After generating the object code, the compiler also invokes linker. One of the main tasks for linker is to make code of library functions (eg printf(), scanf(), sqrt(), ..etc) available to your program. A linker can accomplish this task in two ways, by copying the code of library function to your object code, or by making some arrangements so that the complete code of library functions is not copied, but made available at run-time.

Static Linking and Static Libraries is the result of the linker making copy of all used library functions to the executable file. Static Linking creates larger binary files, and need more space on disk and main memory. Examples of static libraries (libraries which are statically linked) are, .a files in Linux and .lib files in Windows.

Steps to create a static library Let us create and use a Static Library in UNIX or UNIX like OS.
1. Create a C file that contains functions in your library.

/* Filename: lib_mylib.c */
#include <stdio.h>
void fun(void)
{
  printf("fun() called from a static library");
}

We have created only one file for simplicity. We can also create multiple files in a library.

2. Create a header file for the library

/* Filename: lib_mylib.h */
void fun(void);

3. Compile library files.

 gcc -c lib_mylib.c -o lib_mylib.o

4. Create static library. This step is to bundle multiple object files in one static library (see ar for details). The output of this step is static library.

 ar rcs lib_mylib.a lib_mylib.o

5. Now our static library is ready to use. At this point we could just copy lib_hello_static.a somewhere else to use it. For demo purposes, let us keep the library in the current directory.

Let us create a driver program that uses above created static library.
1. Create a C file with main function

/* filename: driver.c  */
#include "lib_mylib.h"

Static and Dynamic Libraries | Set 1

When a C program is compiled, the compiler generates object code. After generating the object code, the compiler also invokes linker. One of the main tasks for linker is to make code of library functions (eg printf(), scanf(), sqrt(), ..etc) available to your program. A linker can accomplish this task in two ways, by copying the code of library function to your object code, or by making some arrangements so that the complete code of library functions is not copied, but made available at run-time.

Static Linking and Static Libraries is the result of the linker making copy of all used library functions to the executable file. Static Linking creates larger binary files, and need more space on disk and main memory. Examples of static libraries (libraries which are statically linked) are, .a files in Linux and .lib files in Windows.

Steps to create a static library Let us create and use a Static Library in UNIX or UNIX like OS.
1. Create a C file that contains functions in your library.

/* Filename: lib_mylib.c */
#include <stdio.h>
void fun(void)
{
  printf("fun() called from a static library");
}

We have created only one file for simplicity. We can also create multiple files in a library.

2. Create a header file for the library

/* Filename: lib_mylib.h */
void fun(void);

3. Compile library files.

 gcc -c lib_mylib.c -o lib_mylib.o

4. Create static library. This step is to bundle multiple object files in one static library (see ar for details). The output of this step is static library.

 ar rcs lib_mylib.a lib_mylib.o

5. Now our static library is ready to use. At this point we could just copy lib_hello_static.a somewhere else to use it. For demo purposes, let us keep the library in the current directory.

Let us create a driver program that uses above created static library.
1. Create a C file with main function

/* filename: driver.c  */
#include "lib_mylib.h"
void main()
{
  fun();
}

2. Compile the driver program.

gcc -c driver.c -o driver.o

3. Link the compiled driver program to the static library. Note that -L. is used to tell that the static library is in current folder (See this for details of -L and -l options).

gcc -o driver driver.o -L. -l_mylib

4. Run the driver program

./driver 
fun() called from a static library

Following are some important points about static libraries.
1. For a static library, the actual code is extracted from the library by the linker and used to build the final executable at the point you compile/build your application.

2. Each process gets its own copy of the code and data. Where as in case of dynamic libraries it is only code shared, data is specific to each process. For static libraries memory footprints are larger. For example, if all the window system tools were statically linked, several tens of megabytes of RAM would be wasted for a typical user, and the user would be slowed down by a lot of paging.

3. Since library code is connected at compile time, the final executable has no dependencies on the the library at run time i.e. no additional run-time loading costs, it means that you don’t need to carry along a copy of the library that is being used and you have everything under your control and there is no dependency.

4. In static libraries, once everything is bundled into your application, you don’t have to worry that the client will have the right library (and version) available on their system.

5.One drawback of static libraries is, for any change(up-gradation) in the static libraries, you have to recompile the main program every time.

6. One major advantage of static libraries being preferred even now “is speed”. There will be no dynamic querying of symbols in static libraries. Many production line software use static libraries even today.

Dynamic linking and Dynamic Libraries Dynamic Linking doesn’t require the code to be copied, it is done by just placing name of the library in the binary file. The actual linking happens when the program is run, when both the binary file and the library are in memory. Examples of Dynamic libraries (libraries which are linked at run-time) are, .so in Linux and .dll in Windows.

We will soon be covering more points on Dynamic Libraries and steps to create them.

This article is compiled by Abhijit Saha and reviewed by GeeksforGeeks team. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

2. Compile the driver program.

Posted in Linux Internals and System Programming, Project 00: Linux System / Network Administration | Leave a comment

create, open ,written and read file using functions()

head    1.1;
access;
symbols;
locks; strict;
comment    @ * @;

1.1
date    2014.03.12.12.30.53;    author root;    state Exp;
branches;
next    ;

desc
@i have created,read and write a file using function.
@

1.1
log
@Initial revision
@
text

Posted in Uncategorized | Leave a comment

HOW TO USE atoi() function for conversion of strings into integers……..

RCS file: atoi.c,v

3 Working file: atoi.c

4 head: 1.3

5 branch:

6 locks: strict

7 access list:

8 symbolic names:

9 keyword substitution: kv

10 total revisions: 3; selected revisions: 3

11 description:

12 this program tells the use of atoi() function

13 the atoi function is declared in the header file stdlib.h

14 in this program the string to be converted into integer contains numeric characters in the starting

15 the atoi converts the numeric characters encountered in string before encountering any alphabet or any special symbol into an inte ger

16 the atoi function returns the integer converted from string

17 —————————-

18 revision 1.3

19 date: 2014/03/13 06:00:49; author: root; state: Exp; lines: +1 -1

20 this revision of program uses atoi() functon to convert a string starting with special symbols or characters

21 he function returns 0

22 —————————-

23 revision 1.2

24 date: 2014/03/13 05:58:47; author: root; state: Exp; lines: +1 -1

25 this revision of the program has converted a string which is starting with alphaets

26 in this the atoi function returns 0

27 —————————-

28 revision 1.1

29 date: 2014/03/13 05:54:27; author: root; state: Exp;

30 Initial revision

31 =============================================================================

~

Posted in Data Structures with C | Leave a comment

A typical memory representation of C program

A typical memory representation of C program consists of following sections.

1. Text segment
2. Initialized data segment
3. Uninitialized data segment
4. Stack
5. Heap


A typical memory layout of a running process

1. Text Segment:
A text segment , also known as a code segment or simply as text, is one of the sections of a program in an object file or in memory, which contains executable instructions.

As a memory region, a text segment may be placed below the heap or stack in order to prevent heaps and stack overflows from overwriting it.

Usually, the text segment is sharable so that only a single copy needs to be in memory for frequently executed programs, such as text editors, the C compiler, the shells, and so on. Also, the text segment is often read-only, to prevent a program from accidentally modifying its instructions.

2. Initialized Data Segment:
Initialized data segment, usually called simply the Data Segment. A data segment is a portion of virtual address space of a program, which contains the global variables and static variables that are initialized by the programmer.

Note that, data segment is not read-only, since the values of the variables can be altered at run time.

This segment can be further classified into initialized read-only area and initialized read-write area.

For instance the global string defined by char s[] = “hello world” in C and a C statement like int debug=1 outside the main (i.e. global) would be stored in initialized read-write area. And a global C statement like const char* string = “hello world” makes the string literal “hello world” to be stored in initialized read-only area and the character pointer variable string in initialized read-write area.

Ex: static int i = 10 will be stored in data segment and global int i = 10 will also be stored in data segment

3. Uninitialized Data Segment:
Uninitialized data segment, often called the “bss” segment, named after an ancient assembler operator that stood for “block started by symbol.” Data in this segment is initialized by the kernel to arithmetic 0 before the program starts executing

uninitialized data starts at the end of the data segment and contains all global variables and static variables that are initialized to zero or do not have explicit initialization in source code.

For instance a variable declared static int i; would be contained in the BSS segment.
For instance a global variable declared int j; would be contained in the BSS segment.

4. Stack:
The stack area traditionally adjoined the heap area and grew the opposite direction; when the stack pointer met the heap pointer, free memory was exhausted. (With modern large address spaces and virtual memory techniques they may be placed almost anywhere, but they still typically grow opposite directions.)

The stack area contains the program stack, a LIFO structure, typically located in the higher parts of memory. On the standard PC x86 computer architecture it grows toward address zero; on some other architectures it grows the opposite direction. A “stack pointer” register tracks the top of the stack; it is adjusted each time a value is “pushed” onto the stack. The set of values pushed for one function call is termed a “stack frame”; A stack frame consists at minimum of a return address.

Stack, where automatic variables are stored, along with information that is saved each time a function is called. Each time a function is called, the address of where to return to and certain information about the caller’s environment, such as some of the machine registers, are saved on the stack. The newly called function then allocates room on the stack for its automatic and temporary variables. This is how recursive functions in C can work. Each time a recursive function calls itself, a new stack frame is used, so one set of variables doesn’t interfere with the variables from another instance of the function.

5. Heap:
Heap is the segment where dynamic memory allocation usually takes place.

The heap area begins at the end of the BSS segment and grows to larger addresses from there.The Heap area is managed by malloc, realloc, and free, which may use the brk and sbrk system calls to adjust its size (note that the use of brk/sbrk and a single “heap area” is not required to fulfill the contract of malloc/realloc/free; they may also be implemented using mmap to reserve potentially non-contiguous regions of virtual memory into the process’ virtual address space). The Heap area is shared by all shared libraries and dynamically loaded modules in a process.

Posted in Data Structures with C | Leave a comment

pipes

implemented interprocess communication using pipes.

Posted in Uncategorized | Leave a comment