Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 12-24-2011, 06:47 AM   #1
LQ Newbie
Registered: Dec 2011
Location: Rome, Italy
Posts: 3

Rep: Reputation: Disabled
Question Kernel programming - how to get an array of mutex or spinlock?

Hi to all. I hope to post in the right forum

I'm writing some kernel code and I have some problem with the initialization of a mutex array.

I inherited some code from other guys but I can't make it work.

#define NUM_BUCKETS 256;

static barrier_struct *barrier_hash[NUM_BUCKETS];
struct mutex bucket_lock[NUM_BUCKETS];

int insert_barrier(int key){
	barrier->next = barrier_hash[buck_bd];
	barrier_hash[buck_bd] = barrier;	
	return bd;

The problem is with the mutexes.
I can compile this code but when I try to use this it kills my process.
If I use only a single mutex and I use DEFINE_MUTEX() it works, but I want to have a mutex array. How could I initialize a whole array withous initializing it one-by-one?
Also, the same question is for spinlock, how could I use them?
Old 12-24-2011, 01:25 PM   #2
Registered: Sep 2003
Location: Germany/BW
Distribution: My own
Posts: 235

Rep: Reputation: 21
You have to reserve memory for your array, else an access is undeffined - runtime error.
- type name[256]
- (new)
- SECTION .bss

Think about - kernel memory is rare.
Old 12-24-2011, 06:00 PM   #3
LQ Newbie
Registered: Dec 2011
Location: Rome, Italy
Posts: 3

Original Poster
Rep: Reputation: Disabled
Yes, I know about the kernel memory... maybe you are right, I'll use less mutexes.

I was confused because I though that the code my friends gave me was working. But they told me just some time ago that it wasn't the working code.

Thank you really much.

Last edited by Fratty; 12-24-2011 at 06:07 PM.


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
Array question C programming chibi2666 Programming 7 07-16-2009 12:53 PM
[C programming] sorting strings in an array miguetoo Programming 1 05-26-2003 02:48 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:26 AM.

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