0% found this document useful (0 votes)
331 views6 pages

Operating system: Full name: Trịnh Mạnh Hùng Student id: 1952740

The document contains instructions and code for two programming exercises involving processes and forking. The first exercise involves reading integers from a file into an array, forking a child process, and having the parent count even numbers while the child counts odd numbers. The second exercise involves forking multiple processes to create a process tree with a specific structure, printing the process IDs and relationships.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
331 views6 pages

Operating system: Full name: Trịnh Mạnh Hùng Student id: 1952740

The document contains instructions and code for two programming exercises involving processes and forking. The first exercise involves reading integers from a file into an array, forking a child process, and having the parent count even numbers while the child counts odd numbers. The second exercise involves forking multiple processes to create a process tree with a specific structure, printing the process IDs and relationships.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Operating system

Lab 2
Full name : Trịnh Mạnh Hùng
Student id: 1952740

3. Exercise

3.1. Questions:

3.1.1. What the output will be at LINE A? Does this value not change for every process
execution? Explain your answer.

The output at LINE A is "PID of child process = 276", which is the value of child process's
id. We can get the value of current process by calling getpid(). This ID changes over every process
execution because though we keep launching our program from the same parent process, this
parent process creates a new child process which will have a new PID.

3.1.2. What the output will be at LINE C? Explain your answer.

The output at LINE C is "PARENT: value = 19".

Because the global variable "value" is initialized by 19 and the parent process does not change the
value variable at all.

Although "value" is increased by 15 in the child process, it does not change the "value" of parent
process at all since OS allocate different data and memory for these two processes.

3.1.3. Remove line B from the program. Observe your display result onto the screen and give
your remark.

When we remove the line B from the program, the display result will usually be different in order.
In this case, the parent process runs faster.

But when we use "wait(NULL)" in parent process, it will wait for the child process finishes and
runs right after that.
Code for ex 3.1:

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

int value = 19;

int main() {
pid_t pid;
pid = fork();
if(pid == 0) { /*child process*/
value += 15;
printf("PID of child process = %d \n", getpid()); /*line A*/
return 0;
}
else if(pid > 0) {
wait(NULL); /*line B*/
printf("PARENT: value = %d \n",value); /*line C*/
return 0;
}
}

The result when using wait (NULL):

The result when not using wait(NULL):

3.2. Programming exercises (required):


Problem 1 (5 points): Given a file named "numbers.txt" containing multiple lines of text. Each line
is a non-negative integer. Write a C program that reads integers listed in this file and stores them
in an array (or linked list). The program then uses the fork() system call to create a child process.
The parent process will count the numbers of integers in the array that are divisible by 2. The child
process will count numbers divisible by 3. Both processes then send their results to the stdout. For
examples, if the file "numbers.txt" contains the following lines:

Code for ex 3.2 problem 1:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int arr[1005];

int main(int argc, char **argv) {


FILE *f = fopen("numbers.txt", "r");
int c, n = 0, count3 = 0, count2 = 0;
while(!feof(f)) {
fscanf(f, "%d", &arr[n++]);
}
pid_t pid;
pid = fork();
int bol1 = 0, bol2 = 0;
for(int i = 0; i< n; ++i) {

if(pid > 0) {
if(arr[i] % 2 == 0) count2++;
bol1 = 1;
}
else {
bol2 = 1;
if(arr[i] % 3 == 0) count3++;
}
}
if(bol1) printf("Parent: %d\n", count2);
if(bol2) printf("Child: %d\n", count3);
return 0;
}

The result when running the with numbers.txt:

Problem 2 (3 points): The relationship between processes could be represented by a tree. When a
process uses fork system call to create another process then the new process is a child of this
process. This process is the parent of the new process. For examples, if process A uses two fork
system calls to create two new processes B and C then we could display their relationship by a tree
in figure 3.1. B is a child process of A. A is the parent of both B and C.

Write a program that uses fork system calls to create processes whose relationship is similar to the
one showed in Figure 3.2. Note: If a process has multiple children then its children must be created
from left to right. For example, process A must creates B first then create C and finally D.

Code for ex 3.2 problem 2:

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

int main(int argc, char** argv) {

pid_t B, C, D, E, F, G, I;
//printf("Tier 1:\n");
B = fork();
int i = 0;
fflush(stdout);

if(B > 0) printf("Process A (id = %d)\n", getpid());


if(B == 0) {
//B
//printf(" Tier 2:\n");
printf(" Process A(id = %d) spawns process B (id = %d)\n", getppid(),
getpid());
E = fork();
if(E == 0) {
//E
//printf(" Tier 3:\n");
sleep(0.1);
printf(" Process B(id = %d) spawns process E (id = %d)\n", ge
tppid(), getpid());
I = fork();

if(I == 0) {
//printf(" Tier 4:\n");
printf(" Process E(id = %d) spawns process I (id =
%d)\n", getppid(), getpid());
}
else wait(NULL);

}
else {
//F
F = fork();
if(F == 0) printf(" Process B(id = %d) spawns process F (id =
%d)\n", getppid(), getpid());
else wait(NULL);

}
}
else {
C = fork();
if(C == 0) {

printf(" Process A(id = %d) spawns process C (id = %d)\n", getppi


d(), getpid());
G = fork();
sleep(0.1);
if(G == 0) printf(" Process C(id = %d) spawns process G (id =
%d)\n", getppid(), getpid());
else wait(NULL);

}
else {
//wait(NULL);
D = fork();

if(D == 0) printf(" Process A(id = %d) spawns process D (id = %d)


\n", getppid(), getpid());
else wait(NULL);
}
}
return 0;
}

The result when running the code above:

You might also like