Tuesday, July 26, 2022

Operating System Assignment 1

 Operating System Assignment 1 

 Operations on Processes

Practical Assignments:

Set A

(1) Implement the C Program to create a child process using fork(), display parent and child

process id. Child process will display the message “I am Child Process” and the parent

process should display “I am Parent Process”.

Program:-


#include <stdio.h>

#include <sys/types.h>

#include <unistd.h>

int main()

{

    // fork() Create a child process

    int pid = fork();

    if (pid > 0) {

        printf("I am Parent process\n");

        printf("ID : %d\n\n", getpid());

    }

    else if (pid == 0) {

        printf("I am Child process\n");

        printf("ID: %d\n", getpid());

    }

    else {

        printf("Failed to create child process");

    }

    return 0;

}


(2) Write a program that demonstrates the use of nice() system call. After a child process is

started using fork(), assign higher priority to the child using nice() system call.

Program:-

#include <stdio.h>

#include <sys/types.h>

#include <unistd.h>

int main()

{

pid_t pid;

pid = fork();

if (pid == 0)

{

printf("\nI am child process, id=%d\n",getpid());

printf("\nPriority :%d,id=%d\n",nice (-7),getpid());

}

else

{

printf("\nI am parent process, id=%d\n",getpid());

nice(1);

printf("\nPriority :%d,id=%d\n",nice (15),getpid());

}

return 0;

}


Set B

(1) Implement the C program to accept n integers to be sorted. Main function creates child

process using fork system call. Parent process sorts the integers using bubble sort and

waits for child process using wait system call. Child process sorts the integers using

insertion sort.

Program :-

#include<stdio.h>

#include<sys/types.h>

#include<unistd.h>

#include<stdlib.h>

void bubblesort(int arr[30],int n)

{

int i,j,temp;

for(i=0;i<n;i++)

{

for(j=0;j<n-1;j++)

{

if(arr[j]>arr[j+1])

{

temp=arr[j];

arr[j]=arr[j+1];

arr[j+1]=temp;

}

}

}

}

void insertionsort(int arr[30], int n) 

{  

    int i, j, temp;  

    for (i = 1; i < n; i++) {  

        temp = arr[i];  

        j = i - 1;  

  

        while(j>=0 && temp <= arr[j]) 

        {    

            arr[j+1] = arr[j];     

            j = j-1;    

        }    

        arr[j+1] = temp;    

    }  

}  

void fork1()

{

  int arr[25],arr1[25],n,i,status;

  printf("\nEnter the no of values in array :");

  scanf("%d",&n);

  printf("\nEnter the array elements :");

  for(i=0;i<n;i++)

    scanf("%d",&arr[i]);

  int pid=fork();

  if(pid==0)

    {

      sleep(10);

      printf("\nchild process\n");

      printf("child process id=%d\n",getpid());

      insertionsort(arr,n);

        printf("\nElements Sorted Using insertionsort:");

      printf("\n");

        for(i=0;i<n;i++)

    printf("%d,",arr[i]);

    printf("\b");

      printf("\nparent process id=%d\n",getppid());

      system("ps -x");

       }    

      else

       {

      printf("\nparent process\n");

      printf("\nparent process id=%d\n",getppid());

bubblesort(arr,n);       

printf("Elements Sorted Using bubblesort:");

      printf("\n");

      for(i=0;i<n;i++)

    printf("%d,",arr[i]);

    printf("\n\n\n"); 

      } 

 }     

 int main()

 {

    fork1();

    return 0;

 }  

 


(2) Write a C program to illustrate the concept of orphan process. Parent process creates a

child and terminates before child has finished its task. So child process becomes orphan

process. (Use fork(), sleep(), getpid(), getppid()).

Program:- 

#include <stdio.h> 

#include <sys/types.h>

#include <unistd.h>

int main()

 int pid;

 pid=getpid(); 

 printf("Current Process ID is : %d\n",pid); 

 printf("\n[Forking Child Process ... ] \n"); 

 pid=fork(); 

 if(pid < 0) 

 {    

  printf("\nProcess can not be created ");

 } 

 else

 {

         if(pid==0)

  { 

   printf("\nChild Process is Sleeping ..."); 

   sleep(5); 

   printf("\nOrphan Child's Parent ID : %d",getppid()); 

  }

  else

  {

   printf("\nParent Process Completed ..."); 

  } 

 }

 return 0; 

}


Set C

(1) Implement the C program that accepts an integer array. Main function forks child

process. Parent process sorts an integer array and passes the sorted array to child process

through the command line arguments of execve() system call. The child process uses

execve() system call to load new program that uses this sorted array for performing the

binary search to search the particular item in the array.

Program:-

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include<unistd.h>

int main(int argc,char *argv[])

{

int val[10],ele;

int n,i,j;

char *cval[10];

pid_t pid;

char *newe[]={NULL};

printf("Enter size of Array");

scanf("%d",&n);

printf("\nEnter Array");

for(i=0;i<n;i++)

{

scanf("%d",&val[i]);

}

printf("\nEntered Array is");

for(i=0;i<n;i++)

{

printf("\t%d",val[i]);

}

for(i=1;i<n;i++)

{ for(j=0;j<n-1;j++) {

if(val[j]>val[j+1])

{

int temp=val[j];

val[j]=val[j+1];

val[j+1]=temp;

} } }

printf("\nSorted Array is");

for(i=0;i<n;i++)

{

printf("\t%d",val[i]);

}

printf("\nEnter Element to search");

scanf("%d",&ele);

val[i]=ele;

for(i=0;i<n+1;i++)

{

char a[sizeof(int)];

snprintf(a,sizeof(int),"%d",val[i]);

cval[i]=malloc(sizeof(a));

strcpy(cval[i],a);

}

cval[i]=NULL;

pid=fork();

if(pid==0)

{

execve("./p2b",cval,newe);

perror("error");

}

}

Child program

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

int main(int argc,char *argv[],char *en[])

{

int i,j,mid,ele;

int arr[argc];

for(j=0;j<argc-1;j++)

{

int n=atoi(argv[j]);

arr[j]=n;

}

ele=atoi(argv[j]);

i = 0;

j= argc - 1;

mid = (i+j)/2;

while (arr[mid]!=ele && i<=j)

{

if (ele>arr[mid])

i = mid + 1;

else

j=mid-1;

mid = (i+j)/2;

}

if(i<=j)

printf("Element is present in the list");

else

printf("not found!" );

}


(2) Implement the C Program to create a child process using fork(), Using exec() system call,

child process will execute the program specified in Set A(1) and parent will continue by

printing message “I am parent “.

No comments:

Post a Comment