Wednesday, March 30, 2011

How to run 100 apps

1. Using shell script, and run all the apps by background

#!/bin/bash
i=1
while [ $i -le 5 ]
do
    ./while1&
    (( i++ ))
done


2. Using fork and exec
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>

#define TIMES 100
#define PROGRAM "./while1"

int main()
{
    int pid;
    int i;
    for(i = 0; i< TIMES; i++)
    {
    pid = fork();
    if(pid == -1)
    {
        fprintf(stderr, "fork() failed\n");
        exit(1);
    }
    else if(pid == 0)
    {
        // child process
        execv(PROGRAM, NULL);
    }
    else
    {
        // parent process
        // wait(0); //comment out this line, you only can fork 1 program
    }
    }
//    wait(0); // comment out this line if you want to this process to wait 100 while1 program
    return 1;
}

No comments: