羅左欣 BE STRONG TO BE USEFUL

20170317 [學習筆記] Linux 系統程式 (3)


一、作業系統

Basic Concepts

  • Maximum CPU utilization obtained with multiprogramming.
  • CPU–I/O Burst Cycle – Process execution consists of a cycle of CPU execution and I/O wait.
  • CPU burst followed by I/O burst.
  • CPU burst distribution is of main concern.

CPU Scheduler

  • Short-term scheduler selects from among the processes in ready queue, and allocates the CPU to one of them.
    • Queue may be ordered in various ways.
  • CPU scheduling decisions may take place when a process:
    1. Switches from running to waiting state
    2. Switches from running to ready state
    3. Switches from waiting to ready
    4. Terminates
  • Scheduling under 1 and 4 is nonpreemptive.
  • All other scheduling is preemptive.
    • Consider access to shared data.
    • Consider preemption while in kernel mode.
    • Consider interrupts occurring during crucial OS activities.

Dispatcher

  • Dispatcher module gives control of the CPU to the process selected by the short-term scheduler; this involves:
    1. switching context
    2. switching to user mode
    3. jumping to the proper location in the user program to restart that program
  • Dispatch latency:time it takes for the dispatcher to stop one process and start another running.

Scheduling Algorithm Optimization Criteria

  1. CPU utilization – keep the CPU as busy as possible.
  2. Throughput – # of processes that complete their execution per time unit.
  3. Turnaround time – amount of time to execute a particular process.
  4. Waiting time – amount of time a process has been waiting in the ready queue.
  5. Response time – amount of time it takes from when a request was submitted until the first response is produced, not output (for time-sharing environment).
  • Length of Next CPU Burst
    • Determining
    • Prediction

Scheduling Algorithm

(一) First-Come, First-Served (FCFS):先到先服務法


  • Convoy effect:很多短時間的 process,都在等一個 長時間的 process 時,所產生的效應 (因為等待時間很長)。

(二) Shortest-Job-First (SJF):最短優先法

  • Associate with each process the length of its next CPU burst.
    • Use these lengths to schedule the process with the shortest time.
  • SJF is optimal – gives minimum average waiting time for a given set of processes.
    • The difficulty is knowing the length of the next CPU request.
    • Could ask the user.

(三) Priority Scheduling (PS):優先權排班法

  • A priority number (integer) is associated with each process.
  • The CPU is allocated to the process with the highest priority (smallest integer » highest priority).
    • Preemptive
    • Nonpreemptive
  • SJF is priority scheduling where priority is the inverse of predicted next CPU burst time.
  • Problem:Starvation – low priority processes may never execute.
  • Solution:Aging – as time progresses increase the priority of the process.

(四) Round Robin (RR):輪流法

  • Each process gets a small unit of CPU time (time quantum q), usually 10-100 milliseconds. After this time has elapsed, the process is preempted and added to the end of the ready queue.
  • If there are n processes in the ready queue and the time quantum is q, then each process gets 1/n of the CPU time in chunks of at most q time units at once. No process waits more than (n-1)q time units.
  • Timer interrupts every quantum to schedule next process.
  • Performance.
    • q large » FIFO.
    • q small » q must be large with respect to context switch, otherwise overhead is too high.

1. Time Quantum and Context Switch Time

2. Turnaround Time Varies With The Time Quantum

(五) Multilevel

1. Queue
  • Ready queue is partitioned into separate queues, eg:
    • foreground (interactive)
    • background (batch)
  • Process permanently in a given queue. Each queue has its own scheduling algorithm:
    • foreground – RR
    • background – FCFS
  • Scheduling must be done between the queues:
    • Fixed priority scheduling; (i.e., serve all from foreground then from background). Possibility of starvation.
    • Time slice – each queue gets a certain amount of CPU time which it can schedule amongst its processes; i.e., 80% to foreground in RR.
    • 20% to background in FCFS.

2. Feedback Queue
  • A process can move between the various queues; aging can be implemented this way.
  • Multilevel-feedback-queue scheduler. defined by the following parameters:
    • number of queues.
    • scheduling algorithms for each queue.
    • method used to determine when to upgrade a process.
    • method used to determine when to demote a process.
    • method used to determine which queue a process will enter when that process needs service.

  • 課程作業

二、Linux 程式設計

1. What is a Thread?

(1) OS view

  • A thread is an independent stream of instructions that can be scheduled to run by the OS.

(2) Software developer view

  • a thread can be considered as a “procedure” that runs independently from the main program.
    • Sequential program: a single stream of instructions in a program.
    • Multi-threaded program: a program with multiple streams.
      • Multiple threads are needed to use multiple cores/CPUs.

[Example]

  1. Computer games
    • each thread controls the movement of an object.
  2. Scientific simulations
    • Hurricane movement simulation: each thread simulates the hurricane in a small domain.
    • Molecular dynamic: each thread simulates a subset of particulars.
  3. Web server
    • Each thread handles a connection.

2. Process and Thread

(1) Process context

  • Two parts in the context: self-contained domain (protection) and execution of instructions.
    • Process ID, process group ID, user ID, and group ID
    • Environment
    • Working directory.
    • Program instructions
    • Registers (including PC)
    • Stack
    • Heap
    • File descriptors
    • Signal actions
    • Shared libraries
    • Inter-process communication tools
  • What are absolutely needed to support a stream of instructions, given the process context?
    • Registers (including PC)
    • Stack

(2) Threads

  • Advantages
    • Light-weight
      • Lower overhead for thread creation.
      • Lower Context Switching Overhead.
        • Fewer OS resources
    • Shared State
      • Don’t need IPC-like mechanism to communicate between threads of same process.
  • Disadvantages
    • Shared State!
      • Global variables are shared between threads. Accidental changes can be fatal.
    • Many library functions are not thread-safe
      • Library Functions that return pointers to static internal memory. E.g. gethostbyname()
    • Lack of robustness
      • Crash in one thread will crash the entire process.

3. Pthreads

  • Hardware vendors used to implement proprietary versions of threads
    • Thread programs are not portable
  • Pthreads = POSIX threads, specified in IEEE POSIX 1003.1c (1995)

(1) The Pthreads API

  • Three types of routines:
    1. Thread management: create, terminate, join, and detach
    2. Mutexes: mutual exclusion, creating, destroying, locking, and unlocking mutexes
    3. Condition variables: event driven synchronizaiton.
      • Mutexes and condition variables are concerned about synchronization.
      • Why not anything related to inter-thread communication?
  • The concept of opaque objects pervades the design of the API.
  • API naming convention

(2) Thread management

  • Pthread header file
  • Compiling pthread programs:

    gcc aaa.c -o aaa -lpthread

  1. Creation
    • pthread_create
  2. Termination
    • Return
    • Pthread_exit
    • Can we still use exit?
  3. Wait (parent/child synchronization)
    • pthread_join
  • 課程作業:找出 1 - 100 的所有質數
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h> 
#include <time.h>

#define NUM_THREADS 10
#define MSIZE 100

// 找出 1 - 100 的所有質數

static double getDoubleTime();
void *thread_function(void *arg);
pthread_mutex_t work_mutex;

// 宣告 prime_array 陣列
int prime_array[NUM_THREADS][(MSIZE / NUM_THREADS)];


int main(void) {
	int res;
	pthread_t a_thread[NUM_THREADS];
	void *thread_result;
	int lots_of_threads;
    int print_prime = 0;


	// start to measure time...
	double start_time = getDoubleTime();
	
	// initialize mutex...
	res = pthread_mutex_init(&work_mutex, NULL);
	if (res != 0) {
       	perror("Mutex initialization failed");
	    exit(EXIT_FAILURE);
    }

	// pthread_create...
	for (lots_of_threads = 0; lots_of_threads < NUM_THREADS; lots_of_threads ++) {
		res = pthread_create(&(a_thread[lots_of_threads]), NULL, thread_function, (void*)(long)lots_of_threads);

        if (res != 0) {
			perror("Thread creation failed");
        		exit(EXIT_FAILURE);
		}
	}

	// pthread_join...
	for (lots_of_threads = NUM_THREADS - 1; lots_of_threads >= 0; lots_of_threads--) {
        	res = pthread_join(a_thread[lots_of_threads], &thread_result); 

            if (res != 0) {
	            perror("pthread_join failed");
        	}
    }	
    
    /* 輸出 prime_array 陣列 */
    int i = 0;  // 設定計數器
    for (lots_of_threads = 0; lots_of_threads < NUM_THREADS; lots_of_threads ++) { 
        printf("\n\nThe thread[%d]'s numbers:\n", lots_of_threads);
        
        for (i = 0; i < (MSIZE / NUM_THREADS); i++) {
            if (prime_array[lots_of_threads][i] != 0)
                printf("%d\t", prime_array[lots_of_threads][i]);                        }
    }

	printf("\nThread joined\n");

	// stop measuring time...
	double finish_time = getDoubleTime();
	printf("Execute Time:  %.3lf ms\n", (finish_time - start_time));
    exit(EXIT_SUCCESS);
}


void *thread_function(void *arg) {
    // pthread_mutex_lock(&work_mutex);
	
    int my_num = (long)arg;
	// if (MSIZE % NUM_THREADS != 0){    printf("error");   pthread_exit(-1);    }

    int start_num = (MSIZE / NUM_THREADS) * my_num + 1;
	int end_num = (MSIZE / NUM_THREADS) * (my_num + 1);

    int i = 0, j = 0, k = 0;   // Set the loop
    int count = 0;      // Set the counter
    int result = 0;     // result

    printf("I'm thread[%d], start_num:%d, end_num:%d\n", my_num, start_num, end_num);

    /* find the prime number */
    for (i = start_num; i <= end_num; i++) {
        count = 0;      // Reset counter

        for (j = 1; j <= i; j++) {
            if (i % j == 0)
                count += 1;
        }
        
        if (count == 2) { 
            prime_array[my_num][k] = i;
            k++;
        }
    }


    // pthread_mutex_unlock(&work_mutex);
	pthread_exit(0);
}


static double getDoubleTime() {
        struct timeval tm_tv;
        gettimeofday(&tm_tv,0);
        return (double)(((double)tm_tv.tv_sec * (double)1000. + (double)(tm_tv.tv_usec)) * (double)0.001);
}
  • 執行結果


Similar Posts

Comments