Parallel Programming using Threads

An Operating System requires numerous tasks to be performed at the same time. The ‘Parallel Processing’ is the term which refers to the technique of executing many tasks at the same time. However actual parallel processing is not possible in commonly available computers, but they can simulate such an effect by high speed switching of small amount of CPU time to each of the process that need to be executed. In Operating Systems this process is called ‘Scheduling’ and it helps to achieve the multitasking in an Operating System.

The Raspberrypi is a microcontroller board which is powerful enough to run large operating systems like Linux, Mac and Windows. The Raspberrypi is a microcontroller board which runs on a SoC chip from the Broadcom with ARM11 processor at the core. The Board is a mini computer itself without any input or output devices but ports provided to connect them. The Raspberrypi is called a mini-computer because the SoC has the powerful ARM11 processor which runs on 700 MHz at its core and having the peripherals like timers, interrupt controller, GPIO, PCM / I2S, DMA controller, I2C, SPI slave, PWM, UART, USB, graphical processing unit (GPU) which includes VideoCore, MPEG-2 and MPEG-4 and a 512 MB SDRAM. There is an Ethernet port which can be used to connect the board to a computer network.

Parallel Programming using Threads

In this project the Raspberrypi board is loaded with Ubuntu and is remotely accessed using VNC. The Raspberrypi board is also connected to the internet. There are 26 connectors which can be taken out from the connector port of the Raspberrypi board. All the connector pins are taken out using 13*2 pin female connectors and at the other end of their wire 26 pin Burg stick male connectors are attached. The Burg stick male connectors allow each pin out from the Raspberrypi board to be plugged into the holes of a breadboard.  To access the pins that coming out of the Broadcom controller of the Raspberrypi board using C language, a C library is available called “bcm2835” which has been downloaded and installed.

The term ‘Process’ refers to the code which is currently in execution and the term ‘thread’ refers to the small part of that code which the CPU will execute at a particular time. The fork () and execve () functions simply creates a new process from one particular process which then starts executing individually. However threading is a technique which makes different parts of the same process to execute in parallel. The following section discusses how to create a multi-threading code for blinking 8 LEDs for a Raspebrrypi board in such a way that they are being operated parallel.

There are eight general purpose IO pins on the 13*2 pin connectors of the Raspberrypi board and to each one of them a LED is connected through 1K resistor. Separate code can be written to blink the LEDs individually and made them into executable files named blink2, blink3, blink4, blink5, blink6, blink7 and blink8. The user can run any of the LED blinking programs from the command line. For example to execute the file ‘blink1’, the user can use the following command:

#include <bcm2835.h>
#include <pthread.h>
#include <unistd.h>
#define PIN1 RPI_GPIO_P1_11
#define PIN2 RPI_GPIO_P1_12
#define PIN3 RPI_GPIO_P1_24
#define PIN4 RPI_GPIO_P1_15
#define PIN5 RPI_GPIO_P1_16
#define PIN6 RPI_GPIO_P1_18
#define PIN7 RPI_GPIO_P1_22
#define PIN8 RPI_GPIO_P1_07
 void blink1 ( void );
void blink2 ( void );
void blink3 ( void );
void blink4 ( void );
void blink5 ( void );
void blink6 ( void );
void blink7 ( void );
void blink8 ( void );
 int main()
{    pthread_t th1, th2, th3, th4, th5, th6, th7, th8;
     if (!bcm2835_init())
        return 1;
     pthread_create ( &th1, NULL, ( void* ) blink1, NULL );
    pthread_create ( &th2, NULL, ( void* ) blink2, NULL );
    pthread_create ( &th3, NULL, ( void* ) blink3, NULL );
    pthread_create ( &th4, NULL, ( void* ) blink4, NULL );
    pthread_create ( &th5, NULL, ( void* ) blink5, NULL );
    pthread_create ( &th6, NULL, ( void* ) blink6, NULL );
    pthread_create ( &th7, NULL, ( void* ) blink7, NULL );
    pthread_create ( &th8, NULL, ( void* ) blink8, NULL );
     while ( 1 );        bcm2835_close();
     return 0;
} void blink1 ( void )
{    // Set the pin to be an output
    bcm2835_gpio_fsel(PIN1, BCM2835_GPIO_FSEL_OUTP);
    // Blink
    while (1)     {        // Turn it on
        bcm2835_gpio_write(PIN1, HIGH);
                // wait a bit
                // turn it off
        bcm2835_gpio_write(PIN1, LOW);
                // wait a bit        bcm2835_delay(500);    }}
 void blink2 ( void ) {    // Set the pin to be an output
    bcm2835_gpio_fsel(PIN2, BCM2835_GPIO_FSEL_OUTP);

© 2015 Powered By Engineering Projects Team, Raspberry Pi Projects

Scroll to top