Linux实验指导书(5)

2019-02-15 14:43

share_int = -10; if(res != 0){ perror(\ exit(EXIT_FAILURE);

}

printf(\ res = pthread_join(a_thread,&thread_result); if(res != 0){ perror(\ exit(EXIT_FAILURE);

}

printf(\ printf(\ exit(EXIT_SUCCESS);

}

void *thread_function(void *arg){

printf(\//printf(\// sleep(3); *(int *)arg = *(int *)arg * 10;

pthread_exit(\

}

//程序清单 5-2 #include #include #include #include #include #include #include

void *thread_function(void *arg); sem_t bin_sem;

#define WORK_SIZE 1024 char work_area[WORK_SIZE];

int main() { int res;

pthread_t a_thread;

void *thread_result;

21

res = sem_init(&bin_sem,0,0); if (res != 0){ perror(\ exit(EXIT_FAILURE);

}

res = pthread_create(&a_thread,NULL,thread_function,NULL); if(res != 0){ perror(\ exit(EXIT_FAILURE);

}

printf(\ while(strncmp(\ fgets(work_area,WORK_SIZE,stdin); sem_post(&bin_sem);

}

printf(\ res = pthread_join(a_thread,&thread_result); if(res != 0){ perror(\ exit(EXIT_FAILURE);

}

printf(\ sem_destroy(&bin_sem); exit(EXIT_SUCCESS);

}

void *thread_function(void *arg) { sem_wait(&bin_sem);

while(strncmp(\ printf(\ sem_wait(&bin_sem); }

pthread_exit(NULL);

}

22

实验六 Linux进程间的IPC

1、实验目的

学习和掌握Linux进程间的IPC及同步方法。 2、实验内容和步骤

步骤1:输入程序6-1,6-2编译并运行写出执行结果。

_____________________________________________________________________________________________________________________________________________________________________________________________________________________________________________ 步骤 2:仔细研读代码,写出程序中实现P、V操作的相关函数和代码,描述程序的同步工作过程。

___________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________ 步骤3:输入程序6-3和6-4,编译执行并写出结果。

____________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________ 步骤4:程序6-3和6-4中哪段代码实现了共享,描述实现内存共享的主要函数的参数意义和这些函数的使用方法。

_____________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

23

______________________________________________________________________________________________________________________________________________________________ 步骤5:学习并使用IPC中信号量和共享内存的使用方法,重新改写实验3步骤6要求的程序。要求启动多个进程,每一个进程都可以单独对文件进行操作,进程间通过信号量进行同步,对文件的操作映射到共享内存中。

3、实验结论

_________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

4、程序清单

//程序清单 6-1

#if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED) /* union semun is defined by including */ #else

/* according to X/OPEN we have to define it ourselves */ union semun {

int val; /* value for SETVAL */

struct semid_ds *buf; /* buffer for IPC_STAT, IPC_SET */ unsigned short int *array; /* array for GETALL, SETALL */ struct seminfo *__buf; /* buffer for IPC_INFO */ }; #endif

//程序清单 6-2 #include #include #include

#include #include #include

#include \

24

static int set_semvalue(void); static void del_semvalue(void); static int semaphore_p(void); static int semaphore_v(void);

static int sem_id;

int main(int argc, char *argv[]) {

int i;

int pause_time; char op_char = 'O';

srand((unsigned int)getpid());

sem_id = semget((key_t)1234, 1, 0666 | IPC_CREAT); printf(\

if (argc > 1) {

if (!set_semvalue()) {

fprintf(stderr, \ exit(EXIT_FAILURE); }

op_char = 'X'; sleep(5); }

/* Then we have a loop which enters and leaves the critical section ten times. There, we first make a call to semaphore_p which sets the semaphore to wait, as this program is about to enter the critical section. */

for(i = 0; i < 10; i++) {

if (!semaphore_p()) exit(EXIT_FAILURE); printf(\ pause_time = rand() % 3; sleep(pause_time);

printf(\

/* After the critical section, we call semaphore_v, setting the semaphore available, before going through the for loop again after a random wait. After the loop, the call to del_semvalue is made to clean up the code. */

25


Linux实验指导书(5).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:2016-2017学年仁爱版八年级英语初二上册Unit3单元测试卷及答案

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: