LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-05-2007, 10:46 AM   #1
tsakunthala
LQ Newbie
 
Registered: Jan 2007
Posts: 2

Rep: Reputation: 0
Creating random seed different for different processors


I need to create random numbers within specific range but different for different processors..Is that possible in C? for example when i call random function to select numbers within 10...calling this thrice i may get 0,4,7 on Processor1 and 3,5,9 on another processor..
 
Old 01-05-2007, 11:33 AM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 34
First off: you have a bad assumption. Processor affinity in Linux is set to be "let this process use all CPU's". This means that the process will bounce around from CPU to CPU - it is not required not stay on just one CPU for the lifetime of the process.

sched_set_affinity() can be used to alter the bitmap in the task_struct's cpus_allowed bitmask - force the process to run on just one cpu.

Once you know which processor number to use, you can call some thing like this to seed the PNRG: Don't know whether you are using rand() or not, this uses srand. Pass it the cpu number you selected.
Code:
#include <stdlib.h>
#include <time.h>

int foo(const long processor_number)
{
	struct timeval seed;
	struct timezone tzp;
	int retval=0;
	
	if(!gettimeofday (&seed, &tzp))
	{
		srand(seed.tv_usec/processor_number);
		retval=1;
	}   
	return retval;
 }
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: TimeSys to Host Educational Webinars for Embedded Developers Creating Custom Linux Platforms for PowerPC and x86 Processors LXer Syndicated Linux News 0 01-13-2006 01:01 AM
system hanging on 'Saving random seed' PennyroyalFrog Linux - Newbie 9 10-01-2005 02:45 PM
Kernel panic while saving random seed SAnton Linux - Software 1 09-27-2005 03:01 AM
Creating random numbers from shell with /dev/random khermans Linux - General 1 07-13-2004 12:12 PM
using /dev/(u)random for seed ? PTBmilo Programming 2 04-15-2003 01:19 AM


All times are GMT -5. The time now is 09:18 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration