Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Análisis Parallel and Distributed Programming with Pthreads and Rthreads

Presentado por:
Luigi Davian Patiño Cardona
Cod: 1093539823

Presentado a:
Cesar Jaramillo Acevedo

Materia:
Sistemas Distribuidos

Ingeniería de Sistemas, UTP


30/09/2021
Introduction
The threads differ from the processes in that the former share the, the same resources of the
program that contains them, as long as the processes have their code, as well as their data,
separately. Strands of two types of flow can be identified:

56 Distributed systems

• Single stream: In this case, a program uses only one thread to control its execution.

• Multiple flow: These are programs that use various contexts of execution to get your job done.

In a multithreaded system, each task starts and ends as soon as

possible, this facilitates data entry into systems in real time, especially if this data comes from
different sources. In a multithreaded program you have the main thread of the program running,
who in turn it has other threads or parallel tasks running. A thread is defined as a single flow control
sequence within a program, in a program can have more than one control sequence or threads. A
thread it is a part of the program that runs independently of the rest.

Thread is the smallest unit of code that can be executed in a multitasking environment. The use of
threads allows the programmer to write more efficient programs, since threads allow optimizing
resources so important as better CPU performance by minimizing its downtime. inactivity. The use
of threads is very valuable in interactive network environments, since they allow to synchronize the
difference between the transmission speed of the network with the processing of the CPU. The
speed in the handling of the file system for reading and writing is slower compared to the speed of
processing of this data by the CPU, in this case the use of threads helps a lot. One of the important
reasons for the study of multithreaded scheduling is that it allows access to time resources Free of
CPU while performing other tasks. The figure schematically illustrates a one-wire program and a
two-wire program.

Process / thread comparison


A process is an instance of a program that is running. Each instance has its own memory
space and execution state. The OS keeps track of PIDs and corresponding statuses and uses
this information to allocate and manage system resources. A process has a memory space and
has at least one flow of control called a thread.
When a program runs, the value of the program counter determines which instruction will be
executed next. The resulting sequence of instructions is called a thread of execution.
A natural extension of the process model is to allow multiple threads to run within the same
process. Multiple threads avoid making context switches and allow code and data sharing. A
thread is an abstract data type that represents a thread of execution within a process. A thread
has its own execution stack, program counter, recordset, and status. Since the same process
memory space is shared between several threads of the same process, synchronization
mechanisms are required. The threads are sometimes called lightweight processes.
Create a thread
Synopsis:
#include <pthread.h>
int pthread_create (pthread_t * threadID, const pthread_attr_t * attr, void * (* start_routine)
(void *), void * arg);
Thread ID is the identifier for that thread.
attr is used to indicate attributes for the thread. If we use null, the new thread will take the
default attributes.
start_routine is the name of the function that will be called when the thread begins its
execution. This function has a single parameter specified by arg. The called function must
return a pointer to void, which is interpreted as the term status by pthread_join.
Detaching and Joining
#include <pthread.h>
int pthread_detach (pthread_t thread);
When a thread terminates (pthread_exit), it does not liberate its resources unless it is detached
(detach).
This function specifies that the storage of a thread can be reclaimed by the system when it
finishes. A detached thread does not report its status when it ends. A non-detached thread
can be reunited (we wait for its termination) and not return all its resources until another
thread calls pthread_join or the entire process exits (exit).
int pthread_join (pthread_t thread, void ** value_ptr);
This function suspends the calling thread until the argument thread ends. value_ptr provides
the location of the pointer to the return status sent by the thread with pthread_exit or return.
Term and cancellation
A process can end up calling exit or calling return from main or when some thread in the
process calls exit.

void pthread_exit (void * value_ptr);

value_ptr must point to a data that exists after the end of the thread.

One thread can force another to return through its cancellation.


int pthread_cancel (pthread_t thread);
this call requests the cancellation of another thread. this call is not blocking.
If a thread receives a cancellation and is in the PTHREAD_CANCEL_DISABLE state, the
cancellation is pending. IF its status is PTHREAD_CANCEL_ENABLE the thread will be
canceled.

A thread may or may not enable cancellation by calling:


int pthread_setcacelstate (int state, int * oldstate);

Overview of Rthreads
In operating systems, a thread or thread, thin process or thread is a sequence of very small,
chained tasks that can be executed by an operating system.
The destruction of old threads by new ones is a feature that does not allow an application to
multitask (concurrently). The different threads share a series of resources such as memory
space, open files, authentication status, etc. This technique allows you to simplify the design
of an application that must carry out different functions simultaneously.
A thread is simply a task that can be executed at the same time as another task.
Threads of execution that share the same resources, added to these resources, are collectively
known as a process. The fact that the execution threads of the same process share the
resources means that any of these threads can modify these resources. When one thread
modifies data in memory, the other threads access that data immediately.
What is specific to each thread is the program counter, the execution stack, and the state of
the CPU (including the value of the registers).
The process continues to run if at least one of its threads is active. When the process finishes,
all your threads have also ended. Likewise, now when all the execution threads end, the
process no longer exists, and all its resources are freed.
Some programming languages have design features expressly created to allow programmers
to deal with threads of execution (such as Java or Delphi). Others (the majority) are unaware
of the existence of threads and these must be created special library calls that depend on the
operating system in which these languages are being used (such as C and C ++).
Advantages of using threads
It takes less time to create a thread from an existing task than to create a new process.
· It takes less time to finish a thread than to finish a process.
· It takes less time to switch between two threads of the same task than to switch between
two processes (because resources do not change, for example)
· Communication (message passing for example) between threads of the same task is easier
than between different processes.
· When switching from one process to another, the operating system kernel has to intervene
for protection. When switching from one thread to another, since the resource allocation is
the same, there is no need for the operating system to intervene.

Process and thread completion


Animated spirit of a show it is a specific execution of a program, with a specific path and a
specific value of its variables.
Two threads of the same task (called peer threads) share the code segment, the data segment,
and a stack space, that is, the resources assigned to the task. We can grasp the functionality
of threads by comparing multi-threaded control with multi-process control. In the case of
processes, each one operates independently of the others; each process has its own program
counter, stack register, and address space.
When a thread is running, it has access to all resources assigned to the task.

You might also like