Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as C by PinkiBlondy ( 17 years ago )
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <time.h>

#define SECONDS 3600

int main() {
        int i;
        pid_t pids[4];
        pid_t pid;
        int who_am_i = 0; // 0 - parent, 1 - child

        for ( i = 0; i < 4; i++ ) {
                pid = fork();
                if ( pid ) {
                        pids[i] = pid;
                }
                else {
                        who_am_i = 1;
                        break;
                }
        }

        pid = getpid();
        if ( who_am_i ) {
                i = 0;
                time_t start = time(0);
                printf("[%d]: starting loop
", pid);
                for ( ;; ) {
                        i++;
                        if ( (int)time( 0 ) - (int)start  > SECONDS ) {
                                break;
                        }
                }
        }
        if ( who_am_i ) {
                printf("[%d]: child terminating
", pid);
                return 0;
        }
        printf("[%d]: waiting for childs
", pid);
        for ( i = 0; i < 4; i++ ) {
                int status;
                wait(&status);
        }
        printf("[%d]: parent terminating
", pid);
        return 0;
}

 

Revise this Paste

Your Name: Code Language: