Operating System 作業-02

4.3、在統一進程的多線程之間,下列哪些程序狀態部分會被共享?

堆內存和全局變量

4.5、第三章討論了Google的chrome瀏覽器,以及在單獨進程中打開每個新網站的做法。如果chrome設計成在單獨線程中打開每個新網頁,那麼會有什麼樣的好處?

每個標籤頁都會啓動一個獨立的進程,這樣即使因爲某個頁面崩潰了也不會影響到其他頁面。

4.9、具有2個雙核處理器的系統有4個處理核可用於調度。這個系統有一個cpu密集型應用程序運行。在程序啓動時,所有輸入通過打開一個文件而讀入。同樣,在程序終止前,所有程序輸出的結果都寫入一個文件。在程序啓動和終止之間,該程序爲cpu密集型。你的任務是通過多線程技術來提高這個應用程序的性能。這個應用程序運行在採用一對一線程模型的系統。

(1)、你將創建多少個線程,用於執行輸入和輸出?請解釋。
線程數取決於應用程序的要求,因此創建一個線程用於執行輸入和輸出就足夠了。
(2)、你將創建多少個形成,用於應用程序的cpu密集型部分?請解釋。
線程數應該和處理核數是一樣的,因此要創建四個線程。

4.10、考慮下面的代碼

a.創建了多少個單獨的進程?
b.創建了都少個單獨的線程?

創建了5個單獨的進程
創建了2個單獨的線程。

4.15、修改第三章的習題3.13.這個修改包括寫一個多線程程序,以測試你的習題3.13的解決方案

#include <stdlib.h>
#include <pthread.h>
#include <stdio.h>
#include <time.h>
#include <omp.h>
#include <unistd.h>
#define MIN_PID 300
#define MAX_PID 5000
#define TRUE 1
#define FALSE 0
pthread_mutex_t mutex;
int threadVar = 0;
 
struct PidTable{
    int pid;
    int isAvailable;
}*PID;
 
int allocate_map(){
    int i;
    PID = (struct PidTable*)calloc((MAX_PID - MIN_PID + 1), sizeof(struct PidTable));
    if(PID == NULL) return -1;
    PID[0].pid = MIN_PID;
    PID[0].isAvailable = TRUE;
    for(i = 1; i < MAX_PID - MIN_PID + 1; i++){
        PID[i].pid = PID[i - 1].pid + 1;
        PID[i].isAvailable = TRUE;
    }
    return 1;
}
 
int allocate_pid(){
    int i ;
    for(i = 0;i < MAX_PID - MIN_PID + 1; i++){
        if(PID[i].isAvailable == TRUE){
            PID[i].isAvailable = FALSE;
            return PID[i].pid;
        }
    }
    if(i == MAX_PID - MIN_PID + 1)
        return -1;
    else
        return 0;
}
 
void release_pid(int pid){
    PID[pid - MIN_PID].isAvailable = TRUE;
}
 
void *threadCall(void* X)                         
{
    int ret = allocate_pid();       
    printf("%d %d\n",threadVar++, ret);
    sleep(rand());
    release_pid(ret);
}
 
int main()
{
    srand(time(NULL));
    int pid;
    allocate_map();
    pthread_t thread[100];
    printf("Every thread will print the value of pid and increment it by 1.\n");
    omp_set_num_threads(5);
    #pragma omp parallel for
    for(int i = 0; i < 100; i++){
        pthread_create(&thread[i], NULL, &threadCall, NULL);
    }
    return 0;
}

總結:在for循環中利用pthread_create創建100個線程運行threadCall函數,以檢驗
習題3.13的程序是否正確。

4.17、計算Π時的已給有趣的辦法時,使用一個稱爲Monte Carlo的技術,這種技術涉及隨機。編寫一個多線程算法,它創建一組單獨線程以產生一組隨機點。該線程計算圈內點的數量,並將結果存儲到一個全局變量。

#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <math.h>
#include <time.h>
#include <stdlib.h>

int desired_amount = 0;
int total_points  = 0;

void *count(void *X){
    for (int i=0; i < desired_amount; i++){
        double X = (double)rand() / RAND_MAX; // random numbers 0~1
        double Y = (double)rand() / RAND_MAX;

        if (((X * X) + (Y * Y)) <= 1){
            total_points++;
        }
    }
}

int main(){
    printf("Welcome to Threaded Monte Carlo.\n");
    srand(time(NULL));
    pthread_t thread;

    do{
        printf("Please enter a positive number for the amount of points you would like to generate? \n");
        scanf("%d", &desired_amount);
    }while (desired_amount <= 0);

    pthread_create(&thread, NULL, &count, NULL);
    pthread_join(thread, NULL);  // End thread

    double points = 4.0 * total_points;
    double pi = points / desired_amount;
    printf("The approximate value of pi for the desired amount of points %d is: %f \n", desired_amount, pi);
    return 0;
}

總結:利用pthread_create創建一個單獨的縣線程調用count生成隨機點。

4.18、重複4.17,但不是使用一個單獨的線程來生成隨機點,而是採用OpenMP並行化點的生成。注意:不要把Π的計算放在並行區域,因爲你只需要計算Π一次。

#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <omp.h>

int desired_amount = 0;
int total_points  = 0;
double X, Y;
double wall_time;
clock_t clock_timer;

int main(){
    printf("Welcome to Threaded Monte Carlo.\n");
    srand(time(NULL));

    do{
        printf("Please enter a positive number for the amount of points you would like to generate? \n");
        scanf("%d", &desired_amount);
    }while (desired_amount <= 0);

    omp_set_num_threads(5);
#pragma omp parallel for
    for (int i=0; i < desired_amount; i++){
        double X = (double)rand() / RAND_MAX; // random numbers 0~1
        double Y = (double)rand() / RAND_MAX;

        if (((X * X) + (Y * Y)) <= 1){
            total_points++;
        }
    }

    double points = 4.0 * total_points;
    double pi = points / desired_amount;
    printf("The approximate value of pi for the desired amount of points %d is: %f \n", desired_amount, pi);
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章