2015:04:07:threads

De wiki-prog
Aller à : navigation, rechercher

Foreword

Issue your code

Make sure your code respects the following architecture on your git repository:

.
├── BONUS
├── l0
│   ├── code.c
│   └── Makefile
├── l1
│   ├── code.c
│   └── Makefile
├── l2
│   ├── code.c
│    └── Makefile
├── l3
│   ├── code.c
│   └── Makefile
└── README

For the 'challenge' submit you will have to zip your code and makefile For level 1:

zip login_x-l1.zip l1.c Makefile

(Same for level 2)

And then you have to zip your zip files in one other so that you can upload it via http://asm.epita.it (choose any TP and Upload)

zip login_x.zip login_zip-l1.zip login_x-l2.zip

Notes (READ THIS)

  • Your code must compile. You have to submit your Makefile. You learned a lot about Makefiles in the past, you should be able to cope with this.

Subject maintainers

Feel free to contact us with any question if something is unclear.

Threading in C

What is a thread ?

Technically, a thread is defined as an independent stream of instructions that
can be scheduled to run as such by the operating system. But what does this
mean?
To go one step further, imagine a main program (a.out) that contains a number
of procedures. Then imagine all of these procedures being able to be
scheduled to run simultaneously and/or independently by the operating system.
That would describe a “multi-threaded” program

Basically, this is all you need to know but to understand how it works more | deeply we hardly invite you to read [manual]

Basic stuff

pthread_t

pthread_t is a type of the thread’s identifier. Like process have a PID here we will say that a thread have a TID.

On Linux system this id is generally represented by an unsigned long.

Useful man pages

I let you see the man page for this function needed for manipulate thread.

  • pthread_create
  • pthread_exit
  • pthread_cancel
  • pthread_join

Advanced documentation

Exercices

Level 0

This level is mandatory. Failure on this level means that your grade will automaticaly be multiplied by 0.

Your goal is to create a program that will create 42 threads and dislay the following message by each thread:

Hello World! It’s me, thread #n” with n the number of the thread starting from 0.

One more rule: each thread with an even number will be displayed on stdout but each one with an odd number has to be displayed on stderr.

Level 1

On this level you will have to write a threaded factorial function. You are free to implement it however you want however you have to respect some rules:

  • You must have a Makefile
  • Your Makefile must produce an executable named fact
  • Your output must be properly formated:
$ ./fact 1
1
  • Try to have the best perfs. A scoreboard will be displayed somewhere. (http://lokhran.fr).
  • Use threads (Failure to do this means that your grade will be multiplied by -1)
  • Have fun

Note: Values for testing will be between 500 and 700 so be carefull to standart type overflow

Level 2

Level 2 is a little challenge where you have to construct the best threaded sorting algorithm. Rules are very simples:

  • You must have a Makefile
  • Your Makefile must produce an executable named sort
  • Your program takes the list to sort in arguments and has to display it properly:
$ ./sort 1 4 3 42 0
0 1 3 4 42
  • Try to have the best perfs. A scoreboard will be displayed somewhere and the students with the best performance will get bonus points.
  • Use threads

Level 3

To pass this level you have to implement a thread-safe queue.

A queue will be created and multiple threads will append (or remove) elements to it (this is called concurrency).

The functions you have to implement are described in the include/queue.h header file.

Bonux:

These are idea of bonus you can add and would be really appreciated:

  • Multi-threaded chat server
  • Multi-threaded pi calculation
  • Threaded image processing