5/5 - (2 votes)

Lab 2
On-Line Documentation
man command display on-line manual pages about command
SPACEBAR moves down one screen
RETURN move down one line
^d move down 1/2 screen
^b move up 1/2 screen
q exit
h help
mkdir directory-name create a directory called directory-name
cd change to your home directory
cd directory-name change to another directory
~ home directory (tilde)
. current or working directory
.. parent of working directory

rm filename remove a file
rmdir directory-name remove an empty directory
cp source-filename destination-filename: copy a file into another file
cp source-filename destination-directory copy a file into another directory
mv present-filename new-filenames rename a file
mv source-filename destination-directory move a file into another directory
chmod [who op permission] filename change file access information
who any combination of u (user), g (group), o (other), a (all)
op adds or takes away permission, + (add), – (remove), = (set)
permission any combination of r (read), w(write), x (execute)
pwd display name of present working directory
History: command repetition
history display list of most recent commands
!! repeat entire last command line at any point in current command line
!$ repeat last word of previous command
!^ repeat first argument from previous command
!n repeat command line n
!!:p display previous command
!string command beginning with string
!* repeat all arguments to previous command
Process Control
ps display status of current processes
-a include processes owned by other users
-g display all processes
-u display user-oriented processes
kill id-number terminate a process having id-number owned by you
File System Manipulation
grep search-string filename [filename..] find and type out lines containing search-string in filename
-v type out lines that don’t contain search-string
wc filename counts the number of words, lines, or characters in filename
-w words
-l lines
-m characters
diff filename1 filename2 compare contents of filename1 and filename2 on a line-by-line basis
cat filename create, view, and concatenate filename
echo text-string echo text-string on stdout
ed or ex filename Unix line editors
emacs filename full-screen editor
ls list contents of current directory
ls directory-name list contents of directory
-a list all files, including files that start with “.”
-s list size of files (in kbytes)
-l long list, shown ownership, permission, and links
-t list files chronologically
-u list files using time of last access
more filename page through filename, same navigation as man
head filename display first ten lines of a file
tail filename display last ten lines of a file
less filename page through filename
pg filename page through filename
passwd set or change password
Other commands
env list environment settings
who users on local system
finger [email protected] looks up information on another user
clear clear screen
date display current time and date
cal year display yearly calendar
cal ## #### display monthly calendar
bc basic calculator (^d to exit)
du display number of disk blocks used per directory or file
du –s display your total disk usage
quota –v display your disk quota and usage
fork() is used to create processes. The fork() returns 0 to the child and returns the child’s process ID to the
parent. The getpid() and getppid() get the process ID and the parent process ID.
1) Directory (% is UNIX/LINUX prompt)
% cd
% mkdir lab326
% mkdir lab326A
% cd lab326
2) Use cat to create a file named myinfo
% cat >myinfo
name your name
SSN your student number
Major your major
^D to get out
% cat myinfo
% cat >>myinfo
Class your class level
% cat >mystat
Pre CECS-274
% cat myinfo mystat >mynewinfo
3) Practice with other commands: who, pwd, cd, date, echo, cal, wc, etc.
4) Create a chain of processes.
Study the following program. Determine if there is any compilation error. Compile and run with
values for number of processes ranging from 2 to 10.
/* PROGRAM: lab2.c */
/* DESCRIPTION: This program generates a chain of processes */
/* using fork(). The number of processes n is a command line argument. */
/* Each process sleeps for a random time less than 10 seconds then prints out */
/* process ID, parent ID, and child ID */
# include <stdio.h>
# include <stdlib.h>
# include <sys/types.h>
# include <unistd.h>
int main(int argc, char *argv[])
int i, m, n, sleeptime, seed;
pid_t childpid;
if (argc !=2) {
printf(“\n Usage: %s processes\n”, argv[0]);
n = atoi(argv[1]); m = 10;
childpid = 0;
for (i=0; i<n;i++)
if (childpid = fork()) break;
if (childpid == -1) {
perror (“\n The fork failed\n”);
printf(“\n %d: process ID:%6ld parent ID:%6ld child
ID:%6ld”,i, (long)getpid(), (long)getppid(), (long)childpid);
seed = (int)(getpid() + childpid);
/* since each process has a different childpid, using the childpid
as the seed number will restart the random function.
Therefore, each process will have a different sleeptime
sleeptime = rand()%m;
printf (” sleep = %d\n”, sleeptime);
1) Create a directory named cecs326. Use cat to create a file named lab2A having the following
This is a test file
using UNIX commands
Then, create a directory named lab2 in directory cecs326. Use cat to create a file named lab2B
having the following contents:
This file will be merged with lab2A
End of file
Then copy lab2B to cecs326. Then merge lab2A and lab2B into lab2C.
Then, remove all files and directories cecs326 and lab2.
2) Copy the above program lab2.c into directory cecs326. Use a UNIX command to determine the
number of words in this program. Change the program to include another command line argument
to indicate the maximum sleep time. This maximum sleep time is used as m. The number of
processes must be less than 10 and m must be less than 20. Check for input errors.

Open chat
Need help?
Can we help?