CSCE 313 Lecture 5

From Notes
Jump to navigation Jump to search

« previous | Tuesday, January 31, 2012 | next »

Why are Operating Systems so Slow?

HARD DISK, Memory bandwidth

Programs, Processes, and Threads

Continuation of CSCE 313 Lecture 4#Programs, Processes, and Threads

Process Creation

fork() -- Create a new process that is identical to the current process. exec() -- Replace memory and state with new state. exit() -- Terminate process.

Fan of Processes

#include <stdio.h>
#include <unistd.h>

int main(int argc, char* argv[])
{
    pid_t childpid = 0;
    int i, n;

    if (argc != 2) {
        fprintf(stderr, "Usage: %s processes\n", argv[0]);
        return 1;
    }
        
    n = atoi(argv[1]);
    for (i=1; i<n; i++) {
        if ((childpid = fork()) <= 0) break;
    }
        
    fprintf(stderr, "i:%d processID:%ld parentID:%ld childID:%ld\n",
        i, (long) getpid(), (long) getppid(), (long) childpid);
    return 0;
}

Chain of Processes

#include <stdio.h>
#include <unistd.h>

int main(int argc, char* argv[])
{
    pid_t childpid = 0;
    int i, n;

    if (argc != 2) {
        fprintf(stderr, "Usage: %s processes\n", argv[0]);
        return 1;
    }
        
    n = atoi(argv[1]);
    for (i=1; i<n; i++) {
        if (childpid = fork()) break;
    }
        
    fprintf(stderr, "i:%d processID:%ld parentID:%ld childID:%ld\n",
        i, (long) getpid(), (long) getppid(), (long) childpid);
    return 0;
}