Professional Documents
Culture Documents
OS Assignment-04
OS Assignment-04
OS Assignment-04
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main()
{
int pid, pid1, pid2;
return 0;
}
#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
pid_t pid = 0;
int num = 0;
if (pid == 0)
{
sleep(5 - i);
printf("Hello from Child %d\n", i + 1);
num++;
return 0;
}
else
{
wait(NULL);
}
}
return 0;
}
Q4. Create two child process C1 and C2. Make sure that only C1 becomes an orphan process.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main()
{
pid_t pid, c1_pid, c2_pid;
c1_pid = fork();
wait(NULL);
if (c1_pid == 0)
{
printf("Child_1:\npid: %d\nppid: %d\n", getpid(), getppid());
exit(0);
} else
{
c2_pid = fork();
if (c2_pid == 0)
{
sleep(10);
printf("Child_2:\npid: %d\nppid: %d\n", getpid(), getppid());
exit(0);
}
}
return 0;
}
Q5. Create a scenario where a parent has two child process C1 and C2 such that C1 becomes a
zombie while C2 becomes an orphan process.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main()
{
pid_t c1_pid, c2_pid;
c1_pid = fork();
if (c1_pid > 0)
{
c2_pid = fork();
if (c2_pid == 0)
{
sleep(5);
printf("Child_2:\npid: %d\nppid: %d\n", getpid(), getppid());
exit(0);
}
else if (c2_pid > 0)
{
sleep(15);
}
}
else if (c1_pid == 0)
{
printf("Child_1:\npid: %d\nppid: %d\n", getpid(), getppid());
sleep(17);
printf("Child_1:\npid: %d\nppid: %d\n", getpid(), getppid());
exit(0);
}
if (c1_pid > 0)
printf("Parent terminated with ppid: %d\n", getpid());
return 0;
}