0% found this document useful (0 votes)
5 views5 pages

21bce6205 Os 3

The document contains a series of programming assignments submitted by Dhairya, demonstrating the simulation of Linux commands (cp and mv) using C, performing arithmetic operations with command line arguments, checking for palindromes, creating child processes, and discussing the use of command line arguments in C. Each assignment includes code snippets and brief descriptions of their functionality. The document serves as a practical application of C programming concepts related to file handling, process management, and command line interaction.

Uploaded by

dhairyapatel0277
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)
5 views5 pages

21bce6205 Os 3

The document contains a series of programming assignments submitted by Dhairya, demonstrating the simulation of Linux commands (cp and mv) using C, performing arithmetic operations with command line arguments, checking for palindromes, creating child processes, and discussing the use of command line arguments in C. Each assignment includes code snippets and brief descriptions of their functionality. The document serves as a practical application of C programming concepts related to file handling, process management, and command line interaction.

Uploaded by

dhairyapatel0277
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/ 5

Assignment - 3

Submitted by - Dhairya
Application number - 21BCE6205
Question –1 Simulate CP linux command using C.
Code-:
#include <stdio.h>
int main(int argc, char *argv[])
{
FILE *fp = fopen(argv[1], "r");
FILE *fp1 = fopen(argv[2], "w");
char c;
while ((c = fgetc(fp)) != EOF)
{
fputc(c, fp1);
}
fclose(fp);
fclose(fp1);
}
Output-:

Question-2 Simulate MV linux command using C


Code-:
#include <stdio.h>
int main(int argc, char *argv[])
{
FILE *fp = fopen(argv[1], "r");
FILE *fp1 = fopen(argv[2], "w");
char c;
while ((c = fgetc(fp)) != EOF)
{
fputc(c, fp1);
}
fclose(fp);
fclose(fp1);
remove(argv[1]);
}
Output-: a.txt got deleted and its content is copied to b.txt

Question-3 Perform arithmetic operations using command line arguments.


Code-:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int a, b, result;
char opr;
a = atoi(argv[1]);
b = atoi(argv[2]);
opr = argv[3][0];
switch (opr)
{
case '+':
result = a + b;
break;
case '-':
result = a - b;
break;
case '*':
result = a * b;
break;
case '/':
result = a / b;
}
printf("Result: %d %c %d = %d\n", a, opr, b, result);
return 0;
}
Output-:
Question-4 Check whether the given string is palindrome or not and ensure to
take the input while executing the program.
Code-:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
int n = strlen(argv[1]);
int j = n - 1;
int i = 0;

while (i <= j)
{
if (argv[1][i] == argv[1][j])
{
j--;
i++;
}
else
{
printf("Not a valid Palindrom");
break;
}
}
}
Output-:

Question - 5 Create 3 child processes from the same parent process and show
the child processes are created from the same parent process.
Code-:
#include <stdio.h>
#include <unistd.h>
int main()
{
pid_t child1, child2, child3;
child1 = fork();
if (child1 == 0)
{
printf("Child 1: PID = %d, PPID = %d\n", getpid(), getppid());
return 0;
}
child2 = fork();
if (child2 == 0)
{
printf("Child 2: PID = %d, PPID = %d\n", getpid(), getppid());
return 0;
}
child3 = fork();
if (child3 == 0)
{
printf("Child 3: PID = %d, PPID = %d\n", getpid(), getppid());
return 0;
}
printf("Parent: PID = %d, PPID = %d\n", getpid(), getppid());
return 0;
}
Output-:

Question – 6 Discuss the use of Command line arguments in C


Answer -: Command line arguments are strings of text passed to a program when it
is run from the command line. In C, we can access the command line arguments
passed to a program using the argc and argv arguments of the main () function.
The argc argument is an integer that indicates the number of command line
arguments passed to the program and it is always at least 1, because the program's
name is considered the first command line argument.
The argv argument is an array of pointers to strings that contain the actual
command line arguments. The first element of the array (argv[0]) is the name of the
program, and the subsequent elements are the command line arguments passed to
the program.

You might also like