Skip to main content

Assignment No:1


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:

👇

  1.  #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<unistd.h>
  4. void main()
  5. {
  6. int pid;
  7. pid=fork();
  8. if(pid ==0)
  9. {
  10.                 printf("\n I am child process")
  11. printf("\n My parent process id is : %d",getppid());
  12. }
  13. else if(pid > 0)
  14. {
  15.                 printf("\n I am parent process"); 
  16.   printf("my pid is :%d",getpid());
  17. }
  18. else if(pid < 0)
  19. {
  20. printf("\n Unable to create a process..!");
  21. exit(0);
  22. }
  23. }


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:

👇

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<unistd.h>
  4. void main()
  5. {
  6. int pid,cnice, pnice;
  7. pid=fork();
  8. if(pid==0)
  9. {
  10. printf("\nI am child process and my pid=%d",getpid());
  11. //printf("\nMy parent pid is=%d",getppid());
  12. cnice = nice(-7);
  13. printf("\n");
  14. printf("Low nice value=%d i.e high priority of child process",cnice);
  15. printf("\n");
  16. }
  17. else if(pid > 0)
  18. {
  19. printf("\nI am parent process and my pid=%d",getpid());
  20. //printf("\nMy child process pid is = %d",pid);
  21. pnice = nice(+5);
  22. printf("\n");
  23. printf("High nice value=%d i.e Low priority of parent process",pnice);
  24. printf("\n");
  25. }
  26. else if(pid < 0)
  27. {
  28. printf("\n Unable to create a process..!");
  29. exit(0);
  30. }
  31. }


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:

👇

  1.  #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<unistd.h>
  4. #include<sys/wait.h>
  5. void main()
  6. {
  7. int n, i, a[20], temp,j, pid;
  8. printf("\nHow many  element you want in array \t");
  9. scanf("%d",&n);
  10. printf("Enter element of array");
  11. for(i=0; i< n ; i++)
  12. scanf("%d",&a[i]);
  13. pid=fork();
  14. if(pid > 0)
  15. {
  16. for(i=1; i< n; i++)
  17. {
  18. temp = a[i];
  19. j = i-1;
  20. while(a[j] > temp && j >= 0)
  21. {
  22. a[j+1] = a[j];
  23. j--;
  24. }
  25. a[j+1] = temp;
  26. }
  27. for(i=0;i<n;i++)
  28. printf("\nAfter Sorting array is  %d \t",a[i]);
  29.       wait(0);
  30. }
  31. else if(pid==0)
  32. {
  33. for(i=0; i<n; i++)
  34. for(j=i+1; j<n; j++)
  35. {
  36. if(a[i] > a[j])
  37. {
  38. temp=a[i];
  39. a[i]=a[j];
  40. a[j]=temp;
  41. }
  42. }
  43. for(i=0;i<n;i++)
  44. printf("\n sorted array is  %d \t",a[i]);
  45. }
  46. else if(pid < 0)
  47. {
  48. printf("\n Unable to create a process..!");
  49. exit(0);
  50. }
  51. }



                                                                                                    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:

                                                                                                    👇 

                                                                                                    1.   #include<stdio.h>
                                                                                                    2. #include<stdlib.h>
                                                                                                    3. #include<unistd.h>
                                                                                                    4. void main()
                                                                                                    5. {
                                                                                                    6. int pid;
                                                                                                    7. pid=fork();
                                                                                                    8. if(pid > 0)
                                                                                                    9. {
                                                                                                    10. printf("\n I am parent process"); 
                                                                                                    11.   printf("my pid is :%d",getpid());
                                                                                                    12. }
                                                                                                    13. else if(pid==0)
                                                                                                    14. {
                                                                                                    15. printf("\n I am child process");
                                                                                                    16. sleep(10);
                                                                                                    17. printf("\n My parent process id is : %d",getppid());
                                                                                                    18. }
                                                                                                    19. else if(pid < 0)
                                                                                                    20. {
                                                                                                    21. printf("\n Unable to create a process..!");
                                                                                                    22. exit(0);
                                                                                                    23. }
                                                                                                    24. }




                                                                                                    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