ASsignments on System call related to procesS 26.
0424
Write a Cprogram to display the process id of child and parent processes, where the child is created
using the fork) system call.
Write a Cprogram to create an orphan process. Also explain which process takes responsibility ofthe
orphan process.
3. Write aCProgram using a system call so that parent is going to wait untilchild finishes.
4. Write aC program using fork) system call to show how a file can be shared between processes.
5. Write a C program that willshow the use of exec) and fork) system cailtogether.
#EXample 1
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
main(void) {
int pid = 0;
pid = fork():
if (pid == 0) {
printf("I am the child.\\n"):
if (pid >printf("I
0) { am the parent, the child is d.\\n", pid):
if (pid < 0) {
perror("In fork):"):
}
exit(0):
#example 2
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types. h>
#include <sys/wait.h>
main(void) {
int pid = 0;
int Status;
pid = fork():
if (pid = 0){
printf("I am the child.\\n"):
sleep(10);
printf("I am the chi ld, 10 seconds later.\\n");
printf("I am the parent, the child is %d.\n", pid):
pid = wait (&status);
printf("End of process %d: pid):
if (WIFEXITED (Status)) {
printf{"The process ended with exit(d).\n, EXITSTATUS (Status)):
if (wIFSIGNALED (Status)) {
printf("The process ended with kill -%d.\\n", WTERMSIG(Status)):
{f (pid <0) {
perror("In fork():");
exit(0) ;
#Example 3
#include <stdio. h>
#include <unistd.h>
#include <stdlib.h>
int main(){
/ display Current process id
printf("\nCurrently running process has PID: %d\n", getpidO);
// exec to run ps instead of exXecl
int error = exec1("/usr/bin/ps", "ps", "-1", NULL);
1/ for error handling
printf("\nThere was an error during the execl functi on!\n"):
exit(-1);