进程与线程——Linux进程与线程通讯…

实验一 进程与线程——Linux进程与线程通讯

一、实验目的

深刻理解线程和进程的概念,掌握线程与进程在组成成分上的差别,以及与其相适应的通讯方式和应用目标。

二、实验内容

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 CLONE_VM 0x00000100

#define CLONE_FS 0x00000200

#define CLONE_FILES 0x00000400

#define CLONE_SIGHAND 0x00000800

 

int producer(void *args);

int consumer(void *args);

 

pthread_mutex_t mutex;

sem_t product;

sem_t warehouse;

 

char buffer[8][4];

int bp=0;

 

int main(int argc,char**argv)

{

pthread_mutex_init(&mutex,NULL);

sem_init(&product,0,0);

sem_init(&warehouse,0,8);

int clone_flag,*arg, args[2] {0, 1},retval;

char *stack;

clone_flag= CLONE_VM|CLONE_SIGHAND|CLONE_FS|CLONE_FILES;

int i;

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

{

 

arg &args[i] ;

stack=(char*)malloc(4096);

retval clone((void*)producer,&(stack[4095]),clone_flag,(void*)arg);

stack=(char*)malloc(4096);

retval clone((void*)consumer,&(stack[4095]),clone_flag,(void*)arg);

}

exit(1);

}

 

int producer(void *args)

{

int id=*((int*)args);

int i;

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

{

sleep(i+1);

sem_wait(&warehouse);//P operate

pthread_mutex_lock(&mutex);

if(id==0)

strcpy(buffer[bp],"aaa\0");//producer write

else

strcpy(buffer[bp],"bbb\0");//producer write

bp++;

printf("producer %d produce %s in %d\n",id, buffer[bp-1],bp-1);

pthread_mutex_unlock(&mutex);

sem_post(&product);//V operate

}

 

printf("producer %d is over!\n",id);

}

 

int consumer(void *args)

{

int id=*((int*)args);

int i;

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

{

sleep(10-i);

sem_wait(&product);//P operate

pthread_mutex_lock(&mutex);

bp--;

printf("consumer %d get %s in %d\n",id,buffer[bp],bp);

strcpy(buffer[bp],"zzz\0");

pthread_mutex_unlock(&mutex);

sem_post(&warehouse);

}

 

printf("consumer %d is over!\n",id);

}

运行结果

dbwi@dbwi-virtual-machine:~$ gcc experiment2.c -o exper2 -lpthread

dbwi@dbwi-virtual-machine:~$ ./exper2

dbwi@dbwi-virtual-machine:~$ producer produce bbb in 0

producer produce aaa in 1

producer produce bbb in 2

producer produce aaa in 3

producer produce bbb in 4

producer produce aaa in 5

consumer get aaa in 5

consumer get bbb in 4

producer produce bbb in 4

producer produce aaa in 5

producer produce bbb in 6

producer produce aaa in 7

consumer get aaa in 7

consumer get bbb in 6

producer produce bbb in 6

producer produce aaa in 7

consumer get aaa in 7

consumer get bbb in 6

producer produce bbb in 6

producer produce aaa in 7

consumer get aaa in 7

consumer get bbb in 6

producer produce bbb in 6

producer produce aaa in 7

consumer get aaa in 7

consumer get bbb in 6

consumer get aaa in 5

consumer get bbb in 4

producer produce aaa in 4

producer produce bbb in 5

consumer get bbb in 5

consumer get aaa in 4

consumer get aaa in 3

consumer get bbb in 2

consumer get aaa in 1

consumer get bbb in 0

producer produce aaa in 0

producer is over!

consumer get aaa in 0

consumer is over!

producer produce bbb in 0

producer is over!

consumer get bbb in 0

consumer is over!

 

2、用clone()创建四个轻进程(线程),用参数指明共享内存等资源,通过共享内存模拟生产消费问题,利用pthread_mutex_lock(), pthread_mutex_unlock()等函数实现对共享存储区访问的互斥。

 

程序代码:

#include"sys/types.h"

#include"sys/file.h"

#include"unistd.h"

#include"stdio.h"

#include"stdlib.h"

#include"string.h"

char r_buf[4];//

char w_buf[4];

int pipe_fd[2];

pid_t pid1,pid2,pid3,pid4;

 

int producer(int id);

int consumer(int id);

 

int main(int argc,char**argv)

{

if(pipe(pipe_fd)<0)

{

printf("pipe create error.\n");

exit(-1);

}

else

{

printf("pipe is created successfully!\n");

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 i,pid,status;

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

pid=wait(&status); 

exit(0);

}

 

int producer(int id)

{

printf("producer %d is running!\n",id);

close(pipe_fd[0]);// first close the read of consumer

int i=0;

for(i=1;i<10;i++)

{

sleep(3);

if(id==1)

{

strcpy(w_buf,"AAA\0");//producer write

printf("producer %d write %s to the w_buf\n",id,w_buf);

}

else

{

strcpy(w_buf,"BBB\0");//producer write

printf("producer %d write %s to the w_buf\n",id,w_buf);

}

if(write(pipe_fd[1],w_buf,4)==-1)

printf("write to pipe error\n");

}

close(pipe_fd[1]);

printf("producer %d is over!\n",id);

exit(id);

}

 

int consumer(int id)

{

close(pipe_fd[1]);//first close writing of producer

printf("consumer %d is running!\n",id);

 

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 %d get %s,while the w_buf is %s\n",id,r_buf,w_buf);

}

close(pipe_fd[0]);

printf("consumer %d is over!\n",id);

exit(id);

}

 

运行结果

dbwi@dbwi-virtual-machine:~$ gcc experiment1.c -o exper1

dbwi@dbwi-virtual-machine:~$ ./exper1

pipe is created successfully!

consumer is running!

consumer is running!

producer is running!

producer is running!

producer write BBB to the w_buf

consumer get BBB,while the w_buf is DDD

producer write AAA to the w_buf

consumer get AAA,while the w_buf is CCC

producer write BBB to the w_buf

consumer get BBB,while the w_buf is DDD

producer write AAA to the w_buf

consumer get AAA,while the w_buf is CCC

producer write BBB to the w_buf

consumer get BBB,while the w_buf is DDD

producer write AAA to the w_buf

consumer get AAA,while the w_buf is CCC

producer write BBB to the w_buf

consumer get BBB,while the w_buf is CCC

producer write AAA to the w_buf

consumer get AAA,while the w_buf is DDD

producer write BBB to the w_buf

consumer get BBB,while the w_buf is DDD

producer write AAA to the w_buf

consumer get AAA,while the w_buf is CCC

producer write BBB to the w_buf

consumer get BBB,while the w_buf is CCC

producer write AAA to the w_buf

consumer get AAA,while the w_buf is DDD

producer write AAA to the w_buf

consumer get AAA,while the w_buf is DDD

producer write BBB to the w_buf

consumer get BBB,while the w_buf is CCC

producer write BBB to the w_buf

consumer get BBB,while the w_buf is DDD

producer write AAA to the w_buf

consumer get AAA,while the w_buf is CCC

producer write AAA to the w_buf

producer is over!

consumer get AAA,while the w_buf is CCC

producer write BBB to the w_buf

producer is over!

consumer get BBB,while the w_buf is DDD

consumer is over!

consumer is over!

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值