RTS - Unit 1 Jay
RTS - Unit 1 Jay
UNIT – 1
1. Introduction to UNIX/LINUX
2. Overview of Commands
3. File I/O
Most UNIX I/O can be performed using only 5 functions: open, read, write,
lseek, and close.
The functions described are often refered to as Unbuffered I/O. The term unbuffered
refers to the fact that each read or write invokes a system call in the kernel. These
unbuffered I/O functions are not part of ANSI C, but are POSIX.1 and XPG3.
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
This study source was downloaded by 100000894355947 from CourseHero.com on 11-27-2024 00:34:38 GMT -06:00
https://www.coursehero.com/file/63447224/RTS-Notesodt/
RTS
We show the teh third argument as ...,which isthe ANSI C way to specify that
the number and types of the remaining arguments may vary. Here the third argument
is only used when a new file is being created.
The pathname is the name of the file to open or create.there are many options for this
function, which are specified by the oflag argument.this argument is formed by
OR’ing together one or more of the following constants(from the <fcntl.h> header).
O_RDONLY - Open for reading only.
O_WRONLY - Open for writing only.
O_RDWR – Open for reading and writing.
One and only one of these 3 constants must be specified. The following are optional:
O_APPEND, O_CREAT, O_EXCL, O_TRUNC, O_NOCTTY, O_NONBLOCK, O_SYNC.
The file descriptor returned by open is guaranteed to be the lowest numbered unused descriptor.
This is used by some applications to open a new file on standard input, standard output, or standard
error.
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
Int close(int filedes) ;
Returns: 0 if OK, -1 in error.
This study source was downloaded by 100000894355947 from CourseHero.com on 11-27-2024 00:34:38 GMT -06:00
https://www.coursehero.com/file/63447224/RTS-Notesodt/
RTS
Closing a file also releases any record locks that the process may have on the file.
When a process terminates, all open files are closed by the kernel. Many programs take advantage
of this fact and don’t explicitly close open files.
The interpretation of the offset depends on the value of the whence argument.
• If whence is SEEK_SET, the file's offset is set to offset bytes from the beginning of the file.
• If whence is SEEK CUT, the file's offset is set to its current value plus the offset.The offset
can be positive or negative.
• If whence is SEEK_END,the files’s offset is set to the size of the file plus the offset. The
offset can be positive or negative.
#include<unistd.h>
If the read is successful, the number of bytes read is returned. If the end of file is encountered, 0 is
returned.
There are several cases in which the number of bytes actually read is less then the amount
requested:
• When reading from a rergular file, if the end of file is reached before the requested number
of bytes has been read.
This study source was downloaded by 100000894355947 from CourseHero.com on 11-27-2024 00:34:38 GMT -06:00
https://www.coursehero.com/file/63447224/RTS-Notesodt/
RTS
• When reading from a terminal device, normally up to one line is read at a time.
• When reading from a network, buffering within the network may cause less than the
requested amount to be returned.
• Some record-oriented devices, such as a magnetic tape, return up to a single record at a time.
The read operation starts at the file’s current offset. Before a successful return, the offset is
incremented by the number of bytes actually read.
#include<unistd.h>
The return value is usually equal to the nbytes argument, otherwise an error has occurred. A
common cause for a write error is either filling up a disk or exceeding the file size limit for a given
process.
For a regular file, the write starts at the file's current offset. After a successful write, the file's offset
is incremented by the number of bytes actually written.
4. Process Control
The process control includes the creation of new processess,executing programs, and
process termination. There are also various ID’s that are the property of the process – real, effective,
and saved, user and group ID’s – and how are they affected by the process control primitives.
This study source was downloaded by 100000894355947 from CourseHero.com on 11-27-2024 00:34:38 GMT -06:00
https://www.coursehero.com/file/63447224/RTS-Notesodt/
RTS
4.2. fork Function
The only way a new process is created by the Unix kernel is when an existing process calls
the fork function.
#inlcude<sys/types.h>
#include<unistd.h>
pid_t fork(void);
Returns: 0 in child, process ID of child in parent, -1 on error.
The new process created by fork is called the child process, This function is called once but returns
twice. The only difference in the retums is that the return value in the child is 0 while the return
value in the parent is the process ID of the new child. The reason the child's process ID is returned
to the parent is because a process can have more than one child, so there is no function that allows a
process to obtain the process IDs of its children. The reason fork returns0 to the child is because a
process can have only a single parent, so the child can always call getppid to obtain the process id
of its parent.
Both the child and parent continue executing with the instruction that follows the call to fork. The
child is a copy of parent.
This study source was downloaded by 100000894355947 from CourseHero.com on 11-27-2024 00:34:38 GMT -06:00
https://www.coursehero.com/file/63447224/RTS-Notesodt/
RTS
b) Calling the exit function. This function is defined by ANSI C and includes the calling of
all exit handlers that have been registered by calling atexit and closing all standard I/0
streams.
c) Calling the _exit function. This function is called by exit and handles the Unix-specific
details. _exit is specified by POSIX 1.
2. Abnormal termination:
a) Calling abort. This is a special case of the next item, since it generates the SIGABRT signal.
b) When the process receives certain signals. The signal can be generated by the process itself
by some other process or by the kernel.
#include <sys/types.h>
#include<sys/wait.h>
This study source was downloaded by 100000894355947 from CourseHero.com on 11-27-2024 00:34:38 GMT -06:00
https://www.coursehero.com/file/63447224/RTS-Notesodt/
RTS
Waitpid returns the process ID of the terminated child , and its termination status is returned through
statloc.
The waitpid function provides three features that aren't provided by the wait function.
1. waitpid lets us wait for one particular process
2. waitpid provides a nonblocking version of wait. There are times when we want to fetch a
child's status, but we don't want to block.
3. waitpid supports job control.
This study source was downloaded by 100000894355947 from CourseHero.com on 11-27-2024 00:34:38 GMT -06:00
https://www.coursehero.com/file/63447224/RTS-Notesodt/
Powered by TCPDF (www.tcpdf.org)