实验一
一、实验目的
深刻理解线程和进程的概念,掌握线程与进程在组成成分上的差别,以及与其相适应的通讯方式和应用目标。
二、实验内容
1、以Linux系统进程和线程机制为背景,掌握fork()和clone()系统调用的形式和功能,以及与其相适应的高级通讯方式。由fork派生的子进程之间通过pipe通讯,由clone创建的线程之间通过共享内存通讯,对于后者需要考虑互斥问题。
2、以生产者/消费者问题为例,通过实验理解fork()和clone()两个系统调用的区别。程序要求能够创建4个进程或线程,其中包括两个生产者和两个消费者,生产者和消费者之间能够传递数据。
三、实验设计
1、用pipe()创建一个管道文件,然后用fork()创建两个生产进程和两个消费进程,它们之间通过pipe()传递信息。
程序代码:
#include"sched.h"
#include"pthread.h"
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
#include"semaphore.h"
#define
#define
#define
#define
int
int
pthread_mutex_t
sem_t
sem_t
char
int
int
{
pthread_mutex_init(&mutex,NULL);
sem_init(&product,0,0);
sem_init(&warehouse,0,8);
int
char
clone_flag=
int
for(i=0;i<2;i++)
{
arg
stack=(char*)malloc(4096);
retval
stack=(char*)malloc(4096);
retval
}
exit(1);
}
int
{
int
int
for(i=0;i<10;i++)
{
sleep(i+1);
sem_wait(&warehouse);//P
pthread_mutex_lock(&mutex);
if(id==0)
strcpy(buffer[bp],"aaa\0");//producer
else
strcpy(buffer[bp],"bbb\0");//producer
bp++;
printf("producer
pthread_mutex_unlock(&mutex);
sem_post(&product);//V
}
printf("producer
}
int
{
int
int
for(i=0;i<10;i++)
{
sleep(10-i);
sem_wait(&product);//P
pthread_mutex_lock(&mutex);
bp--;
printf("consumer
strcpy(buffer[bp],"zzz\0");
pthread_mutex_unlock(&mutex);
sem_post(&warehouse);
}
printf("consumer
}
运行结果
dbwi@dbwi-virtual-machine:~$
dbwi@dbwi-virtual-machine:~$
dbwi@dbwi-virtual-machine:~$
producer
producer
producer
producer
producer
consumer
consumer
producer
producer
producer
producer
consumer
consumer
producer
producer
consumer
consumer
producer
producer
consumer
consumer
producer
producer
consumer
consumer
consumer
consumer
producer
producer
consumer
consumer
consumer
consumer
consumer
consumer
producer
producer
consumer
consumer
producer
producer
consumer
consumer
2、用clone()创建四个轻进程(线程),用参数指明共享内存等资源,通过共享内存模拟生产消费问题,利用pthread_mutex_lock(),
程序代码:
#include"sys/types.h"
#include"sys/file.h"
#include"unistd.h"
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
char
char
int
pid_t
int
int
int
{
if(pipe(pipe_fd)<0)
{
printf("pipe
exit(-1);
}
else
{
printf("pipe
if((pid1=fork())==0)
producer(1);
if((pid2=fork())==0)
producer(2);
if((pid3=fork())==0)
consumer(1);
if((pid4=fork())==0)
consumer(2);
}
close(pipe_fd[0]);
close(pipe_fd[1]);
int
for(i=0;i<4;i++)
pid=wait(&status);
exit(0);
}
int
{
printf("producer
close(pipe_fd[0]);//
int
for(i=1;i<10;i++)
{
sleep(3);
if(id==1)
{
strcpy(w_buf,"AAA\0");//producer
printf("producer
}
else
{
strcpy(w_buf,"BBB\0");//producer
printf("producer
}
if(write(pipe_fd[1],w_buf,4)==-1)
printf("write
}
close(pipe_fd[1]);
printf("producer
exit(id);
}
int
{
close(pipe_fd[1]);//first
printf("consumer
if(id==1)
strcpy(w_buf,"CCC\0");
else
strcpy(w_buf,"DDD\0");
while(1)
{
sleep(1);
strcpy(r_buf,"EEE\0");
if(read(pipe_fd[0],r_buf,4)==0)
break;
printf("consumer
}
close(pipe_fd[0]);
printf("consumer
exit(id);
}
运行结果
dbwi@dbwi-virtual-machine:~$
dbwi@dbwi-virtual-machine:~$
pipe
consumer
consumer
producer
producer
producer
consumer
producer
consumer
producer
consumer
producer
consumer
producer
consumer
producer
consumer
producer
consumer
producer
consumer
producer
consumer
producer
consumer
producer
consumer
producer
consumer
producer
consumer
producer
consumer
producer
consumer
producer
consumer
producer
producer
consumer
producer
producer
consumer
consumer
consumer