LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 05-07-2012, 01:50 AM   #1
mohd2012
LQ Newbie
 
Registered: Apr 2012
Posts: 8

Rep: Reputation: Disabled
Crtical Sections - Test And Set


Hello every one

I am new here I need your help to understand how we use The Gnome library (glib).. to solve critical section problem..


How can I a Adapt the following C program from class in such a way that it uses the test and set() functionto to protect the critical sections in the two calculating threads.

Test and set function:


gboolean test_and_set(int *target) {
return !g_atomic_int_compare_and_exchange(target,0,1);
}

C programme :

Code:
#include <pthread.h>
#include <stdio.h>
#include <glib.h>
double sum;
void* runner1(void *param);
void* runner2(void *param);
int main(int argc, char *argv[]) {
pthread_t tid1;
pthread_t tid2;
pthread_attr_t attr;

if (argc != 2) {
  fprintf(stderr,"usage: program <positive integer>\n");
  return -1;
}
if (atoi(argv[1]) < 0) {
  fprintf(stderr,"%d must be >= 0\n",atoi(argv[1]));
  return -1;
}

sum = 0;

pthread_attr_init(&attr);
pthread_create(&tid1,&attr,runner1,argv[1]);
pthread_create(&tid2,&attr,runner2,argv[1]);
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);

printf("sum = %f\n",sum);
}
void* runner1(void *param) {
int i;
int N2 = atoi(param) / 2;
for (i=1;i<=N2;++i) {
  sum += i;
}

pthread_exit(0);
}
void* runner2(void *param) {
int j;
int N = atoi(param);
int j1 = N / 2 + 1;
for (j=j1;j<=N;++j) {
  sum += j;
}

pthread_exit(0);
}

Last edited by mohd2012; 05-07-2012 at 01:53 AM.
 
Old 05-07-2012, 09:08 AM   #2
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
Well, obviously I don't want to "give you the answer to a homework problem" but I'll be happy to try turning some lights on.

A "test and set" instruction (by any other name...) is an atomic machine-instruction that examines the value of a memory location and optionally sets its value in a single, uninterruptible operation. It eliminates the race condition that would otherwise occur in a sequence such as if (a=1) a++;.

In order to solve this assignment, you need to now research and decide how to use this capability to create "turnstiles and bathroom stalls" in your program. Instead of simply asking a stranger to write your answer for you (which, BTW, your teacher will most certainly see because you can bet that he/she also has an account here ...), surf for examples and for discussions of how they work. Then you will learn, and forever after you too will know.
 
Old 05-08-2012, 05:47 AM   #3
mohd2012
LQ Newbie
 
Registered: Apr 2012
Posts: 8

Original Poster
Rep: Reputation: Disabled
This my try after search to understand how test and ste funcation use:

i think i have some thing wrong withine the while loop ? any help?

Code:
#include <pthread.h>
#include <stdio.h>
#include <gilb.h>


double sum; 

void* runner1(void *param);
void* runner2(void *param);




int main(int argc, char *argv[]) {

	pthread_t tid1;
	pthread_t tid2;
	pthread_attr_t attr;
	
	if (argc != 2) {
		fprintf(stderr,"usage: program <positive integer>\n");
		return -1;
	}
	if (atoi(argv[1]) < 0) {
		fprintf(stderr,"%d must be >= 0\n",atoi(argv[1]));
		return -1;
	}	
	
	sum = 0;
	
	pthread_attr_init(&attr);
	//pthread_create(&tid1,&attr,runner1,argv[1]);
	//pthread_create(&tid2,&attr,runner2,argv[1]);
	//pthread_join(tid1,NULL);
	//pthread_join(tid2,NULL);
	
	printf("sum = %f\n",sum);



Int lock = 1;
do {
	while (test_and_set(&lock));
				pthread_create(&tid1,&attr,runner1,argv[1]);
				pthread_join(tid1,NULL);
	lock = FALSE;
				pthread_create(&tid2,&attr,runner2,argv[1]);
			        pthread_join(tid2,NULL);
	

		} while (TRUE);	

            printf("sum = %f\n",sum);

}


void* runner1(void *param) {

	int i;
	int N2 = atoi(param) / 2;
	for (i=1;i<=N2;++i) {
		sum += i;
	}
	
	pthread_exit(0);
}
void* runner2(void *param) {

	int j;
	int N = atoi(param);
	int j1 = N / 2 + 1;
	for (j=j1;j<=N;++j) {
		sum += j;
	}
	
	pthread_exit(0);
}

gboolean test_and_set(int *target) {
return !g_atomic_int_compare_and_exchange(target,0,1);
}
 
  


Reply



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
Load test, boundary test & stress test for USB EHCI/xHCI driver rama_toshiba Linux - Kernel 5 02-29-2012 02:43 PM
Korn Shell test if multiple variables are set jonlake Programming 4 04-09-2008 10:41 AM
How to set up iperf for roaming test??? wahaha Linux - Networking 0 03-15-2007 10:25 AM
How to test if an env variable is set King of Men Linux - Newbie 5 11-03-2005 04:39 AM

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

All times are GMT -5. The time now is 09:40 PM.

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