Professional Documents
Culture Documents
ISAS 02 Tread On Linux
ISAS 02 Tread On Linux
(ISAS)
OPERATION SYSTEMS
Written by:
Faculty:
Mrs. Aisah Rini Susanti, S.T., M. Kom.
Class:
1CC1
Thank you, the writer wishes to Allah SWT, the Almighty God for His blessings and mercy,
that we can complete this ISAS task, both in the form of paper and in the form of presentations in a
timely manner. Thank you especially for our faculty, Ivan Firdaus S.T who always help. Thank you
to the fellow students who have supported, and also thank you for being a partner in this education
at CCIT-FTUI.
The ISAS Paper entitled “Thread on Linux OS” which the authors propose as ISAS's 2020
task requirements.
Hope the author, hopefully this paper can be useful for the reader, so that it can add
knowledge and insight. The author realizes that this paper is not perfect. Therefore, the authors
really expect all constructive suggestions and criticisms from the reader to improve this paper.
Finally, hopefully this paper can provide many benefits for the readers.
Author
i
TABLE OF CONTENTS
PREFACE................................................................................................................................................i
TABLE OF CONTENTS........................................................................................................................ii
CHAPTER I INTRODUCTION.............................................................................................................1
1.1 Background..............................................................................................................................1
1.2 Writing Objective.....................................................................................................................1
1.3 Problem Domain.......................................................................................................................1
1.4 Writing Methodology...............................................................................................................1
1.5 Writing Framework..................................................................................................................2
CHAPTER II BASIC THEORY.............................................................................................................3
2.1 Thread.......................................................................................................................................3
2.2 Thread OS.................................................................................................................................3
2.2.1 Single Threading...............................................................................................................3
2.2.2 Multi Threading................................................................................................................4
2.3 Threading Model......................................................................................................................5
2.4 Multi Threading Model............................................................................................................5
2.4.1 Many to One.............................................................................................................................6
2.4.2 One to One................................................................................................................................6
2.4.3 Many to Many..........................................................................................................................7
2.5 Cancellation..............................................................................................................................7
2.6 Thread Pool..............................................................................................................................7
CHAPTER III PROBLEM ANALYSIS.................................................................................................9
3.1 Tread on Linux.........................................................................................................................9
3.2 Tread Creating..........................................................................................................................9
3.3 SMP (Symetric Multiprosesing).............................................................................................12
3.4 Kernel on Linux......................................................................................................................13
3.5 Examples Thread on Linux....................................................................................................13
CHAPTER IV CONCLUSION AND SUGGESTION.........................................................................15
4.1 CONCLUSION......................................................................................................................15
4.2 SUGGESTION.......................................................................................................................15
BIBLIOGRAPHY.................................................................................................................................16
APPENDIX of IMAGE........................................................................................................................17
APPENDIX of TABLE.........................................................................................................................17
ii
CHAPTER I
INTRODUCTION
1.1 Background
Due to technological developments in the field of operating systems, many companies are also
competing in creating operating systems with various feature innovations. One of them is the open
source Linux OS. Linux is an operating system based on free software and open source that uses the
Linux kernel. Typically, Linux is packaged in a form known as a Linux distro (distribution) for
desktop (PC) and server use. The main component of a Linux distribution is the Linux kernel, an
operating system kernel first released on 17 September 1991, by Linus Torvalds.
The operating system is a program that monitors and regulates the use of computer resources and
bridges the user / user with computer hardware, which in the operating system has many components
that play a role in it. One of the components in it is process and thread (Irfan, 2015[1]).
This is the background of the making of this paper, namely to explore the content contained in the
process and thread. With the aim that students can more or less understand what is a process and what
is a thread on a Linux OS.
1.4Writing Methodology
The writing method that applied by author is quite simple that the way is. Research, discuss and
analyze all of the information materials. Search all of the information from official and reliable
sources.
1
1.5 Writing Framework
To be more focused, then the discussion will be organized with a systematics writing as below:
Chapter I Introduction
This chapter describes the background of the problem, the problem boundary, the purpose of the
writing, the writing methodology used, and the systematic writing of the paper.
This chapter describes what thread is, what thread OS is, what threading model and multi-
threading model is, what cancellation is, and what thread pool is.
This chapter has more in depth discussion of what thread on Linux is, and how to create or test a
thread, and also the examples thread on Linux.
This chapter contains the conclusions of the authors based on the experience gained after doing
research and getting useful suggestions from various sources.
2
CHAPTER II
BASIC THEORY
2.1 Thread
Thread is a control flow of a process. Threads are divided into two types, namely Single Thread
and multithreaded. Judging from its utility, multithread is the most widely used thread these days. A
multithreaded process contains several different control flows with the same address space. The
advantages of multithreaded include increased user responsiveness, sharing of process resources,
economy, and the ability to take advantage of multiprocessor architectures. A user level thread is a
thread that is visible to the programmer and is not recognized by the kernel. User thread levels are
typically managed by a library of threads in the user space. The thread level kernel is supported and
maintained by the operating system kernel. In general, user-level threads are faster to create and
maintain than kernel threads. There are three different types of models related to user and kernel
threads.
2.2 Thread OS
A thread in an operating system can be interpreted as a set of commands (instructions) that can
be executed parallel to other threads, using the method time slice (when one CPU performs a transfer
from one thread to another) or Multi process (when the threads are executed by different CPUs in one
system).
Threads are actually similar to processes, but the way of sharing resources between processes
with threads is very different. Multiple thread can be implemented parallel to the computer system.
Generally multithreading to do time-slicing (together with time-division multiplex), where a CPU is
working on different threads, where a case is handled not completely synchronously, for a single CPU
basically actually doing a job at one time.
Threads share parts of programs, parts of data and operating system resources with other
threads that refer to the same process. A thread consists of! "Threads, program counters, register set,
and stack." Its multiple control process threads can do more than one job at the same time.
3
Figure 2.1 Single Thread
Advantage
1. Responsive Multi-Threading allows a program to run continuously even
if that part of the program is blocked or is in a state of running long /
long operations. For example, a multithreaded web browser can allow
users to interact with a thread while an image is being loaded by another
thread.
2. Resource sharing by default, threads share memory and resources of
processes. When threads run on the same data, they can share cache
4
memory.
3. Economical Allocating memory and resources to create processes is very
expensive. Alternatively, because threads share the resources of the
process, it is more economical to create thread.
4. Multiprocessor architecture empowerment the advantages of multi-
threading can be enhanced with a multiprocessor architecture, where
each thread can run in parallel on different processors. In single-
processor architectures, the CPU usually switches between threads
quickly, giving the illusion of parallelism, but in reality only one thread
is running at a time.
Disadvantage
1. Multiple threads can interfere with each other when sharing hardware
resources, for example cache memory.
2. The execution time of a single thread cannot be improved, but can be
decreased. This occurs because of a decrease in the frequency required
when there is a change in threads that are running.
3. There must be support from hardware or software for multi-threading.
Kernel-level Threading
Kernel threads are supported directly by the operating system. The creation, scheduling, and
management of threads are carried out by the kernel in the kernel space. The creation and
management of kernel threads is slower than user threads. The advantage is that if a thread runs
a blocking system call, the kernel can schedule other threads in the application to execute. In a
multiprocessor environment, the kernel can schedule threads on different processors, Examples
of operating systems that support kernel threads are:
1. windows NT
2. Solaris
3. Digital Unix
6
2.4.3 Many to Many
This model multiplexes a lot thread levels user to thread the number of kernels that are
few or the same as levels user. this model wants developer make thread as much as that
developer want but concurrency cannot be obtained for only one thread which can be
scheduled by the kernel at any given time. The advantage of this system is that the kernel
thread can run in parallel on the multiprocessor. Model Many to Many shown in the
image below.
2.5 Cancellation
Thread cancellation is the termination of a thread before its task is complete. For example, if
the Java program wants to turn off the Java Virtual Machine (JVM). Before the JVM is turned off, all
running threads must be stopped first. The thread to be terminated is usually called the target thread.
Termination of the target thread can occur in two different ways:
The alternative is to use deffered cancellation. The way deffered cancellation works is by using
a single thread which functions as an indication that the target thread is about to be terminated. But the
stop will only happen if the target thread checks whether it should stop or not. This allows the thread to
check whether it should stop at a time when it can safely be terminated. Thread refers to these as
cancellation points.
7
So when the server receives a request it will wake up the thread from the pool and if the thread is
available then the request will be served. When the thread has finished doing its job it returns to the
pool and waits for other work. If there are no available threads, additional thread pool requests remain
queued until thread pool threads become available.
8
CHAPTER III
PROBLEM ANALYSIS
In the Linux Kernel each process is represented as a unique data structure. So, instead of
Creating a new one, the new data structure contains a pointer that points to where the data is Located.
So when fork () is called, a new process is created along with the duplication of all Contents in the
parent process data structure, but when clone () is called, it does not duplicate the Parent process but
creates a pointer to the parent process data structure which allows the child Process to share the
memory and resources of its parent process. The Linux Thread project uses these system calls to
simulate threads in user space. Unfortunately, this approach has several Drawbacks.
This approach to threads contrasts greatly with operating systems such as Microsoft Windows
or Sun Solaris, which have explicit kernel support for threads (and sometimes call threads lightweight
processes). The name "lightweight process" sums up the difference in philosophies between Linux and
other systems. To these other operating systems, threads are an abstraction to provide a lighter, quicker
execution unit than the heavy process. To Linux, threads are simply a manner of sharing resources
between processes (which are already quite lightweight). For example, assume the author have a
process that consists of four threads. On systems with explicit thread support, there might exist one
process descriptor that in turn points to the four different threads. The process descriptor describes the
shared resources, such as an address space or open files. The threads then describe the resources they
alone possess. Conversely, in Linux, there are simply four processes and thus four normal task_struct
structures. The four processes are set up to share certain resources.
9
The previous code results in behavior identical to a normal fork (), except that the address
space, file system resources, file descriptors, and signal handlers are shared. In other words, the new
task and its parent are what are popularly called threads.
clone(SIGCHLD, 0);
The flags provided to clone () help specify the behavior of the new process and detail what resources
the parent and child will share. Table III.1 list the clone flags, which are defined in <linux/sched.h>,
and their effect.
10
Flag Meaning
CLONE_SIGHAND Parent and child share signal handlers and blocked signals.
CLONE_VFORK vfork() was used and the parent will sleep until the child wakes it.
11
Flag Meaning
12
In order to enhance Linux Thread capabilities, kernel support and thread library rewrites are
required. Two competing projects answer this challenge. A team of developers from IBM Created
NGPT (Next Generation POSIX Threads). While Red Hat's developers created the NPTL (Native
POSIX Thread Library), Linux doesn't really differentiate between processes and Threads. In fact,
Linux uses the term task rather than process and thread when referring to Control flow settings in a
program. Process status on Linux:
Runining
Interruptable
Uninterruptable
Stopped
Zombie
The SMP architecture applies to the cores, treating them as separate processors. In the case of multi-
core processors, SMP architecture applies to cores, treating them as separate processors. Processors
may be interconnected using buses, crossbar switches on-chip mesh networks. Processors may
interconnect using buses, crossbar switches or on-chip mesh networks.
13
3.4 Kernel on Linux
The kernel is a system from Linux, which controls the hardware and forms various low-level
functions, the functions carried out by the kernel, including: single service (system calls) and system
clock, file management and security management, input and output operation services, process
management and scheduling, memory management, performing system accounting activities, handling
interruption errors.
1.) Make sure that the include and id of the thread are included in the code
#include<stdio.h>
#include<string.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
pthread_t tid[2];
Note pthread_tid is to create a thread id
2.) Create a thread at the core of the program (int main) using the pthread_create function
pthread_create (& (tid [i]), NULL, & doSomeThing, NULL);
Note:
● tid [i] is the thread id created in step 1
● do Some Thing is a function executed by a thread
3.) Make sure the thread has a function that is performed
14
void* doSomeThing(void *arg) {
unsigned long i = 0;
pthread_t id = pthread_self();
if(pthread_equal(id,tid[0])) {
printf("\n First thread processing\n");
}
else {
printf("\n Second thread processing\n");
}
for(i=0; i<(0xFFFFFFFF);i++);
return NULL;
}
Note: this function corresponds to step 2. This function is passed to pthread_create
4) If you want between threads to know each other (one thread knows there is another thread, so that
the process does not get an error) use the pthread_join function
pthread_join(th1, NULL);
pthread_join(th2, NULL);
Note: For example, if there are two threads named th1 and th2 that will be joined.
15
CHAPTER IV
CONCLUSION AND SUGGESTION
4.1 CONCLUSION
Thread is a control flow of a process. Threads are divided into two types, namely Single
Thread and multithread. Judging from its usefulness, multithread is the most widely used
thread lately. A multithreaded process contains several different control flows with the same
address space. The advantages of multithreaded include increased user responsiveness,
sharing of process resources, economics, and the ability to take advantage of multiprocessor
architectures User-level threads are threads that are visible to the programmer and are not
recognized by the kernel. User-level threads are typically managed. By the operating system
kernel, in general, thread-level users are faster to create and maintain than kernel threads.
There are three different types of models related to user and kernel threads.
Many to one model: maps multiple user level threads to only one kernel thread
One to one model: maps each user thread to one kernel thread, ending
Many to many model: allows development to create as many user threads as possible,
concurrency cannot be achieved because only one thread can be scheduled by the
kernel at a time
When it was first developed, Linux was not supported with threading in its kernel, but it did
support processes as entities that could be scheduled via clone () system calls. Linux now
Supports duplicating processes using the clone () and fork () system calls. Then there is
Symmetric multiprocessing which involves a multiprocessor computer hardware architecture
where two or more identical processors are connected to one shared main memory and
controlled by a single OS. and the linux kernel, The kernel is a system from Linux, which
controls hardware and forms a variety of low-level functions, the functions performed by the
kernel include: single service (system calls) and system clock, file management and security
management, etc.
4.2 SUGGESTION
16
BIBLIOGRAPHY
Engineering, Dayanu Ikhsanudin University, Bau Bau, South East Sulawesi. [29/11/2020]
https://www.academia.edu/30582405/
[3]
MAKALAH_SISTEM_OPERASI_THREAD_Pengertian_Thread [27/11/2020]
https://vivimargaretha494.wordpress.com/2015/10/01/thread-pada-sistem-operasi-2/
[4]
[27/11/2020]
http://lutfiprod.blogspot.com/2017/10/contoh-thread-pada-os-linux.html [28/11/2020]
[5]
https://studylibid.com/doc/723204/perbedaan-threads--smp--kernel-di-windows-dan-linux
[6]
[28/11/2020]
17
APPENDIX of IMAGE
APPENDIX of TABLE
Table III.1 List the clone flags...........................................................................................................11
18