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:
The user can perform multi-tasking on them using the following commands one after the other:
The best method is to use create a thread for each of the above LED blinking code in a single C code and then execute it. The C code should have all the LED blinking codes as separate functions and then allow them to execute as threads when the entire code is executed. The threads can be created in a C code using the function ‘pthread_create ()’.
The function ‘pthread_create ()’ can start execute a function as a separate thread within the main process which calls the ‘pthread_create ()’. The prototype of the function ‘pthread_create ()’ is defined in the header file <pthread.h> as given below:
int pthread_create (
const pthread_attr_t *attr,
void *(*start_routine) (void *),
The third argument ‘start_routine’ in the above prototype is the pointer to a function which the ‘pthread_create ()’ can make the thread. A successful call to pthread_create() stores the ID of the new thread in the buffer pointed to by ‘thread’, which is the first argument.
The second argument ‘attr’ is the attributes that required to create the new thread and if the ‘NULL’ is used the default attributes will be used. The last argument ‘arg’ is the argument that need to be passed to the function which will start execute as a thread.
If the codes for ‘blink1’, ‘blink2’ etc. are written as separate functions in a single C file which are declared as the following:
void blink1 ( void );
void blink2 ( void );
void blink3 ( void );
void blink4 ( void );
void blink5 ( void );
void blink6 ( void );
void blink7 ( void );
void blink8 ( void );
The above functions can be executed as thread from the main code as given below:
pthread_t th1, th2, th3, th4, th5, th6, th7, th8;
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 );
***Note that in this project the latest version of library “bcm2835” is used with an old version of Raspberrypi board. It is not possible to access the pin number 13 of the old board with the latest library version and hence in the code “blink3.c” the pin number 24 is used to blink the 3rd LED. The circuit diagram is also drawn accordingly. Those who have the latest version of the board can use the pin 13 without any trouble.
To find the ‘Revision’ and other important details about the Raspberrypi board, use the following command: