LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-07-2003, 06:07 PM   #1
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Rep: Reputation: 46
pthreads


hello again.
im trying to get multiple threads started from main().

i have a coupla questions regarding how threads work:

1. i havent really understood what it means when they say "child is created with pid 0". this doubt comes from my second question, which is
2. how can u initialize more than one thread from within a code block. for instance, i would want to intialize, say 4 threads, from within main(). do i call pthread_create() 4 times with the names of the intialization functions one after the other? and going back to question 1, do they all get a pid of 0?

thanks again.
 
Old 10-07-2003, 10:47 PM   #2
lyle_s
Member
 
Registered: Jul 2003
Distribution: Slackware
Posts: 392

Rep: Reputation: 55
[list=1][*]Did you read "child is created with pid 0" in man fork? Forking is another thing altogether from threads.[*]Yes, you call pthread_create 4 times; don't assume the threads will progress in the order you called pthread_create. The third one out of the four might finish before any of the other ones start, for example.[/list=1]

Lyle
 
Old 10-07-2003, 11:55 PM   #3
Kumar
Member
 
Registered: Sep 2003
Location: Pune, India
Distribution: Red Hat
Posts: 106

Rep: Reputation: 15
Hi,

1. fork() creates child process and not threads. If you fork a process, and then if you check the value returned by fork() inside the child part of the code, it will be equal to 0. This doesn't mean a child process is created with pid equal to 0. If you check the value returned by fork() inside the parent part of the code, it will return the actual process id of the child process. This is how you distinguish a child process from the parent process.

2. Yes, you will have to call pthread_create four times to create 4 threads of execution.

Last edited by Kumar; 10-08-2003 at 12:01 AM.
 
Old 10-09-2003, 09:17 AM   #4
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Original Poster
Rep: Reputation: 46
thanks to both of you. i was getting mixed up with fork and threads. i was thinking that creating a thread was another way of forking the process. im so forked up.
 
Old 10-09-2003, 09:30 AM   #5
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Original Poster
Rep: Reputation: 46
but im still a bit confused between the two. both spawn child processes running in the resources allocated for the calling process. how is one different from the other. and apparently, overhead for threads is less than for forked processes.
what am i missing out over here?
 
Old 10-09-2003, 11:11 AM   #6
llama_meme
Member
 
Registered: Nov 2001
Location: London, England
Distribution: Gentoo, FreeBSD
Posts: 590

Rep: Reputation: 30
When you fork a process, the entire process is copied [1] - so you get a new set of global variables, etc. When you create a new thread, that thread shares the memory allocated to its parent, so you don't get a new set of global variables.

You almost certainly want to use threads.

[1] Actually, most of it is only copied when it really needs to be (copy-on-write), so this isn't quite as slow as it sounds.

Alex
 
  


Reply



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
Pthreads a12ctic Linux - Software 1 06-09-2005 10:05 PM
Pthreads christheavatar Programming 4 03-05-2005 09:30 PM
pthreads ftgow Linux - Software 0 07-08-2004 03:55 AM
pthreads socket9001 Programming 2 12-29-2003 12:23 AM
pthreads fr0ggeh Linux - Software 9 07-18-2003 10:43 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:17 AM.

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
Open Source Consulting | Domain Registration