Problem writing thread program in Solaris

This is a discussion on Problem writing thread program in Solaris within the C Programming forums, part of the General Programming Boards category; I writing a program creating a virtual circle with n threads, they execute one by one, printing their id and ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    4

    Problem writing thread program in Solaris

    I writing a program creating a virtual circle with n
    threads, they execute one by one, printing their id
    and sending a signal to the next.
    Basically, I need to create n number of threads, each receiving a
    different parameter, zero to n-1, which is going
    to be each threadís id. I need to create an array of cond_t variables
    using malloc (n * sizeof (cond_t)). How do I make n number of threads?
    Also, am I using malloc correctly?
    This is the psuedo code I have so far:

    Code:
    #include <stdio.h>
    #include <thread.h>
    #include <synch.h>
    
    mutex_t	mp;
    cond_t	cp;
    malloc (n * sizeof (cond_t));
    
    
    int main()
    {
    	int i;
    
    	mutex_init (&mp, 0, NULL);
    	cond_init (&cp, 0, NULL);
    	thr_create(NULL, i, loop, (void *) NULL, NULL, NULL);
    
    	for(i = 0, i>n, i++)
    	{
    		
    	}
    }
    void*
    loop(void* arg)
    {
    	if(id = 0)
    	{
    		printf("%s\n", id);
    		sleep(5);
    		cond_signal(&cp[i]);
    		mutex_lock(&mp);
    		cond_wait();
    		mutex_unlock(&imp);
    	}
    	else
    	{
    		mutex_lock(&mp);
    		cond_wait(&cp[i]);
    		mutex_unlock(&mp)
    		printf("%s\n", id)
    		sleep(5);
    		cond_signal(id+1)%n;
    	}

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    Also, am I using malloc correctly?
    Nope. You have to call malloc() inside of a function. You also have to store its result somewhere. For example:
    Code:
    #include <stdio.h>
    
    int main() {
        int *array = malloc(sizeof(*array) * 10);
        free(array);
        return 0;
    }
    You also should free() the memory when you're done with it.

    Your code is full of syntax errors and such. You have a single '=' instead of '=='. You have undeclared variables. You may be printing a number with &#37;s. You're using commas instead of semicolons in the for loop. The loop itself has the wrong condition; you probably want i<n instead of i>n. You're missing a closing curly brace.

    I suggest starting over again. Get something that compiles and runs, and then try adding something. Writing a bunch of code that's almost right won't work very well. The errors and oversights will build up until you have a mess.

    I believe sleep() is in a different header file as well. Perhaps <unistd.h>.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing a C program for class....
    By Bizmark in forum C Programming
    Replies: 10
    Last Post: 11-13-2007, 09:31 AM
  2. Running Program Problem
    By warfang in forum C++ Programming
    Replies: 10
    Last Post: 03-28-2007, 02:02 PM
  3. Thread Synchronization in Win32
    By passionate_guy in forum C Programming
    Replies: 0
    Last Post: 02-06-2006, 04:34 AM
  4. Inserting text into MDI program
    By Rutabega in forum Windows Programming
    Replies: 0
    Last Post: 12-23-2005, 10:25 AM
  5. Replies: 3
    Last Post: 03-04-2005, 01:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21