Page 423 - DCAP103_Principle of operating system
P. 423

Principles of Operating Systems



                   Notes         of shares it with the calling thread. Figure 14.9 shows some of the items that can be be shared
                                 or copied according to bits in sharing flags.


                                                     Figure 14.9: Bits in the Sharing Flags Bitmap

                                     Flag                Meaning when set         Meaning when cleared

                                     CLONE_VM            Create a new thread      Create a new process
                                     CLONE_FS            Share  umask,  root,  and  Do not share them
                                                         working dirs
                                     CLONE_FILES         Share the file descriptors  Copy the file descriptors

                                     CLONE_SIGHAND       Share the signal handler tale  Copy the table

                                     CLONE_PID           New thread gets old PID  New thread gets own PID
                                     CLONE_PARENT        New thread has same parent  New thread’s parent is caller
                                                         as caller

                                 The CLONE_ VM bit determines whether the virtual memory (i.e., address space) is shared
                                 with the old threads or copied. If it is set, the new thread just moves in with the existing ones,
                                 so the clone call effectively creates a new thread in an existing process. If the bit is cleared, the
                                 new thread gets its own address space. Having its own address space means that the effect of
                                 its STORE instructions are not visible to the existing threads. This behaviour is similar to fork,
                                 except as noted below. Creating a new address space is effectively the definition of a new process.

                                 The CLONE_ FS bit controls sharing of the root and working directories and of the umask flag.
                                 Even if the new thread has its own address space, if this bit is set, the old and new threads
                                 share working directories. This means that a call to chdir by one thread changes the working
                                 directory of the other thread, even though the other thread may have its own address space.
                                 In UNIX, a call to chdir by a thread always changes the working directory for other threads in
                                 its process, but never for threads in another process. Thus this bit enables a kind of sharing not
                                 possible in traditional UNIX versions. The CLONE_ FILES bit is analogous to the CLONE_ FS
                                 bit. If set, the new thread shares its file descriptors with the old ones, so calls to lseek by one
                                 thread are visible to the other ones, again as normally holds for threads within the same process
                                 but not for threads in different processes. Similarly, CLONE_ SIGHAND enables or disables
                                 the sharing of the signal handler table between the old and new threads. If the table is shared,
                                 even among threads in different address spaces, then changing a handler in one thread affects
                                 the handlers in the others. Finally, CLONE_ PID controls whether the new thread gets its own
                                 PID or shares its parent’s PID. This feature is needed during system booting. User processes
                                 are not permitted to enable it.

                                 Finally, every process has a parent. The CLONE_ PARENT bit controls who the parent of the
                                 new thread is. It can either be the same as the calling thread (in which case the new thread is a
                                 sibling of the caller) or it can be the calling thread itself, in which case the new thread is a child
                                 of the caller. There are a few other bits that control other items, but they are less important. This
                                 fine-grained sharing is possible because Linux maintains separate data structures for the various
                                 items. (scheduling parameters, memory image, and so on). The task structure just points to these
                                 data structures, so it is easy to make a new task structure for each cloned thread and have it
                                 either point to the old thread’s scheduling, memory, and other data structures or to copies of
                                 them. The fact that such fine-grained sharing is possible does not mean that it is useful, however,
                                 especially since traditional UNIX versions do not offer this functionality. A Linux program that
                                 takes advantage of it is then no longer portable to UNIX.



        416                               LOVELY PROFESSIONAL UNIVERSITY
   418   419   420   421   422   423   424   425   426   427   428