EmbLogic's Blog

file i/o (password program)

#include<stdio.h>
#include<stdlib.h>
int main()
{
char *pwd=NULL;
char *buff=NULL;
pwd=(char *)malloc(sizeof(char)*10);
buff=(char *)malloc(sizeof(char)*10);
int i=0,j,ret=0;
int ch;
FILE *fptr=NULL;
printf(“enter the password”);
scanf(“%s”,pwd);
fptr=fopen(“file.txt”,”r”);
if(fptr<0)
{
perror(“fopen”);
return(-1);
}
ch=fgetc(fptr);
for(j=0;j<3;j++)
{
while(ch!=EOF)
{
if(ch!=’\n’)
{
buff[i] = ch;
i++;
}
else
{
ret=strcmp(pwd,buff);
if(ret==0)
{
printf(“login successfully”);
exit(1);
}
else
{
i=0;
}
}
ch=getc(fptr);
}

if(ch==EOF)
{
printf(“try again\n”);
printf(“enter the password”);
scanf(“%s”,pwd);
if(j==2)
{
printf(“\nyou have tried maximum time.\nyour password is blocked\n”);
return (-1);
}
}
}
return 0;
}

Posted in Uncategorized | Leave a comment

Program of concatenate 2 string without strcat

RCS file: fun_3.c,v
Working file: fun_3.c
head:
branch:
locks: strict
access list:
symbolic names:
keyword substitution: kv
total revisions: 0
description:
Program to concatenate two strings without strcat
=============================================================================

Posted in Uncategorized | Leave a comment

some thing about memset

memset- use for fill the date into any type of pointer

see aguments of memset:

memset(void *,int c,size_t n);

return type of memset – memset return pointer of same type of data which fill with memset.

Posted in Uncategorized | Leave a comment

comm. throu pipeb/w two process

access;
symbols;
locks
root:1.1; strict;
comment    @ * @;

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

desc
@req client for pipe.
@

1.1
log
@In

server

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

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

desc
@server for ipc
@

1.1
log
@Initial revision
@
text

proc.client

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

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

desc
@processing client .
@

1.1
log
@Initial revision
@

Posted in Uncategorized | Leave a comment

about pipes

A pipe is a mechanism for interprocess communication; data written to the pipe by one process can be read by another process. The data is handled in a first-in, first-out (FIFO) order. The pipe has no name; it is created for one use and both ends must be inherited from the single process which created the pipe.

A FIFO special file is similar to a pipe, but instead of being an anonymous, temporary connection, a FIFO has a name or names like any other file. Processes open the FIFO by name in order to communicate through it.

A pipe or FIFO has to be open at both ends simultaneously. If you read from a pipe or FIFO file that doesn’t have any processes writing to it (perhaps because they have all closed the file, or exited), the read returns end-of-file. Writing to a pipe or FIFO that doesn’t have a reading process is treated as an error condition; it generates a SIGPIPE signal, and fails with error code EPIPE if the signal is handled or blocked.

Neither pipes nor FIFO special files allow file positioning. Both reading and writing operations happen sequentially; reading from the beginning of the file and writing at the end.

Creating a Pipe

The primitive for creating a pipe is the pipe function. This creates both the reading and writing ends of the pipe. It is not very useful for a single process to use a pipe to talk to itself. In typical use, a process creates a pipe just before it forks one or more child processes. The pipe is then used for communication either between the parent or child processes, or between two sibling processes.

The pipe function is declared in the header file `unistd.h’.Function: int pipe (int filedes[2])

The pipe function creates a pipe and puts the file descriptors for the reading and writing ends of the pipe (respectively) into filedes[0] and filedes[1].

An easy way to remember that the input end comes first is that file descriptor 0 is standard input, and file descriptor 1 is standard output.

If successful, pipe returns a value of 0. On failure, -1 is returned.

Posted in Uncategorized | Leave a comment

Threads Vs Processes

Difference Between threads and processes

Following are some of the major differences between the thread and the processes :

  • Processes do not share their address space while threads executing under same process share the address space.
  • From the above point its clear that processes execute independent of each other and the synchronization between processes is taken care by kernel only while on the other hand the thread synchronization has to be taken care by the process under which the threads are executing
  • Context switching between threads is fast as compared to context switching between processes
  • The interaction between two processes is achieved only through the standard inter process communication while threads executing under the same process can communicate easily as they share most of the resources like memory, text segment etc

User threads Vs Kernel Threads

Threads can exist in user space as well as in kernel space.

A user space threads are created, controlled and destroyed using user space thread libraries. These threads are not known to kernel and hence kernel is nowhere involved in their processing. These threads follow co-operative multitasking where-in a thread releases CPU on its own wish ie the scheduler cannot preempt the thread. Th advantages of user space threads is that the switching between two threads does not involve much overhead and is generally very fast while on the negative side since these threads follow co-operative multitasking so if one thread gets block the whole process gets blocked.

A kernel space thread is created, controlled and destroyed by the kernel. For every thread that exists in user space there is a corresponding kernel thread. Since these threads are managed by kernel so they follow preemptive multitasking where-in the scheduler can preempt a thread in execution with a higher priority thread which is ready for execution. The major advantage of kernel threads is that even if one of the thread gets blocked the whole process is not blocked as kernel threads follow preemptive scheduling while on the negative side the context switch is not very fast as compared to user space threads.

If we talk of Linux then kernel threads are optimized to such an extent that they are considered better than user space threads and mostly used in all scenarios except where prime requirement is that of cooperative multitasking.

Posted in Project 04: FTP based Client Server using Threads and Sockets | Leave a comment

//program using wait statement.//

RCS file: ipc1.c,v
Working file: ipc1.c
head: 1.1
branch:
locks: strict
access list:
symbolic names:
keyword substitution: kv
total revisions: 1; selected revisions: 1
description:
this is program in which i have created child using fork and printed the pid of parent and child.
in this i have also used wait statement.
—————————-
revision 1.1
date: 2014/05/12 07:00:20; author: priya; state: Exp;
Initial revision

Posted in Uncategorized | Leave a comment

program of signal.

RCS file: signal1.c,v
Working file: signal1.c
head: 1.1
branch:
locks: strict
access list:
symbolic names:
keyword substitution: kv
total revisions: 1; selected revisions: 1
description:
this is simple program of signal in which i have used SIGINT and SIG_DLF.
—————————-
revision 1.1
date: 2014/05/12 06:55:15; author: priya; state: Exp;
Initial revision

Posted in Uncategorized | Leave a comment

Character Driver (opening and closing with my routines)

RCS file: start.c,v
3 Working file: start.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 module param used
14 alloc_chardev_region done
15 kmalloc and memset done
16 node created
17 cdev_init and cdev_add done for 5 devices
18 —————————-
19 revision 1.1    locked by: root;
20 date: 2014/05/12 06:42:43;  author: root;  state: Exp;
21 Initial revision
=============================================================================
2 RCS file: my_open.c,v
3 Working file: my_open.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 container_of used
14 then address of sculldev stored in private data
15 wronly flag is checked
16 —————————-
17 revision 1.1    locked by: root;
18 date: 2014/05/12 06:48:42;  author: root;  state: Exp;
19 Initial revision
20 =============================================================================
2 RCS file: my_close.c,v
3 Working file: my_close.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 delared function of s_release
14 just checked if control comes to our routine
15 —————————-
16 revision 1.1    locked by: root;
17 date: 2014/05/12 06:51:24;  author: root;  state: Exp;
18 Initial revision
19 =============================================================================
2 RCS file: app.c,v
3 Working file: app.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 application written
14 open ,close is done succesfully
15 —————————-
16 revision 1.1    locked by: root;
17 date: 2014/05/12 06:54:06;  author: root;  state: Exp;
18 Initial revision
19 =============================================================================

Posted in Uncategorized | Leave a comment

signals

RCS file: signal.c,v
Working file: signal.c
head: 1.1
branch:
locks: strict
access list:
symbolic names:
keyword substitution: kv
total revisions: 1; selected revisions: 1
description:
This of program of signal to find signal and use of SIGINT.
—————————-
revision 1.1
date: 2014/05/12 06:49:58; author: priya; state: Exp;
Initial revision

Posted in Uncategorized | Leave a comment

Major and Minor Numbers

Char devices are accessed through names in the filesystem. Those names are called special files or device files or simply nodes of the filesystem tree; they are conventionally located in the /dev directory. Special files for char drivers are identified by a “c” in the first column of the output of ls -l. Block devices appear in /dev as well, but they are identified by a “b.” The focus of this chapter is on char devices, but much of the following information applies to block devices as well.

If you issue the ls -l command, you’ll see two numbers (separated by a comma) in the device file entries before the date of last modification, where the file length normally appears. These numbers are the major device number and minor device number for the particular device. The following listing shows a few devices as they appear on a typical system. Their major numbers are 1, 4, 7, and 10, while the minors are 1, 3, 5, 64, 65, and 129.

 crw-rw-rw- 1 root   root    1, 3   Feb 23 1999  null
 crw------- 1 root   root   10, 1   Feb 23 1999  psaux
 crw------- 1 rubini tty     4, 1   Aug 16 22:22 tty1
 crw-rw-rw- 1 root   dialout 4, 64  Jun 30 11:19 ttyS0
 crw-rw-rw- 1 root   dialout 4, 65  Aug 16 00:00 ttyS1
 crw------- 1 root   sys     7, 1   Feb 23 1999  vcs1
 crw------- 1 root   sys     7, 129 Feb 23 1999  vcsa1
 crw-rw-rw- 1 root   root    1, 5   Feb 23 1999  zero

The major number identifies the driver associated with the device. For example, /dev/null and /dev/zero are both managed by driver 1, whereas virtual consoles and serial terminals are managed by driver 4; similarly, both vcs1 and vcsa1 devices are managed by driver 7. The kernel uses the major number at open time to dispatch execution to the appropriate driver.

The minor number is used only by the driver specified by the major number; other parts of the kernel don’t use it, and merely pass it along to the driver. It is common for a driver to control several devices (as shown in the listing); the minor number provides a way for the driver to differentiate among them.

Version 2.4 of the kernel, though, introduced a new (optional) feature, the device file system or devfs. If this file system is used, management of device files is simplified and quite different; on the other hand, the new filesystem brings several user-visible incompatibilities, and as we are writing it has not yet been chosen as a default feature by system distributors. The previous description and the following instructions about adding a new driver and special file assume that devfs is not present. The gap is filled later in this chapter, in “The Device Filesystem”.

When devfs is not being used, adding a new driver to the system means assigning a major number to it. The assignment should be made at driver (module) initialization by calling the following function, defined in <linux/fs.h>:

 int register_chrdev(unsigned int major, const char *name,
      struct file_operations *fops);

The return value indicates success or failure of the operation. A negative return code signals an error; a 0 or positive return code reports successful completion. The major argument is the major number being requested, name is the name of your device, which will appear in /proc/devices, and fops is the pointer to an array of function pointers, used to invoke your driver’s entry points, as explained in “File Operations”, later in this chapter.

The major number is a small integer that serves as the index into a static array of char drivers; “Dynamic Allocation of Major Numbers” later in this chapter explains how to select a major number. The 2.0 kernel supported 128 devices; 2.2 and 2.4 increased that number to 256 (while reserving the values 0 and 255 for future uses). Minor numbers, too, are eight-bit quantities; they aren’t passed to register_chrdev because, as stated, they are only used by the driver itself. There is tremendous pressure from the developer community to increase the number of possible devices supported by the kernel; increasing device numbers to at least 16 bits is a stated goal for the 2.5 development series.

Once the driver has been registered in the kernel table, its operations are associated with the given major number. Whenever an operation is performed on a character device file associated with that major number, the kernel finds and invokes the proper function from the file_operations structure. For this reason, the pointer passed to register_chrdev should point to a global structure within the driver, not to one local to the module’s initialization function.

The next question is how to give programs a name by which they can request your driver. A name must be inserted into the /dev directory and associated with your driver’s major and minor numbers.

The command to create a device node on a filesystem is mknod; superuser privileges are required for this operation. The command takes three arguments in addition to the name of the file being created. For example, the command

 mknod /dev/scull0 c 254 0

creates a char device (c) whose major number is 254 and whose minor number is 0. Minor numbers should be in the range 0 to 255 because, for historical reasons, they are sometimes stored in a single byte. There are sound reasons to extend the range of available minor numbers, but for the time being, the eight-bit limit is still in force.

Please note that once created by mknod, the special device file remains unless it is explicitly deleted, like any information stored on disk. You may want to remove the device created in this example by issuing rm /dev/scull0.

Posted in Uncategorized | Leave a comment

Character Device Driver

The Internal Representation of Device Numbers
Within the kernel, the dev_t type (defined in <linux/types.h>) is used to hold device
numbers—both the major and minor parts. As of Version 2.6.0 of the kernel, dev_t is
a 32-bit quantity with 12 bits set aside for the major number and 20 for the minor
number. Your code should, of course, never make any assumptions about the inter-
nal organization of device numbers; it should, instead, make use of a set of macros
found in <linux/kdev_t.h>. To obtain the major or minor parts of a dev_t, use:
MAJOR(dev_t dev);
MINOR(dev_t dev);
If, instead, you have the major and minor numbers and need to turn them into a dev_t,
use:
MKDEV(int major, int minor);
Note that the 2.6 kernel can accommodate a vast number of devices, while previous
kernel versions were limited to 255 major and 255 minor numbers. One assumes

Posted in Uncategorized | Leave a comment

A parent having multiple childern

#include<stdio.h>
#include<sys/types.h>
int main()
{
int i,n;
pid_t pid;
printf(“enter the no of child\n”);
scanf(“%d”,&n);
pid=fork();
for(i=1;i<n;i++) { if(pid>0)
pid=fork();
}
printf(“my id is->%d and my parent id is -> %d\n”,getpid(),getppid());
return 0;
}

Posted in Uncategorized | Leave a comment

character driver

A Linux driver is a Linux module which can be loaded and linked to the kernel at runtime. The driver operates in kernel space and becomes part of the kernel once loaded, the kernel being monolithic. It can then access the symbols exported by the kernel.

When the device driver module is loaded, the driver first registers itself as a driver for a particular device specifying a particular Major number.

It uses the call register_chrdev function for registration. The call takes the Major number, Minor number, device name and an address of a structure of the type file_operations(discussed later) as argument. In our example, we will be using a major number of 89 . The choice of major number is arbitrary but it has to be unique on the system.

The syntax of register_chrdev is

int register_chrdev(unsigned int major,const char *name,struct file_operations *fops)

Driver is unregistered by calling the unregister_chrdev function.

Since device driver is a kernel module, it should implement init_module and cleanup_module functions. The register_chrdev call is done in the init_module function  and unregister_chrdev call is done in the cleanup_module function.

The register_chrdev call returns a non-negative number on success. If we specify the Major number as 0, the kernel returns a Major number unique at that instant which can be used to create a device file.
A device file can be created either before the driver is loaded if we know the major and minor number beforehand or it can be created later after letting the driver specify a major number for us.

Creating a device file

A device file is a special file. It can’t just be created using cat or gedit or shell redirection for that matter. The shell command mknod is usually used to create device file. The syntax of mknod is

mknod path type major minor

Posted in Uncategorized | Leave a comment

ipc server using pipes

#include”header.h”
#include”declaration.h”

int main()
{
int ret,len,sum,sub,n;
int r11[2];
int r12[2];
int r21[2];
int r22[2];
int p11[2];
int p12[2];
int p21[2];
int p22[2];
struct req rq;
char br11[4],br12[4],br21[4],br22[4],bp11[4],bp12[4],bp21[4],bp22[4];
ret=pipe(r11);
ret=pipe(r21);
ret=pipe(r22);
ret=pipe(r12);
ret=pipe(p11);
ret=pipe(p21);
ret=pipe(p22);
ret=pipe(p12);
ret=fork();
if(ret == 0)
{
sprintf(br11,”%d”,r11[1]);
sprintf(br12,”%d”,r12[0]);
execl(“./rq1″,”./rq1″,br11,br12,NULL);
}
else
{
len=sizeof(struct req);
read(r11[0],&rq,len);
printf(“rq.a=%d\nrq.b=%d\nrq.c=%c\n”,rq.a,rq.b,rq.opr);
ret=fork();
if(ret == 0)
{
sprintf(bp11,”%d”,p11[0]);
sprintf(bp12,”%d”,p12[1]);
execl(“./pq1″,”./pq1″,bp11,bp12,NULL);
}
else
{
write(p11[1],&rq,len);
read(p12[0],&sum,4);
printf(“\nserver ….sum =%d\n”,sum);
}
ret=write(r12[1],&sum,4);
printf(“written for server at rq1=%d”,ret);
ret=fork();
if(ret==0)
{
sprintf(br21,”%d”,r21[1]);
sprintf(br22,”%d”,r22[0]);
execl(“./rq2″,”./rq2″,br21,br22,NULL);
}
else
{
len=sizeof(struct req);
read(r21[0],&rq,len);
printf(“rq.a=%d\nrq.b=%d\nrq.c=%c\n”,rq.a,rq.b,rq.opr);
ret=fork();
if(ret == 0)
{
sprintf(bp21,”%d”,p21[0]);
sprintf(bp22,”%d”,p22[1]);
execl(“./pq2″,”./pq2″,bp21,bp22,NULL);
}
else
{
write(p21[1],&rq,len);
read(p22[0],&sub,4);
printf(“\nserver ….sub =%d\n”,sum);
ret=write(r22[1],&sub,4);
}
}
return 0;
}
}

Posted in Uncategorized | Leave a comment