A parent process should create a child process which will execute system call command "ls"
and will create its child process. The newly created child process should execute system call command "cat" and create a child process. The newly created process should execute system call"whoami" and then terminate. Every parent process should wait for terminating its child process.
Solution :-Following program demostrate you how to create processes using fork() method and how to use system call to execute commands on shell terminal. Using simple wait() method parent wait for child execution till partent pause state. just try it out yourself/
#include<stdio.h> #include<sys/types.h> #include<unistd.h> #include<stdlib.h> int main() { pid_t pid; char cmd[256]; pid=fork(); //Child create by fork() method if(pid == 0) { printf("\n****I AM THE CHILD PROCESS for ls Command****\n"); printf("The process id is %d and ppid is %d \n\n ls -l output:\n\n",getpid(),getppid()); sprintf(cmd,"ls -l"); system(cmd); printf("\n"); pid=fork(); wait(NULL); if(pid==0) { printf("\n****I AM THE CHILD PROCESS for cat Command****\n"); printf("The process id is %d and ppid is %d \n\n cat cat.txt output:\n\n",getpid(),getppid()); sprintf(cmd,"cat cat.txt "); system(cmd); pid=fork(); wait(NULL); if(pid==0) { printf("\n****I AM THE CHILD PROCESS for whoami Command****\n"); printf("The process id is %d and ppid is %d \n\n whoami output:\n\n",getpid(),getppid()); sprintf(cmd,"whoami"); system(cmd); printf("\n"); } } } else if(pid>0) { wait(NULL); printf("\n======>I AM THE PARENT PROCESS<======\n"); printf("The process id is %d and gid is %d \n\n",getpid(),getuid()); } return 0; }
0 comments:
Post a Comment