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
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
/* 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 \
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