LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-08-2012, 07:48 AM   #1
maya1
LQ Newbie
 
Registered: Apr 2012
Posts: 1

Rep: Reputation: Disabled
creating threads in linux?


hello sir/madam,
im doing my dbms project on parallel sorting so i want to create some number of threads in shell script. anyone please help me how to create threads?
 
Old 04-08-2012, 10:18 AM   #2
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 946Reputation: 946Reputation: 946Reputation: 946Reputation: 946Reputation: 946Reputation: 946Reputation: 946
You cannot create threads in typical shell scripts, but you can create any number of parallel processes. The difference is that you need to communicate via files or pipes or some other interprocess communication methods, as shell and environment variables are local to each process.

In Bash (bash) and POSIX shells (sh), you can run anything concurrently by adding & after the command. The shell instance that started such background jobs is the parent process, and the job itself is called the child process. To wait until all background jobs have finished, simply use the wait shell command. (It will return when there are no more background jobs running.)

In almost all cases, the above suffices. The actual job control support is a lot more versatile, if you were to need more fine-grained control; see the Job Control section in the Bash Reference Manual for details. Job control options are more or less identical in Bash and POSIX shells.

Here is a Bash example to get you started:
Code:
#!/bin/bash

(
  # Do something here.
  # This is a subshell, so variables, working directory, et cetera
  # are private to this part.
  # If you need to save results to the 'main' script,
  # use for example a (temporary) file.

  echo 'Task 1 is going to sleep for five seconds.'
  sleep 5
  echo 'Task 1 is done.'

) &

(
  # Do something here too.
  # This part will be run concurrently with the above.

  echo 'Task 2 is going to sleep for three seconds.'
  sleep 3
  echo 'Task 2 is done.'
) &

(
  # Yes, you can have as many of these sections as you like.

  echo 'Task 3 is going to sleep for four seconds.'
  sleep 4
  echo 'Task 3 is done.'
) &

# Okay, let us wait until all of the above tasks have completed.
echo 'The parent is going to wait until all tasks are done.'
wait
echo 'The parent is done.'
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem creating threads in C++ mending73 Linux - Newbie 1 09-30-2009 03:51 AM
Creating Kernel threads in uClinux!!! ananth86coolguy Programming 0 03-14-2009 08:40 AM
Creating Threads lucky6969b Linux - Software 2 12-02-2005 02:44 AM
Creating Linux Threads in C++ George_gk Programming 2 01-27-2005 04:29 AM


All times are GMT -5. The time now is 02:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration