in Operating System edited by
16,807 views
48 votes
48 votes

Threads of a process share

  1. global variables but not heap
  2. heap but not global variables
  3. neither global variables nor heap
  4. both heap and global variables
in Operating System edited by
by
16.8k views

7 Answers

57 votes
57 votes
Best answer

A thread shares with other threads a process’s (to which it belongs to) :

  • Code section
  • Data section (static + heap)
  • Address Space
  • Permissions
  • Other resources (e.g. files)

Therefore, (D) is the answer. 

edited by

3 Comments

yes Right
2
2
Sharing heap segment means sharing of dynamically created object ryt?
1
1
edited by

Threads share:

  • Address space
  • Heap
  • Static data
  • Code segments
  • File descriptors
  • Global variables
  • Child processes
  • Pending alarms
  • Signals and signal handlers
  • Accounting information

Threads have their own:

  • Program counter
  • Registers
  • Stack
  • State

References:

39
39
20 votes
20 votes

Ans is D.

Threads of a process share everything ( code , data , files , heap ) except stack and registers 

14 votes
14 votes

Global variables are stored on Heap and Heap is shared so both of them are shared.

Not able to find any "proper" references. Read this is in some book, forgot which one.

Here is a link for its reference:

http://cs.stackexchange.com/questions/48345/what-threads-share-in-general/48415

In practice global variables can be kept in the stack of Main function so that it can be accessed by all functions. Correction: Global variables cannot be inside a function, as that will limit its visibility.

The following argument is for when we need large memory, which is not asked in the question:

If its is kept in the main, the memory required for it will be taken from stack (whose size is fixed) and if the memory required is too much then this is will results into the famous error "Stack Overflow". However if it is outside any functions, then it is allocated from the heap or the data section of the code whose size can be configured as per the space required by the global variables.

Example:

This gives Stack overflow error:

int main(){
	int arr[10000000];
	cout<<"This is working"<<endl;
	return 0;
}

This does not:

int arr[10000000];
int main(){
	cout<<"This is working"<<endl;
	return 0;
}

Thanks for the question. It taught me something which I took for granted. Hope this helps.

edited by

4 Comments

Globals variables are stored on Heap

Hi @sandeep007734 ji, is this always true. could you please provide some reference ?

1
1
Edited the answer.
0
0

In practice global variables can be kept in the stack of Main function

Hi @sandeep007734 ji, But threads can not share stacks then how can they access.

0
0
You are correct. Global variables cannot be declared inside functions as that will limit it reference. Thank you for the question. Answer edited.
1
1
9 votes
9 votes
i think both heap and global variables.Global variables are shared by threads.Data on heap is available throughout the program execution.

2 Comments

Can you provide any reference for your ans....???It will be helpful.....
0
0
threads share everything except stack, and registers.
3
3
Answer:

Related questions