source: trunk/threads-isolated.c@ 6

Last change on this file since 6 was 2, checked in by Mattia Monga, 13 years ago

Importazione sorgenti

File size: 1.9 KB
Line 
1/* Copyright (C) 2009 by Mattia Monga <mattia.monga@unimi.it> */
2/* $Id$ */
3
4#include <unistd.h>
5#include <stdio.h>
6#include <stdlib.h>
7#include <signal.h>
8#include <sched.h>
9
10int run(void* s)
11{
12 int* shared = (int*)s; /* alias per comodita` */
13 while (shared[0] < 10) {
14 sleep(1);
15 printf("Processo figlio (%d). s = %d\n",
16 getpid(), shared[0]);
17 if (!(shared[0] < 10)){
18 printf("Corsa critica!!!!\n");
19 abort();
20 }
21 shared[0] += 1;
22 }
23 return 0;
24}
25
26
27int main(void){
28
29 int shared[2] = {0 , 0};
30
31 /* int clone(int (*fn)(void *),
32 * void *child_stack,
33 * int flags,
34 * void *arg);
35 * crea una copia del chiamante (con le caratteristiche
36 * specificate da flags) e lo esegue partendo da fn */
37 if (clone(run, /* il nuovo
38 * processo esegue run(shared), vedi quarto
39 * parametro */
40 malloc(4096)+4096, /* lo stack del nuovo processo
41 * (cresce verso il basso!) */
42 SIGCHLD, /* in questo caso la clone e` analoga alla fork */
43 shared) < 0){
44 perror("Errore nella creazione");
45 exit(1);
46 }
47
48 if (clone(run, malloc(4096)+4096, SIGCHLD, shared) < 0){
49 perror("Errore nella creazione");
50 exit(1);
51 }
52
53 /* Isolati: ciascuno dei figli esegue 10 volte. Per il padre
54 * shared[0] e` sempre 0 */
55
56 while(1) {
57 sleep(1);
58 printf("Processo padre. s = %d\n", shared[0]);
59 }
60 return 0;
61}
62
63
64
65
66
67
68
69/* Local Variables: */
70/* compile-command: "make -k " */
71/* End: */
Note: See TracBrowser for help on using the repository browser.