PHP Code:
#include <sys/ipc.h>
#include <sys/shm.h>
#include <st***.h>
#include <sys/types.h>
#include <stdlib.h>
#include <errno.h>
#include <pthread.h>
/***
* _ _
* (_) (_)
* __ ____ _ _ _____ ___ ___ ___ ___ _ __ ___
* \ \/ / _` | |/ _ \ \ / / |/ _ \ / __/ _ \| '_ ` _ \
* > < (_| | | (_) \ V /| | (_) | (_| (_) | | | | | |
* /_/\_\__, |_|\___/ \_/ |_|\___(_)___\___/|_| |_| |_|
* __/ |
* |___/
*
* Process syncronization between 4 processes
* 2 processes will decrement a variable of 1 unit 50000 times
* Other 2 processes will increment the same variable of 1 unit 50000 times
*
* The result is 0 using mutex in a shared memory.
*
* Typical Output:
* I'm process 1 and i have the control NOW! I'm incrementing the shared variable
* I'm process 2 and i'm waiting to decrement the shared variable..
* I'm process 4 and i'm waiting to decrement the shared variable..
* I'm process 3 and i'm waiting to increment the shared variable..
* I'm process 2 and i have the control NOW! I'm decrementing the shared variable
* I'm process 4 and i'm waiting to decrement the shared variable..
* I'm process 3 and i have the control NOW! I'm incrementing the shared variable
* I'm process 4 and i have the control NOW! I'm decrementing the shared variable
* Numero finale :0
*/
#define SHMSZ 500
#define KEY 9999
int main ( void ) {
int shmid, status, shmdet, shmcontrol,err;
void * shmatt;
int * new_area;
/////////////creazione memoria condivisa
shmid = shmget ( KEY, SHMSZ, IPC_CREAT | IPC_EXCL | 660);
if (shmid == -1) { fprintf (stderr, "Impossibile creare segmento condiviso\n");
err= errno;
fprintf (stderr, "Errore %d\n",err);
exit (-1);}
//////////// attacco memoria condivisa a spazio processo
shmatt = shmat ( shmid, (void *)0 ,0);
if (shmatt == (void *) -1) { fprintf (stderr, "Impossibile modificare spazio indirizzi processo\n");
err= errno;
fprintf (stderr, "Errore %d\n",err);
exit (-2);}
//////////////////////////////// setting uo variables in shared memory
new_area = (int *) shmatt;
int *waiting;
waiting = new_area;
int *shared_variable;
shared_variable = new_area + 4;
*shared_variable = 0;
pthread_mutex_t * x_mute_x; // mutex variable
x_mute_x = (pthread_mutex_t *)(new_area + 5);
pthread_mutex_init(x_mute_x,NULL); // mutex initialized
///////////////////////////////////// begin fork /////////////////////////////////////////////////////////
int i;
int key;
int current,next;
int pid1,pid2,pid3,pid4;
pid1 = fork();
if ( pid1 == 0) {
//precritical section
*(waiting + 0)=1;
key=1;
current = 0;
for (;(*(waiting + 0)) == 1 && key!=0;) {
key = (int) (pthread_mutex_trylock(x_mute_x));
if ( key!=0) {
fprintf(stdout,"I'm process 1 and i'm waiting to increment the shared variable..\n");
sleep(1);
}
}
fprintf(stdout,"I'm process 1 and i have the control NOW! I'm incrementing the shared variable\n");
(*(waiting + 0)) = 0;
////////////////////////////// begin critical section child process
//critical section
for (i=0;i<50000;i++){(*(shared_variable))++;}
///////////////////////////// end critical section child process
// post critical section
next = ( current + 1) % 4;
for (;next!=current && !(*(waiting + next));) {
next = ( next + 1) % 4;}
if ( next == current) {
pthread_mutex_unlock(x_mute_x);
} else{
(*(waiting + next))= 0;
}
shmdet = shmdt (shmatt);
if (shmatt == (void *)-1) { fprintf (stderr, "Impossibile detachare memoria condivisa\n");
err= errno;
fprintf (stderr, "Errore %d\n",err);
exit (-3);}
exit(0);
}
pid2 = fork();
if ( pid2 == 0) {
//precritical section
*(waiting + 1)=1;
key=1;
current = 1;
for (;(*(waiting + 1)) == 1 && key!=0;) {
key = (int) (pthread_mutex_trylock(x_mute_x));
if ( key!=0) {
fprintf(stdout,"I'm process 2 and i'm waiting to decrement the shared variable..\n");
sleep(1);
}
}
fprintf(stdout,"I'm process 2 and i have the control NOW! I'm decrementing the shared variable\n");
(*(waiting + 1)) = 0;
////////////////////////////// begin critical section child process
//critical section
for (i=0;i<50000;i++){(*(shared_variable))--;}
///////////////////////////// end critical section child process
// post critical section
next = ( current + 1) % 4;
for (;next!=current && !(*(waiting + next));) {
next = ( next + 1) % 4;}
if ( next == current) {
pthread_mutex_unlock(x_mute_x);
} else{
(*(waiting + next))= 0;
}
shmdet = shmdt (shmatt);
if (shmatt == (void *)-1) { fprintf (stderr, "Impossibile detachare memoria condivisa\n");
err= errno;
fprintf (stderr, "Errore %d\n",err);
exit (-3);}
exit(0);
}
pid3 = fork();
if ( pid3 == 0) {
//precritical section
*(waiting + 2)=1;
key=1;
current = 2;
for (;(*(waiting + 2)) == 1 && key!=0;) {
key = (int) (pthread_mutex_trylock(x_mute_x));
if ( key!=0) {
fprintf(stdout,"I'm process 3 and i'm waiting to increment the shared variable..\n");
sleep(1);
}
}
fprintf(stdout,"I'm process 3 and i have the control NOW! I'm incrementing the shared variable\n");
(*(waiting + 2)) = 0;
////////////////////////////// begin critical section child process
//critical section
for (i=0;i<50000;i++){(*(shared_variable))++;}
///////////////////////////// end critical section child process
// post critical section
next = ( current + 1) % 4;
for (;next!=current && !(*(waiting + next));) {
next = ( next + 1) % 4;}
if ( next == current) {
pthread_mutex_unlock(x_mute_x);
} else{
(*(waiting + next))= 0;
}
shmdet = shmdt (shmatt);
if (shmatt == (void *)-1) { fprintf (stderr, "Impossibile detachare memoria condivisa\n");
err= errno;
fprintf (stderr, "Errore %d\n",err);
exit (-3);}
exit(0);
}
pid4 = fork();
if ( pid4 == 0) {
//precritical section
*(waiting + 3)=1;
key=1;
current = 3;
for (;(*(waiting + 3)) == 1 && key!=0;) {
key = (int) (pthread_mutex_trylock(x_mute_x));
if ( key!=0) {
fprintf(stdout,"I'm process 4 and i'm waiting to decrement the shared variable..\n");
sleep(1);
}
}
fprintf(stdout,"I'm process 4 and i have the control NOW! I'm decrementing the shared variable\n");
(*(waiting + 3)) = 0;
////////////////////////////// begin critical section child process
//critical section
for (i=0;i<50000;i++){(*(shared_variable))--;}
///////////////////////////// end critical section child process
// post critical section
next = ( current + 1) % 4;
for (;next!=current && !(*(waiting + next));) {
next = ( next + 1) % 4;}
if ( next == current) {
pthread_mutex_unlock(x_mute_x);
} else{
(*(waiting + next))= 0;
}
shmdet = shmdt (shmatt);
if (shmatt == (void *)-1) { fprintf (stderr, "Impossibile detachare memoria condivisa\n");
err= errno;
fprintf (stderr, "Errore %d\n",err);
exit (-3);}
exit(0);
}
int waiterr;
waiterr = waitpid(-1,&status,0);
for (;waiterr > 0;){
waiterr = waitpid(-1,&status,0);
}
printf ( "Numero finale :%d\n",*(shared_variable));
shmdet = shmdt (shmatt);
if (shmatt == (void *)-1) { fprintf (stderr, "Impossibile detachare memoria condivisa\n");
err= errno;
fprintf (stderr, "Errore %d\n",err);
exit (-4);}
shmcontrol = shmctl (shmid,IPC_RMID,NULL );
if (shmcontrol == -1) { fprintf (stderr, "Impossibile settare memoria condivisa per eliminazione\n");
err= errno;
fprintf (stderr, "Errore %d\n",err);
exit (-5);}
return 0;
}
Comment