Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
lab_6_OS.doc
Скачиваний:
2
Добавлен:
01.03.2025
Размер:
54 Кб
Скачать
☆
    1. Листинг файла critical-section.C

#include <pthread.h>

#include <stdio.h>

#include <string.h>

int main (int from_acct, int to_acct, int dollars)

{

int old_cancel_state;

/* Check the balance in FROM_ACCT. */

if (from_acct < dollars)

printf("Error \n");

return 1;

/* Begin critical section. */

pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &old_cancel_state);

/* Move the money. */

to_acct += dollars;

from_acct -= dollars;

/* End critical section. */

pthread_setcancelstate (old_cancel_state, NULL);

printf("success \n");

return 0;

}

    1. Листинг файла tsd.C

#include <malloc.h>

#include <pthread.h>

#include <stdio.h>

/* The key used to assocate a log file pointer with each thread. */

static pthread_key_t thread_log_key;

/* Write MESSAGE to the log file for the current thread. */

void write_to_thread_log (const char* message)

{

FILE* thread_log = (FILE*) pthread_getspecific (thread_log_key);

fprintf (thread_log, "%s\n", message);

}

/* Close the log file pointer THREAD_LOG. */

void close_thread_log (void* thread_log)

{

fclose ((FILE*) thread_log);

}

void* thread_function (void* args)

{

char thread_log_filename[20];

FILE* thread_log;

/* Generate the filename for this thread's log file. */

sprintf (thread_log_filename, "thread%d.log", (int) pthread_self ());

/* Open the log file. */

thread_log = fopen (thread_log_filename, "w");

/* Store the file pointer in thread-specific data under thread_log_key. */

pthread_setspecific (thread_log_key, thread_log);

write_to_thread_log ("Thread starting.");

/* Do work here... */

return NULL;

}

int main ()

{

int i;

pthread_t threads[5];

/* Create a key to associate thread log file pointers in

thread-specific data. Use close_thread_log to clean up the file

pointers. */

pthread_key_create (&thread_log_key, close_thread_log);

/* Create threads to do the work. */

for (i = 0; i < 5; ++i)

pthread_create (&(threads[i]), NULL, thread_function, NULL);

/* Wait for all threads to finish. */

for (i = 0; i < 5; ++i)

pthread_join (threads[i], NULL);

return 0;}

    1. Листинг файла job-queue1.C

#include <malloc.h>

struct job {

/* Link field for linked list. */

struct job* next;

/* Other fields describing work to be done... */

};

/* A linked list of pending jobs. */

struct job* job_queue;

extern void process_job (struct job*);

/* Process queued jobs until the queue is empty. */

void* thread_function (void* arg)

{

while (job_queue != NULL) {

/* Get the next available job. */

struct job* next_job = job_queue;

/* Remove this job from the list. */

job_queue = job_queue->next;

/* Carry out the work. */

process_job (next_job);

/* Clean up. */

free (next_job);

}

return NULL;

}

    1. Листинг файла job-queue2.C

#include <malloc.h>

#include <pthread.h>

struct job {

struct job* next;

};

/* A linked list of pending jobs. */

struct job* job_queue;

extern void process_job (struct job*);

/* A mutex protecting job_queue. */

pthread_mutex_t job_queue_mutex = PTHREAD_MUTEX_INITIALIZER;

/* Process queued jobs until the queue is empty. */

void* thread_function (void* arg)

{

while (1) {

struct job* next_job;

/* Lock the mutex on the job queue. */

pthread_mutex_lock (&job_queue_mutex);

/* Now it's safe to check if the queue is empty. */

if (job_queue == NULL)

next_job = NULL;

else {

/* Get the next available job. */

next_job = job_queue;

/* Remove this job from the list. */

job_queue = job_queue->next;

}

/* Unlock the mutex on the job queue, since we're done with the queue for now. */

pthread_mutex_unlock (&job_queue_mutex);

/* Was the queue empty? If so, end the thread. */

if (next_job == NULL)

break;

/* Carry out the work. */

process_job (next_job);

/* Clean up. */

free (next_job);

} return NULL;}

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]