21bce6205 Os 3
21bce6205 Os 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-:
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-: