0% found this document useful (0 votes)
4 views7 pages

RTS - Unit 1 Jay

RTOS unit 1

Uploaded by

vision3745
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

RTS - Unit 1 Jay

RTOS unit 1

Uploaded by

vision3745
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

RTS

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.

3.1. File Descriptors


To the kernel all open files are referred to by file descriptors. A file descriptor
is a non-negative integer. When we open an existing file or create a new file, the
kernel returns a file descriptor to the process, when we want to read or write a file, we
identify the file with the file descriptor that was returned by open or creat as an
argument to either read or write.
By convention Unix shells associate file descriptor 0 with the standard input of a
process, file descriptor 1 with the standard output, file descriptor 2 with the standard
error.It is convention applied by the unix shells and many unix applications- It is not
an application of the kernel.
File descriptors range from 0 through OPEN_MAX. Older versions of unix have an
upper limit of 19 but this was increased to 63 by many systems.

3.2. open Function


A file opened or created by calling the open function.

#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>

int open (const char *pathname, int oflag, . . . /* , mode_t mode */ );


Returns: file descriptor if OK, -1on 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
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.

3.3. creat Function


A new file can also be created by

#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>

int creat (const char *pathname, mode_t mode );


Returns: file descriptor opened forwrite-only if OK, -1 on error

This function is also equivalent to:


open(pathname, O_WRONLY | O_CREAT | O_TRUNC, mode);
One deficiency with creat is that the file is opened only for writing.

3.4. close Function


An open file is closed by

#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.

3.5. lseek Function


Every open file has an associated “current file offset." This is a non negative integer that
measures the number of bytes from the beginning of the file. Read and write operation normally
start at the current file offset and cause the offset to be incremented by the number of bytes read or
written. By default, this offset is initialized to 0 when a file is opened unless the . APPEND option
is specified.
An open file can be explicitly positioned by calling I seek.

#include <sys / types.h>


#include<unistd.h>

off_t lseek(int filedes, off_t offset, int whence ):


Returns: new file offsetif OK, -1 on error

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.

3.6. read Function


Data is read from an open file with the read function.

#include<unistd.h>

ssize_t read(int filedes, void *buff, size_t nbytes);


Returns: number of bytes read, 0 if end of file, -1 on error.

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.

3.7. write Function


Data is written toan open file with the write function.

#include<unistd.h>

ssize_t write(int filedes,const void *buff, sze_t nbytes) ;


Returns: number of bytes written if OK, -1 on error.

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.

4.1. Process Identifiers


Every process has a unique process Id, a non-negative integer. Since the process ID is the
only well-known identifier of a process that is always unique, it s often used as a piece of other
identifiers , to guarantee uniqueness.
There are some special processes. Process ID () is usually the scheduler process and is often known
as the swapper.
• Process ID -1 is usually the init process and is invoked by the kernel at the end of the
bootstrap procedure.
• Process ID-2 is the pagedaemon. This process is responsible for supporting the paging of the
virtual memory system. Like he swapper, the pagedaemon is a kernel process.

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.

4.3. vfork Function


The function vfork has the same calling sequence and same return values as fork. But the
semantics of the two functions differ.
vfork is intended to create a new process when the purpose of the new process is to exec a
new program. vfork creates the new process just like fork, without fully copying the address space
of the parent into the child, since the child won’t reference that address space - the child just calls
exec rigth after the vfork. Instead, while the child is running, until it calls either exec or exit, the
child runs in the address space of the parent.
Another difference between the 2 functions is that vfork guarentees that the child runs first,
until the child calls exec or exit.

4.4. exit Function


There are three ways for a process to terminate normally and two forms of abnormal
termination.
1. Normal termination:
a) Executing a return from the main function. This equivalent to calling exit.

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.

4.5. wait and waitpd Functions


When a process terminates, either normally or abnormally, the parent notified by he kernel
sending the parent the SIGCHLD signal. Since the termination of a child is an asynchronous event
this signal is the asynchronous notification from the kernel to the parent. The default action for this
signal is to ignored. A process that calls wai and waitpid can
• block or
• return immediately with the termination status of a child or
• return immediately with an error
If the process is calling wait because it received the SIGCHLD signal, we expect wait to return
immediately. But if we call it at any random point in time it can block.

#include <sys/types.h>
#include<sys/wait.h>

pid_t wait( int *statloc)


pid_t waitpid(pid_t pid, int *statloc, int options);
Both return: Process ID if OK, 0, or -1 on error.

The differences between these two functions are


• wait can block the caller until a child process terminates, while waitpid has an option that
prevents it from blocking.
• waitpid doesn't wait for the first child to terminate--it has a number of options that control
which process it, waits for.
For both functions the argument static is a pointer to an integer. If the argument is not a null pointer,
the termination status of the terminated process is stored in the location pointed to by the argument.

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.

4.6. exec Functions


one use of the fork function was to create a new process that then causes another program to
be executed by calling one of the exec functions. When a process calls one of the exec functions,
that process is completely replaced by the new program, and the new program starts executing at its
main function. The process ID does not change across an exec because a new process is not created.
exec merely replaces the current process with a brand new program from disk.
There are six different exec functions. They are
1. execl
2. execv
3. execle
4. execve
5. execlp
6. execvp
These 6 functions round out the unix process control primitives i.e, fork, exec, exit and two wait
functions.
The first difference in these functions is that the first four take a pathname argument while the last
two take a file name argument. When a filename argument is specified
• if filename contains a slash, it is taken as a pathname.
• Otherwise, the executable file is searched for in the directories specfied by the PATH
environment variable.

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)

You might also like