THIS PROGRAMS ONLY EXECUTE ON LINUX OPERATING SYSTEM.
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”.
CODE:
👇
- #include<stdio.h>
- #include<stdlib.h>
- #include<unistd.h>
- void main()
- {
- int pid;
- pid=fork();
- if(pid ==0)
- {
- printf("\n I am child process")
- printf("\n My parent process id is : %d",getppid());
- }
- else if(pid > 0)
- {
- printf("\n I am parent process");
- printf("my pid is :%d",getpid());
- }
- else if(pid < 0)
- {
- printf("\n Unable to create a process..!");
- exit(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.
CODE:
👇
- #include<stdio.h>
- #include<stdlib.h>
- #include<unistd.h>
- void main()
- {
- int pid,cnice, pnice;
- pid=fork();
- if(pid==0)
- {
- printf("\nI am child process and my pid=%d",getpid());
- //printf("\nMy parent pid is=%d",getppid());
- cnice = nice(-7);
- printf("\n");
- printf("Low nice value=%d i.e high priority of child process",cnice);
- printf("\n");
- }
- else if(pid > 0)
- {
- printf("\nI am parent process and my pid=%d",getpid());
- //printf("\nMy child process pid is = %d",pid);
- pnice = nice(+5);
- printf("\n");
- printf("High nice value=%d i.e Low priority of parent process",pnice);
- printf("\n");
- }
- else if(pid < 0)
- {
- printf("\n Unable to create a process..!");
- exit(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.
CODE:
👇
- #include<stdio.h>
- #include<stdlib.h>
- #include<unistd.h>
- #include<sys/wait.h>
- void main()
- {
- int n, i, a[20], temp,j, pid;
- printf("\nHow many element you want in array \t");
- scanf("%d",&n);
- printf("Enter element of array");
- for(i=0; i< n ; i++)
- scanf("%d",&a[i]);
- pid=fork();
- if(pid > 0)
- {
- for(i=1; i< n; i++)
- {
- temp = a[i];
- j = i-1;
- while(a[j] > temp && j >= 0)
- {
- a[j+1] = a[j];
- j--;
- }
- a[j+1] = temp;
- }
- for(i=0;i<n;i++)
- printf("\nAfter Sorting array is %d \t",a[i]);
- wait(0);
- }
- else if(pid==0)
- {
- for(i=0; i<n; i++)
- for(j=i+1; j<n; j++)
- {
- if(a[i] > a[j])
- {
- temp=a[i];
- a[i]=a[j];
- a[j]=temp;
- }
- }
- for(i=0;i<n;i++)
- printf("\n sorted array is %d \t",a[i]);
- }
- else if(pid < 0)
- {
- printf("\n Unable to create a process..!");
- exit(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()).
CODE:
👇
- #include<stdio.h>
- #include<stdlib.h>
- #include<unistd.h>
- void main()
- {
- int pid;
- pid=fork();
- if(pid > 0)
- {
- printf("\n I am parent process");
- printf("my pid is :%d",getpid());
- }
- else if(pid==0)
- {
- printf("\n I am child process");
- sleep(10);
- printf("\n My parent process id is : %d",getppid());
- }
- else if(pid < 0)
- {
- printf("\n Unable to create a process..!");
- exit(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.
CODE:
👇
Comments
Post a Comment