Changeset 25 for trunk/threads-sem.c


Ignore:
Timestamp:
Apr 13, 2013, 4:53:54 PM (11 years ago)
Author:
Mattia Monga
Message:

Concorrenza 2013

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/threads-sem.c

    r2 r25  
    99#include <semaphore.h>
    1010
    11 void enter_section(sem_t *s){
     11void down(sem_t *s){
    1212        if (sem_wait(s) < 0){
    1313                perror("Errore semaforo (down)");
     
    1616}
    1717
    18        
    19 
    20 void leave_section(sem_t *s)
    21 {
     18void up(sem_t *s){
    2219        if (sem_post(s) < 0){
    2320                perror("Errore semaforo (up)");
     
    2623}
    2724
    28 int run(const int p, void* s)
    29 {
    30         int* shared = (int*)s; /* alias per comodita` */
    31         while (enter_section((sem_t*)shared[1]),
    32                shared[0] < 10) {
     25int shared = 0;
     26pthread_t p1, p2;
     27sem_t ss;
     28
     29void* run(void* s){
     30        while (down(&ss),
     31               shared < 10) {
    3332                sleep(1);
    34                 printf("Processo figlio (%d). s = %d\n",
    35                        getpid(), shared[0]);
    36                 if (!(shared[0] < 10)){
     33                printf("Processo thread (%p). s = %d\n",
     34                       pthread_self(), shared);
     35                if (!(shared < 10)){
    3736                        printf("Corsa critica!!!!\n");
    3837                        abort();
    3938                }
    40                 shared[0] += 1;
    41                 leave_section((sem_t*)shared[1]);
    42                 sched_yield();
     39                shared += 1;
     40                up(&ss);
     41                pthread_yield();
    4342        }
    44         return 0;
     43        up(&ss);
     44        return NULL;
    4545}
    4646
    47 int run0(void*s){ return run(0, s); }
    48 int run1(void*s){ return run(1, s); }
    49        
     47
    5048
    5149int main(void){
    52  
    53         int shared[4] = {0 , 0, 0, 0};
    5450
    55         sem_t *ss;
    56         if (sem_init(ss,
     51        if (sem_init(&ss,
    5752                     0 /* thread local semaphore */,
    58                      0 /* init value */
     53                     1 /* init value */
    5954                    ) < 0){
    6055                perror("Errore semaforo");
    6156                exit(1);
    6257        }
    63         shared[1] = (int)ss;
    6458
    65         /* int clone(int (*fn)(void *),
    66          *           void *child_stack,
    67          *           int flags,
    68          *           void *arg);
    69          * crea una copia del chiamante (con le caratteristiche
    70          * specificate da flags) e lo esegue partendo da fn */
    71         if (clone(run0, /* il nuovo
    72                          * processo esegue run(shared), vedi quarto
    73                          * parametro */
    74                   malloc(4096)+4096,  /* lo stack del nuovo processo
    75                                        *  (cresce verso il basso!) */
    76                   CLONE_VM | SIGCHLD, /* la (virtual) memory e` condivisa */
    77                   shared) < 0){
    78                 perror("Errore nella creazione");
    79                 exit(1);
    80         }
     59        pthread_create(&p1, NULL, run, NULL);
     60        pthread_create(&p2, NULL, run, NULL);
    8161
    82         if (clone(run1, malloc(4096)+4096,  CLONE_VM | SIGCHLD, shared) < 0){
    83                 perror("Errore nella creazione");
    84                 exit(1);
    85         }
    86 
    87         /* Memoria condivisa: i due figli nell'insieme eseguono 10 o
    88          * 11 volte: e` possibile una corsa critica. Il padre
    89          * condivide shared[0] con i figli */
    90 
    91         while(shared[0] < 10) {
    92                 sleep(1);
    93                 printf("Processo padre. s = %d %d %d %d\n",
    94                        shared[0],
    95                        shared[1],
    96                        shared[2],
    97                        shared[3]);
    98         }
    99         sem_destroy(ss);
     62        pthread_join(p1, NULL);
     63        pthread_join(p2, NULL);
     64       
     65        sem_destroy(&ss);
     66       
    10067        return 0;
    10168}
Note: See TracChangeset for help on using the changeset viewer.