0% found this document useful (0 votes)
14 views19 pages

Lab 5 Os

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

Lab 5 Os

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

OS(LAB-5)

K KRISHNANUNNI
CSE-C
AM.EN.U4CSE22231

1)

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <sys/types.h>

typedef struct tree {

pid_t process_id;

pid_t parent_id;

char name;

struct tree **children;

int n;

} ptree;

void dfs(ptree* process)

{ if (process == NULL)

return;

else {

printf("Process ID: %d\n", process->process_id);

printf("Process Parent ID: %d\n", process->parent_id);

printf("Process ID Name: %c\n", process->name);

for (int i = 0; i < process->n; i+

+) dfs(process->children[i]);

}
}

ptree* construct(pid_t process_id, pid_t parent_id, char name, int n) {

ptree* p = (ptree*)malloc(sizeof(ptree));

if (p == NULL) {

printf("DMA failed");

return NULL;

} else {

p->process_id = process_id;

p->name = name;

p->n = n;

p->children = (ptree*)malloc(sizeof(ptree) * n);

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

p->children[i] = NULL;

return p;

void destroy_tree(ptree* root) {

if (root == NULL)

return;

for (int i = 0; i < root->n; i++)

destroy_tree(root->children[i]);

free(root->children);

free(root);

}
int main(void) {

ptree *p1, *p2, *p3, *p4, *p5, *p6, *p7, *p8, *p9;

pid_t P1, P2, P3, P4, P5, P6, P7, P8, P9;

if(P1==0)

P1 = getpid();

if(P2==0)

P2 = getpid();

if(P3==0)

P3 = getpid();

if(P4==0)

P4 = getpid();

if(P5==0)

P5 = getpid();

if(P6==0)

P6 = getpid();

if(P7==0)

P7 = getpid();

if(P8==0)

P8 = getpid();

if(P9==0)

P9 = getpid();

p1 = construct(P1, 0, 'A', 3);

p2 = construct(P2, P1, 'B', 0);

p3 = construct(P3, P1, 'C', 2);

p4 = construct(P4, P1, 'D', 1);

p5 = construct(P5, P3, 'E', 0);

p6 = construct(P6, P3, 'F', 0);

p7 = construct(P7, P4, 'G', 2);

p8 = construct(P8, P7, 'H', 0);

p9 = construct(P9, P7, 'I',

0); p1->children[0] = p2;


p1->children[1] = p3;

p1->children[2] = p4;

p3->children[0] = p5;

p3->children[1] = p6;

p4->children[0] = p7;

p7->children[0] = p8;

p7->children[1] =

p9;

p2->parent_id = p1-

>process_id; p3->parent_id=

p1->process_id; p4->parent_id=

p1->process_id; p5->parent_id=

p3->process_id; p6->parent_id=

p3->process_id; p7->parent_id

= p4->process_id; p8-

>parent_id = p7->process_id;

p9->parent_id = p7-

>process_id; dfs(p1);

destroy_tree(p1);
2)

#include <stdio.h>

#include <sys/types.h>

#define MAX_COUNT 200

void ChildProcess(void); /* child process prototype */

void ParentProcess(void); /* parent process prototype

*/
int main(void)

pid_t pid;

pid = vfork();

if (pid == 0)

ChildProcess();

else

ParentProcess();

void ChildProcess(void)

int i;

for (i = 1; i <= MAX_COUNT; i++)

printf(" This line is from child, value = %d\n", i);

printf(" *** Child process is done ***\n");

void ParentProcess(void)

int i;

for (i = 1; i <= MAX_COUNT; i++)

printf("This line is from parent, value = %d\n", i);

printf("*** Parent is done ***\n");

}
3)

#include <stdio.h>

#include <sys/types.h>

#define MAX_COUNT 200

void ChildProcess(void); /* child process prototype */

void ParentProcess(void); /* parent process prototype

*/

int main(void)

pid_t pid;

pid = vfork();
ChildProcess();

sleep(10);

ParentProcess();

void ChildProcess(void)

int i;

for (i = 1; i <= MAX_COUNT; i++)

printf(" This line is from child, value = %d\n", i);

printf(" *** Child process is done ***\n");

void ParentProcess(void)

int i;

for (i = 1; i <= MAX_COUNT; i++)

printf("This line is from parent, value = %d\n", i);

printf("*** Parent is done ***\n");

}
4)

#include <stdio.h>

#include <sys/types.h>

#define MAX_COUNT 200

int main(void)

pid_t pid;

pid = vfork();

if(pid==0)

ChildProcess();

else

ParentProcess();

}
void ChildProcess(void)

int i;

printf("Enter i for square area:

"); scanf("%d",&i);

printf("Area : %d\n", i*i);

printf("Perimeter: %d \n", 4 * i);

printf(" *** Child process is running, with PID = %d\n", getpid());

printf(" *** Child is done ***\n");

void ParentProcess(void)

int i;

printf("Enter i for circle are");

scanf("%d",&i);

printf("Area:%f \n", 3.14 * i * i);

printf("Perimeter:%f\n",2 * 3.14 * i);

printf(" *** Parent process is running, with PID = %d\n", getpid());

printf("Parent process done\n");

}
5)

#include <stdio.h>

#include <sys/types.h>

#include <unistd.h>

#include <stdlib.h>

void ChildProcess1(void);

void ChildProcess2(void);

void ParentProcess(void);

int main(void)

pid_t pid;

pid = fork();

if (pid < 0) {

fprintf(stderr, "Fork failed");

return 1;

} else if (pid == 0) {

ChildProcess1();

exit(0);

} else {

wait(NULL);

pid = fork();

if (pid < 0) {

fprintf(stderr, "Fork failed");

return 1;

} else if (pid == 0) {

ChildProcess2();

exit(0);

} else {
wait(NULL);

ParentProcess();

return 0;

void ChildProcess1(void)

int i;

printf("Enter i for square area: ");

scanf("%d", &i);

printf("Area : %d\n", i * i);

printf("Perimeter: %d \n", 4 * i);

printf(" *** Child process 1 is running, with PID = %d\n", getpid());

printf(" *** Child process 1 is done ***\n");

void ChildProcess2(void)

int i;

printf("Enter i for circle area: ");

scanf("%d", &i);

printf("Area: %f \n", 3.14 * i * i);

printf("Perimeter: %f\n", 2 * 3.14 * i);

printf(" *** Child process 2 is running, with PID = %d\n", getpid());

printf(" *** Child process 2 is done ***\n");

void ParentProcess(void)
{

printf(" *** Parent process is running, with PID = %d\n", getpid());

6)

#include <stdio.h>

#include <sys/types.h>

#include <unistd.h>

#include <stdlib.h>

void ChildProcess1(void);

void ChildProcess2(void);

void ParentProcess(void);

int main(void)

pid_t pid;

pid = vfork();

if (pid < 0) {

fprintf(stderr, "Fork failed");

return 1;

} else if (pid == 0) {

ChildProcess2();
_exit(0);

} else {

wait(NULL);

pid = vfork();

if (pid < 0) {

fprintf(stderr, "Fork failed");

return 1;

} else if (pid == 0) {

ChildProcess1();

_exit(0);

} else {

wait(NULL);

ParentProcess();

return 0;

void ChildProcess1(void)

int i;

printf("Enter i for square area: ");

scanf("%d", &i);

printf("Area: %d\n", i * i);

printf("Perimeter: %d \n", 4 * i);

printf("*** Child process 1 is running, with PID = %d\n", getpid());

printf("*** Child process 1 is done ***\n");

void ChildProcess2(void)
{

int i;

printf("Enter i for circle area: ");

scanf("%d", &i);

printf("Area: %f \n", 3.14 * i * i);

printf("Perimeter: %f\n", 2 * 3.14 * i);

printf("*** Child process 2 is running, with PID = %d\n", getpid());

printf("*** Child process 2 is done ***\n");

void ParentProcess(void)

printf("*** Parent process is running, with PID = %d\n", getpid());

7)

#include <stdio.h>

#include <sys/types.h>
int main(void)

pid_t pid;

pid = fork();

printf("Enter a:");

int a;

scanf("%d",&a);

if(pid==0){

ChildProcess2(a);

wait(1);

else{

pid=fork();

if(pid==0)

ParentProcess();

else{

wait(1);

ChildProcess1(a);

void ChildProcess1(int i)

{
printf("Area : %d\n", i*i);

printf("Perimeter: %d \n", 4 * i);

printf(" *** Child process is running, with PID = %d\n", getpid());

printf(" *** Child is done ***\n");

void ChildProcess2(int i)

printf("Area:%f \n", 3.14 * i * i);

printf("Perimeter:%f\n",2 * 3.14 * i);

printf(" *** Child process is running, with PID = %d\n", getpid());

printf("Children process done\n");

void ParentProcess(void){

printf("*** Parent process is running, with PID = %d\n", getpid());

}
8)

#include <stdio.h>

#include <sys/types.h>

int main(void)

pid_t pid;

pid = fork();

if(pid==0){

ChildProcess2();

else{

wait(1);

pid=fork();

if(pid == 0){

ChildProcess1();

else{

wait(10);

ParentProcess();

void ChildProcess2(void)
{

printf("Happy New Year\n");

void ChildProcess1(void)

int ele;

printf("Enter the number");

scanf("%d",&ele);

int sum=0;

while(ele){

sum += ele % 10;

ele/=10;

printf("The sum is %d\n",sum);

void ParentProcess(void){

printf("Parent Exiting Good Bye\n");

You might also like