0% found this document useful (0 votes)
15 views3 pages

Lab-03 OS BSCS-3C

The document provides instructions for three questions related to command line arguments and file handling in C/C++. Question 1 involves writing a program that counts the occurrences of a substring in a file given as command line arguments. Question 2 involves creating header and source files and using a Makefile. Question 3 involves using fork to create a child process that counts vowels in a file while the parent process waits.

Uploaded by

lolman78781
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views3 pages

Lab-03 OS BSCS-3C

The document provides instructions for three questions related to command line arguments and file handling in C/C++. Question 1 involves writing a program that counts the occurrences of a substring in a file given as command line arguments. Question 2 involves creating header and source files and using a Makefile. Question 3 involves using fork to create a child process that counts vowels in a file while the parent process waits.

Uploaded by

lolman78781
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

National University of Computer and Emerging Sciences

Laboratory Manual

for
Operating Systems Lab

Course Instructor Mr. Raziuddin


Lab Instructor(s) Usman Anwer

Section BCS-4D
Semester Spring-24

Department of Computer Science


FAST-NU, Lahore, Pakistan
Command Line Arguments:
Command-line arguments are a way to pass data to the program. Command-line arguments are
passed to the main function. Suppose we want to pass two integer numbers to the main function
of an executable program called a.out. On the terminal write the following line:

./a.out 1 22

./a.out is the usual method of running an executable via the terminal. Here 1 and 22 are the
numbers we passed as command-line arguments to the program. These arguments are passed to
the primary function. In order for the main function to be able to accept the arguments, we have
to change the signature of the primary function as follows:

int main(int argc, char *arg[]);

 argc is the counter. It tells how many arguments have been passed.
 arg is the character pointer to our arguments.

argc in this case will not be equal to 2, but it will be equal to 3. This is because the name ./a.out
is also passed as command line argument. At index 0 of arg, we have ./a.out; at index 1, we have
1; and at index 2, we have 22. Here 1 and 22 are in the form of character string, we have to
convert them to integers by using a function atoi. Suppose we want to add the passed numbers
and print the sum on the screen:
cout<< atoi(arg[1]) + atoi(arg[2]);

Question 1: Write a C or C++ program that accepts a file name and a substring as command
line argument and prints the no of occurrences of substring in the given file on the console.If the
file does not exist, print some error on the screen.

The file name is question1.txt with the following content:

“Hello, this is the command line argument practice and my second day in the lab of operating
systems. I am enjoying it the alot. Hello, this is the command line argument practice and my
second day in lab of operating systems. I am enjoying it the alot.Hello, this is the command line
argument practice and my second day in the lab of operating systems. I am enjoying it the alot.”
Question 2: (MakeFile)
Create 3 files,
● header.h
● array.cpp
● main.cpp
header.h contains the declaration of the following functions
int sum(int arr[])
int avg(int arr[])
int max(int arr[])
int min(int arr[])
while array.cpp contains the definition of the above functions.
and main.cpp contains the main function to run the above code.

You are required to take the array from command line arguments.

Question 3: Write a program that uses two processes. One is called a parent and the other is
called a child. The child read the same file (mentioned above) and finds the total number of
vowels (a, e, i, o , u) in the file.
While child is processing, Parent wait for child to finish with code and prints ‘Program
completed’ message.

Note: Use Fork and File handling.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy