Sponsored Content
Full Discussion: problem with threads in C
Top Forums Programming problem with threads in C Post 302481933 by Sevco777 on Monday 20th of December 2010 08:37:34 AM
Old 12-20-2010
problem with threads in C

I have problem that if I create for example 100 threads program work correctly but if I define more threads for example 1000
// if I change static int NUM_E from 100 to 1000
than program stop about 350 threads and doesn't continue
where should be problem please?
Code:
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>

// deklaracia poctu elfov a sobov
int c_elves;
int c_reindeer;
int e_end;
int r_end;
int koniec_cyklu;

// deklaracia semaforov nad vlaknami
sem_t s_elves;
sem_t s_reindeer;
sem_t s_waitingR;
sem_t s_cakaren;

//mutex santa
pthread_mutex_t m_santa;
pthread_mutex_t m_elves;
pthread_mutex_t m_reindeer;

//podmienky
pthread_cond_t con_elves;
pthread_cond_t con_reindeer;

// deklaracia vlakien
void *f_reindeer(void *);
void *f_elves(void *);
void *f_santa(void *);

// deklaracia fukcii
void prepareSleigh();
void getHitched(int ktory);
void helpElves(void);
void getHelp(int ktory);

// definujeme si pocet sobov
static int NUM_R = 9;
static int NUM_E = 1000;

// hlavny program
int main(int argc, char **argv)
{
    //deklaracia lokalnych premmenych
    int i,j,index;
    //inicializacia pociatocnych semaforov a ich hodnot
    sem_init(&s_elves,0,1);
    sem_init(&s_reindeer,0,0);
    sem_init(&s_cakaren,0,0);
    sem_init(&s_waitingR,0,1);

    //inicializacia mutexov
    pthread_mutex_init(&m_santa,NULL);
    pthread_mutex_init(&m_elves,NULL);
    pthread_mutex_init(&m_reindeer,NULL);
    pthread_mutex_lock(&m_santa);

    //definicia pola sobov,skriatkov a santu
    pthread_t t_reindeer[NUM_R];
    pthread_t t_elves[NUM_E];
    pthread_t t_santa;
    //definicia poctu elfov a sobov ktory chcu pomoct resp sa vratili z "dovolenky"
    c_reindeer = 0;
    c_elves = 0;
    e_end = 0;
    r_end = 0;
    koniec_cyklu = 0;
    index = 0;

    pthread_create(&t_santa, NULL, f_santa, (void *)NULL);

    for (i = 0; i < NUM_E; i++)
    {
        pthread_create(&t_elves[i], NULL, f_elves, (void *)i);
    }
    for (i = 0; i < NUM_R; i++)
    {
	pthread_create(&t_reindeer[i], NULL, f_reindeer, (void *)i);
    }

    pthread_join(t_santa,NULL);
    for (i = 0; i < NUM_R; i++)
    {
	pthread_join(t_reindeer[i], NULL);
    }
    for (i = 0; i < NUM_E; i++)
    {
	pthread_join(t_elves[i], NULL);
    }
    return 0;
}

//
//vlakno sob
void *f_reindeer(void *param)
{
    printf("\033[01;37msom sob cislo %d\n",(int)param+1);
    sem_wait(&s_waitingR);
    c_reindeer++;
    if (c_reindeer == 9)
    {
	pthread_mutex_unlock(&m_santa);
    }
    sem_post(&s_waitingR);
    sem_wait(&s_reindeer);
    getHitched((int)param);
    c_reindeer--;
    if(c_reindeer == 0)
    {
	koniec_cyklu = 1;
	while(r_end == 0)
	{
	    pthread_cond_broadcast(&con_reindeer);
	}
    }
    return;
}

//
//vlakno elf
void *f_elves(void *param)
{
    printf("\033[01;37msom skriatok cislo %d\n",(int)param+1);

    //ak uz santa odletel a prisiel skriatok koniec
    if (koniec_cyklu == 1)
    {
	printf("\33[22;31melf konci prisiel neskoro %d\n",param+1);
	return;
    }

    //cakanie semafor
    sem_wait(&s_elves);
    //ak uz caka v semafore a dokoncili sa sobovia iba ukoncenie
    if (koniec_cyklu == 1)
    {
	printf("\33[22;31melf konci prisiel neskoro %d\n",param+1);
	sem_post(&s_elves);
	return;
    }

    //kod elfa ked pride do cakarne
    c_elves++;
    if (c_elves == 3)
    {
	pthread_mutex_unlock(&m_santa);
    }
    else
    {
	sem_post(&s_elves);
    }

    //cakanie pred "dverami kde je santa" teda caka sa kym budu 3 skriatkovia
    sem_wait(&s_cakaren);
    getHelp((int)param);
    c_elves--;
    if(c_elves == 0)
    {
	while (e_end == 0)
	{
	    pthread_cond_broadcast(&con_elves);
	}
	sem_post(&s_elves);
    }
    printf("\33[22;31melf konci normalne %d\n",param+1);
    return;
}

//
//vlakno santa
void *f_santa(void *param)
{
    int i;
while(1)
{
    pthread_mutex_lock(&m_santa);
    e_end = 0;
    r_end = 0;
    
    if (c_reindeer == 9)
    {
	prepareSleigh();
	for(i = 0; i < 9; i++)
	{
	    sem_post(&s_reindeer);
	}
	pthread_mutex_lock(&m_reindeer);
	pthread_cond_wait(&con_reindeer,&m_reindeer);
	r_end = 1;
	pthread_mutex_unlock(&m_reindeer);
	
	e_end = 1;
	sem_post(&s_elves);
	sem_post(&s_cakaren);
	sem_post(&s_cakaren);
	sem_post(&s_cakaren);
	printf("\33[22;33msanta konci\n");
	return;
    }
    else
    {
	if (c_elves == 3)
	{
	    helpElves();
	    for(i = 0; i < 3; i++)
	    {
		sem_post(&s_cakaren);
	    }
	//cez mutex 
	    pthread_mutex_lock(&m_elves);
	    pthread_cond_wait(&con_elves,&m_elves);
	    e_end = 1;
	    pthread_mutex_unlock(&m_elves);
	}
    }
}
    return;
}

//funkcia priprav sane -- santa
void prepareSleigh(void)
{
    printf("\033[22;33mpripravujem sane\n");
    return;
}

//funkcia priputaj -- sob
void getHitched(int ktory)
{
    printf("\033[22;32msom zviazany %d\n",ktory+1);
    return;
}

//funkcia pomoz elfom -- santa
void helpElves(void)
{
    printf("\033[22;33mdavam pomoc\n");
    return;
}

//funkcia dostal som pomoc -- elf
void getHelp(int ktory)
{
    printf("\033[22;31mdostal som pomoc%d\n",ktory+1);
    return;
}


Last edited by Sevco777; 12-20-2010 at 09:50 AM.. Reason: Please use code tags
 

10 More Discussions You Might Find Interesting

1. Post Here to Contact Site Administrators and Moderators

old threads

Neo, if I have an old thread that is a few months old, and a few pages back in the forum it was posted in, is it ok to 'bump' it back to the front? or, would you rather i deleted the old thread, and just create a new one? btw the thread has no replies. (2 Replies)
Discussion started by: norsk hedensk
2 Replies

2. UNIX for Dummies Questions & Answers

threads

i am tring to sort lots of data thats in many columns by just one column but, if I use sort +16 inputfile the column fluctuates because some of the rows have spaces etc within the text, so the end result is just a mess as it jumps around the columns depending whether it has spaces or not ....ie... (2 Replies)
Discussion started by: Gerry405
2 Replies

3. Solaris

threads

Hi all! 1)Is there a way to write a program that will work on both solaris and intel based machines. 2)How can I achive this for a program that creates and synchronizes three threads. Thank you. vij. (3 Replies)
Discussion started by: vijlak
3 Replies

4. UNIX for Advanced & Expert Users

How many threads do I use ?

Hi, I have a program that has two types of threads: 1) Reader threads 2) Worker Threads Readers: Their only job is to read files. They just read data from the files and put them into a buffer. They are obviously I/O intensive. Workers: These are CPU intensive. They do some computation... (5 Replies)
Discussion started by: the_learner
5 Replies

5. UNIX for Advanced & Expert Users

Threads and Threads Count ?

Hi all, How can I get the list of all Threads and the Total count of threads under a particular process ? Do suggest !! Awaiting for the replies !! Thanks Varun:b: (2 Replies)
Discussion started by: varungupta
2 Replies

6. Programming

Threads help

Hello! I started studying studying about POSIX Threads a few days ago... so I am a little confused and I would appreciate some help! I isolated this code... and I wonder if I could use threads in it! #include <unistd.h> #endif #include <math.h> //#include "main.h" #include <sys/time.h>... (1 Reply)
Discussion started by: smurf2
1 Replies

7. Shell Programming and Scripting

Perl v5.8.5 Threads Problem

Hi Unix gurus, I am facing a threading problem in Perl. I have a worker thread in perl in which I am calling a shell script. The shell script echo's output to the Standard Output from time to time as it progresses. In the worker thread, I am unable to display the echo statement of shell... (1 Reply)
Discussion started by: som.nitk
1 Replies

8. HP-UX

%Internal DCE Threads problem (version CMA BL10+)

Hi, I have a module by the name gateway, and it core dumps and gives a cma_dump.log file which says: %Internal DCE Threads problem (version CMA BL10+), terminating execution. % Reason: dispatch: no available VP (uniprocessor) The current thread is 3 (address 0x40107c40) DECthreads... (0 Replies)
Discussion started by: vanz
0 Replies

9. Programming

Java Threads

Hi guys, I want to start studding about java threads. but the only book available on the market is "Java Threads" by O'Reilly... (0 Replies)
Discussion started by: majid.merkava
0 Replies

10. Programming

Problem with timely execution of threads...

Hi Guys, We are having one multi-threaded application. The scenario is as follows: --------------------------------- Client 1 | APP Server -------------- Client 2 ... (1 Reply)
Discussion started by: 14341
1 Replies
All times are GMT -4. The time now is 05:51 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy