~ubuntu-branches/ubuntu/vivid/cctools/vivid

« back to all changes in this revision

Viewing changes to dttools/src/process.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Hanke
  • Date: 2011-05-07 09:05:00 UTC
  • Revision ID: james.westby@ubuntu.com-20110507090500-lqpmdtwndor6e7os
Tags: upstream-3.3.2
ImportĀ upstreamĀ versionĀ 3.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (C) 2008- The University of Notre Dame
 
3
This software is distributed under the GNU General Public License.
 
4
See the file COPYING for details.
 
5
*/
 
6
 
 
7
#include "process.h"
 
8
#include "list.h"
 
9
 
 
10
#include <sys/wait.h>
 
11
#include <unistd.h>
 
12
#include <stdlib.h>
 
13
#include <signal.h>
 
14
 
 
15
static struct list * complete_list = 0;
 
16
 
 
17
static void alarm_handler( int sig )
 
18
{
 
19
        // do nothing except interrupt the wait
 
20
}
 
21
 
 
22
static int process_work( int timeout )
 
23
{
 
24
        void *old_handler = 0;
 
25
        int flags = 0;
 
26
 
 
27
        if(timeout==0) {
 
28
                flags = WNOHANG;
 
29
        } else {
 
30
                flags = 0;
 
31
                old_handler = signal(SIGALRM,alarm_handler);
 
32
                alarm(timeout);
 
33
        }
 
34
 
 
35
        struct process_info p;
 
36
 
 
37
        p.pid = wait4(-1,&p.status,flags,&p.rusage);
 
38
        if(p.pid<=0) return 0;
 
39
 
 
40
        struct process_info *i = malloc(sizeof(*i));
 
41
        *i = p;
 
42
 
 
43
        list_push_tail(complete_list,i);
 
44
 
 
45
        return 1;
 
46
}
 
47
 
 
48
struct process_info * process_wait( int timeout )
 
49
{
 
50
        struct process_info *p;
 
51
 
 
52
        if(!complete_list) complete_list = list_create();
 
53
 
 
54
        p = list_pop_head(complete_list);
 
55
        if(p) return p;
 
56
 
 
57
        process_work(timeout);
 
58
 
 
59
        return list_pop_head(complete_list);
 
60
}
 
61
 
 
62
void process_putback( struct process_info *p )
 
63
{
 
64
        if(!complete_list) complete_list = list_create();
 
65
 
 
66
        list_push_tail(complete_list,p);        
 
67
}
 
68
 
 
69
int process_pending()
 
70
{
 
71
        if(!complete_list) complete_list = list_create();
 
72
 
 
73
        if(list_size(complete_list)>0) return 1;
 
74
 
 
75
        return process_work(0);
 
76
}