OS Assignment-04

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

OS Lab Assignment-03

ANAY GUPTA | 2005780

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

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


{
for (int i = 0; i < 3; i++)
{
if (fork() == 0)
{
printf("child[%d] pid --> %d and parent pid --> %d\n", i+1,
getpid(), getppid());
exit(0);
}
wait(NULL);
}

return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

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


{
for (int i = 0; i < 3; i++)
{
if (fork() != 0)
{
wait(NULL);
printf("child[%d] pid --> %d and parent pid --> %d\n", i+1,
getpid(), getppid());
exit(0);
}
}

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;

for (int i = 0; i < 2; i++)


{
pid = fork();
wait(NULL);
if (pid == 0)
{
printf("child[%d] pid --> %d and parent pid --> %d\n", i + 1,
getpid(), getppid());
exit(0);
}
}
pid = fork();
wait(NULL);
if (pid == 0)
{
printf("child[3] pid --> %d and parent pid --> %d\n", getpid(),
getppid());
pid1 = fork();
wait(NULL);
if (pid1 == 0)
{
printf("child[4] pid --> %d and parent pid --> %d\n", getpid(),
getppid());
for (int i = 5; i < 7; i++)
{
pid2 = fork();
wait(NULL);
if (pid2 == 0)
{
printf("child[%d] pid --> %d and parent pid --> %d\n", i +
1, getpid(), getppid());
exit(0);
}
}
exit(0);
}
printf("Parent pid = %d\n", getppid());
exit(0);
}

return 0;
}

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

int main()
{
pid_t pid = 0;
int num = 0;

for (int i = 0; i < 5; i++)


{
pid = fork();

if (pid == 0)
{
sleep(5 - i);
printf("Hello from Child %d\n", i + 1);
num++;
return 0;
}
else
{
wait(NULL);
}
}

printf("Hello from the process, currentPid : %d\n", getpid());

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);
}
}

printf("Parent terminated with ppid: %d\n", getpid());

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;
}

You might also like