How to start multiple threads in Solaris?

Hello,

In a unix Solaris environment, (for simulation) how to start multiple threads (as Light Weight Process, not background process)?

thanks,

J.

Start with the pthread_create() man page, look at the EXAMPLES and follow the SEE ALSO links.

1 Like

Thanks for the tip. The following comes straight from the manual page, (except the first line) and it could not compile -

#!/bin/ksh
 
/* cc thisfile.c -lthread -lpthread */
       #define _REENTRANT    /* basic 3-lines for threads */
       #include <pthread.h>
       #include <thread.h>
       #define NUM_THREADS 5
       #define SLEEP_TIME 10
       void *sleeping(void *);   /* thread routine */
       int i;
       thread_t tid[NUM_THREADS];      /* array of thread IDs */
       int
       main(int argc, char *argv[])
       {
           if (argc == 1)  {
               printf("use 0 as arg1 to use pthread_create()\n");
               printf("or use 1 as arg1 to use thr_create()\n");
               return (1);
           }
           switch (*argv[1])  {
           case '0':  /* POSIX */
               for ( i = 0; i < NUM_THREADS; i++)
                   pthread_create(&tid, NULL, sleeping,
                       (void *)SLEEP_TIME);
               for ( i = 0; i < NUM_THREADS; i++)
                   pthread_join(tid, NULL);
               break;
           case '1':  /* Solaris */
               for ( i = 0; i < NUM_THREADS; i++)
                   thr_create(NULL, 0, sleeping, (void *)SLEEP_TIME, 0,
                       &tid);
               while (thr_join(0, NULL, NULL) == 0)
                   ;
               break;
           }  /* switch */
           printf("main() reporting that all %d threads have
               terminated\n", i);
           return (0);
       }  /* main */
       void *
       sleeping(void *arg)
       {
           int sleep_time = (int)arg;
           printf("thread %d sleeping %d seconds ...\n", thr_self(),
               sleep_time);
           sleep(sleep_time);
           printf("\nthread %d awakening\n", thr_self());
           return (NULL);
       }

 
$ time ./a.out 1
./a.out[10]: syntax error at line 10 : `(' unexpected

looks like problem with this line -

void *sleeping(void *);

Adding #!/bin/ksh to the start of a C language source file doesn't turn C source into a shell script. It creates a C language source file that can't be compiled because #!/bin/ksh is not a valid C language command.

Remove the line you added.

Change the name of the source file from a.out to thisfile.c .

Run the command suggested in the comment at the start of you example:

cc thisfile.c -lthread -lpthread

And, then, if the cc command completes successfully run the compiled code:

./a.out 0

or:

./a.out 1

If you haven't looked at the code to see why I specified 0 and 1 as possible arguments, run it with no arguments and follow the directions it prints.

1 Like

Thanks for your reply, I see I totally mistaken the c function for a unix command. I haven't looked at any C code for 20 some years hehe.

so I took out the first line and tried to compile, I think it still complains about that line:

$ cc a.c -lthread -lpthread
a.c:12: undefined or invalid # directive

You don't have direct access to threads from shell, so it has to be a C program...

Well, which line is it complaining about? Assuming you removed the first two lines, line 12 is blank in your code... Assuming you only removed the first, line 12 is not an # directive. Post your entire file again please.

1 Like

thanks for your reply, I got it working - there was an extra # I had earlier in the code when I was experimenting with it.

Is there any way to modify the code to give these threads names so they could be monitored or verified? such as using the "ps -eLf" to grep for them?

Does ps -eLf show thread ID's? I'm not sure you can label a thread, but you can print the ID's to match later. On some systems, the thread ID is the same as the thread_t, try printing it as an integer.

printf("%d\n", threadid);
1 Like