You are on page 1of 7

Jaypee Institute of Information Technology, Noida

5thSemB.Tech CSE/IT
ODD Sem 2014
OS Lab Exercise Sheet


Teacher Incharge for Lab:
Dhanalekshmi.G
Chetna Dabas
GaganDeepKaur
AmanPreet Kaur
RohitPalSingh


Lab Plan
Lab Test1 20 Marks
Lab Test2 20 Marks
OS Project Synopsis 5 Marks (5
th
September deadline)
Final Project Evaluation 15 Marks
D2D Evaluation 40 Marks (Notified as per Lab Exercise)

Instructions for the Lab
1. All students have to maintain separate Record File for OS Lab. Hard copy
of the exercise should be available during lab evaluations.
2. Students have <65% Lab attendance will get ZERO in D2D marks.
3. OS project will have three evaluations ( Synopsis, Final project +Report)
4. Deadline for submission should be strictly followed. Any absence during evaluation will get
Zero.
5. Before leaving lab your system should be logged off from FEDORA.
Operating System Lab(10B17CI571) Exercise Sheet

















Revision Exercise

Lab1 (Week 1: 11
th
August 16
th
August)

Refer Unix_Commands.PDF and execute all commands from it.
FILE COMMANDS
DIRECTORY COMMANDS
SYMOLIC LINKS
TERMINAL COMMANDS
HELP COMMANDS
INFORMATION COMMANDS
USEFUL CSHELL SYMBOLS
PERMISSIONS AND FILE STORAGE (UNIX)
PERMISSIONS AND FILE STORAGE (ANDREW)
PROCESSES
PRINTING
ENVIRONMENT
CUSTOMIZING
NETWORKING
X-APPLICATIONS
UNIX FILTERS

Exercises
After you have practiced with the above commands, try to solve the following problems using
the UNIX commands.
1. Find the number of words in 3rd line of a given file.
2. List the number of files starting with the letter A.
3. List the FULL NAMEs of all the users currently logged in.
4. Save the output of the command (from problem 3) into a file.
6. Append the output of a command into a file.
7. Find the size of a given file.









Lab2 (Week 2: 18
th
August- 23
rd
August)

Refer Enhancing Your UNIX skills.PDF and solve practical exercise from it.
Additional to the above exercise execute the following

a. WriteLinuxCProgramtoprinttheentireenvironmentvariableonthescreen.
b. WriteLinuxCProgramtoprintHOMEDIRECTORY,HOSTandSERVERNAMEusing
environmentvariable.
c. Write a Linux C program that prints out the size (number of bytes) of basic types:
sizeof(short), sizeof(int), sizeof(long), sizeof(float), sizeof(double), sizeof(long double).
d. Write Linux C program named prog1 which is invoked with the command line:
prog1 add 12 5
Should print the output in the following format.
No.Of Arguments Passed is 4,
the value of argv[0] is "prog1",
the value of argv[1] is "add",
the value of argv[2] is "12",
and the value of argv[3] is "5".



Lab3 ( Week 25
th
August 30
th
August)

Refer Systemcalls.pdf (file and Directory Management System Calls ) and solve the following
problems.

Note: - DONOTsimplyexec Unix commands fromtheprogram.

a. Implement your own version of acommand to copy a file that should be almost same as
the UNIX cp command that makes a copy of the file in the given name. Name your
command as copyfile that should have the following syntax:
copyfile source_file_name destination_file_name

that copies source_file_name into destination_file_name and reports with an
appropriate message if the source_file_name does not exist or cant be opened. You can
use open( ), read( ), write( ) and close( ) system calls

b. Implement your own version of acommand to display a file that should be almost same
as the UNIX cat command that displays a requested file on the screen. Name your
command as displayfile that should have the following syntax:
displayfile file_name
that displays the file called file_name and reports with an appropriate message if the
file_name does not exist or cant be opened. You can use open( ), read( ), write( ) and
close( ) system calls.
c. Write a program that implements the ls utility of UNIX by using getdents ( ) system call
to first get the names of files in the given directory and then using the stat ( ) system call
get all the needed information for the files present and print them one by one.

d. A program that reads a name, which can be a filename or a directory name and if it is a
directory it counts the number of files in the directory and prints the count as well as the
names of all the files.

Lab 4 : ( Week 4 & 5: 1st Sept-13
th
Sept, Evaluative : 20 Marks)

1. Write Linux C Program to display the process attributed as follows:
a. The process ID or PID
b. The parent process ID or PPID
c. Nice number
d. Terminal or TTY
e. User name of the real and effective user (RUID and EUID).
Sort the output of the above into files called procatt0.txt and procatt1.txt depending on process
creation time and CPU utilization time.

2. Write Linux C Program to create the child process using fork command. Display the output in
following format:

Before Fork: Hello World ! I am 10539
I am the Parent : 10539 of Child: 10540
I am the Child : 10540

Note: 10539 and 10540 are parent ID and Child ID


3. Write a program that uses the child to compute partial sums and parent to compute the partial
products of an array of integers.

4. Write C code which does operation given below.
Create a child process which calculates total of even numbers between 0 and 10,
displays sub steps of adding operation. Then child process will execute ls command.
Parent process will calculate total of numbers which are multiple of 3 between 0 and 10.

5. Write a program that will read a directory containing a set of files, create a new subfolder
called backup on same folder and copy all the files from directory to the new sub directory
backup. You are supposed to use exec() with cp command. Create a child process for each
file that can execute each of the cp commands to copy the file from directory to subfolder
backup

6. write a program which creates one child process, loads new binary code (compiled from other
source) for the child, and then parent waits (without using CPU nor I/O) until child finishes his
task (counting from 1 to 10 and displaying counter status for example). The output screen may
look like this:
[PARENT]: PID 2421, waits for child with PID 2422
[CHILD]: PID 2421, starts counting:
[CHILD]: i =1
[CHILD]: i =2
...
[CHILD]: i =10
[PARENT]: Child with PID 2422 finished and unloaded.

7.Write a program that executes the "cat -b -v -t filename" command. Call your executable
myforkdemo
Details:
a. The call to your program will be made with the following command:
$ myforkdemo filename
b. Your code will fork()
c. The child will use the execl to call cat and use the filename passed as an argument
on the command line
d. The parent will wait for the child to finish
e. Your program will also print from the child process:
The process id
The parent id
The process group id
and print from the parent process:
the process id
the parent id
the process group id
f. Comment out the execl call and add instead a call to execv. Add any necessary
variables to do that.
2. Answer the following questions:
a. If you try to print a message after the exec* call, does it print it? Why? Why not?
b. Who is the parent of your executable (myforkdemo) program?
c. How would you change the code so that the child and parent run concurrently?
8.Given the following code:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>

char mynum='0';
int main(void)
{
int i;
pid_t fork_return;
static char buffer[10];
fork_return =fork();
if (fork_return ==0)
{
strcpy(buffer, "CHILD"); /*in the child process*/
for (i=0; i<5; ++i) /*both processes do this*/
{
mynum=i +'0';
sleep(1); /*5 times each*/
write(1, buffer, sizeof(buffer));
write(1, &mynum, 1);
write(1, "\n", 1);
}
return 0;
}
else
{
strcpy(buffer, "FATHER"); /*in the parent process*/
for (i=0; i<5; ++i) /*both processes do this*/
{
sleep(1); /*5 times each*/
write(1, buffer, sizeof(buffer));
write(1, &mynum, 1);
write(1, "\n", 1);
}
return 0;
}
}
Run the above code and answer the following question:
1. Notice that mynum is a global variable.
o Child is incrementing the ascii value of mynum
o Parent is not
o Child and Parent can take turns running
Why does child print CHILD0, CHILD1, CHILD2, etc whereas parent prints FATHER0,
FATHER0, FATHER0, etc? Remember mynum is a global variable.
9. Write a C program which uses Unix system calls to create a new process. The childprocess
should create an empty file called abc and then terminate. The parent process should wait for
the child process to terminate and then output the process number of the child process. Don't
forget to check for error conditions.

10. Write a Program to demonstrate the creation of Zombiee process and OrphanProcess.