The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How Can I use pthread_create ? shvalb High Level Programming 5 03-11-2008 10:17 AM
undefined reference to `pthread_create' netman High Level Programming 2 01-28-2008 09:31 AM
pthread_create and scope usage jenmead High Level Programming 3 09-20-2006 01:16 PM
unresolve pthread_create etc zhshqzyc High Level Programming 3 02-13-2006 03:38 PM
pthread_create problem _rocky High Level Programming 5 03-11-2005 03:38 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 05-19-2008
Registered User
 

Join Date: May 2008
Posts: 2
Pthread_create issue

Hello

My problem goes like this:

I have used Pthread_create, and I have tryed to create 2 proccess but nothing happens! It does not even matter what the function im trying to create do. It is if im trying to activate an empty function. This is my code.

Any help will be highly appreciated.

please note that the code does compile but actually does nothing.

my code is:

================================================
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include "semaphore.h"



#pragma warning(disable:4996)
//Disable c4496 warning, regarding the potentially unsafe
//methods in the Standard C++ Library.


int arr[5] = {0};
int i;
int j = 0;
int init = 1;

void* manufacturer(void* Thread_data)
{
for (i = init; i <= 100; i++)
{
arr[i-1] = i;
j++;
if (j == 4)
{
down(&semaphore1);


j = 0;
init = i;
up(&semaphore2);
}
}

pthread_exit(NULL);
}

void* consumer(void* Thread_data)
{
if (arr[0] != 0)
{
for (i; i >= 0; i--)
{
if (arr[i] != 0)
{
printf("%d",arr[i]);
arr[i] = 0; //Removing the number from the array.
}
}
}
else
{
down(&semaphore2);
up(&semaphore1);
}

pthread_exit(NULL);
}


int main()
{
int status; //Used to recieve the result of the thread creation.
init_sem(&semaphore1, 0);
init_sem(&semaphore2, 0);

pthread_t manufacturer_thread; //The manufacturer thread's name. pthread_t consumer_thread; //The consumer thread's name.
status = pthread_create(&manufacturer_thread, NULL, manufacturer, NULL);

if (status == 0)
{
printf("The manufacturer process has been successfully created\n");
}
else
{
printf("Error: The manufacturer process could not be created\n");
}

status = pthread_create(&consumer_thread, NULL, consumer, NULL);

if (status == 0)
{
printf("The consumer process has been successfully created\n");
}
else
{
printf("Error: The manufacturer process could not be created\n");
}

}

==========================================================
I know it's a little long, But I just dont know what to do. Im using the knoppix CD( im booting from it) so its under Linux OS.

when I try to run the application, I get:


The manufacturer process has been successfully created
The consumer process has been successfully created

and that's it! meaning that the thread has been created successfully, but the functions are never called..

thanks
Reply With Quote
Forum Sponsor
  #2  
Old 05-19-2008
Moderator
 

Join Date: Feb 2007
Posts: 2,317
- First, enable the warning to see if you get some usefull messages.
- Declare enough elements for arr[] as used for the functions manufacturers and consumer.
- The 3th parameter should be an address of the thread function:

Code:
status = pthread_create(&manufacturer_thread, NULL, &manufacturer, NULL);
status = pthread_create(&consumer_thread, NULL, &consumer, NULL);
instead of:

Code:
status = pthread_create(&manufacturer_thread, NULL, manufacturer, NULL);
status = pthread_create(&consumer_thread, NULL, consumer, NULL);
Regards
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 05:02 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0