EX. NO: DATE: IMPLEMENTATION OF BASIC OS COMMANDS OF UNIX USING C AIM: To write a C program to implement the basic OS commands of UNIX such as, ls grep cat USAGE: 1.ls ls lists the files in the current working directory. Without options, ls displays files in a bare format. The most common options to reveal this information or change the list of files are: -l long format, displaying Unix file types, permissions, number of hard links, owner, group, size, date, and filename -F appends a character revealing the nature of a file, for example, * for an executable, or / for a directory. Regular files have no suffix. -a lists all files in the given directory, including those whose names start with "." (which are hidden files in Unix). By default, these files are excluded from the list. -R recursively lists subdirectories. The command ls -R / would therefore list all files.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
EX. NO:
DATE:
IMPLEMENTATION OF BASIC OS COMMANDS OF UNIX USING C
AIM:
To write a C program to implement the basic OS commands of UNIX such as,
ls
grep
cat
USAGE:
1.ls
ls lists the files in the current working directory. Without options, ls displays files in a bare format. The most common options to reveal this information or change the list of files are:
-l long format, displaying Unix file types, permissions, number of hard links, owner, group, size, date, and filename
-F appends a character revealing the nature of a file, for example, * for an executable, or / for a directory. Regular files have no suffix.
-a lists all files in the given directory, including those whose names start with "." (which are hidden files in Unix). By default, these files are excluded from the list.
-R recursively lists subdirectories. The command ls -R / would therefore list all files.
-d shows information about a symbolic link or directory, rather than about the link's target or listing the contents of a directory.
-t sort the list of files by modification time.
-h print sizes in human readable format.
$ls
Lists all files in the present working directory
Eg:$ ls
a.out fact.c ffs.c hi max.c one.sh rad1.c rr.c sjf.c su two.c
$ls –l
Lists the permission given to each file
Eg:$ ls -l
total 248
-rwxrwxr-x 1 it40 it40 6356 Feb 24 21:34 a.out
-rw-rw-r-- 1 it40 it40 0 Mar 10 23:31 f1
-rw-rw-r-- 1 it40 it40 193 Jan 6 21:58 fact
$ls –a
Displays all hidden files of the user
Eg:$ ls -a
.bash_logout fact ffs.c lg.c one.c rad .ragr.c.swp six.c ss two.c
Thus a UNIX C program to implement interprocess communication using shared memory is executed successfully.
EX.NO:
DATE:
INTERPROCESS COMMUNICATION (PIPES)
AIM:
To write a UNIX C program to implement interprocess communication using pipes.
DESCRIPTION:
This program illustrates the usage of pipe() system call. The pipe has two ends, one for
reading and the other for writing data. The program shows single process writing message to
the pipe and reading the same.
ALGORITHM:
Step 1: Start the program.
Step 2: Create a pipe structure using pipe() system call.Pipe() system call returns 2 file descriptors fd[0] and fd[1].fd[0] is opened for reading and fd[1] is opened for writing.
Step 3:Create a child process using fork() system call.
Step 4:Close the read end of the parent process using close().
Step 5:Write the data in the pipe using write().
Step 6:Close the write end of the child process using close().
Step 7:Read the data in the pipe using read()..
Step 8:Stop the program.
PROGRAM:
#include<stdio.h>
int main()
{
int fd[2],child;
char a[20];
printf("\nEnter the string to enter into the pipe:");
scanf("%s",a);
pipe(fd);
child=fork();
if(!child)
{
close(fd[0]);
write(fd[1],a,5);
}
else
{
close(fd[1]);
read(fd[0],a,5);
printf("\n The string retrieved from the pipe is %s\n",a);
}
return 0;
}
OUTPUT:
$ vi iiiiiiiiic.c
]$ cc iiiiiiiiic.c
$ ./a.out
Enter the string to enter into the pipe: srinivasan
The string retrieved from the pipe is:srinivasan
RESULT:
Thus a UNIX C program to implement interprocess communication using pipes is executed successfully.
EX.NO:
DATE:
INTERPROCESS COMMUNICATION (MESSAGE QUEUE)
AIM:
To write a UNIX C program to implement interprocess communication using message queue.
DESCRIPTION:
Two (or more) processes can exchange information via access to a common system message
queue. The sending process places via some (OS) message-passing module a message onto a
queue which can be read by another process. Each message is given an identification or type
so that processes can select the appropriate message. Before a process can send or receive a
message, the queue must be initialized (through the msgget function see below) Operations to
send and receive messages are performed by the msgsnd() and msgrcv() functions,
respectively. When a message is sent, its text is copied to the message queue. The msgsnd()
and msgrcv() functions can be performed as either blocking or non-blocking operations. Non-
blocking operations allow for asynchronous message transfer -- the process is not suspended
as a result of sending or receiving a message. In blocking or synchronous message passing
the sending process cannot continue until the message has been transferred or has even been
acknowledged by a receiver. IPC signal and other mechanisms can be employed to
implement such transfer.
ALGORITHM:
1. Start
2. create message queue using msgget( ) system call
3. while creating message queue it returns id into file
descriptor
4. print the message queue id
5. if successful rite into message queue using msgsnd()
system call
6. read the contents from message queue using msgrcv( )
Thus a UNIX C program to implement interprocess communication using message queue is executed successfully.
EX.NO:
DATE:
IMPLEMENTATION OF FILE ALLOCATION TECHNIQUE
AIM:
To write a C program to implement File Allocation Techniques namely,
Indexed
Linked
Contiguous
DESCRIPTION:
The different file allocation techniques are:
Contiguous allocation.
Linked allocation.
Indexed allocation.
Contiguous Allocation:
This method requires each file to occupy a set of contiguous blocks on the disk. Disk addresses define a linear ordering on the disk. With this ordering, assuming that only one job is accessing the disk, accessing block b+1 after block b requires no head movement. When head movement is needed it is only one track. The IBM VM operating system uses contiguous allocation because it provides a good performance.
Contiguous allocation of a file is defined by the disk address and length of the first block. The directory entry for each file indicates the address of the starting block and length of area allocated for this file.
Accessing a file that has been allocated contiguously is easy. For sequential access, the file system remembers the disk address of the last block referenced and reads the next block when necessary. For direct access to block i of a file that starts at block b, we can immediately access block b + i. Both sequential and direct access can be supported by contiguous allocation.
Linked Allocation:
It solves all problems of contiguous allocation. In this method, each file is a linked list of disk blocks; the disk blocks may be scattered anywhere on the disk. The directory contains a pointer to the first and last blocks of the file. Each block contains a pointer to the next block. These pointers are not made available to the user.The drawback of this method is that it can be used effectively only for sequential access files. Another drawback is the space required for the pointers
Pointers use a smaller percentage of the file’s disk space. Another problem is reliability.
This method uses a File Allocation Table (FAT). This method is simple but efficient for disk-space allocation and it is used by MS-DOS and OS/2 operating systems. The table has one entry for each disc block and is indexed by block number. The FAT is used as a linked list. The directory entry contains the block number of the first block of the file. The table entry indexed by that block number then contains the block number of the next block in the file.
Indexed Allocation:
In the absence of FAT, the linked allocation cannot support efficient direct access, since the pointers to the blocks are scattered with the blocks themselves all over the disk and need to be retrieved in order. Indexed allocation solves this problem by bringing all pointers together in one location: the index block.
Each file has its own index block, which is an array of disk- block addresses. The ith entry in the index block points to the ith block of file. The directory contains the address of the index block. To read the ith block, we use the pointer in the ith index block entry to find and read the desired block.