0% found this document useful (0 votes)
4 views18 pages

Lab Assignment, Exams, Universities, Lab Work, Programming Questions

The document outlines a weekend assignment for students in the Computer Science and Engineering department, focusing on processes in UNIX. It includes various programming tasks that require students to create process trees, analyze process creation, and understand output related to process management. Students are instructed to write original code, avoid plagiarism, and explore the implications of process inheritance and manipulation.

Uploaded by

mardiblbiswajit
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)
4 views18 pages

Lab Assignment, Exams, Universities, Lab Work, Programming Questions

The document outlines a weekend assignment for students in the Computer Science and Engineering department, focusing on processes in UNIX. It includes various programming tasks that require students to create process trees, analyze process creation, and understand output related to process management. Students are instructed to write original code, avoid plagiarism, and explore the implications of process inheritance and manipulation.

Uploaded by

mardiblbiswajit
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/ 18

Department of Computer Science and Engineering

Institute of Technical Education & Research, SOA, Deemed to be University


W EEK -E ND A SSIGNMENT-S01
Processes in UNIX
Operating Systems Workshop (CSE 3541)

Problem Statement:
Experiment with programs, processes, memory allocation and manipulation for processes in UNIX.

Assignment Objectives:
Students will be able to differentiate programs and processes, also able to learn how to create processes and
able to explore the implication of process inheritance.

Instruction to Students (If any):


Students are required to write his/her own program/output by avoiding any kind of copy from any
sources. Additionally, They must be able to realise the outcome of that question in relevant to systems
programming. You may use additional pages on requirement.

Programming/ Output Based Questions:


1. Construct the process tree diagram and also find the number of processes along with output of the
following code snippet.
int main(void) { Process tree, # of processes and output
fork();
fork(); there are a ITER
fork(); total of 1 + 2 + ITER
ITER
printf("ITER\n"); 4 + 8 = 15 ITER
processes. ITER
printf("ITER\n"); ITER
return 0; ITER
ITER
}
/* Any formula can be
devised for number
of processes
created here?
If so, state.*/

2. Construct the process tree diagram and also find the number of processes along with output of the
following code snippet.
int main(void){ Process tree, # of processes and output
printf("hello\n");
fork(); there are a hello
printf("hello\n"); total of 1 + 2 + hello
fork(); 4 + 8 = 15 hello
processes. hello
printf("hello\n");
hello
fork(); hello
printf("hello\n"); hello
return 0; hello
}
/* Any formula for
nuber of outputs?
If so, state.*/

ARYA BHARADWAJ MISHRA [Wa S01-1] 2141013147


Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University

3. Run the following code on your machine and write the output. Suggest a way to avoid the mismatch
of machine output w.r.t. dry run output.
#include<stdio.h> Output, Reason and Suggestion
#include<unistd.h> When fork() is called,
int main(void) A the process splits into
To avoid mismatches between
P two: the parent machine output and dry run output,
{ A process and the child you can introduce newline
printf("A"); P process. Both characters (\n) in your printf
processes continue statements. This helps in flushing
fork(); executing the code
from the point of the the output buffer, making it more
printf("P\n"); fork() call. predictable.
return 0;
}

4. Draw the process tree and write the output of the following code snippet.
Process tree and output
Able to
Able to
int main() Able to
Able to
{ Able to
fork() && fork(); Able to
Able to
printf("Able to\n"); Able to
return 0;
}

5. Draw the process tree and write the output of the following code snippet.
Process tree and output
Got!!!
Got!!!
int main() Got!!!
{ Got!!!
Got!!!
fork(); Got!!!
fork() && fork(); Got!!!
fork(); Got!!!
Got!!!
printf("Got!!!\n");
return 0;
}

6. Draw the process tree and write the output of the following code snippet.
Process tree and output
doing!
doing!
doing!
doing!
doing!
doing!
doing!
int main() doing!
{ doing!
fork();
fork() + fork();
fork();
printf("doing!\n");
return 0;
}

ARYA BHARADWAJ MISHRA [Wa S01-2] 2141013147


Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University

7. Draw the process tree and write the output of the following code snippet.
int main(){
fork(); Process tree Output
fork() || fork();
fork(); Really!!!
printf("Really!!!\n"); Really!!!
Really!!!
return 0; Really!!!
} Really!!!
Really!!!
Really!!!
Remark Really!!!
There are a total of 8 lines in the
output, corresponding to the 8
processes in the process tree. The
logical OR (||) ensures that the
second fork() is executed only in the
child process created by the first
fork(), leading to a total of 8
processes.

8. Draw the process tree and write the output of the following code snippet.
int main(){ Process tree Output
fork();
fork()&& fork()||fork(); guess
fork(); guess
printf("guess\n"); guess
guess
return 0; guess
} guess
guess
guess
Remark
There are a total of 8 lines in the output,
corresponding to the 8 processes in the
process tree. The logical AND (&&) and
logical OR (||) operators determine the
flow of execution, but the printf statement
is executed by all processes.

9. Draw the process tree and write the output of the following code snippet.
int main(){ Process tree Output
fork()&&fork();
fork()||fork(); Hi
Hi
printf("Hi\n"); Hi
return 0; Hi
} Hi
Hi
Hi
Hi
Remark
There are a total of 8 lines in the output,
corresponding to the 8 processes in the
process tree. The logical AND (&&) and
logical OR (||) operators determine the
flow of execution, but the printf statement
is executed by all processes.

ARYA BHARADWAJ MISHRA [Wa S01-3] 2141013147


Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University

10. Construct the process tree diagram and also find the number of processes along with output of the
following code snippet.
int main(){ Process tree, # of processes and output
int pid,pid2;
pid=fork(); So, there are I
C
if(pid){ a total of 1 + I
pid2=fork(); 2+2=5 I
printf("I\n"); processes. I
}
else{
printf("C\n");
pid2=fork();
}
return 0;
}

11. Construct the process tree diagram and also find the number of processes along with output of the
following code snippet.
int
main(void){
Process tree, # of processes and output
pid_t childpid; So, there are i:1
int i, n=3; a total of 1 + i:2
for(i=1;i<n;i++){ 2+4+8= i:3
childpid=fork(); 15
if(childpid==-1) processes.
break;
}
printf("i:%d\n",i);
return 0;
}

12. Draw the process tree because of the following code snippet and state number of times x=0 as well
as x6=0will be displayed.
pid_t Process tree # of x=0 # of x6=0
add(pid_t a, pid_t b){
return a+b; Number of Number of
} times x=0 : 2 times x≠0 : 1
int main(void){
(in the child (in the parent
processes). process).
pid_t x=10;
printf("%d\n",x);
x=add(fork(),fork());
printf("%d\n",x);
return 0;}

13. Determine the total number of displayed for the given code snippet.
int main(void){
int x[]={10,20,fork(),fork()+fork()}; # of display # of Processes
int len=sizeof(x)/sizeof(int); Number of times The fourth element in the
for(int i=0;i<len;i++) displayed: 7 times (in array involves two fork()
each of the 7 processes). calls, creating three
fprintf(stderr," %d ",x[i]); additional processes. So,
printf("\n"); there are a total of 4 + 3 =
7 processes.
return 0;
}

ARYA BHARADWAJ MISHRA [Wa S01-4] 2141013147


Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University

14. Determine the number of process(s) will be created when the below program becomes process and
also write the output.
void show(){
if(fork()==0)
# of processes Output
printf("1\n"); There are a total of 7 1
if(fork()==0) processes created. 2
printf("2\n"); 3
if(fork()==0)
printf("3\n");
}
int main(void){
show();
return 0;
}

15. Draw the process tree of the following code snippet. Also give a count of processes and the output of
the following code. Can the code segment generate fan of processes.
int main(void){ Process tree, # of processes and output
if(fork()==0)
printf("1\n"); Using fork() in if
statements 1
else if(fork()==0) yields 16 2
printf("2\n"); processes. The 3
else if(fork()==0) printf outputs 1- 4
5 based on
printf("3\n"); conditions, with 5
else if(fork()==0) the final else 5
printf("4\n");
statement 5
exclusive to the 5
else parent process.
printf("5\n");
return 0;
}

16. Find the output of the code segment showing the corresponding process tree.
int main(){
Process tree Output
pid_t p1,p2; done
p2=0;
p1=fork();
if (p1 == 0)
p2 = fork();
if (p2 > 0)
fork();
printf("done\n");
return 0;
}

17. Find the number of direct children to the main process, the total number of processes and the output.
int main(){pid_t c1=1,c2=1;
c1=fork(); # of direct children to main process Total processes Output
if(c1!=0) Number of direct children Total number of 1
c2=fork(); to the main process: 1 processes: 3
if(c2==0){
fork();printf("1\n");
}
return 0;}

ARYA BHARADWAJ MISHRA [Wa S01-5] 2141013147


Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University

18. Find the output of the code segment.


struct stud{
int r;
int main(){
int m;
struct stud s1={1,20};
};
pid_t pid=fork();
if(pid==0){
struct stud s1={2,30}; Output
printf("%d %d\n",s1.r,s1.m);
return 0; 2 30
} 1 20
else{
sleep(10);
printf("%d %d\n",s1.r,s1.m);
return 0;
}
}

19. Find the output of the code segment showing the corresponding process tree.
int main(){
if(fork()){
if(!fork()){
Process tree Output
fork();
printf("S "); DTAS
}
else{
printf("T ");
}
}
else{
printf("D ");
}
printf("A ");
return 0;
}

20. Calculate the number of processes the following code snippet will generate.
int main(){int i;
Process tree Output
for(i=0;i<12;i++){ There is no specific output
if(i%3==0){ statement in the code, but we can
note that each process continues
fork(); the execution from the point where
} the fork occurred. If you had an
output statement, it would be
} printed by each process.
return 0;
}

21. State the possible values of x for the given code snippet;
Process tree Output
25
int x;
int a[2]={10,20};
x=5+a[fork() || fork()];
printf("%d ",x);

ARYA BHARADWAJ MISHRA [Wa S01-6] 2141013147


Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University

22. Suppose four user-defined exit handlers X, Y, P, and Q are installed in the order X then Y then P then
Q using atexit() function in a C program. Exit hadler X is designed to display 1, Y is designed to
display 2, P is designed to display 3, and Q to display 4. State the order of their display, when the
program is going to terminate after calling return 0/exit(0).
Choice
(A) 4, 3, 2 1 (C) 1, 2, 4, 3 (C) 1, 2, 4, 3

(B) 1,2,3,4 (D) none

23. You know that the ps utility in UNIX reports a snapshot of the current processes. Determine the state
code of the given program, that became a process.
int main(void){
fprintf(stderr,"PID=%ld\n",(long)getpid());
while(1);
return 0;
}

Choice
(A) R (C) T (A) R

(B) S (D) Z

24. Find the process state code of the given program, that became a process using the Unix utility ps. As
you know ps displays information about a selection of the active processes.
int main(void){
fprintf(stderr,"PID=%ld\n",(long)getpid());
while(1)
sleep(1);
return 0;
}

Choice
(A) R (C) T (B) S

(B) S (D) Z

25. Develop a C code to create the following process tree. Display the process ID, parent ID and return
value of fork() for each process.

OBSERVARIONS:
• Use ps utility to verify the is-a-parent relationship?
• Are ypu gettig any orphan process case?
• Are you getting any ZOMBIE case?

Figure 1: Process tree

ARYA BHARADWAJ MISHRA [Wa S01-7] 2141013147


Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University

Code here
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
int main() {
pid_t pid1, pid2, pid3, pid4, pid5, pid6;
printf("Initial process:\n");
printf("PID: %d, PPID: %d, fork() return value: %d\n", getpid(), getppid(), 0);
pid1 = fork();
if (pid1 < 0) {
perror("fork");
exit(1);
} else if (pid1 == 0) {
printf("Process 1:\n");
printf("PID: %d, PPID: %d, fork() return value: %d\n", getpid(), getppid(), pid1);
pid4 = fork();
if (pid4 < 0) {
perror("fork");
exit(1);
} else if (pid4 == 0) {
printf("Process 4:\n");
printf("PID: %d, PPID: %d, fork() return value: %d\n", getpid(), getppid(), pid4);
exit(0);
}
pid5 = fork();
if (pid5 < 0) {
perror("fork");
exit(1);
} else if (pid5 == 0) {
printf("Process 5:\n");
printf("PID: %d, PPID: %d, fork() return value: %d\n", getpid(), getppid(), pid5);
exit(0);
}
waitpid(pid4, NULL, 0);
waitpid(pid5, NULL, 0);
exit(0);
}
pid2 = fork();
if (pid2 < 0) {
perror("fork");
exit(1);
} else if (pid2 == 0) {
printf("Process 2:\n");
printf("PID: %d, PPID: %d, fork() return value: %d\n", getpid(), getppid(), pid2);
exit(0);
}
pid3 = fork();
if (pid3 < 0) {
perror("fork");
exit(1);
} else if (pid3 == 0) {
printf("Process 3:\n");
printf("PID: %d, PPID: %d, fork() return value: %d\n", getpid(), getppid(), pid3);
pid6 = fork();
if (pid6 < 0) {
perror("fork");
exit(1);
} else if (pid6 == 0) {
printf("Process 6:\n");
printf("PID: %d, PPID: %d, fork() return value: %d\n", getpid(), getppid(), pid6);
exit(0);
}
waitpid(pid6, NULL, 0);
exit(0);
}
waitpid(pid1, NULL, 0);
waitpid(pid2, NULL, 0);
waitpid(pid3, NULL, 0);
printf("All child processes have terminated.\n");
return 0;
}

ARYA BHARADWAJ MISHRA [Wa S01-8] 2141013147


Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University

26. Create two different user-defined functions to generate the following process hierarchy shown in
Figure-(a) and Figure-(b). Finally all the processes display their process ID and parent ID.

Figure-(a) Figure-(b)

Figure 2: Process tree

Code here

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

void figureA() {
pid_t child1, child2, child3;

if((child1 = fork()) == 0) {
printf("Process ID: %d, Parent ID: %d\n", getpid(), getppid());
exit(0);
}

if((child2 = fork()) == 0) {
printf("Process ID: %d, Parent ID: %d\n", getpid(), getppid());
exit(0);
}

if((child3 = fork()) == 0) {
printf("Process ID: %d, Parent ID: %d\n", getpid(), getppid());
exit(0);
}

printf("Parent Process ID: %d\n", getpid());


}

ARYA BHARADWAJ MISHRA [Wa S01-9] 2141013147


Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University

Code here
void figureB() {
pid_t child1;

if((child1 = fork()) == 0) {

pid_t grandChild;

if ((grandChild = fork()) == 0){


printf("Process ID: %d, Parent ID: %d\n", getpid(), getppid());
exit(0);
}

if ((grandChild = fork()) == 0){


printf("Process ID: %d, Parent ID: %d\n", getpid(), getppid());
exit(0);
}

printf("Process ID: %d, Parent ID: %d\n", getpid(), getppid());


exit(0);
}

if((child1 = fork()) == 0) {
printf("Process ID:% d, ParentID:% d \n ",getpid (),getppid ());
exit ( 0 );
}

printf ("Parent ProcessID:% d \n ",getpid ());


}

int main() {

printf("\nFigure-(a)\n");

figureA();

sleep(5); // To separate the outputs of two figures

printf("\nFigure-(b)\n");

figureB();

return 0;
}

ARYA BHARADWAJ MISHRA [Wa S01-10] 2141013147


Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University

27. What output will be at Line X and Line Y?


#define SIZE 5
int nums[SIZE] = { 0,1,2,3,4 } ;
int main(){
int i;
pid t pid;
pid = fork();
Output
if(pid == 0){ CHILD:0 CHILD:0 CHILD:-8 CHILD:
for (i = 0; i < SIZE; i++) { -36 CHILD:-80 PARENT:0 PARENT:
1 PARENT:2 PARENT:3 PARENT:4
nums[i] *= nums[i] *-i;
printf("CHILD:%d ",nums[i]); /* LINE X */
}
}
else if (pid > 0) {
wait(NULL);
for (i = 0; i < SIZE; i++)
printf("PARENT: %d ",nums[i]); /* LINE Y */
}
return 0;
}

28. The Fibonacci sequence is the series of numbers 0, 1, 1, 2, 3, 5, 8,


Write a C program using the fork() system call that generates the Fibonacci sequence in the child
process. The number of the sequence will be provided in the command line. For example, if 5 is
provided, the first five numbers in the Fibonacci sequence will be output by the child.
Code here
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
int fibonacci(int n) {
return (n <= 1) ? n : fibonacci(n - 1) + fibonacci(n - 2);
}
int main(int argc, char *argv[]) {
if (argc != 2 || atoi(argv[1]) <= 0) {
fprintf(stderr, "Usage: %s <number_of_terms (positive integer)>\n", argv[0]);
return 1;
}
int n = atoi(argv[1]);
pid_t pid = fork();
if (pid == -1) {
perror("fork");
return 1;
} else if (pid == 0) { // Child process
for (int i = 0; i < n; i++)
printf("%d ", fibonacci(i));
printf("\n");
} else { // Parent process
wait(NULL);
printf("Parent process completed.\n");
}
return 0;
}

ARYA BHARADWAJ MISHRA [Wa S01-11] 2141013147


Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University

29. Write a MulThree.c program to multiply three numbers and display the output. Now write another
C program PracticeExecl.c, which will fork a child process and the child process will execute
the file MulThree.c and generate the output. The parent process will wait till the termination of
the child and the parent process will print the process ID and exit status of the child.

Code here

// MulThree.c // PracticeExecl.c
#include <stdio.h> #include <stdio.h>
int main() { #include <stdlib.h>
int num1 = 2, num2 = 3, num3 = 4; #include <sys/types.h>
int result = num1 * num2 * num3; #include <sys/wait.h>
printf("Result: %d\n", result); #include <unistd.h>
return 0;
} int main() {
pid_t pid = fork();
if (pid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (pid == 0) { // Child process
After implementing the code, we'll get execl("./MulThree", "MulThree",
the following result : NULL);
perror("execl");
Result: 24 exit(EXIT_FAILURE);
Child process (PID 12345) terminated } else { // Parent process
with exit status: 0 int status;
waitpid(pid, &status, 0);
The MulThree.c program calculates the if (WIFEXITED(status)) {
product of 2, 3, and 4, which is 24. printf("Child process (PID %d)
terminated with exit status: %d\n", pid,
The PracticeExecl.c program forks a WEXITSTATUS(status));
child process, and the child process } else {
executes MulThree.c.
printf("Child process (PID %d)
The parent process waits for the child terminated abnormally\n", pid);
to terminate and prints the process ID }
and exit status of the child. }
The exit status of 0 indicates successful return 0;
execution. }
Note: The actual process ID (PID) will
be different in your case.

ARYA BHARADWAJ MISHRA [Wa S01-12] 2141013147


Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University

30. You know the usages of the command grep. Implement the working of grep -n pattern
filename in a child process forked from the parent process using execl system call. The par-
ent process will wait till the termination of the child and the parent process will print the process ID
and exit status of the child.
Code here
// grep_example.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
if (argc != 3) {
fprintf(stderr, "Usage: %s <pattern> <filename>\n", argv[0]);
exit(EXIT_FAILURE);
}
pid_t pid = fork();
if (pid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (pid == 0) { // Child process
execl("/bin/grep", "grep", "-n", argv[1], argv[2], NULL);
perror("execl");
exit(EXIT_FAILURE);
} else { // Parent process
int status;
waitpid(pid, &status, 0);
if (WIFEXITED(status)) {
printf("Child process (PID %d) terminated with exit status: %d\n", pid, WEXITSTATUS
(status));
} else {
printf("Child process (PID %d) terminated abnormally\n", pid);
}
}
return 0;
}

ARYA BHARADWAJ MISHRA 2141013147


[Wa S01-13]
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University

31. Implement the above question using execv, execlp, execvp, execle, execve system calls.

Code here for execv


// grep_example_execv.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
if (argc != 3) {
fprintf(stderr, "Usage: %s <pattern> <filename>\n", argv[0]);
exit(EXIT_FAILURE);
}
pid_t pid = fork();
if (pid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (pid == 0) { // Child process
char *grep_args[] = {"grep", "-n", argv[1], argv[2], NULL};
execv("/bin/grep", grep_args);
perror("execv");
exit(EXIT_FAILURE);
} else { // Parent process
int status;
waitpid(pid, &status, 0);
if (WIFEXITED(status)) {
printf("Child process (PID %d) terminated with exit status: %d\n", pid, WEXITSTATUS
(status));
} else {
printf("Child process (PID %d) terminated abnormally\n", pid);
}
}
return 0;
}

ARYA BHARADWAJ MISHRA 2141013147


[Wa S01-14]
Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University

Code here for execlp


// grep_example.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
if (argc != 3) {
fprintf(stderr, "Usage: %s <pattern> <filename>\n", argv[0]);
exit(EXIT_FAILURE);
}
pid_t pid = fork();
if (pid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (pid == 0) { // Child process
execlp("grep", "grep", "-n", argv[1], argv[2], NULL);
perror("execlp");
exit(EXIT_FAILURE);
} else { // Parent process
int status;
waitpid(pid, &status, 0);
if (WIFEXITED(status)) {
printf("Child process (PID %d) terminated with exit status: %d\n", pid, WEXITSTATUS
(status));
} else {
printf("Child process (PID %d) terminated abnormally\n", pid);
}
}
return 0;
}

ARYA BHARADWAJ MISHRA [Wa S01-15] 2141013147


Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University

Code here for execvp


// grep_example_execvp.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
if (argc != 3) {
fprintf(stderr, "Usage: %s <pattern> <filename>\n", argv[0]);
exit(EXIT_FAILURE);
}
pid_t pid = fork();
if (pid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (pid == 0) { // Child process
char *grep_args[] = {"grep", "-n", argv[1], argv[2], NULL};
execvp("grep", grep_args);
perror("execvp");
exit(EXIT_FAILURE);
} else { // Parent process
int status;
waitpid(pid, &status, 0);
if (WIFEXITED(status)) {
printf("Child process (PID %d) terminated with exit status: %d\n", pid, WEXITSTATUS
(status));
} else {
printf("Child process (PID %d) terminated abnormally\n", pid);
}
}
return 0;
}

ARYA BHARADWAJ MISHRA [Wa S01-16] 2141013147


Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University

Code here for execve


// grep_example_execve.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
if (argc != 3) {
fprintf(stderr, "Usage: %s <pattern> <filename>\n", argv[0]);
exit(EXIT_FAILURE);
}
pid_t pid = fork();
if (pid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (pid == 0) { // Child process
char *grep_args[] = {"grep", "-n", argv[1], argv[2], NULL};
execve("/bin/grep", grep_args, NULL);
perror("execve");
exit(EXIT_FAILURE);
} else { // Parent process
int status;
waitpid(pid, &status, 0);
if (WIFEXITED(status)) {
printf("Child process (PID %d) terminated with exit status: %d\n", pid, WEXITSTATUS
(status));
} else {
printf("Child process (PID %d) terminated abnormally\n", pid);
}
}
return 0;
}

ARYA BHARADWAJ MISHRA [Wa S01-17] 2141013147


Department of Computer Science and Engineering
Institute of Technical Education & Research, SOA, Deemed to be University

Code here for execle


// grep_example_execle.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
if (argc != 3) {
fprintf(stderr, "Usage: %s <pattern> <filename>\n", argv[0]);
exit(EXIT_FAILURE);
}
pid_t pid = fork();
if (pid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (pid == 0) { // Child process
char *envp[] = {NULL}; // Empty environment for execle
execle("/bin/grep", "grep", "-n", argv[1], argv[2], NULL, envp);
perror("execle");
exit(EXIT_FAILURE);
} else { // Parent process
int status;
waitpid(pid, &status, 0);
if (WIFEXITED(status)) {
printf("Child process (PID %d) terminated with exit status: %d\n", pid, WEXITSTATUS
(status));
} else {
printf("Child process (PID %d) terminated abnormally\n", pid);
}
}
return 0;
}

ARYA BHARADWAJ MISHRA [Wa S01-18] 2141013147

You might also like