"Операционные системы"
ПРИЛОЖЕНИЕ
к лабораторной работе
"Потоки в ОС Linux"
# makefile for the compilation and linking of the programs
CC = cc
CFLAGS = -g
all : simple simple_threads simple_processes simple_mutex
simple : simple.o
${CC} ${CFLAGS} simple.o -o simple
simple_threads : simple_threads.o
${CC} ${CFLAGS} simple_threads.o -lpthread -o \
simple_threads
simple_processes : simple_processes.o
${CC} ${CFLAGS} simple_processes.o -o \
simple_processes
simple_mutex : simple_mutex.o
${CC} ${CFLAGS}${THREAD_CFLAGS} \
simple_mutex.o -lpthread -o simple_mutex
clean :
rm -f *.o *~ *# core a.out\
simple simple_threads simple_processes\
simple_mutex
/********************************************************
* Example source modules to accompany the book
* "Using POSIX Threads: Programming with Pthreads"
* by Brad Nichols, Dick Buttlar, Jackie Farrell
* O'Reilly & Associates, Inc.
*
********************************************************
* simple.c
*
* Simple serial example. Calls three procedures.
*/
#include <stdio.h>
void do_one_thing(int *);
void do_another_thing(int *);
void do_wrap_up(int, int);
int r1 = 0, r2 = 0;
extern int
main(void)
{
do_one_thing(&r1);
do_another_thing(&r2);
do_wrap_up(r1, r2);
return 0;
}
void do_one_thing(int *pnum_times)
{
int i, j, x;
for (i = 0; i < 4; i++) {
printf("doing one thing \n");
for (j = 0; j < 10000; j++) x = x + i;
(*pnum_times)++;
}
}
void do_another_thing(int *pnum_times)
{
int i, j, x;
for (i = 0; i < 4; i++) {
printf("doing another \n");
for (j = 0; j < 10000; j++) x = x + i;
(*pnum_times)++;
}
}
void do_wrap_up(int one_times, int another_times)
{
int total;
total = one_times + another_times;
printf("All done, one thing %d, another %d for a total of %d\n",
one_times, another_times, total);
}
/* simple_processes.c
*
* Simple multi-process example.
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/wait.h>
#include <errno.h>
void do_one_thing(int *);
void do_another_thing(int *);
void do_wrap_up(int, int);
int shared_mem_id;
int *shared_mem_ptr;
int *r1p;
int *r2p;
extern int main(void)
{
pid_t child1_pid, child2_pid;
int status;
/* initialize shared memory segment */
if ((shared_mem_id = shmget(IPC_PRIVATE, 2*sizeof(int), 0660)) == -1)
perror("shmget"), exit(1);
if ((shared_mem_ptr = (int *)shmat(shared_mem_id, (void *)0, 0)) == (void *)-1)
perror("shmat failed"), exit(1);
r1p = shared_mem_ptr;
r2p = (shared_mem_ptr + 1);
*r1p = 0;
*r2p = 0;
if ((child1_pid = fork()) == 0) {
/* first child */
do_one_thing(r1p);
return 0;
} else if (child1_pid == -1) {
perror("fork"), exit(1);
}
/* parent */
if ((child2_pid = fork()) == 0) {
/* second child */
do_another_thing(r2p);
return 0;
} else if (child2_pid == -1) {
perror("fork"), exit(1);
}
/* parent */
if ((waitpid(child1_pid, &status, 0) == -1))
perror("waitpid"), exit(1);
if ((waitpid(child2_pid, &status, 0) == -1))
perror("waitpid"), exit(1);
do_wrap_up(*r1p, *r2p);
return 0;
}
void do_one_thing(int *pnum_times)
{
int i, j, x;
for (i = 0; i < 4; i++) {
printf("doing one thing \n");
for (j = 0; j < 10000; j++) x = x + i;
(*pnum_times)++;
}
}
void do_another_thing(int *pnum_times)
{
int i, j, x;
for (i = 0; i < 4; i++) {
printf("doing another \n");
for (j = 0; j < 10000; j++) x = x + i;
(*pnum_times)++;
}
}
void do_wrap_up(int one_times, int another_times)
{
int total;
total = one_times + another_times;
printf("All done, one thing %d, another %d for a total of %d\n",
one_times, another_times, total);
}
/* simple_threads.c
*
* Simple multi-threaded example.
*/
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <pthread.h>
void do_one_thing(int *);
void do_another_thing(int *);
void do_wrap_up(int, int);
int r1 = 0, r2 = 0;
extern int
main(void)
{
pthread_t thread1, thread2;
if (pthread_create(&thread1,
NULL,
(void *) do_one_thing,
(void *) &r1) != 0)
perror("pthread_create"), exit(1);
if (pthread_create(&thread2,
NULL,
(void *) do_another_thing,
(void *) &r2) != 0)
perror("pthread_create"), exit(1);
if (pthread_join(thread1, NULL) != 0)
perror("pthread_join"),exit(1);
if (pthread_join(thread2, NULL) != 0)
perror("pthread_join"),exit(1);
do_wrap_up(r1, r2);
return 0;
}
void do_one_thing(int *pnum_times)
{
int i, j, x;
for (i = 0; i < 4; i++) {
printf("doing one thing\n");
for (j = 0; j < 10000; j++) x = x + i;
(*pnum_times)++;
}
}
void do_another_thing(int *pnum_times)
{
int i, j, x;
for (i = 0; i < 4; i++) {
printf("doing another \n");
for (j = 0; j < 10000; j++) x = x + i;
(*pnum_times)++;
}
}
void do_wrap_up(int one_times, int another_times)
{
int total;
total = one_times + another_times;
printf("All done, one thing %d, another %d for a total of %d\n",
one_times, another_times, total);
}
/* simple_mutex.c
*
* Simple multi-threaded example with a mutex mechanism.
*/
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <pthread.h>
void do_one_thing(int *);
void do_another_thing(int *);
void do_wrap_up(int);
int common = 0; /* A shared variable for two threads */
int r1 = 0, r2 = 0, r3 = 0;
pthread_mutex_t r3_mutex=PTHREAD_MUTEX_INITIALIZER;
/* extern int */
main(int argc, char **argv)
{
pthread_t thread1, thread2;
if (argc > 1)
r3 = atoi(argv[1]);
if (pthread_create(&thread1,
NULL,
(void *) do_one_thing,
(void *) &common) != 0)
perror("pthread_create"),exit(1);
if (pthread_create(&thread2,
NULL,
(void *) do_another_thing,
(void *) &common) != 0)
perror("pthread_create"),exit(1);
if (pthread_join(thread1, NULL) != 0)
perror("pthread_join"), exit(1);
if (pthread_join(thread2, NULL) != 0)
perror("pthread_join"), exit(1);
do_wrap_up(common);
return 0;
}
void do_one_thing(int *pnum_times)
{
int i, j, x;
unsigned long k;
int work;
for (i=0;i<50;i++){
pthread_mutex_lock(&r3_mutex);
printf("doing one thing\n");
work = *pnum_times;
printf("counter = %d\n", work);
work++; /* increment, but not write */
for (k=0;k<500000;k++); /* long cycle */
*pnum_times = work; /* write back */
pthread_mutex_unlock(&r3_mutex);
}
}
void do_another_thing(int *pnum_times)
{
int i, j, x;
unsigned long k;
int work;
for (i=0;i<50;i++) {
pthread_mutex_lock(&r3_mutex);
printf("doing another thing\n");
work = *pnum_times;
printf("counter = %d\n", work);
work++; /* increment, but not write */
for (k=0;k<500000;k++); /* long cycle */
*pnum_times = work; /* write back */
pthread_mutex_unlock(&r3_mutex);
}
}
void do_wrap_up(int counter)
{
int total;
printf("All done, counter = %d\n", counter);
}
