Docsity

Understanding Unix Pipelines: Shell, System Calls, Processes, and IPC - Prof. Prasad Kulka, Lab Reports of Operating Systems

Partial preview of the text

Download Understanding Unix Pipelines: Shell, System Calls, Processes, and IPC - Prof. Prasad Kulka and more Lab Reports Operating Systems in PDF only on Docsity!

The Shell, System Calls, Processes,

and Basic Inter-Process

Communication

Michael Jantz

Dr. Prasad Kulkarni

Shell Programs

●

A shell program provides an interface to the

services of the operating system.

●

It interprets user commands and uses the

system calls provided by the operating

system to execute commands.

●

As programmers sought to increase efficiency

and convenience of shell programs, shells

with fairly sophisticated command languages

developed.

Utility Programs

● Any Unix distribution comes with several utility programs for

interacting with the OS.

  • (^) grep – search for strings in a file
  • (^) find – find a particular file
  • (^) du – Determine the disk usage of files and directories
  • (^) ls – List files and their permissions

● Many, many more. Proficiency with these basic tools will

make you a much more effective developer on your platform.

● Unix provides a manual (accessible from the shell) that

documents the use and syntax of each core utility. e.g:

  • (^) man grep

Parsing a Command

● (^) Say you needed to determine which files in some directory tree had the highest number of occurrences of a particular string. You could write a program in C to determine this, but it would be much faster if you could combine some core utilities to achieve this. Take the command:

  • (^) grep -cr str dir | sort -t : +1.0 -2.0 –numeric –reverse | head –lines= ● (^) To understand this command, you must understand each of its parts
  • (^) grep -cr str dir – Search for the string str in the directory dir. -c says that instead of printing out each usage in each file, give me the number of times str is used in each file. -r says recursively search all subdirectories.
  • (^) sort – Sort my standard input and print the sorted order to standard output. -t : +1.0 -2.0 says sort using the second column on each line (delimited by the ':' character) as a key. --numeric says to sort numerically (as opposed to alphabetically). --reverse says sort in reverse order.
  • (^) head – print only the first n lines of standard input. --lines=5 sets n to 5 (it is 10 by default). ● (^) The man page for each utility is a good reference for each option and argument for these commands.

Getting Started

● Grab the starter code from here:

  • (^) http://people.eecs.ku.edu/~mjantz/678labs/lab03/ ●

The first thing to notice after untarring the tar file is the

makefile:

  • (^) Set the DIR variable equal to where your NACHOS source directory is located. (Or some other tree you would like to search).
  • (^) Test the command. In this lab's directory, type: ● (^) make grep ● (^) Should see output as described two slides above ●

The goal of this lab is to write a program – pipe.c – that takes

no arguments and produces the same output as this

command when executed.

pipe.c

●

As it is given, this program is a skeleton for a

three stage pipeline.

●

All it currently does is start a process, which

forks off three children (which do nothing),

waits for them to finish, and exits.

System Calls

●

In order to accomplish this, you will need to make

use of a few different system calls.

●

The starter code uses fork() and waitpid() to create

the general framework under which you will

implement the desired functionality.

  • (^) If you do not understand how these work, please ask me. ●

You will extend this program to create the desired

functionality using:

  • (^) pipe(), dup2(), execl(), and close()

File Descriptors

● Before getting into how these system calls work, you must

first understand file descriptors:

  • (^) A file descriptor is a per-process, unique, non-negative integer used to identify an open file for the purpose of file access.
  • (^) System calls (such as open, close, read, and write) use file descriptors to identify a particular file or pipe.

● IMPORTANT:

  • (^) Each process has its own file descriptor table that maps each open file descriptor to a file object maintained by the operating system.
  • (^) The file descriptor table is located in the process' address space and is inherited by children of the process.

dup2()

●

int dup2(int oldfd, int newfd)

  • (^) On return, the newfd provided will be a copy of oldfd (i.e.

newfd will refer to the same file (or pipe) as oldfd).

●

Things to think about:

  • (^) Each utility program we want to use in the pipeline reads

from stdin and writes to stdout.

  • (^) Why have I showed you dup2() and not the closely related

dup()? See man dup for an explanation of dup.

Constructing the Pipeline

● Go ahead and try to setup the pipeline using the pipe() and

dup2() system calls.

● Remember to close() unused file descriptors for each

process (See the comment in pipe.c for an explanation).

● When you are finished, uncomment the code in each child

process to test your implementation.

● The test code should push the contents of /proc/meminfo

down the pipeline and be displayed by the third child's stdout.

  • (^) If your program displays the contents of this file to the terminal, but does not terminate, you do not have a correct implementation.

execl()

● There are many different flavors of exec.

  • (^) They give the programmer options as to how he or she would like to specify an executable and its arguments.

● int execl(const char *path, const char *arg1,... )

  • (^) Allows you to specify the path to the executable and the arguments to the executable (as you would type at the command line) as a variable number of const char * arguments.
  • (^) Final argument must be (char *) 0
  • (^) Example for grep: ● (^) execl(“/bin/grep”, “grep”, “-cr”, “Thread”, “/usr/nachos”, (char *) 0)

Finishing Up

●

After you have each execl in the appropriate

place, remake the pipe program and run the

test code:

– make test

●

If the diff line does not produce an error, your

implementation is correct.