Lab2 Process
Lab2 Process
Lab2 Process
Lab 2: Process
Course: Operating Systems
Goal This lab helps student to practice with the process in OS, and understand how we can manipulate
process and perform multi-process interaction using communication mechanism.
Contents In detail, this lab requires student practice with examples fork() API to create processes
and do interaction with these instances through the following experiments:
• retrieve the process information (with PID) and determine the process status in its life cycle
Besides, the practices also introduces and includes some additional process interfaces i.e. environment,
system call, arguments, IO and files.
Result After doing this lab, student can understand the definition of process and write a program with
multi-process creation and communication.
Requirements Student need to review the theory of program and how to create a process from the
associated program by executing it.
$ gcc - o h e l l o h e l l o . c
$ ./ hello
$ ps a u x f | g r e p h e l l o
1
CONTENTS CONTENTS
Contents
1 Background 3
2 Programming interface 6
2.1 Fork API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.2 Proc FS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.3 Process specification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.3.1 Process state . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.3.2 Send signal to specified process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.3.3 Process statistics environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.4 Proces memory layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.5 Process Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.6 Inter-process Communication Programming Interfaces . . . . . . . . . . . . . . . . . . . . 10
2.6.1 Shared Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.6.2 Message Passing - An illustration of Message Queue . . . . . . . . . . . . . . . . . 11
2.7 Mapped memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.8 POSIX Thread (pthread) library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3 Practices 16
3.1 Practice 1: Create process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.2 Practice 2: Traverse the tree of processes . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.3 Practice 3: Examine the process memory regions . . . . . . . . . . . . . . . . . . . . . . . 17
3.4 Practice 4: Inter Process Communication . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
3.4.1 Shared Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
3.4.2 Message Passing - An illustration of Message Queue . . . . . . . . . . . . . . . . . 21
3.5 Practice 5: Create thread using Pthread library . . . . . . . . . . . . . . . . . . . . . . . . 22
4 Exercise 25
4.1 Problem1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
4.2 Problem2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
4.3 Problem3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
4.4 Problem4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
2
1 BACKGROUND
1 Background
In this section, we recall the basic background material which is related to the process experiment.
• Process concept: program in execution and each process has an unique PID
• Memory layout.
• A tree of process.
• Process environment
Process ID - pid Most operating systems (including UNIX, Linux, and Windows) identify processes
according to a unique process identifier (or PID) which is typically an integer number.
Task struct the task struct structure contains all the information about a process. Much information
is investigated in this lab experiments.
Process state The process state is represented in the following diagram. We can send a signal to a
process to change it status. The detailed commands are introduced in section 2.3.2.
waiting
3
1 BACKGROUND
Memory layout The memory layout of a process is typically divided into multiple section
We hava an examination in each section using the variable declaration in different program scope to
illustrated the memory layout as Figure.3 in section 2.4
stack int x;
int y=5;
int *value;
heap int i;
A tree of process During the OS execution, a process, called parent process, may create several new
processes which are referred as children of that process. Each of these new (child) processes may in turn
create other processes, forming a tree of processes. An example of process tree is shown in Figure.4 and
will be investigated in section 2.5
systemd
bash sshd
ps vim tcsh
4
1 BACKGROUND
...
send recv
heap heap heap heap
SHARED
MEMORY
POSIX thread A traditional process has a single thread of control. If a process has multiple threads
of control, it can perform more than one task at a time. Figure 6 illustrates the difference between a
traditional single-threaded process and a multi-threaded process.
We use the POSIX thread library (Pthread) to create additional thread inside a traditional single-
thread process. The details instructions and guidelines are at section 2.8.
register PC stack
register register register
PC PC PC
5
2 PROGRAMMING INTERFACE
2 Programming interface
2.1 Fork API
fork() creates a new process by duplicating the calling process. The new process, referred to as the
child, is an exact duplicate of the calling process, referred to as the parent
#include <u n i s t d . h>
p i d t f o r k ( void ) ;
p r i n t f ( ” S t a r t o f main . . . \ n” ) ;
pid = f o r k ( ) ;
i f ( pid > 0) {
/∗ p a r e n t p r o c e s s ∗/
p r i n t f ( ” Parent s e c t i o n . . . \ n” ) ;
}
e l s e i f ( p i d == 0 ) {
/∗ c h i l d p r o c e s s ∗/
p r i n t f ( ” \ n f o r k c r e a t e d . . . \ n” ) ;
}
else {
/∗ f o r k c r e a t i o n f a i l e ∗/
p r i n t f ( ” \ n f o r k c r e a t i o n f a i l e d ! ! ! \ n” ) ;
}
return 0 ;
}
2.2 Proc FS
ProcFS presents the information about processes and other system information. It provides a more
convenience and standardized method for dynamically accessing process data held in kernel instead of
tracing and direct acessing to kernel memory. For example, the GNU version of processing report ultility
ps used the proc filesystem to obtain data, without using any specialized system calls.
We can retrieve various information in read-only part of /proc file system:
6
2.3 Process specification 2 PROGRAMMING INTERFACE
Each process is mapped to a process-specific subdirectory a under the path associated with its Pid as
/proc/<pid>
File Content
clear refs Clears page referenced bits shown in smaps output
cmdline Command line arguments
cpu Current and last cpu in which it was executed (2.4)(smp)
cwd Link to the current working directory
environ Values of environment variables
exe Link to the executable of this process
fd Directory, which contains all file descriptors
maps Memory maps to executables and library files (2.4)
mem Memory held by this process
root Link to the root directory of this process
stat Process status
statm Process memory status information
status Process status in human readable form
pagemap Page table
stack A symbolic trace of the process’s kernel stack
... ...
filepath /proc/[pid]/status Provides much of the information in a format that’s easier for humans
to parse. The state is under file path /proc/<pid>/status
$ head - n 10 / p r o c/<pid >/ s t a t u s
Name : helloworld
State : S ( sleeping )
Tgid : 1163
Ngid : 0
7
2.3 Process specification 2 PROGRAMMING INTERFACE
Pid : 1163
PPid : 1162
TracerPid : 0
Uid : 1000 1000 1000 1000
Gid : 1000 1000 1000 1000
...
The command kill sends the specified signal to the specified processes or process groups. If no signal is
specified, the TERM signal is sent.
$ k i l l - SIGNAL <pid>
$ k i l l a l l - SIGNAL name
For example:
kill -SIGCONT 2378
killall hello
The list of support sign is as follows:
8
2.4 Proces memory layout 2 PROGRAMMING INTERFACE
filepath /proc/[pid]/stack This file provides a symbolic trace of the function calls in this process’s
kernel stack.
$ cat / p r o c/<pid >/s t a c k
filepath /proc/[pid]/environment This file contains the initial environment that was set when the
currently executing program was started.
$ s t r i n g s / p r o c/<pid >/e n v i r o n
9
2.5 Process Tree 2 PROGRAMMING INTERFACE
In addition, we can use the process-specific information retrieved from /proc filesystem to identify the
parent pid and the list of child pid.
From child process, we can get the ppid (the PID of parent process)
$ head - n 10 <pid>
Meanwhile, the parent process can retrive the list of its child list under the pathname
$ c a t / p r o c/<pid >/t a s k/<pid >/ c h i l d r e n
By getting the ppid and the children list we can traverse through the process tree manually.
shmat attaches the System V shared memory segment identified by shmid to the address space of the
calling process
10
2.6 Inter-process Communication Programming Interfaces 2 PROGRAMMING INTERFACE
shmdt detaches the shared memory segment located at the address specified by shmaddr from the
address space of the calling process.
#include <s y s /shm . h>
To illustrate the message passing mechanism, we using here the message queue library.
• The first argument, key, recognizes the message queue. The key can be either an arbitrary value.
• The second argument, shmflg, specifies the required message queue flags such as IPC CREAT (cre-
ating message queue if not exists) or IPC EXCL (Used with IPC CREAT to create the message
queue and the call fails, if the message queue already exists). Need to pass the permissions as well.
msgbuf - The message buffer structure the structure of message is defined the following form:
struct msgbuf {
long mtype ;
char mtext [ 1 ] ;
};
• The variable mtype is used for communicating with different message types
• The variable mtext is an array or other structure whose size is specified by msgsz (positive value).
msgsnd This system call sends/appends a message into the message queue
#include <s y s / t y p e s . h>
#include <s y s / i p c . h>
#include <s y s /msg . h>
11
2.6 Inter-process Communication Programming Interfaces 2 PROGRAMMING INTERFACE
• The first argument, msgid, recognizes the message queue i.e., message queue identifier. The iden-
tifier value is received upon the success of msgget()
• The second argument, msgp, is the pointer to the message, sent to the caller, defined in the structure
of msgbuf
• The third argument, msgsz, is the size of message (the message should end with a null character)
• The fourth argument, msgflg, indicates certain flags such as IPC NOWAIT (returns immediately
when no message is found in queue or MSG NOERROR (truncates message text, if more than msgsz
bytes)
msgrcv This system call retrieves the message from the message queue
#include <s y s / t y p e s . h>
#include <s y s / i p c . h>
#include <s y s /msg . h>
int msgrcv ( int msgid , const void ∗msgp , s i z e t msgsz , long msgtype , int m s g f l g )
• The first argument, msgid, recognizes the message queue i.e., the message queue identifier. The
identifier value is received upon the success of msgget()
• The second argument, msgp, is the pointer of the message received from the caller. It is defined in
the structure of msgbuf
• The third argument, msgsz, is the size of the message received (message should end with a null
character)
– If msgtype is 0 (or NULL): Reads the first received message in the queue
• The fifth argument, msgflg, indicates certain flags such as IPC NOWAIT (returns immediately
when no message is found in the queue or MSG NOERROR (truncates the message text if more
than msgsz bytes)
msgctl The system call performs control operations of the message queue
#include <s y s / t y p e s . h>
#include <s y s / i p c . h>
#include <s y s /msg . h>
12
2.7 Mapped memory 2 PROGRAMMING INTERFACE
• The first argument, msgid, recognizes the message queue i.e., the message queue identifier. The
identifier value is received upon the success of msgget()
• The second argument, cmd, is the command to perform the required control operation on the
message queue. Valid values for cmd are
The mmap() function asks to map length bytes starting at offset offset from the file (or other object)
specified by the file descriptor fd into memory, preferably at address start. If start is NULL, then
the kernel chooses the (page-aligned) address at which to create the mapping; this is the most portable
method of creating a new mapping. (In the case that start is not NULL, you can read more details in
https://man7.org/linux/man-pages/man2/mmap.2.html).
The prot argument is used to determine the access permissions of this process to the mapped memory.
The available options for prot are as below.
The flags argument is used to control the nature of the map. The following are some common options
of flags.
Flag Description
MAP SHARED This flag is used to share the mapping with all other processes, which are mapped
to this object. Changes made to the mapping region will be written back to the file.
MAP PRIVATE When this flag is used, the mapping will not be seen by any other processes, and the
changes made will not be written to the file.
MAP ANONYMOUS This flag is used to create an anonymous mapping. Anonymous mapping means the
/ MAP ANON mapping is not connected to any files. This mapping is used as the basic primitive
to extend the heap.
MAP FIXED When this flag is used, the system has to be forced to use the exact mapping address
specified in the address If this is not possible, then the mapping will fail.
13
2.8 POSIX Thread (pthread) library 2 PROGRAMMING INTERFACE
The pthread create() function starts a new thread in the calling process. The new thread starts
execution by invoking start routine(); arg is passed as the sole argument of start routine().
The new thread terminates in one of the following ways:
It calls pthread exit(), specifying an exit status value that is available to another thread in the same
process that calls pthread join().
It returns from start routine(). This is equivalent to calling pthread exit() with the value supplied in
the return statement.
It is canceled (see pthread cancel()).
Some basic routines are available in pthread library:
• pthread create()
• pthread join()
• pthread exit()
Master
pthread_create() pthread_join()
thread
Worker
thread
DO WORK pthread_exit()
Worker
thread
struct s t u d e n t t {
char∗ name ;
int s i d ;
};
void ∗ p r i n t i n f o ( void ∗ i n p u t ) {
p r i n t f ( ”name : %s \n” , ( ( struct s t u d e n t t ∗ ) i n p u t ) ->name ) ;
p r i n t f ( ” s t u d e n t ID : %d\n” , ( ( struct s t u d e n t t ∗ ) i n p u t ) ->s i d ) ;
}
int main ( ) {
struct s t u d e n t t ∗ John = ( struct s t u d e n t t ∗ ) m a l l o c ( s i z e o f ( struct s t u d e n t t ) ) ;
char jname [ ] = ” John ” ;
14
2.8 POSIX Thread (pthread) library 2 PROGRAMMING INTERFACE
pthread t tid ;
p t h r e a d c r e a t e (& t i d , NULL, p r i n t i n f o , ( void ∗ ) John ) ;
p t h r e a d j o i n ( t i d , NULL ) ;
return 0 ;
}
15
3 PRACTICES
3 Practices
3.1 Practice 1: Create process
Recall the experiment of creating a process with additional IO waiting
return 0 ;
}
$ ./ hello wait
$ l s / p r o c/<pid>
return 0 ;
}
16
3.3 Practice 3: Examine the process memory regions 3 PRACTICES
int g l o i n i t d a t a = 9 9 ;
int g l o n o n i n i t d a t a ;
p r i n t f ( ” P r o c e s s ID = %d\n” , g e t p i d ( ) ) ;
p r i n t f ( ” A d d r e s s e s o f t h e p r o c e s s : \ n” ) ;
p r i n t f ( ” 1 . g l o i n i t d a t a = %p\n” , & g l o i n i t d a t a ) ;
p r i n t f ( ” 2 . g l o n o n i n i t d a t a = %p\n” , & g l o n o n i n i t d a t a ) ;
p r i n t f ( ” 3 . p r i n t f u n c ( ) = %p\n” , & f u n c ) ;
p r i n t f ( ” 4 . l o c a l d a t a = %p\n” , & l o c a l d a t a ) ;
}
17
3.3 Practice 3: Examine the process memory regions 3 PRACTICES
int main ( ) {
func ( 1 0 ) ;
while ( 1 )
usleep (0);
}
$ ./ multivar
P r o c e s s ID = 1429
Addresses of the p r o c e s s :
1. g l o i n i t d a t a = 0 x601058
2 . g l o n o n i n i t d a t a = 0 x601068
3 . p r i n t f u n c ( ) = 0 x40060d
4. local data = 0 x7ffe08c57f68
18
3.4 Practice 4: Inter Process Communication 3 PRACTICES
Results Notify the empty value of reader process output. Try to self explain based on the ordering of
process execution to see something wrong when we execute the writer before the redear. Revese the order
of reader/writer execution and see the updated result.
In this section, we implement 2 separated program called ”reader.c” and ”writer.c”. These two programs
are implemented in different source code files and have 2 main function. During their execution, we can
get 2 different Pid and process information.
The purpose of this experiment is providing an illustration of the shared information through a shared
memory region where each process can access separately. With a correct setting, we can transfer a message
”hello world” between the two process.
Implement of message transferring we implement the writer process which set the value to the
pre-shared memory region obtained by shmget() and shmat(). In the other side, we implement another
process called reader get the value form the same memory region.
The experiment is performed following these steps:
/∗
∗ Reader . c u s i n g the pre - shared k e y SHM KEY 0 x 1 2 3
∗
∗/
i n t main ( i n t a r g c , char ∗ a r g v [ ] ) {
i n t shmid ;
char ∗ shm ;
shmid = shmget (SHM KEY, 1 0 0 0 , 0644 | IPC CREAT ) ;
i f ( shmid < 0 ) {
p e r r o r ( ” shmget ” ) ;
return 1 ;
} else {
p r i n t f ( ” s h a r e d memory ID : %d\n” , shmid ) ;
}
shm = ( char ∗ ) shmat ( shmid , 0 , 0 ) ;
i f ( shm == ( char ∗ ) - 1 ) {
p e r r o r ( ” shmat ” ) ;
exit (1);
}
p r i n t f ( ” s h a r e d memory mm: %p\n” , shm ) ;
i f ( shm != 0 ) {
p r i n t f ( ” s h a r e d memory c o n t e n t : %s \n” , shm ) ;
}
sleep (10);
i f ( shmdt ( shm ) == - 1 ) {
p e r r o r ( ” shmdt ” ) ;
return 1 ;
}
return 0 ;
}
19
3.4 Practice 4: Inter Process Communication 3 PRACTICES
/∗
∗ Writer . c using the pre - shared k e y SHM KEY 0 x 1 2 3
∗
∗/
i n t main ( i n t a r g c , char ∗ a r g v [ ] ) {
i n t shmid ;
char ∗shm ;
shmid = shmget (SHM KEY, 1 0 0 0 , 0644 | IPC CREAT ) ;
i f ( shmid < 0 ) {
p e r r o r ( ” S h a r e d - memory” ) ;
return 1 ;
} else {
p r i n t f ( ” S h a r e d - memory ID : %d\n” , shmid ) ;
}
shm = ( char ∗ ) shmat ( shmid , 0 , 0 ) ;
if ( shm == ( char ∗ ) - 1) {
p e r r o r ( ” shmat ” ) ;
exit (1);
}
p r i n t f ( ” s h a r e d memory mm: %p\n” , shm ) ;
s p r i n t f ( shm , ” h e l l o w o r l d \n” ) ;
p r i n t f ( ” s h a r e d memory c o n t e n t : %s \n” , shm ) ;
sleep (10);
// d e t a c h f r o m t h e s h a r e d memory
i f ( shmdt ( shm ) == - 1 ) {
p e r r o r ( ” shmdt ” ) ;
return 1 ;
}
// Mark t h e s h a r e d s e g m e n t t o b e d e s t r o y e d .
i f ( s h m c t l ( shmid , IPC RMID , 0 ) == - 1 ) {
p e r r o r ( ” shmctl ” ) ;
return 1 ;
}
return 0 ;
}
Step 2 Compile and execute the two program separately. Open one terminal for ”reader.c”
$ gcc - o reader reader . c
$ ./ reader
s h a r e d memory ID : 196608
s h a r e d memory mm: 0 x 7 f 4 5 d 3 a 7 3 0 0 0
s h a r e d memory c o n t e n t :
Aftermath Recognize the different mm address of the different process since they are different program
and hence, the local stack variable is placed in different layout. But by leveraging the shared memory
technique, they ”magically” can access to the exact same message content. Verify this result or further
investigating by changing the message content and complete the experiment.
20
3.4 Practice 4: Inter Process Communication 3 PRACTICES
We reproduce the same experiment in shared memory section except that the message is transferred
using Message Queue in which it does not explicitly allocate memory to store the variable. Instead, it
provides two basic operators called ”send” and ”receive” and the rest of data transferring mechanism is
held by the system. Therefore, in this section, we use the two programs associated with their operation
of sending/receiving and name ”msgsnd.c”/”msgrcv.c”
Implement of message transferring we implement the writer process which set the value to the
pre-shared memory region obtained by shmget() and shmat(). In the other side, we implement another
process called reader get the value form the same memory region.
The experiment is performed following these steps:
/∗
∗ Filename : msgsnd . c
∗/
#d e f i n e PERMS 0644
#d e f i n e MSG KEY 0 x123
s t r u c t my msgbuf {
long mtype ;
char mtext [ 2 0 0 ] ;
};
i n t main ( void ) {
s t r u c t my msgbuf b u f ;
i n t msqid ;
int len ;
k e y t key ;
s y s t e m ( ” t o u c h msgq . t x t ” ) ;
21
3.5 Practice 5: Create thread using Pthread library 3 PRACTICES
/∗
∗ Filename : msgrcv . c
∗/
#d e f i n e PERMS 0644
#d e f i n e MSG KEY 0 x123
s t r u c t my msgbuf {
long mtype ;
char mtext [ 2 0 0 ] ;
};
i n t main ( void ) {
s t r u c t my msgbuf b u f ;
i n t msqid ;
int toend ;
k e y t key ;
for ( ; ; ) { /∗ n o r m a l l y r e c e i v i n g n e v e r e n d s b u t j u s t t o make c o n c l u s i o n
/∗ t h i s p r o g r a m e n d s w u t h s t r i n g o f end ∗/
if ( msgrcv ( msqid , &buf , s i z e o f ( b u f . mtext ) , 0 , 0 ) == - 1 ) {
p e r r o r ( ” msgrcv ” ) ;
exit (1);
}
p r i n t f ( ” r e c v d : \”% s \”\ n” , b u f . mtext ) ;
t o e n d = s t r c m p ( b u f . mtext , ” end ” ) ;
i f ( t o e n d == 0 )
break ;
}
p r i n t f ( ” m e s s a g e queue : done receiving m e s s a g e s . \ n” ) ;
s y s t e m ( ”rm msgq . t x t ” ) ;
return 0 ;
}
Step 2 Compile and execute the two program separately. Open one terminal for ”reader.c”
$ g c c - o msgsnd msgsnd . c
$ . / msgsnd
message queue : ready t o send message
Enter l i n e s o f t e x t , ˆD t o q u i t :
Results The input messages are sent from the msgsnd to the msgrcv.
Aftermath In this experiment, we have only one-way direction from msgsnd to msgrcv and this mode
is officially called HALF-DUPLEX communication. There is another mode which support 2-way direction
communication which we temporarily leave it to the exercise section.
22
3.5 Practice 5: Create thread using Pthread library 3 PRACTICES
Step 1 : We implement a program using Pthread to create 2 threads. Since the execution of these
threads will be terminated at the end of the passed function, we insert an I/O waiting with getc() to
keep them alive. Implement the source code of the program ”hello thread.c” as follows:
#include <s t d i o . h>
#include <p t h r e a d . h>
void ∗ f c o u n t ( void ∗ s i d ) {
int i ;
f o r ( i = 0 ; i < MAX COUNT; i ++) {
count = count + 1 ;
}
p r i n t f ( ” Thread %s : h o l d i n g %d \n” , ( char ∗ ) s i d , count ) ;
getc ( stdin ) ;
}
count = 0 ;
/∗ C r e a t e i n d e p e n d e n t t h r e a d s each o f which w i l l e x e c u t e f u n c t i o n ∗/
p t h r e a d c r e a t e ( &thread1 , NULL, &f c o u n t , ” 1 ” ) ;
p t h r e a d c r e a t e ( &thread2 , NULL, &f c o u n t , ” 2 ” ) ;
// Wait f o r t h r e a d t h 1 f i n i s h
p t h r e a d j o i n ( thread1 , NULL ) ;
// Wait f o r t h r e a d t h 1 f i n i s h
p t h r e a d j o i n ( thread2 , NULL ) ;
getc ( stdin ) ;
return 0 ;
}
Step2 Compile and execute the program ”hello thread.c”. In this step, it need to remind that Pthread
is 3rd party library in which it need an explicit declaration of library usage through gcc option -pthread
$ gcc - pthread - o h e l l o t h r e a d h e l l o t h r e a d . c
$ ./ hello thread
H e l l o world
Thread 2 : h o l d i n g 10000
Thread 1 : h o l d i n g 20000
23
3.5 Practice 5: Create thread using Pthread library 3 PRACTICES
$ ps a u x f | g r e p h e l l o t h r e a d
oslab 3314 ... \ ./ hello thread
oslab 3353 ... \ g r e p - - c o l o r=auto h e l l o t h r e a d
Results In this experiment, we expect to see that there is only one process hello thread but it is existed
two execution instances with the 3 different printing messages (remember the 3 messages of ”Hello World”,
”Thread1:...”, ”Thread2*...”. Verify this output and complete the experiment.
24
4 EXERCISE
4 Exercise
4.1 Problem1
Firstly, downloading two text files from the url: https://drive.google.com/file/d/1fgJqOeWbJC4gh
MKHkuxfIP6dh2F911-E These file contains the 100000 ratings of 943 users for 1682 movies in the following
format:
userID <tab> movieID <tab> rating <tab> timeStamp
userID <tab> movieID <tab> rating <tab> timeStamp
...
Secondly, you should write a program that spawns two child processes, and each of them will read a
file and compute the average ratings of movies in the file. You implement the program by using shared
memory method.
4.2 Problem2
Given the following function:
sum(n) = 1 + 2 + . . . + n
This is the sum of a large set including n numbers from 1 to n. If n is a large number, this will take
a long time to calculate the sum(n). The solution is to divide this large set into pieces and calculate the
sum of these pieces concurrently by using threads. Suppose the number of threads is numThreads, so
the 1st thread calculates the sum of {1, n/ numThreads }, the 2 nd thread carries out the sum of {n/
numThreads +1, 2n/ numThreads }, . . .
Write two programs implementing algorithm describe above: one serial version and one multi-thread
version.
The program takes the number of threads and n from user then creates multiple threads to calculate
the sum. Put all of your code in two files named ”sum serial.c” and ”sum multi-thread.c”. The number of
threads and n are passed to your program as an input parameter. For example, you will use the following
command to run your program for calculating the sum of 1M :
$ . / s u m s e r i a l 1000000
$ . / s u m m u l t i t h r e a d 10 1000000
(#numThreads=10)
Requirement: The multi-thread version may improve speed-up compared to the serial version. There
are at least 2 targets in the Makefile sum serial and sum multi-thread to compile the two program.
4.3 Problem3
Conventionally, message queue in the practice is used in a one-way communication method. However, we
still can have some tricks to adapt it for two-way communication by using multi-thread mechanism.
4.4 Problem4
Use mmap to implement the mapping created file into local address space. After the address range
mapping, use it as a demonstration for data sharing between the two processes.
25
Revision History 4 EXERCISE
Revision History
26