Welcome to Linux Forums! With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.
Find the answer to your Linux question:
New to Linux Forums? Register here for free!
    Linux Forums > GNU Linux Zone > The Linux Kernel > writing a simple char device, help needed

Forgot Password?
 The Linux Kernel   Compiling, theory, programming or other discussion about the linux kernel

Site Navigation
Linux Articles
Linux Forums
Linux Downloads
Linux Hosting
Free Magazines
Job Board
IRC Chat
RSS Feeds


Linux Forum Topics
Linux Forums
Your Distro
Linux Resources
GNU Linux Zone
The Community
Reply
 
Thread Tools Display Modes
Old 06-17-2009   #1 (permalink)
Just Joined!
 
Join Date: May 2009
Posts: 24
writing a simple char device, help needed

hi there,

I am writing a simple char device that generates pseudo-random numbers, the problem is when I try to read from that char device, i.e when I write this command "cat (device's dictionary)" it reads 3 times which is really weird. anyone can help me out please.

here is some of my code

Code:
unsigned int random_seed;
char msg[256];
int msg_len;

unsigned char rg_hw()
{
	random_seed = random_seed * 1103515245 + 12345;
	return (unsigned char)((unsigned int)(random_seed / 65536) % 256);
}

ssize_t read(struct file *file, char __user *buf, size_t len, loff_t *offset)
{
	printk(KERN_ALERT "reading a char device, seed = %d\n", random_seed);

	sprintf (msg, "%d", rg_hw());
	msg_len = strlen(msg);	

	if (*offset >= msg_len)
	{
	      	return 0;  
	}

  	if (*offset + len >= msg_len)
	{
      		len = msg_len - *offset;  
	}

	if(copy_to_user(buf, msg + *offset , len))
	{
		printk(KERN_EMERG "loool\n");
		return -EFAULT;
	}

	*offset += len;
	return len;
}
here is the printk messages i wrote to test my read device
Code:
Starting char device
allocated a new major, 248
opend the char device, with minor 0
reading a char device, seed = 0
reading a char device, seed = 12345
reading a char device, seed = -740551042
releasing the char device.
thanks in advance

EDIT: upon further testing, i think i found out what is the problem, i made a tester from user space and used the functions open and read, now the problem is that reads only one random number then stops, is there a way to make it read as many times as the buffer's size?
a_n_h is offline  


Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Free Magazines
Run Your Own Web Server Using Linux & Apache - Free 191 Page Preview
Learn about everything you'll need to build and maintain your Linux servers, and to deploy Web applications to them.
subscribe
Open Source Security Myths Dispelled
Dispel the five major myths surrounding Open Source Security and gain the tools necessary to make a truly informed decision for your IT organization
subscribe
InformationWeek
InformationWeek is the only newsweekly you'll need to stay on top of the latest developments in information technology.
subscribe



All times are GMT. The time now is 05:46 PM.






© 2000 - 2009 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.3.0 RC2