multithreading - Process Vs Thread : Looking for best explanation with example c# -


apologized posting above question here because read few same kind of thread here still things not clear.

as know both processes , threads independent sequences of execution. typical difference threads (of same process) run in shared memory space, while processes run in separate memory spaces. (quoted this answer)

the above explanation not enough visualize actual things. better if explain process example , how different thread example.

suppose start ms-pain or accounting program. can accounting program process ? guess no. accounting apps may have multiple process , each process can start multiple thread.

i want visualize area can called process when run application. please explain , guide me example better visualization , explain how process , thread not same. thanks

suppose start ms-pain or accounting program. can accounting program process ?

yes. or rather current running instance of is.

i guess no. accounting apps may have multiple process , each process can start multiple thread.

it possible process start process, relatively usual windowed software.

the process given executable; windowed application, console application , background application each involve running process.

process refers space in application runs. simple process notepad if open twice have 2 notepad windows open, have 2 notepad processes. (this true of more complicated processes, note own work keep things down one, e.g. if have firefox open , run firefox again there briefly 2 firefox processes second 1 tell first open new window before exiting , number of processes returns one; having single process makes communication within application simpler reasons we'll now).

now each process have have @ least 1 thread. thread contains information trying (typically in stack, though not possible approach). consider simple c# program:

static int doadd(int a, int b) {     return + b; } void main() {     int x = 2;     int y = 3;     int z = doadd(x, y);     console.writeline(z); } 

with simple program first 2 , 3 stored in places in stack (corresponding labels x , y). pushed onto stack again , thread moves doadd. in doadd these popped , added, , result pushed stack. stored in stack (corresponding labels z). pushed again , thread moves console.writeline. thing , thread moves main. leaves , thread dies. foreground thread running death leads process ending.

(i'm simplifying here, , don't think there's need nitpick of simplifications right now; i'm presenting reasonable mental model).

there can more 1 thread. example:

static int doadd(int a, int b) {     return + b; } static void printtwomore(object num) {     thread.sleep(new random().next(0, 500));     console.writeline(doadd(2, (int)num)); } void main() {     for(int = 0; != 10; ++i)         new thread(printtwomore).start(i); } 

here first thread creates ten more threads. each of these pause different length of time (just demonstrate independent) , similar task first example's thread.

the first thread dies upon creating 10th new thread , setting going. last of these 10 threads running last foreground thread , when dies process.

each of these threads can "see" same methods , can "see" data stored in application though there limits on how stamp on each other won't now.

a process can start new process, , communicate it. common in command-line programs, less in windowed programs. in case of windowed programs more common on *nix on windows.

one example of when geany find-in-directory operation. geany doesn't have own find-in-directory functionality rather runs program grep , interprets results. start 1 process (geany) own threads running 1 of threads causes grep program run, means we've got grep process running threads. geany's threads , grep's threads cannot communicate each other threads in same process can, when grep outputs results thread in geany can read output , use display results.


Comments