• edited by
26,806 views
68 68 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

8 Answers

Best answer
75 75 votes

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
24 24 votes

Ans is D.

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

15 15 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
9 9 votes
i think both heap and global variables.Global variables are shared by threads.Data on heap is available throughout the program execution.
5 5 votes

Thread share all resources of process except local storage like - register , stack.

Ans: Option C

• edited by
Answer:
Position:
Show:

Related questions

71 71 votes
7 answers 7 answers
29.1k
29.1k views
Arjun asked Feb 14, 2017
29,098 views
Recall that Belady's anomaly is that the page-fault rate may increase as the number of allocated frames increases. Now, consider the following statements:$S_1$: Random pa...
193 193 votes
13 answers 13 answers
61.3k
61.3k views
Arjun asked Feb 14, 2017
61,251 views
A multithreaded program $P$ executes with $x$ number of threads and uses $y$ number of locks for ensuring mutual exclusion while operating on shared memory locations. All...
34 34 votes
4 answers 4 answers
13.4k
13.4k views
Arjun asked Feb 14, 2017
13,438 views
Consider the following CPU processes with arrival times (in milliseconds) and length of CPU bursts (in milliseconds) as given below:$$\small \begin{array}{|c|c|c|} \hline...
48 48 votes
5 answers 5 answers
18.8k
18.8k views
Madhav asked Feb 14, 2017
18,819 views
Which of the following is/are shared by all the threads in a process?Program counterStackAddress spaceRegisters(I) and (II) only(III) only(IV) only(III) and (IV) only