0% found this document useful (0 votes)
53 views5 pages

C Program for Process Control Demo

Uploaded by

shubham.shelake
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views5 pages

C Program for Process Control Demo

Uploaded by

shubham.shelake
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd

/*

Name :
Class :
RollNO :-
Problem Statement: Process control system calls: The demonstration of FORK, EXECVE and
WAIT system calls
along with zombie and orphan states.

a. Implement the C program in which main program accepts the integers to be sorted. Main program
uses
the FORK system call to create a new process called a child process. Parent process sorts the
integers using
sorting algorithm and waits for child process using WAIT system call to sort the integers using any
sorting
algorithm. Also demonstrate zombie and orphan states.
*/

#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>

void bass(int arr[10],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;
}
}
}
printf("\n Ascending Order \n");
for(i=0;i<n;i++)
printf("\t%d",arr[i]);
printf("\n\n\n");
}

void bdsc(int arr[10],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;
}
}
}
printf("\n Descending Sorting \n\n");
for(i=0;i<n;i++)
printf("\t%d",arr[i]);
printf("\n\n\n");

}
void forkeg()
{
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());
bdsc(arr,n);
printf("\nElements Sorted Using Quick Sort");
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());
bass(arr,n);
printf("Elements Sorted Using Bubble Sort");
printf("\n");
for(i=0;i<n;i++)
printf("%d,",arr[i]);
printf("\n\n\n");
}
}
int main()
{
forkeg();
return 0;
}

/*OUTPUT:
samarth@samarth-OptiPlex-7010:~$ gcc assign2.c
samarth@samarth-OptiPlex-7010:~$ ./a.out

Enter the no of values in array5

Enter the array elements1 4 2 3 5

parent process

parent process id=2462

Ascending Order
1 2 3 4 5

Elements Sorted Using Bubble Sort


1,2,3,4,5,

samarth@samarth-OptiPlex-7010:~$
child process
child process id=3400

Descending Sorting

5 4 3 2 1

Elements Sorted Using Quick Sort


5,4,3,2,1,
parent process id=1304
PID TTY STAT TIME COMMAND
1304 ? Ss 0:00 /lib/systemd/systemd --user
1305 ? S 0:00 (sd-pam)
1311 ? S<sl 0:00 /usr/bin/pipewire
1312 ? Ssl 0:00 /usr/bin/pipewire-media-session
1313 ? S<sl 0:00 /usr/bin/pulseaudio --daemonize=no --log-target=jou
1316 ? Ssl 0:00 /usr/bin/ubuntu-report service
1331 ? Ss 0:00 /usr/bin/dbus-daemon --session --address=systemd: -
1339 ? Ssl 0:00 /usr/libexec/gvfsd
1343 ? Ssl 0:00 /usr/libexec/xdg-document-portal
1345 ? Sl 0:00 /usr/libexec/gvfsd-fuse /run/user/1000/gvfs -f
1358 ? Sl 0:00 /usr/bin/gnome-keyring-daemon --daemonize --login
1361 ? Ssl 0:00 /usr/libexec/xdg-permission-store
1387 ? SNsl 0:00 /usr/libexec/tracker-miner-fs-3
1405 tty2 Ssl+ 0:00 /usr/libexec/gdm-wayland-session env GNOME_SHELL_SE
1408 tty2 Sl+ 0:00 /usr/libexec/gnome-session-binary --session=ubuntu
1456 ? Ssl 0:00 /usr/libexec/gnome-session-ctl --monitor
1467 ? Ssl 0:00 /usr/libexec/gnome-session-binary --systemd-service
1487 ? Sl 0:00 /usr/libexec/at-spi-bus-launcher --launch-immediate
1493 ? Ssl 0:00 /usr/libexec/gvfs-udisks2-volume-monitor
1495 ? S 0:00 /usr/bin/dbus-daemon --config-file=/usr/share/defau
1498 ? Ssl 0:19 /usr/bin/gnome-shell
1507 ? Ssl 0:00 /usr/libexec/gvfs-gphoto2-volume-monitor
1518 ? Ssl 0:00 /usr/libexec/gvfs-afc-volume-monitor
1524 ? Ssl 0:00 /usr/libexec/gvfs-mtp-volume-monitor
1530 ? Ssl 0:00 /usr/libexec/gvfs-goa-volume-monitor
1534 ? Sl 0:00 /usr/libexec/goa-daemon
1567 ? Sl 0:00 /usr/libexec/goa-identity-service
1587 ? Sl 0:00 /usr/libexec/gnome-shell-calendar-server
1593 ? Ssl 0:00 /usr/libexec/evolution-source-registry
1599 ? Ssl 0:00 /usr/libexec/dconf-service
1603 ? Ssl 0:00 /usr/libexec/evolution-calendar-factory
1613 ? Ssl 0:00 /usr/libexec/evolution-addressbook-factory
1629 ? Sl 0:01 /usr/bin/nautilus --gapplication-service
1630 ? Sl 0:00 /usr/libexec/gvfsd-trash --spawner :1.3 /org/gtk/gv
1638 ? Sl 0:00 /usr/bin/gjs /usr/share/gnome-shell/org.gnome.Shell
1640 ? Sl 0:00 /usr/libexec/at-spi2-registryd --use-gnome-session
1654 ? Ss 0:00 sh -c /usr/bin/ibus-daemon --panel disable $([ "$XD
1655 ? Ssl 0:00 /usr/libexec/gsd-a11y-settings
1656 ? Ssl 0:00 /usr/libexec/gsd-color
1658 ? Ssl 0:00 /usr/libexec/gsd-datetime
1660 ? Ssl 0:00 /usr/libexec/gsd-housekeeping
1661 ? Ssl 0:00 /usr/libexec/gsd-keyboard
1663 ? Sl 0:00 /usr/bin/ibus-daemon --panel disable
1669 ? Ssl 0:00 /usr/libexec/gsd-media-keys
1670 ? Ssl 0:00 /usr/libexec/gsd-power
1671 ? Ssl 0:00 /usr/libexec/gsd-print-notifications
1672 ? Ssl 0:00 /usr/libexec/gsd-rfkill
1673 ? Ssl 0:00 /usr/libexec/gsd-screensaver-proxy
1675 ? Ssl 0:00 /usr/libexec/gsd-sharing
1678 ? Ssl 0:00 /usr/libexec/gsd-smartcard
1679 ? Ssl 0:00 /usr/libexec/gsd-sound
1691 ? Ssl 0:00 /usr/libexec/gsd-wacom
1726 ? Sl 0:00 /usr/libexec/evolution-data-server/evolution-alarm-
1734 ? Sl 0:00 /usr/libexec/gsd-disk-utility-notify
1762 ? Sl 0:00 /usr/libexec/ibus-memconf
1764 ? Sl 0:01 /usr/libexec/ibus-extension-gtk3
1774 ? Sl 0:00 /usr/libexec/gsd-printer
1782 ? Sl 0:00 /usr/libexec/ibus-portal
1815 ? Sl 0:00 /usr/libexec/ibus-engine-simple
1820 ? Ssl 0:00 /usr/libexec/xdg-desktop-portal
1832 ? Ssl 0:00 /usr/libexec/xdg-desktop-portal-gnome
1866 ? Sl 0:00 /usr/bin/gjs /usr/share/gnome-shell/org.gnome.Scree
1882 ? Ss 0:00 /snap/snapd-desktop-integration/83/usr/bin/snapd-de
1885 ? Ssl 0:00 /usr/libexec/xdg-desktop-portal-gtk
1964 ? Sl 0:00 /snap/snapd-desktop-integration/83/usr/bin/snapd-de
1968 ? Sl 0:00 gjs /usr/share/gnome-shell/extensions/ding@rasterso
2014 ? Ssl 0:00 /usr/libexec/gvfsd-metadata
2039 ? Sl 0:17 /usr/bin/gedit --gapplication-service
2090 ? Sl 0:00 /usr/libexec/gvfsd-network --spawner :1.3 /org/gtk/
2103 ? Sl 0:00 /usr/libexec/gvfsd-dnssd --spawner :1.3 /org/gtk/gv
2148 ? Sl 0:00 /usr/bin/gnome-calendar --gapplication-service
2149 ? SLl 0:00 /usr/bin/seahorse --gapplication-service
2226 ? Sl 0:00 /usr/lib/libreoffice/program/oosplash --writer
2242 ? Sl 0:03 /usr/lib/libreoffice/program/soffice.bin --writer
2244 ? Sl 0:00 /usr/bin/Xwayland :0 -rootless -noreset -accessx -c
2257 ? Ssl 0:00 /usr/libexec/gsd-xsettings
2292 ? Sl 0:00 /usr/libexec/ibus-x11
2310 ? Sl 0:00 update-notifier
2444 ? Ssl 0:01 /usr/libexec/gnome-terminal-server
2462 pts/0 Ss+ 0:00 bash
2486 ? Sl 0:16 /snap/firefox/3779/usr/lib/firefox/firefox
2651 ? Sl 0:00 /snap/firefox/3779/usr/lib/firefox/firefox -content
2668 ? Sl 0:01 /snap/firefox/3779/usr/lib/firefox/firefox -content
2704 ? Sl 0:00 /usr/bin/snap userd
2838 ? Sl 0:00 /snap/firefox/3779/usr/lib/firefox/firefox -content
3009 ? Sl 0:02 /snap/firefox/3779/usr/lib/firefox/firefox -content
3168 ? Sl 0:00 /snap/firefox/3779/usr/lib/firefox/firefox -content
3170 ? Sl 0:01 /snap/firefox/3779/usr/lib/firefox/firefox -content
3179 ? Sl 0:03 /snap/firefox/3779/usr/lib/firefox/firefox -content
3198 ? Sl 0:00 /snap/firefox/3779/usr/lib/firefox/firefox -content
3265 ? Sl 0:00 /snap/firefox/3779/usr/lib/firefox/firefox -content
3298 ? Sl 0:00 /snap/firefox/3779/usr/lib/firefox/firefox -content
3327 ? Sl 0:00 /snap/firefox/3779/usr/lib/firefox/firefox -content
3400 pts/0 S 0:00 ./a.out
3402 pts/0 S 0:00 sh -c ps -x
3403 pts/0 R 0:00 ps -x
^C
*/

You might also like