The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.
Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by haotiepstyle, 2018-05-13 10:24:57

Lab2

Lab2

Student name and ID: Laboratory 2
Score:
Date: 3 October 2017
Subject: Operating System (CS4323.I11.CTTT)
Department of Computer Engineering
University of Information Technology
Instructor: Lầu Phi Tường

The purpose of this lab is to understand the interprocess communication, the process creation

using fork() system call in Linux system.

Problem 1: (2 points)

a) What is the process in Linux system. Describe how the fork() system call works in Linux

system?

b) Write and run the C program below on Linux. How many processes are created by the below

code and explain why?
#include <stdio.h>
#include <unistd.h>
int main()
{

fork();
fork();
fork();
printf(“Hello World\n”);
return 0;
}

Problem 2: (2 points)
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
int main()
{

pid_t pid, pid1;
/* fork a child process */
pid = fork();
if (pid < 0)
{

/* error occurred */
fprintf(stderr, "Fork Failed");
return 1;
}

else if (pid == 0)
{

/* child process */
pid1 = getpid();
printf("child: pid = %d",pid); /* A */
printf("child: pid1 = %d",pid1); /* B */
}
else
{
/* parent process */
pid1 = getpid();
printf("parent: pid = %d",pid); /* C */
printf("parent: pid1 = %d",pid1); /* D */
wait(NULL);
}
return 0;
}

Write and run the C program above in Linux. What are the values of pid at line A, B, C, D?

Observe the results whether it matches with the definition of fork() described in the problem 1.

Problem 3: (3 points)
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
int value = 5;
int main()
{

pid_t pid;
pid = fork();
if (pid == 0)
{

/* child process */
value += 15;
printf("CHILD: value = %d",value); /* LINE A */
return 0;
}
else if (pid > 0)
{
/* parent process */
wait(NULL);
printf("PARENT: value = %d",value); /* LINE B */
return 0;
}
}

a) Write and run C program above on Linux. What are the values at LINE A and LINE B?

b) Remove wait(NULL) from the program and try to run the code 10 times. Is the output the

same for each execution? Fill in the results into the table below.

No. LINE X LINE Y
1
2
3
4
5
6
7
8
9
10

Problem 4: (3 points)
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#define SIZE 5
int nums[SIZE] ={0,1,2,3,4};
int main()
{

int i;
pid_t pid;
pid = fork();
if (pid == 0)
{

for (i = 0; i < SIZE; i++)
{
nums[i] *= -i;
printf("CHILD: %d ",nums[i]); /* LINE X */
}
}
else if (pid > 0)
{
wait(NULL);
for (i = 0; i < SIZE; i++)
printf("PARENT: %d ",nums[i]); /* LINE Y */
}
return 0;
}

a) Write and run C program above on Linux. What the output will be at LINE X and LINE Y?

b) Remove wait(NULL) from the program and try to run the code 10 times. Is the output the

same for each execution? Explain why does it use wait(NULL)? Fill in the results into the table

below.

No. LINE X LINE Y
1
2
3
4
5
6
7
8
9
10


Click to View FlipBook Version