Download Client Server Model - Introduction to Operating System - Lecture Notes and more Study notes Operating Systems in PDF only on Docsity!
Operating Systems--[CS-604] Lecture No. 11
Operating Systems
Lecture No. 11
Reading Material
UNIX/Linux manual pages for the mknod() system call, the mkfifo() library
call, and the mkfifo, ps, and top commands
Lecture 11 on Virtual TV
Summary
More on the use of FIFOs in a program
Example code for a client-server model
A few UNIX/Linux process management commands
Use of FIFOs
We continue to discuss the API for using FIFOs for IPC between UNIX/Linux processes.
We call these processes client and server. The server process creates two FIFOs, FIFO
and FIFO2, and opens FIFO1 for reading and FIFO2 for writing. The client process opens
FIFO1 for writing and FIFO2 for reading. The client process writes a message to the
server process and waits for a response from the server process. The server process reads
the message sent by the client process and displays it on the monitor screen. It then sends
a message to the client through FIFO2, which the client reads and displays on the monitor
screen. The server process then closes the two FIFOs and terminates. The client, after
displaying server’s message, deletes the two FIFOs and terminates. The protocol for the
client-server interaction is shown in Figure 10.1.
Figure 10.1 Client-server communication using UNIX/Linux FIFOs
The codes for the server and client processes are shown in Figure 10.2 and Figure
10.3, respectively.
FIFO
Server Client
Display Screen
FIFO
#include <stdio.h> #include <string.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/errno.h>
extern int errno;
#define FIFO1 "/tmp/fifo.1" #define FIFO2 "/tmp/fifo.2" #define PERMS 0666 #define MESSAGE1 "Hello, world!\n" #define MESSAGE2 "Hello, class!\n" #include "fifo.h“ main() { char buff[BUFSIZ]; int readfd, writefd; int n, size;
if ((mknod (FIFO1, S_IFIFO | PERMS, 0) < 0) && (errno != EEXIST)) { perror ("mknod FIFO1"); exit (1); } if (mkfifo(FIFO2, PERMS) < 0) { unlink (FIFO1); perror("mknod FIFO2"); exit (1); } if ((readfd = open(FIFO1, 0)) < 0) { perror ("open FIFO1"); exit (1); } if ((writefd = open(FIFO2, 1)) < 0) { perror ("open FIFO2"); exit (1); } size = strlen(MESSAGE1) + 1; if ((n = read(readfd, buff, size)) < 0) { perror ("server read"); exit (1); } if (write (1, buff, n) < n) { perror("server write1"); exit (1); } size = strlen(MESSAGE2) + 1; if (write (writefd, MESSAGE2, size) != size) { perror ("server write2"); exit (1); } close (readfd); close (writefd); }
Figure 10.2 Code for the server process
UNIX/Linux Command for Process Management
We now discuss some of the UNIX/Linux commands for process management, including
ps and top. More commands will be discussed in lecture 12.
ps – Display status of processes
ps gives a snapshot of the current processes. Without options, ps prints information
about processes owned by the user. Some of the commonly used options are -u, -e, and
-l.
-e selects all processes
-l formats the output in the long format
-u displays the information in user-oriented format
The shell session in Figure 10.5 shows sample use of the ps command. The first
command shows the processes running in your current session. The second command
shows, page by page, the status of all the processes belonging to root. The last command
shows the status of all the processes running on your system.
$ ps
PID TTY TIME CMD
1321 pts/0 00:00:00 csh
1345 pts/0 00:00:00 bash
1346 pts/0 00:00:00 ps
$ ps -u root | more
PID TTY TIME CMD
1? 00:00:04 init
5? 00:00:01 kswapd
712? 00:00:00 inetd
799? 00:00:00 cron
864? 00:00:00 sshd
934? 00:00:00 httpd
1029 tty1 00:00:00 getty
$ ps -e | more
PID TTY TIME CMD
1? 00:00:04 init
2? 00:00:00 keventd
3? 00:00:00 ksoftirqd_CPU
4? 00:00:00 ksoftirqd_CPU
5? 00:00:01 kswapd
6? 00:00:00 kreclaimd
7? 00:00:00 bdflush
8? 00:00:00 kupdated
Figure 10.5 Use of the ps command
top – Display CPU usage of processes
top displays information about the top processes (as many as can fit on the terminal or
around 20 by default) on the system and periodically updates this information. Raw CPU
percentage is used to rank the processes. A sample run of the top command is shown in
Figure 10.6. The output of the command also shows the current time, how long the
system has been up and running, number of processes running on the system and their
status, number of CPUs in the system and their usage, amount of main memory in the
system and its usage, and the size of swap space and its usage. The output also shows a
lot of information about each process, including process ID, owner’s login name, priority,
nice value, and size. Information about processes is updated periodically. See the manual
page for the top command for more information by using the man top command.
$ top
9:42am up 5:15, 2 users, load average: 0.00, 0.00, 0. 55 processes: 54 sleeping, 1 running, 0 zombie, 0 stopped CPU0 states: 0.0% user, 0.1% system, 0.0% nice, 99.4% idle CPU1 states: 0.0% user, 0.2% system, 0.0% nice, 99.3% idle Mem: 513376K av, 237732K used, 275644K free, 60K shrd, 17944K buff Swap: 257032K av, 0K used, 257032K free 106960K cached
PID USER PRI NI SIZE RSS SHARE STAT %CPU %MEM TIME COMMAND 1406 sarwar 19 0 896 896 700 R 0.3 0.1 0:00 top 1382 nobody 10 0 832 832 660 S 0.1 0.1 0:00 in.telnetd 1 root 9 0 536 536 460 S 0.0 0.1 0:04 init 2 root 9 0 0 0 0 SW 0.0 0.0 0:00 keventd ...
Figure 10.6 Use of the top command