How to start multiple threads in Solaris?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to start multiple threads in Solaris?
# 1  
Old 04-15-2013
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.
# 2  
Old 04-15-2013
Start with the pthread_create() man page, look at the EXAMPLES and follow the SEE ALSO links.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 04-16-2013
pthread_create

Quote:
Originally Posted by Don Cragun
Start with the pthread_create() man page, look at the EXAMPLES and follow the SEE ALSO links.
Thanks for the tip. The following comes straight from the manual page, (except the first line) and it could not compile -

Code:
#!/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[i], NULL, sleeping,
                       (void *)SLEEP_TIME);
               for ( i = 0; i < NUM_THREADS; i++)
                   pthread_join(tid[i], NULL);
               break;
           case '1':  /* Solaris */
               for ( i = 0; i < NUM_THREADS; i++)
                   thr_create(NULL, 0, sleeping, (void *)SLEEP_TIME, 0,
                       &tid[i]);
               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);
       }

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

looks like problem with this line -

Code:
void *sleeping(void *);


Last edited by jim mcnamara; 04-16-2013 at 12:58 PM..
# 4  
Old 04-16-2013
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:
Code:
cc thisfile.c -lthread -lpthread

And, then, if the cc command completes successfully run the compiled code:
Code:
./a.out 0

or:
Code:
./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.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 04-16-2013
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
# 6  
Old 04-16-2013
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.
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 04-16-2013
how to give the threads names

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?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to create multiple threads

Hi , i need to run multiple scripts parallely ,on my server....i have 8 cpus . planning to run minimum of 6 scripts paralley ....could you please suggest someone . thanks in advance , (3 Replies)
Discussion started by: Huvan
3 Replies

2. Shell Programming and Scripting

How to make this run in multiple threads

Hi, I have a list of URLs in a csv file which I'm checking for page status. It just prints the URL and the status as output. This works perfectly fine. I'm looking to run this in multiple threads to make this process faster. I'm pretty new to Perl and I managed to complete this. It would be... (9 Replies)
Discussion started by: kzenthil
9 Replies

3. Programming

creating multiple threads using single thread id

Hi all, Can I create multiple threads using single thread_id like pthread_t thread_id; pthread_create(&thread_id, NULL, &print_xs, NULL); pthread_create(&thread_id, NULL, &print_ys, NULL); pthread_create(&thread_id, NULL, &print_zs, NULL); pthread_join(thread_id, NULL); what... (2 Replies)
Discussion started by: zing_foru
2 Replies

4. UNIX for Dummies Questions & Answers

Copy files in Multiple Threads

Hello all, I have a directory of files of varying sizes. I want to copy all these files in n number of threads to another directory such that each copy set is more or less the same size. Example : Say /mydirA It has around say 23 files of various sizes. Number of copy... (0 Replies)
Discussion started by: samoo
0 Replies

5. Programming

File access from multiple processes or threads?

Hi, Can anyone give me any idea when multiple processes access a file (like opening it, modifying it etc.) how can the synchronization can be done if they can access the same file at any time? How can this scenario is different from when multiple threads access a same file, modifying it etc- in... (7 Replies)
Discussion started by: sanzee
7 Replies

6. UNIX for Advanced & Expert Users

Which IPC between multiple threads will suit most in scenerio?

Hi Friends, I am designing a solution for a problem: in which my master thread receiving network message and processing them, during processing of each message i need to communicate remote database (that is actually a separate external module)that cause a delay of arount 2 second (), After... (1 Reply)
Discussion started by: johnray31
1 Replies

7. Shell Programming and Scripting

Spawning multiple threads in Unix

Hi, I need to spawn mutilpe threads , each invoking a different set of shell scripts, in parallel. What would be the best way to do that. Any sample script would greatly help. I am a novice at Unix so any help is much appreciated. Thanks (5 Replies)
Discussion started by: neeto
5 Replies

8. IP Networking

How to choose Multiple process or Multiple threads?

Hi All, Please explain me when i have to use multiple process and when I have to use Multiple threads? Please give me an example.It will be very helpful for me. Thanks in advance. (0 Replies)
Discussion started by: ashleykumar
0 Replies

9. Programming

synchronizing multiple threads in unix

hi all! I wanted to know how to synchronize multiple threads in unix It would be better if someone give some code samples Thanx (1 Reply)
Discussion started by: bankpro
1 Replies

10. Programming

How to use pipe() in multiple threads?

Hi, I have a program that runs two threads in stead of two processes. I want to use pipe to redirect the output of the first thread to the input of the second thread. One thread is continuously writing to a pipe, and the other thread will read from the pipe. How do I do that? Is there... (2 Replies)
Discussion started by: wminghao
2 Replies
Login or Register to Ask a Question