導航:首頁 > 網路營銷 > semopen頭文件

semopen頭文件

發布時間:2020-09-12 22:41:57

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 如何實現進程間互斥呢?

文件鎖/信號量,不要用進程共享鎖。

與semopen頭文件相關的知識