diff --git a/README.md b/README.md index 51e3b757..742e580c 100644 --- a/README.md +++ b/README.md @@ -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: @@ -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 +#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; } +``` @@ -35,18 +56,75 @@ Test the C Program for the desired output. +## OUTPUT -##OUTPUT - - +image +## C Program to create new process using Linux APU system calls fork() and exit() +``` +#include +#include +#include +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 +image ## C Program to execute Linux system commands using Linux API system calls exec() , exit() , wait() family +``` +#include +#include +#include +#include +#include +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 +image @@ -70,10 +148,6 @@ Test the C Program for the desired output. - - - -##OUTPUT