Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 81 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Ex02-Linux Process API-fork(), wait(), exec()
# Ex02-OS-Linux-Process API - fork(), wait(), exec()
Operating systems Lab exercise
## NAME:A.DIVIYADHARSHINI
## ROLL NO: 212224040080


# AIM:
Expand All @@ -25,7 +27,26 @@ Test the C Program for the desired output.

## C Program to create new process using Linux API system calls fork() and getpid() , getppid() and to print process ID and parent Process ID using Linux API system calls

```
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main(void)
{ //variable to store calling function's process id
pid_t process_id;
//variable to store parent function's process id
pid_t p_process_id;
//getpid() - will return process id of calling function
process_id = getpid();
//getppid() - will return process id of parent function
p_process_id = getppid();
//printing the process ids

//printing the process ids
printf("The process id: %d\n",process_id);
printf("The process id of parent function: %d\n",p_process_id);
return 0; }
```



Expand All @@ -35,18 +56,75 @@ Test the C Program for the desired output.



## OUTPUT


##OUTPUT


<img width="1037" height="86" alt="image" src="https://github.com/user-attachments/assets/fd833999-794f-439a-b27c-64d3682c4c63" />


## C Program to create new process using Linux APU system calls fork() and exit()

```
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
int pid;
pid = fork();
if (pid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
else if (pid == 0) {
printf("I am child, my pid is %d\n", getpid());
printf("My parent pid is: %d\n", getppid());
exit(EXIT_SUCCESS);
}
else {
printf("I am parent, my pid is %d\n", getpid());
sleep(100);
exit(EXIT_SUCCESS);
}
return 0;
}
```
## OUTPUT
<img width="1037" height="73" alt="image" src="https://github.com/user-attachments/assets/5537f3cb-0f43-4761-9b1c-cd7066412819" />



## C Program to execute Linux system commands using Linux API system calls exec() , exit() , wait() family
```
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main() {
pid_t pid = fork();
if (pid < 0) {
perror("Fork failed");
exit(EXIT_FAILURE);
} else if (pid == 0) {
printf("This is the child process. Executing 'ls' command.\n");
execl("/bin/ls", "ls", "-l", NULL); // Lists files in long format
perror("execl failed");
exit(EXIT_FAILURE);
} else {
int status;
waitpid(pid, &status, 0); // Wait for the child to finish
if (WIFEXITED(status)) {
printf("Child process exited with status %d.\n", WEXITSTATUS(status));
} else {
printf("Child process did not exit normally.\n");
}
printf("Parent process is done.\n");
}
return 0;
}
```
## OUTPUT
<img width="1037" height="241" alt="image" src="https://github.com/user-attachments/assets/79e009ac-6491-4b55-85ca-7891e118b227" />



Expand All @@ -70,10 +148,6 @@ Test the C Program for the desired output.






##OUTPUT



Expand Down