ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
[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]
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.
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.
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?
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.