Wednesday, May 31, 2017

Posted by beni in , , , | May 31, 2017
507 A massa fermentada507 A massa fermentadaPara salvar o arquivo mp3 clique com o bot�o direito do mouse sobre o t�tulo da postagem e salve o arquivo. Os v�deos podem ser encontrados em www.3minutos.net Acompanhe no iTun...
Posted by beni in , , , , , , | May 31, 2017
A confusion Knowledge Discovery or Data MiningAlthough there have been many practical methods developed and used in data mining, the distinction between data mining and knowledge  discovery concepts are not clear yet.The most critical starting point to extinguish this confusion is to summarize the basic concepts about data mining and knowledge discovery.--Knowledge discovery is a non-trivial...
Posted by beni in , , , , , , | May 31, 2017
5 Cara Simpel Memotret Makanan yang InstagramableBudaya tidak hanya sekedar makan kian menjamur di era digital. Terutama dengan hadirnya ponsel, kemudahan dalam mengabadikan gambar semakin merambah ke semua kalangan. Makan tidak lagi hanya sekedar makan. "Makan juga harus cantik" kata sebagian food...
Posted by beni in , , , , , | May 31, 2017
A New Beginning Final Cut PCDescri��o:A New Beginning Final Cut � uma aventura cinematogr�fica thriller feito graphic novel estilo. Nesta aventura encantadora e inteligente, a terra est� � beira do cataclismo clima iminente. � essencial para viajar o mundo a fim de poupar a humanidade e salvar o todo...
Posted by beni May 31, 2017
n=""fals"a,best,internet,download,manager,alternat...
Posted by beni in , , , , | May 31, 2017
5 Anime Fall 2015 Terbaik5 Anime Fall 2015 TerbaikYo guys, balik lagi sama ane, udah lama gak ngeblog karena Laptop sempet rusak + Internet kuota habis + Males... ya... Males...Ok, saya akan memberikan List Anime terbaik berdasarkan Pendapat saya + Myanimelist + Anime Trending +...

Tuesday, May 30, 2017

Posted by beni in , , , , , , | May 30, 2017
5 Top Gamers Kaya Karena Bermain GameBanyak orang menganggap bahwa bermain game dapat merusak hidup dan kesehatan kita, Namun orang hanya melihat dari segi negatifnya saja. Game juga bisa menjadikan kita kaya dan mendapatkan kedudukan tinggi karena game tidak sepenuhnya merusak dan tidak juga sepenuhnya...
Posted by beni in , , , , , , , , , , , | May 30, 2017
A Complete Guide on SEO Search Engine Optimizer Tips Tricks Padsa InformationSEO IntroductionSearch Engine Optimization (SEO) is the activity of optimizing web pages or whole sites in order to make them search engine friendly, thus getting higher positions in search results.This tutorial explains simple...
Posted by beni in , , , , , , , , , , | May 30, 2017
5 best Password managers solution to forgot and hacking of passwordsYouve got enough to keep track of in your day-to-day life without filling your head with the countless logins and passwords youve racked up over the years, and the Post-It note on your monitor just isnt an option. Luckily, there are...
Posted by beni in , , , , | May 30, 2017
A cunning plan in retrospect!Did you notice that the instance we are currently considering "proper content" is in the frozen cold north? With lots of frost attacks and cold things and ice and such?Even though it completely ruins immersion - it really makes me want to go the...
Posted by beni in , , , , | May 30, 2017
A Gameboy powered by SteamSomeone needs to tell this chap how batteries work. With time on his hands and his knowledge of steam, this guy decided to power his Gameboy via a steam. Why? Because he can, thats why....
Posted by beni in , , | May 30, 2017
A BUSINESS MANEven tho SWGP literally killed himself when he decided to turn out his website into hackers lair I believed that one day he might try to make a comeback. My belief was based on the fact that he never actually admitted that used cheats or spambots to ruin the game for other players....
Posted by beni in , , , , , , | May 30, 2017
A Little Bit of Genius at PAX 0 false 18 pt 18 pt 0 0 false false false /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt;...
Posted by beni in , , , , , , , , , , | May 30, 2017
6 Best Multiplayer Android Games you can play with your FriendsHere you find some mind blowing Android Multiplayer games that change your mind , feel free while playing this games.You interact with your friends through the facebook,the iPhone and from other platform where the game exists.This games...
Posted by beni in , , , , | May 30, 2017
566 Falsos apostolos e profetas566 Falsos ap�stolos e profetasPara salvar o arquivo mp3 clique com o bot�o direito do mouse sobre o t�tulo da postagem e salve o arquivo. Os v�deos podem ser encontrados em www.3minutos.net Acompanhe no iTun...
Posted by beni May 30, 2017

5 Pthread Mutex in Linux


Mutex is used to synchronize the thread.

What would be the final value of globalCount in below program?


#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *add1_fun(void* arg);
void *add2_fun(void* arg);
int globalCount = 0;
 
void *add1_fun(void* arg) //thread handler
{
    int t_num = (int)arg;
    int i = 0;
    printf("Thread %d created and running ", t_num);
    for (i=0; i<50000; i++){
        globalCount++;
        }
    printf("Thread %d finishes the work ", t_num);
    pthread_exit(NULL);
}
 
void *add2_fun(void* arg) //thread handler
{
    int t_num = (int)arg;
    int i = 0;
    printf("Thread %d created and running ", t_num);
    for (i=0; i<50000; i++){
                globalCount++;
        }
    printf("Thread %d finishes the work ", t_num);
    pthread_exit(NULL);
}
 
int main(int argc, char *argv[])
{
    pthread_t mythread1;
        pthread_t mythread2;
    pthread_attr_t myattr;
        void *joinResult;
    int x = 0;
    int t_arg = 1;
       
    pthread_attr_init(&myattr);
    pthread_attr_setdetachstate(&myattr, PTHREAD_CREATE_JOINABLE);
    if((pthread_create(&mythread1, &myattr, add1_fun,  (void*)t_arg) != 0)){
       
printf("Error, thread not created properly ");
       
return 1;
   
}
        t_arg
= 2;
       
if((pthread_create(&mythread2, &myattr, add2_fun,  (void*)t_arg) != 0)){
       
printf("Error, thread not created properly ");
       
return 1;
   
}
    pthread_attr_destroy
(&myattr);
       
if(pthread_join(mythread1, &joinResult) != 0 ){
       
printf("Error pthread join ");
       
return 1;
   
}
       
printf("Main : Thread1 joined with result of %d ", (int)joinResult);
       
if(pthread_join(mythread2, &joinResult) != 0 ){
       
printf("Error pthread join ");
       
return 1;
   
}
       
printf("Main : Thread2 joined with result of %d ", (int)joinResult);
   
printf("main finishes the work ");
       
       
printf(" Count at end : %d ", globalCount);
    pthread_exit
(NULL);
}

Thread 1 will increment globalCount for 50000 times and Thread 2 as well. So final value of globalCount would be 100000. But actual output is not 100000.

Each time you execute the program you will get different value.

Because two thread execute concurrently. Consider the situation in which globalCount = 10, suppose in same time each thread increment the globalCount so value will be 11 not 12.

Actual output of above program in my PC is
Thread 1 created and running 
Thread 2 created and running
Thread 1 finishes the work
Thread 2 finishes the work
Main : Thread1 joined with result of 0
Main : Thread2 joined with result of 0
main finishes the work

Count at end : 60286


Solution for above problem :
When one thread trying to increment the globalCount we need to lock the variable, then after increment unlock the variable.
This can be establish with the help of mutex API which available in pthread library.;

Steps need for mutex lock and unlock:

  • create a mutex variable of type pthread_mutex_t 
  • lock the globalCount with the help of pthread_mutex_lock API
  • unlock the globalCount when finishes the work with the help of pthread_mutex_unlock

PROGRAM : 
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void

Search