LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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-10-2010, 12:25 AM   #1
2november
LQ Newbie
 
Registered: Jan 2010
Posts: 1

Rep: Reputation: 0
Please help me.


Quote:
/* compile this program by "gcc -o syncLinux syncLinux.c -lpthread" command on Linux*/
/* and then run it using "./syncLinux -i<#of iteration>" */

#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>

#include <unistd.h>
#include <stdlib.h>

/* Global variables */
sem_t mutex;
int iterations = 0;
long int counter = 0;

long int decrement(long int in){
return in - 1;
}

long int increment(long int in){
return in + 1;
}

void * incrementerThread(void *arg) {
int i;
int id = (int) arg;
for(i = 0; i < iterations; i++){
sem_wait(&mutex);
counter = increment(counter);
sem_post(&mutex);
}

return NULL;

}

void * decrementerThread(void *arg) {
int i;
int id = (int) arg;
for(i = 0; i < iterations; i++){
sem_wait(&mutex);
counter = decrement(counter);
sem_post(&mutex);
}
return NULL;
}


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

pthread_t incrementer1ThreadId;
pthread_t decrementer2ThreadId;
int c;
extern char *optarg;
extern int optind;
int id1 = 1;
int id2 = 2;
int err;

iterations = 10;//default number of iterations

while((c = getopt(argc, argv, "i:")) != EOF){
switch(c) {
case 'i':
iterations = atoi(optarg); //number of iterations from command line
break;
default:
iterations = 10;
}
}

sem_init(&mutex, 0, 1); //initialize semaphore

printf("Number of iterations = %i\n", iterations );

err = pthread_create(&incrementer1ThreadId, NULL, incrementerThread, (void *) id1);

err = pthread_create(&decrementer2ThreadId, NULL, decrementerThread, (void *) id2);

pthread_join(incrementer1ThreadId, NULL);
pthread_join(decrementer2ThreadId, NULL);

printf("Counter = %li\n", counter);
fflush(0);


sem_destroy(&mutex);

return 0;
}

A.sem_wait sem_post prevent the code from this work how errors.
B.sem_wait sem_post removed from the code and why the work is the same.


Thx...

Last edited by 2november; 01-10-2010 at 12:28 AM.
 
Old 01-10-2010, 03:31 AM   #2
carbonfiber
Member
 
Registered: Sep 2009
Location: Sparta
Posts: 237

Rep: Reputation: 46
I don't know what "the code from this work how errors" means, but I'm guessing you're trying to say the program is not doing what you would expect it to do. What are you expecting it to do? It seems to work fine.
 
  


Reply

Tags
linux, programming


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



All times are GMT -5. The time now is 01:31 PM.

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