Parent and child relationship in unix script

[Chapter 6] Parent-Child Relationships

parent and child relationship in unix script

File, filename, parent-child relationship, directory related commands. The UNIX file system, File related commands, Compression and archiving files-zip and tar. ps (processes status) is a native Unix/Linux utility for viewing information concerning a selection of running . Find Parent Child Process. This page discusses relationships between parent and child processes when you run a program in your shell.

A file spec may simply consist of a file name, or it might also include more information about a file, such as where is resides in the overall file system. There are 2 techniques for describing file specifications, absolute and relative. With absolute file specifications, the file specification always begins from the root directory, complete and unambiguous. Absolute file specs are sometimes referred to as fully qualified path names 2. For example, the following are all absolute file specs from the diagram above: With relative file specifications, the file specification always is related to the users current position or location in the file system.

Thus, the beginning left-most part of a relative file spec describes either: Beginning users often ask "How do I know where I am? As mentioned abpve, part of a relative file specification can be a reference to a parent directory.

6 Surprising Effects of Narcissistic Parenting

The way one references a parent of the current directory is with the characters. These characters with no separating spaces describe the parent directory relative to the current directory, again, one directory level up in the file system.

parent and child relationship in unix script

Note that more than one level up a parents parent, for example can be referenced with. The following are examples referencing the diagram above: To identify where we are, we type and the system returns the following: Moving about the file system is accomplished by using the cd command, which allows a user to change directories. Remember, the file specification can always be a relative or an absolute specification. As before, we type and the system returns the following: The simple and open ended answer is "it depends.

This also depends upon how long the file specification is, or how easy it is to type, including any special characters, or how familiar one is with the current location in the file system hierarchy, etc. Take some time to navigate the file system of your Unix implementation and see how it compares to the diagram above.

More information can be found by searching on fully qualified path names in your favorite search engine. Additional File Attributes As was learned in the Getting Starting section, to determine or see what files are in our current working directory, we use the ls command.

This command allows the user to simply list their files. From the above output, we can observe 7 attribute fields listed for each file. From right to left, the attribute fields are: If the file has not been modified within the last year or six months for Linuxthe year of last modification is displayed.

Note also if the file is a directory, this is the size of the structure needed to manage the directory hierarchy.

Understanding and Modifying File Permissions If you look closely at the permission field above, you will notice the permission field for each file consists of 10 characters as described by the diagram below: The first leftmost character indicates the "type" of the file.

Another way to describe this is whether the file has any special attributes associated with it.

parent and child relationship in unix script

If it is an ordinary file i. If it is a directory file, it will have the letter d in this position. Or, if it is a link to another file it will have the letter l ell in this first position. You can see examples of an ordinary file and a directory in the ls -l output above. Other special attributes exist but do not merit discussion here.

The next 9 characters are arranged in 3 groups of 3 characters each; that is 3 characters to describe the permissions for the owner of the file, 3 characters to describe the permissions for the group and 3 characters for all other users permissions.

The 3 characters indicate whether the particular user has read denoted by rwrite denoted by wor execute denoted by x permissions on that particular file.

parent and child relationship in unix script

Thus in the diagram above, the owner of the file has all available permissions indicated by rwxa user belonging to this group has read and execute indicated by r-x permissions, and everyone else also has read and execute permissions. Observe if a user does not have a particular permission, a dash will appear instead of the corresponding letter.

Changing the permission of an existing file is accomplished using the chmod command, that is to change file protection modes. Changing the permission for a file or files can only be done by the owner of the file or the root user. The usage of this command using octal mode 1 is chmod ijk file s where ijk represent 3 octal numbers ; where i selects the user permissions, j selects the group permissions and k selects the permissions for all others.

UNIX for Dummies Questions & Answers

To disable all read, write and execute permissions for any group, you set the respective i, j or k value to 0. To set read permission for any grouping i.

parent and child relationship in unix script

The values of 4, 2, and 1 are derived from the first three powers of two, i. The command to select this would then look as follows: As a user, you will not have to remember every combination of 4, 2, and 1; typically you will standard combinations such as,etc. The permissions of read, write and execute take on a slightly different meaning with respect to directory files. Thus if a file is a directory: This allows a user with write permission to a directory to have the ability to delete files in the directory even if they don't have write permissions for the file see Note: Along with this is displayed the PID of each as well as the TTY connected terminal id and the cumulative execution time.

Only two are listed since these two processes are the only two belonging to this user id uid and this TTY. A bit more information can be viewed using the -x option with ps note that not all options will work on all Unix implementations; see the ps man page on your system as follows: What do the S and R status values indicate?

Recall what happens to the parent when a child process executes. In general, the possible process states are: Can you explain this? All of the processes on a Unix system can be listed using several options as follows: Looking at this output dissected, one should be able to trace the ancestry of the ps command: Process number is the ksh process, and its parent is process Process is sshd a secure shell client 1 and its parent is PID Process is sshd a secure shell server whose parent is PID 1, which leads us to our venerable grandparent process, init.

This command gives you a top-down view of the process tree as follows: Make sure you can identify the parent-child connection from the pstree command as describe in the ps output above.

parent and child relationship in unix script

This has become somewhat controversial, and has been described as a violation of the Unix philosophy. More on the systemd process here. The fork system call creates a new process by duplicating the calling process i. Following a fork system call, 2 processes will co-exist the parent and the child. Often coupled with fork is the exec system call. The exec system call immediately replaces or overlays the newly created child process when used with forkwhich includes inheriting the process id of the process it replaced.

The standard method for creating a new process is to use fork to make a copy of the existing process, then using exec to overwrite the copy with the new child process. This is commonly refered to as the fork-exec call sequence. Following are examples of fork and exec in action. In the first example, initially a Korn shell is present, displayed by the ps command. Next the bash command is entered, which uses a fork call to clone the ksh parent, then overwriting the child via exec with the bash process, resulting in both a Korn shell parent and a bash shell child.

Thus when the exit command is entered the child dies leaving only the Korn shell parent. When the bash shell is exec'd without a forkthis leaves only a bash process and no Korn shell process.

Note the PID of the new bash shell is the same as the overwritten Korn shell.

ps with parent/child process tree Using ps

For jobs that do not complete quickly in a second or twothese sometimes need special care so that other work can be done efficiently. Unix provides the capability to allow processes jobs to run in the foreground where they can allow user interaction, or the background where they can run uninterrupted. By default, jobs run in the foreground and do not return control of the terminal until completed.

Terminate a process If a process is running, and the terminal i.