LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 12-26-2008, 02:15 AM   #16
navderm
Member
 
Registered: Dec 2008
Location: Chandigarh, India
Distribution: Red Hat 9
Posts: 67

Original Poster
Rep: Reputation: 15

#include <malloc.h>
#include <pthread.h>

struct job
{
/* Link field for linked list. */
struct job* next;
/* Other fields describing work to be done... */
};

/* A linked list of pending jobs. */
struct job* job_queue;

/* A mutex protecting job_queue. */
pthread_mutex_t job_queue_mutex = PTHREAD_MUTEX_INITIALIZER;
/* Process queued jobs until the queue is empty. */

void* thread_function (void* arg)
{
while (1)
{
struct job* next_job;
/* Lock the mutex on the job_queue. */
pthread_mutex_lock (&job_queue_mutex);
/* Now it’s safe to check if the queue is empty. */
if (job_queue == NULL)
next_job = NULL;
else
{
/* Get the next available job. */
next_job = job_queue;
/* Remove this job from the list. */
job_queue = job_queue->next;
}
/* Unlock the mutex on the job queue because we’re done with the queue for now. */
pthread_mutex_unlock (&job_queue_mutex);
/* Was the queue empty? If so, end the thread. */
if (next_job == NULL)
break;
/* Carry out the work. */
process_job (next_job);
/* Clean up. */
free (next_job);
}
return NULL;
}
==============================
i had this code from one of the books. The lines which are highlighted are not understood.
How is it that job_queue is connected with job_queue_mutex when no such command is given??
Please clarify this.

And thanks again for the lucid explanation on previous topic jiml8
 
Old 12-26-2008, 09:07 AM   #17
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
Quote:
i had this code from one of the books. The lines which are highlighted are not understood.
How is it that job_queue is connected with job_queue_mutex when no such command is given??
Please clarify this.
It is connected because the programmer says so. It is connected because the programmer has defined a rule for the program that says that NO ONE accesses the job queue without first locking that mutex. Thus, as long as this rule is maintained in the code and the mutex is ALWAYS locked BEFORE reading or writing the job queue, then there is no possibility of one thread reading it just as another thread is changing it.

In this fashion, the job queue can be used to transfer data between the two threads.

There is nothing in the operating system, in the C language, or in the POSIX definitions that connect job_queue with job_queue_mutex. It is an association made by the programmer, specifically so the job queue can be used this way.

In this example, locking the mutex and nothing else was used to synchronize the threads. In what I am doing, I don't want to keep a mutex locked that long because I have huge quantities of data, so instead of that I have a flag that is tested (rather than testing the data itself) and allow access when the flag says it is OK. If the flag doesn't say it is OK, the thread waits in a wait port until it is signalled.

Generally speaking, leaving a mutex locked for a long time is a bad idea.

Last edited by jiml8; 12-26-2008 at 09:12 AM.
 
Old 12-26-2008, 09:18 AM   #18
navderm
Member
 
Registered: Dec 2008
Location: Chandigarh, India
Distribution: Red Hat 9
Posts: 67

Original Poster
Rep: Reputation: 15
in one of the other program that I was trying i used the command (In RTLinux)

#include <linux/kernel.h>
when i compile with gcc -c simple.c (simple.c is the filename)
its perfectly ok

then when i give "insmod simple.o"
the error is:
simple.o:kernel-module version mismatch
simple.o was compled for kernel version 2.4.20
while this kernel is version 2.4.20-rtl3.2-pre3

how do i rectify this??
 
  


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
Execution threads vs normal threads jonty_11 Linux - General 2 03-26-2008 10:37 AM
"Find all threads started by user" not showing all threads Nylex LQ Suggestions & Feedback 3 12-28-2005 08:28 PM
Java threads listed using kill -3 does not contain all threads found using ps -auxww coneheed Programming 2 11-14-2005 08:57 AM
Firestarter - Wireless card - Ethernet nic -- Entangled problems rickh Linux - Software 0 05-28-2005 12:47 AM
Java Threads vs Native Threads rjmendez Programming 0 08-16-2004 05:58 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:31 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration