Practical Os

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Practical 2

Step 1 − Create pipe1 for the parent process to write and the child process to read.
Step 2 − Create pipe2 for the child process to write and the parent process to read.
Step 3 − Close the unwanted ends of the pipe from the parent and child side.
Step 4 − Parent process to write a message and child process to read and display
on the screen.
Step 5 − Child process to write a message and parent process to read and display
on the screen.

Program
#include<stdio.h>
#include<unistd.h>
int main() {
int pipefds1[2], pipefds2[2];
int returnstatus1, returnstatus2;
int pid;
char pipe1writemessage[20] = "Hi";
char pipe2writemessage[20] = "Hello";
char readmessage[20];
returnstatus1 = pipe(pipefds1);
if (returnstatus1 == -1)
{
printf("Unable to create pipe 1 \n");
return 1;
}
returnstatus2 = pipe(pipefds2);

if (returnstatus2 == -1) {
printf("Unable to create pipe 2 \n");
return 1;
}
pid = fork();

if (pid != 0) // Parent process {


close(pipefds1[0]);
close(pipefds2[1]);
printf("In Parent: Writing to pipe 1 –%s\n", pipe1writemessage);
write(pipefds1[1], pipe1writemessage, sizeof(pipe1writemessage));
read(pipefds2[0], readmessage, sizeof(readmessage));
printf("In Parent: Reading from pipe 2 –%s\n", readmessage);
}
else
{
close(pipefds1[1]);
close(pipefds2[0]);
read(pipefds1[0], readmessage, sizeof(readmessage));
printf("In Child: Reading from pipe 1 – %s\n", readmessage);
printf("In Child: Writing to pipe 2 –%s\n", pipe2writemessage);
write(pipefds2[1], pipe2writemessage, sizeof(pipe2writemessage));
}
return 0;
}
Practical 3
Step 1 − Create a message queue or connect to an already existing message queue
(msgget())
Step 2 − Write into message queue (msgsnd())
Step 3 − Read from the message queue (msgrcv())
Step 4 − Perform control operations on the message queue (msgctl())

Program

#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
// structure for message queue
struct msg_buffer
{
long msg_type;
char msg[100];
} message;
main()
{
key_t my_key;
int msg_id;
my_key = ftok("progfile", 65);
msg_id = msgget(my_key, 0666 | IPC_CREAT);
message.msg_type = 1;
printf("Write Message : ");
fgets(message.msg, 100, stdin);
msgsnd(msg_id, &message, sizeof(message), 0);
printf("Sent message is : %s \n", message.msg);
}
Practical 10
1. tee – It reads from the standard input and writes to both standard output and one or
more files at the same time.
Syntax –
tee -a filename – used to append a file
2. pg – it displays the contents of text files, one page at a time.
Syntax –
pg [-number] [-p string] [-cefnrs] [+line] [+/pattern/] [file...]

-number The number of lines per page.


-c Clear the screen before a page is displayed,
-e Do not pause and display the end of a file.
-f Do not split long lines.
-n pg advances once a command letter is
entered.
-p string Instead of the normal prompt ":", string
is displayed.
-r Disallow the shell escape.
-s Print messages in standout mode
+number Start at the given line.
+/pattern/ Start at the line containing the basic regular
expression.

3. comm –
compare two sorted files line by line and write to standard output
Syntax-
$comm [OPTION]... FILE1 FILE2
4. cmp –
used to compare the two files byte by byte and helps to find out whether the two
files are identical or not.
Syntax-
cmp [OPTION]... FILE1 [FILE2 [SKIP1 [SKIP2]]]
eg – cmp file1.txt file2.txt

5. diff – used to compare two files line by line.


Syntax –

diff [options] File1 File2

6. tr – used for translating or deleting characters


Syntax-
$ tr [OPTION] SET1 [SET2]
7. tar - used to create Archive and extract the Archive files.
Syntax –
tar [options] [archive-file] [file or directory to be archived]
8. cpio- cpio stands for “copy in, copy out“. It is used for processing the archive files
like *.cpio or *.tar.
Syntax- Copy-out Mode
cpio -o < name-list > archive
Copy-in Mode:
cpio -i < archive
Copy-pass Mode:
cpio -p destination-directory < name-list

9. mount – used to mount a file system


Syntax –
mount -t type <device> <directory>
10. umount- used to unmount file systems
Syntax-

umount -t type <device> <directory>

11. find –used to find files and directories


Syntax- $ find [where to start searching from]
[expression determines what to find] [-options] [what to find]
12. umask - used to set default permissions for files or directories
Syntax – umask
Binary Value Description

000 No permission

001 only permission to execute

010 only permission to write

011 permission to write and execute

100 only permission to read

101 permission to read and execute

13. ulimit- used to see, set, or limit the resource usage of the current user
Syntax- ulimit -a (for checking limit)
14. sort - used to sort a file
Syntax- $ sort filename.txt
15. grep – it searches a file for a particular pattern of characters
Syntax- grep [options] pattern [files]
16. egrep- egrep is a pattern searching command which belongs to the family of grep
functions
Syntax- egrep [ options ] 'PATTERN' files
17. fgrep - used to search for the fixed-character strings in a file.
Syntax- fgrep [options] [ -e pattern_list] [pattern] [file]
18. paste- It is used to join files horizontally
Syntax- paste [OPTION]... [FILES]...
19. join- used for joining lines of two files on a common field.
Syntax- $join [OPTION] FILE1 FILE2
20. du- used to find out the disk usage of set of files or a directory.
Syntax- du [OPTION]... [FILE]...
21. df - ( disk free), is used to display information related to file systems about total
space and available space.
Syntax- df [OPTION]... [FILE]...
22. ps- is used to list the currently running processes and their PIDs
Syntax - ps [options]
23. who- who command is used to find out the following information :
1. Time of last system boot
2. Current run level of the system
3. List of logged in users and more.
Syntax - $who [options] [filename]

You might also like