2

I read the following, and was wondering if it is possible or correct!

"Creating an application process (A) which execute and creating 1st thread to execute an application (application process (B)). A 2nd thread may be created within the application process (A) to execute the agent program(thread or process C). Therefore, the application process (B) and the agent (thread or process C) may share operating system allocated resources."

My question is:

  1. Do a process B and a thread C that are created by Process A, share any resources?
  2. Do a process B and a process C that are created by Process A, share any resources?

I thought that a process' resources are isolated even if it they are created/executed by other process (parent). If I'm wrong, I will be glad to get what resources they share and in which cases from the use cases I listed above, and in which operating system (Windows/iOS/Linux).

Thanks! Joe

displayName
  • 13,888
  • 8
  • 60
  • 75
Joseph
  • 1,716
  • 3
  • 24
  • 42
  • Can you give the link to place where you read that? – displayName Aug 23 '15 at 17:32
  • This is an internal request from a client, I want to verify that I do not miss anything before I response, and if it is possible!, to learn how it could be possibly done! – Joseph Aug 23 '15 at 17:39
  • http://man7.org/linux/man-pages/man2/fork.2.html – JimmyB Aug 23 '15 at 17:43
  • @HannoBinder this prove that they DO NOT share anything (except maybe flags), mean one can't access the resources (memory) of the other. It should be obvious, but I want to be sure! – Joseph Aug 23 '15 at 17:50
  • Well, it shows that resources are inherited. File descriptors and environment block are among those. If you're asking about shared memory, then no, memory is not automatically shared between processes. – JimmyB Aug 23 '15 at 17:55
  • The specification is bullshit: `man execv (Linux): [...]All threads other than the calling thread are destroyed during an execve()[...]`. Whoever wrote those requirements doesn't know the difference between threads and processes. – EOF Aug 23 '15 at 18:14
  • Simple answer: fork? No. pthread? Maybe. – SwiftMango Aug 23 '15 at 19:30
  • It seems you do not consider a shared memory to be a shared resource. If you change e.g., tty settings in B and it breaks I/O code in C. May tty be considered a shared resource in this case according to your terminology? – jfs Aug 23 '15 at 23:51
  • @J.F.Sebastian: if you read my comments below you would see: (1) "I know about how 2 process can communicate by share memory/IPC.... but I was asking about resources between process." (2) "Yea, probably i was not clear, I meant memory, access the heap/memory/variables.... of each other! , not the Operating system nor the files or hardware resources. :)" thanks :) – Joseph Aug 25 '15 at 06:09

3 Answers3

2

1 - 2: it is possible.

There are two threads, one will spawn child process B and the other child process C. Since both of these two threads belong to same shared address space, threads themselves share IO resources and memory.

Child processes in general may share file handles and possibly other resources with the parent process but that is to discretion of operating system, and not something your application should concern itself with.

John
  • 5,189
  • 2
  • 38
  • 62
  • The threads share the memory, BUT, each thread execute new PROCESS- the processes HAVE nothing to share with the threads. So I assume you meant it is not possible, since this what you wrote in the second section! – Joseph Aug 23 '15 at 18:11
  • Processes B and C do not share memory but they can share other resources. I think you misunderstood me. – John Aug 23 '15 at 18:50
  • Yea, probably i was not clear, I meant memory, access the heap/memory/variables.... of each other! , not the Operating system nor the files or hardware resources. :) – Joseph Aug 24 '15 at 06:39
2

My original answer to both your questions was No, but after John's comments, I'm adding one point - by virtue of how file descriptors are shared between parent and child processes, two sibling processes may end up sharing file descriptors among themselves.

Now can someone explain file descriptors to me in simple terms? Yes, the answer is here.

Parent process A may share file descriptors with its children processes B and C and therefore B & C will share the file descriptors (at the discretion of OS though).

Apart from that, neither will the thread C share any resource with process B since that thread belongs to process A, nor will the process C share any resource with another process B created from the same parent.

If there would be anything shared by the processes B and C, then it would be shared with their parent process only. For certain B and C won't share anything irrespective of both being created from same parent and irrespective of C being a process or a thread. Quoting a wikipedia article here:

typically, the child process performs only a small set of actions before it ceases execution of its program in favour of the program to be started, and it requires very few, if any, of its parent's data structures.

There are multiple ways to achieve Inter Process Communication (aka IPC) and they can be seen here.

Community
  • 1
  • 1
displayName
  • 13,888
  • 8
  • 60
  • 75
  • Thanks, I know about how 2 process can communicate by share memory/IPC.... but I was asking about resources between process. – Joseph Aug 23 '15 at 18:13
  • If process A has open file handles, these will be available to both B and C processes, therefore sharing them. – John Aug 23 '15 at 18:46
  • @John: I stand corrected. You are right... I'm updating my answer. You rightfully deserve points for that. – displayName Aug 23 '15 at 19:13
  • @John: It is strange though that in the two Operating Systems courses that I have studied, this never got pointed out. – displayName Aug 23 '15 at 19:26
  • @Joseph: Here is a simple answer for you to understand file descriptors: http://stackoverflow.com/questions/5256599/what-are-file-descriptors-explained-in-simple-terms But that being said, there will be no memory sharing between B and C. – displayName Aug 23 '15 at 19:29
  • 1
    Thanks guys, I thought that it would be obvious that when I say share resources, It means (memory, stack...) and not (files) :) . awesome response!!! – Joseph Aug 24 '15 at 06:36
0

Threads of same process share: process instructions, descriptors, signals and signal handlers, current working directory , user and group I'd.