diff --git a/README.md b/README.md index 51e3b757..c0796bad 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # Linux-Process-API-fork-wait-exec- + Ex02-Linux Process API-fork(), wait(), exec() + # Ex02-OS-Linux-Process API - fork(), wait(), exec() Operating systems Lab exercise @@ -23,73 +25,92 @@ Test the C Program for the desired output. # PROGRAM: -## 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 - - - - - - - - - - - - - -##OUTPUT - - - - - - - - -## C Program to execute Linux system commands using Linux API system calls exec() , exit() , wait() family - - - - - - - - - - - - - - - - - - - - - - - - - - -##OUTPUT - - - - - - - - - - - - - - - - +## C Program to print process ID and parent Process ID using Linux API system calls +```c +#include +#include +#include +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; } +``` + +## OUTPUT + +![image](https://github.com/Ragu-123/Linux-Process-API-fork-wait-exec/assets/113915622/ab5d4a84-ae5e-4467-8257-538498361c39) + + +## C Program to create new process using Linux API system calls fork() and exit() +```c +#include +#include +#include +#include +int main() +{ int pid; +pid=fork(); +if(pid == 0) +{ printf("Iam child my pid is %d\n",getpid()); +printf("My parent pid is:%d\n",getppid()); +exit(0); } +else{ +printf("I am parent, my pid is %d\n",getpid()); +sleep(100); +exit(0);} +} +``` + +## OUTPUT + +![image](https://github.com/Ragu-123/Linux-Process-API-fork-wait-exec/assets/113915622/53a4d0be-3fac-4c13-acd3-d0bec420d4f6) + +## C Program to execute Linux system commands using Linux API system calls exec() family +```c +#include +#include +#include +#include +#include +int main() +{ int status; + printf("Running ps with execlp\n"); + execl("ps", "ps", "ax", NULL); + wait(&status); + if (WIFEXITED(status)) + printf("child exited with status of %d\n", WEXITSTATUS(status)); + else + puts("child did not exit successfully\n"); + printf("Done.\n"); +printf("Running ps with execlp. Now with path specified\n"); + execl("/bin/ps", "ps", "ax", NULL); + wait(&status); + if (WIFEXITED(status)) + printf("child exited with status of %d\n", WEXITSTATUS(status)); + else + puts("child did not exit successfully\n"); + printf("Done.\n"); + exit(0);} +``` + +## OUTPUT + +![image](https://github.com/Ragu-123/Linux-Process-API-fork-wait-exec/assets/113915622/09d3130a-e22d-4e65-80e0-779b917d3132) + +![image](https://github.com/Ragu-123/Linux-Process-API-fork-wait-exec/assets/113915622/617ad40a-5f5b-4ec3-b1b9-d8b2c82ed045) + +![image](https://github.com/Ragu-123/Linux-Process-API-fork-wait-exec/assets/113915622/d1aa6db5-d34c-4e62-ad8b-7d5f43da0d7b) # RESULT: