1、急求!!多线程生产者消费者问题的c语言程序,要源码、已在linux中执行过的文件,最好有截图!!
||#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <semaphore.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/time.h>
#include<string.h>
#define BUFSIZE 4096
#define SEM_IN_TASK "INX_TASK"
#define SEM_OUT_TASK "OUTX_TASK"
sem_t *sem_in;
sem_t *sem_out;
main(int argc, char** argv) // map a normal file as shared mem:
{
int fd,i;
char *p_map;
char temp;
fd=open(argv[1],O_CREAT|O_RDWR|O_TRUNC,00777);
//lseek(fd,sizeof(people)*5-1,SEEK_SET);
write(fd,"",1);
p_map = (char*) mmap( NULL,BUFSIZE,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0 );//建立共享内存
close( fd );
sem_unlink(SEM_IN_TASK);
sem_unlink(SEM_OUT_TASK);
sem_in = sem_open(SEM_IN_TASK,O_CREAT|O_EXCL,0644,1); //1ok
sem_out=sem_open(SEM_OUT_TASK,O_CREAT|O_EXCL,0644,0); //0ok
printf("[sem_in] [%d]\n",sem_in);
printf("[sem_out] [%d]\n",sem_out);
if(sem_in == SEM_FAILED||sem_out == SEM_FAILED)
{
perror("wangsha-unable to create semaphore");
sem_unlink(SEM_IN_TASK);
sem_unlink(SEM_OUT_TASK);
exit(-1);
}
memset(p_map,0,BUFSIZE);
while(1)
{
//printf("---------A waitting B-----------\n\n\n\n");
sem_wait(sem_out);
printf("A:%s\n",p_map );
memset(p_map,'a',100);
sem_post(sem_in);
//printf("------------A emit B-----------\n\n\n\n");
}
munmap( p_map, BUFSIZE );
printf( "umap ok \n" );
}
#include <sys/mman.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <semaphore.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/time.h>
#include<string.h>
#define SEM_IN_TASK "INX_TASK"
#define SEM_OUT_TASK "OUTX_TASK"
#define BUFSIZE 4096
sem_t *sem_in;
sem_t *sem_out;
main(int argc, char** argv) // map a normal file as shared mem:
{
int fd =-1;
int i= 0;
char *p_map;
double consumetime = 0.0;
struct timeval process_stat , process_end;
fd=open( argv[1],O_CREAT|O_RDWR,00777 );
p_map = (char*)mmap(NULL,BUFSIZE,PROT_READ|PROT_WRITE, MAP_SHARED,fd,0);
sem_in = sem_open(SEM_IN_TASK,O_CREAT,0644,0); //0ok
sem_out=sem_open(SEM_OUT_TASK,O_CREAT,0644,1); //1ok
printf("[sem_in] [%d]\n",sem_in);
printf("[sem_out] [%d]\n",sem_out);
if(sem_in == SEM_FAILED||sem_out == SEM_FAILED)
{
perror("unable to create semaphore-wang");
sem_unlink(SEM_OUT_TASK);
sem_unlink(SEM_IN_TASK);
exit(-1);
}
memset(p_map,0,BUFSIZE);
//gettimeofday(&process_stat,NULL);
//while(1)
for(i =0;i<50000;i++)
{
memset(p_map,'b',100);
//printf("------------B emit A-----------\n\n\n\n");
sem_post(sem_out);
usleep(200);
sem_wait(sem_in);
//printf("------------A emit B-----------\n\n\n\n");
printf( "B:%s\n",p_map );
}
//gettimeofday(&process_end,NULL);
//consumetime = (double)(process_end.tv_sec - process_stat.tv_sec)*1e6 +(double)(process_end.tv_usec - process_stat.tv_usec)/1e6;
//printf("consumetime:[%f]\n",consumetime);
munmap( p_map,BUFSIZE );
}
如果对你有帮助,请给分哦,谢谢!
2、linux编程时的信号量问题。 我以前用过的信号量头文件是<semaphore.h>,而现在又发现还有个<sys/sem.h>
信号量在进程是以有名信号量进行通信的,在线程是以无名信号进行通信的,因为线程linux还没有实现进程间的通信,所以在sem_init的第二个参数要为0,而且在多线程间的同步是可以通过有名信号量也可通过无名信号,但是一般情况线程的同步是无名信号量,无名信号量使用简单,而且sem_t存储在进程空间中,有名信号量必须LINUX内核管理,由内核结构struct ipc_ids 存储,是随内核持续的,系统关闭,信号量则删除,当然也可以显示删除,通过系统调用删除,
消息队列,信号量,内存共享,这几个都是一样的原理。,只不过信号量分为有名与无名
无名使用 <semaphore.h>,
有名信号量<sys/sem.h>
无名信号量不能用进程间通信,
//无名与有名的区别,有名需要KEY值与IPC标识
所以sem_init的第二个参数必须为0,,,,
3、急!LINUX下,GCC编译,原程序包含<semaphore.h>头文件,为什么编译时说sem_wait,sem_post等未定义的引用
编译时加上参数:-lpthread
要看报错的阶段,是在编译还是链接阶段.
如果编译时函数没有找到,那是头文件的问题,如果链接时未定义引用,那是c库的问题.
如果你的头文件都正常包含了,那可能你的c库没有使能semaphore的支持.
4、linux c 如何实现进程间互斥呢?
文件锁/信号量,不要用进程共享锁。