You are on page 1of 7

When a process creates a new process using the fork() operation, which of the following states is

shared between the parent process and the child process?


a. Stack
b. Heap
c. Shared memory segments

Answer:
Only the shared memory segments are shared between the parent process and the newly forked
child process. Copies of the stack and the heap are made for the newly created process.

------------------

Lưu ý: Để kiểm tra kết quả của tất cả các bài tập bên dưới, sinh viên có thể thực hiện:

▬ Mở hệ điều hành Ubuntu, tạo một file *.c rồi gõ các đoạn code muốn kiểm tra vào (ví dụ
test.c)
▬ Mở cửa sổ Terminal, gõ vào: gcc test.c -o test
▬ Sau đó gõ ./test để chạy file test
------------------

1.

Hỏi output tại LINE A sẽ là gì?

CuuDuongThanCong.com https://fb.com/tailieudientucntt
2.

Bao gồm cả process cha, hỏi đoạn code trên tạo ra bao nhiêu process?

3.

Bao gồm cả process cha, hỏi đoạn code trên tạo ra bao nhiêu process?

CuuDuongThanCong.com https://fb.com/tailieudientucntt
4.

Giả sử pid của process cha và process con lần lượt là 2600 và 2603, hỏi trong đoạn code sau, tại
A, B, C và D giá trị in ra màn hình sẽ là gì?

CuuDuongThanCong.com https://fb.com/tailieudientucntt
5.

Hỏi giá trị in ra màn hình tại LINE X và LINE Y trong đoạn code trên là gì

-----------

CuuDuongThanCong.com https://fb.com/tailieudientucntt
Bài tập từ slide cũ:

1.

a.

Cho đoạn code sau:

#include <stdio.h>
#include <unistd.h>
int main (int argc, char *argv[])
{
int pid;

printf(“hi”);
pid = fork();
if (pid > 0){
fork();
printf(“hello”);
}
else
fork();
printf(“bye”);
}
Hỏi chương trình in ra dòng chữ nào trên màn hình

b.

Thay đổi đoạn code trên thành

#include <stdio.h>
#include <unistd.h>
int main (int argc, char *argv[])
{
int pid;

printf(“hi”);
pid = fork();
if (pid > 0){
fork();
printf(“hello”);
}
else

CuuDuongThanCong.com https://fb.com/tailieudientucntt
{
fork();
printf(“bye”);
}
}
Hỏi chương trình in ra dòng chữ nào trên màn hình

2.
a.
#include <stdio.h>
#include <unistd.h>
int main (int argc, char *argv[])
{
int pid;
printf(“hi”);
pid = fork();
if (pid > 0){
fork();
fork();
printf(“hello”);
}
else
fork();
printf(“bye”);
}
Hỏi chương trình in ra dòng chữ nào trên màn hình

b.

Thay đổi đoạn code trên thành

#include <stdio.h>
#include <unistd.h>
int main (int argc, char *argv[])
{
int pid;
printf(“hi”);
pid = fork();
if (pid > 0){
fork();
fork();

CuuDuongThanCong.com https://fb.com/tailieudientucntt
printf(“hello”);
}
else
{
fork();
printf(“bye”);
}
}
Hỏi chương trình in ra dòng chữ nào trên màn hình

3.

a.

#include <stdio.h>
#include <unistd.h>
int main (int argc, char *argv[])
{
int pid;
printf(“ so 1”);
pid = fork();
printf(“ so 2”);
fork();
if (pid = 0){
fork();
printf(“hello”);
}
else{
fork();
printf(“bye”);
}
}
Hỏi chương trình in ra dòng chữ nào trên màn hình?

CuuDuongThanCong.com https://fb.com/tailieudientucntt

You might also like