source: trunk/threads.c@ 20

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

Importazione sorgenti

File size: 1.2 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#include <assert.h>
10
11int run(void* s)
12{
13 int* shared = (int*)s; /* alias per comodita` */
14 while (shared[0] < 10) {
15 sleep(1);
16 printf("Processo figlio (%d). s = %d\n",
17 getpid(), shared[0]);
18 if (!(shared[0] < 10)){
19 printf("Corsa critica!!!!\n");
20 abort();
21 }
22 shared[0] += 1;
23 }
24 return 0;
25}
26
27
28int main(void){
29
30 int shared[2] = {0 , 0};
31 if (clone(run, malloc(4096)+4096, SIGCHLD, shared) < 0){
32 perror("Errore nella creazione");
33 exit(1);
34 }
35
36 if (clone(run, malloc(4096)+4096, SIGCHLD, shared) < 0){
37 perror("Errore nella creazione");
38 exit(1);
39 }
40
41 while(1) {
42 sleep(1);
43 printf("Processo padre. s = %d\n", shared[0]);
44 }
45 return 0;
46}
47
48
49
50
51
52
53
54/* Local Variables: */
55/* compile-command: "make -k " */
56/* End: */
Note: See TracBrowser for help on using the repository browser.