Child Processes Exits or A Signal Is Received. For Our Purpose, We Shall Ignore Signals
Child Processes Exits or A Signal Is Received. For Our Purpose, We Shall Ignore Signals
System call fork() is used to create processes. It takes no arguments and
returns a process ID. The purpose of fork() is to create a new process, which
becomes the child process of the caller. After a new child process is created,
both processes will execute the next instruction following the fork() system
call. Therefore, we have to distinguish the parent from the child. This can be
done by testing the returned value of fork():
• If fork() returns a negative value, the creation of a child process was unsuccessful.
• fork() returns a zero to the newly created child process.
The system call wait() is easy. This function blocks the calling process until one of its
child processes exits or a signal is received. For our purpose, we shall ignore signals.
wait() takes the address of an integer variable and returns the process ID of the
completed process. Some flags that indicate the completion status of the child process
are passed back with the integer pointer. One of the main purposes of wait() is to wait
for completion of child processes.
The function _exit() terminates the calling process "immediately". More generally, an
exit in a multithreading environment means that a thread of execution has stopped
running. For resource management, the operating system reclaims resources (memory,
files, etc.) that were used by the process. The process is said to be a dead process after
it terminates.Most operating systems allow the terminating process to provide a
specific exit status to the system, which is made available to the parent process. The
exit operation typically performs cleanup operations within the process space before
returning control back to the operating system.
a program that needs to access data from a file stored in a file system uses the read
system call. The file is identified by a file descriptor that is normally obtained from
a previous call to open. This system call reads in data in bytes, the number of which is
specified by the caller, from the file and stores then into a buffer supplied by the
calling process. The read system call takes three arguments: 1.The file descriptor
of the file. the buffer where the read data is to be stored and the number of
bytes to be read from the file.
The write is one of the most basic routines provided by a Unix-like operating
system kernel. It writes data from a buffer declared by the user to a given device,
such as a file. This is the primary way to output data from a program by directly using
a system call. The destination is identified by a numeric code. The data to be
written, for instance a piece of text, is defined by a pointer and a size, given in
number of bytes. write thus takes three arguments: The file code (file descriptor or fd). The
pointer to a buffer where the data is stored (buf). The number of bytes to write from the buffer
(nbytes).
For most file systems, a program initializes access to a file in a file system using the open system
call. This allocates resources associated to the file (the file descriptor), and returns a handle that the
process will use to refer to that file. In some cases the open is performed by the first access. The
same file may be opened simultaneously by several processes, and even by the same process,
resulting in several file descriptors for the same file; depending on the file organization and
filesystem.The pathname to the file, The kind of access requested on the file (read, write, append
etc.), The initial file permission is requested using the third argument called mode. This argument is
relevant only when a new file is being created.
A close system call is a system call used to close a file descriptor by the kernel. For most file
systems, a program terminates access to a file in a filesystem using the close system call. This
flushes file buffers, updates file metadata, which may include and end-of-file indicator in the data;
de-allocates resources associated with the file (including the file descriptor) and updates the system
wide table of files in use. Some programming languages maintain a data structure of files opened by
their runtime library and may close when the program terminates. This practice is known as
resource acquisition is initialization (RAII). Some operating systems will invoke the close on
files held by a program if it terminates. Some operating systems will invoke the close syscall as
part of an operating system recovery as a result of a system failure.
In computing, ioctl (an abbreviation of input/output control) is a system call for device-specific
input/output operations and other operations which cannot be expressed by regular system calls. It
takes a parameter specifying a request code; the effect of a call depends completely on the request
code. Request codes are often device-specific. For instance, a CD-ROM device driver which can
instruct a physical device to eject a disc would provide an ioctl request code to do that. Device-
independent request codes are sometimes used to give userspace access to kernel functions which
are only used by core system software or still under development.
getpid() : returns the process ID of the calling process. This is often used by routines that generate
unique temporary filenames.
pid_t getpid(void); getpid() returns the process ID of the current process. It never throws any
error therefore is always successful. If the caller's parent is in a different PID namespace (see
pid_namespaces(7)), getppid() returns 0.
The Unix alarm() system call specifies a number of seconds after which the calling process should
receive an instance of the SIGALRM signal. The signal is generated the specified number of seconds
after the time the alarm() call is executed.This can be used for error recovery, to arrange a "timeout"
for an operation that the application expects might take too long, such as a potentially infinite loop,
or a system call that might block forever.If the application does not install a handler for SIGALRM,
the timeout will kill the process abruptly. That may not be good if the application was doing
anything that cannot safely be left incomplete. For example, the process might be holding locks, or
might have buffered output.Therefore, an application that uses alarm() will usually use sigaction()
to install a handler for SIGALRM, which allows the process to recover or to exit gracefully.
A computer program (process, task, or thread) may sleep, which places it into an inactive state for a
period of time. Eventually the expiration of an interval timer, or the receipt of a signal or interrupt
causes the program to resume execution. the sleep() function is called providing a single
parameter of type unsigned integer of the number of seconds to sleep.A typical sleep system call
takes a time value as a parameter, specifying the minimum amount of time that the process is to
sleep before resuming execution. The parameter typically specifies seconds, although some
operating systems provide finer resolution, such as milliseconds or microseconds.
Conceptually, a pipe is a connection between two processes, such that the standard output from one
process becomes the standard input of the other process. In UNIX Operating System, Pipes are
useful for communication between related processes(inter-process communication). The pipe
system call finds the first two available positions in the process’s open file table and allocates them
for the read and write ends of the pipe.Pipes behave FIFO(First in First out), Pipe behave like a
queue data structure. Size of read and write don’t have to match here.
In order to create a new message queue, or access an existing queue, the shmget() system call is
used.This particular call should almost seem like old news at this point. It is strikingly similar to the
corresponding get calls for message queues and semaphore sets. The first argument to shmget()
is the key value (in our case returned by a call to ftok()). This key value is then compared to
existing key values that exist within the kernel for other shared memory segments. At that point, the
open or access operation is dependent upon the contents of the shmflg argument.
In computing, mmap(2) is a POSIX-compliant Unix system call that maps files or devices into
memory. It is a method of memory-mapped file I/O. It implements demand paging, because file
contents are not read from disk directly and initially do not use physical RAM at all. The actual
reads from disk are performed in a "lazy" manner, after a specific location is accessed. After the
memory is no longer needed, it is important to munmap(2) the pointers to it. Protection
information can be managed using mprotect(2), and special treatment can be enforced using
madvise(2).
In Unix and Unix-like operating systems, chmod is the command and system call which is used to
change the access permissions of file system objects (files and directories). It is also used to change
special mode flags. On success, zero is returned. On error, -1 is returned, and errno is set
appropriately.The mode of the file given by path or referenced by fildes is changed.
In computing, umask is a command that determines the settings of a mask that controls how file
permissions are set for newly created files. It may also affect how the file permissions are changed
explicitly. umask is also a function that sets the mask, or it may refer to the mask itself, which is
formally known as the file mode creation mask. The mask is a grouping of bits, each of which
restricts how its corresponding permission is set for newly created files. The bits in the mask may
be changed by invoking the umask command. This system call always succeeds and the previous
value of the mask is returned.
The command chown, an abbreviation of change owner, is used on Unix and Unix-like operating
systems to change the owner of file system files, directories. Unprivileged (regular) users who wish
to change the group membership of a file that they own may use chgrp. The ownership of any file in
the system may only be altered by a super-user. A user cannot give away ownership of a file, even
when the user owns it. Similarly, only a member of a group can change a file's group ID to that
group.On success, zero is returned. On error, -1 is returned, and errno is set appropriately.