線程池共享內存+信號量
deepfuture@deepfuture-laptop:~/private/mytest$ gcc -std=gnu99 -o testshm testshm.c
testshm.c: In function ‘main’:
testshm.c:38: warning: implicit declaration of function ‘semget’
testshm.c:41: warning: implicit declaration of function ‘exit’
testshm.c:41: warning: incompatible implicit declaration of built-in function ‘exit’
testshm.c:45: warning: implicit declaration of function ‘semctl’
testshm.c:48: warning: incompatible implicit declaration of built-in function ‘exit’
testshm.c:51: warning: implicit declaration of function ‘shmget’
testshm.c:54: warning: incompatible implicit declaration of built-in function ‘exit’
testshm.c:57: warning: implicit declaration of function ‘shmat’
testshm.c:60: warning: incompatible implicit declaration of built-in function ‘exit’
testshm.c:63: warning: implicit declaration of function ‘memset’
testshm.c:63: warning: incompatible implicit declaration of built-in function ‘memset’
testshm.c:69: warning: incompatible implicit declaration of built-in function ‘exit’
testshm.c:78: warning: implicit declaration of function ‘strlen’
testshm.c:78: warning: incompatible implicit declaration of built-in function ‘strlen’
testshm.c:85: warning: implicit declaration of function ‘memcpy’
testshm.c:85: warning: incompatible implicit declaration of built-in function ‘memcpy’
testshm.c:92: warning: implicit declaration of function ‘semop’
testshm.c:95: warning: incompatible implicit declaration of built-in function ‘exit’
testshm.c:119: warning: incompatible implicit declaration of built-in function ‘strlen’
testshm.c:124: warning: incompatible implicit declaration of built-in function ‘exit’
testshm.c:132: warning: implicit declaration of function ‘wait’
testshm.c:134: warning: implicit declaration of function ‘shmdt’
testshm.c:139: warning: implicit declaration of function ‘shmctl’
testshm.c:142: warning: incompatible implicit declaration of built-in function ‘exit’
deepfuture@deepfuture-laptop:~/private/mytest$ ./testshm
deepfuture.javeye.com#line 1$deepfuture
deepfuture.javeye.com#line 2$javaeye
deepfuture.javeye.com#line 3$com
deepfuture.javeye.com#line 4$myhaspl
deepfuture.javeye.com#line 5$Q
deepfuture.javeye.com#line 6$
退出....
deepfuture@deepfuture-laptop:~/private/mytest$ cat abc.txt
deepfuture
javaeye
com
myhaspl
deepfuture@deepfuture-laptop:~/private/mytest$
麥好的AI樂園博客所有內容是原創,如果轉載請注明來源
http://blog.csdn.net/myhaspl/
C代碼
#include
#include
#include
#include
#include
#include
#define MAXS (1024+1)
#define BUFFERSIZE 200
#define SEMID 251//信號標志
#define FILENAME "abc.txt"
#define SHMKEY 241//共享內存標志
#define SHMSIZE MAXS//共享內存大小
//程序完成父進程接收鍵盤輸入,子進程存入文件FILENAME。
//myhaspl
int main(void){
char strbuf[MAXS];
char buf[BUFFERSIZE];
int sem_id;
int shm_id;
int pid;
int rc,res;
struct sembuf sem_op;//信號集結構
union semun sem_val;//信號量數值
char *cur;
FILE *myfile;
char *shm_addr;
int line=1;
//建立信號量集,其中只有一個信號量 myhaspl
sem_id=semget(SEMID,1,IPC_CREAT|0600);//SEMID為為正整數,則為公共的;1為信號集的數量;
if (sem_id==-1){
printf("create sem error!\n");
exit(1);
}
//信號量初始化
sem_val.val=0;
rc=semctl(sem_id,0,SETVAL,sem_val);//設置信號量
if (rc==-1){
printf("initlize sem error!\n");
exit(1);
}
//建立共享內存
shm_id=shmget(SHMKEY,SHMSIZE,IPC_CREAT|0600);//參數為:標志,大小,權限
if (shm_id==-1){
printf("create shm error!\n");
exit(1);
}
//attach共享內存。連接共享內存 myhaspl
shm_addr=(char *)shmat(shm_id,NULL,0);//返回共享內存地址 myhaspl
if (!shm_addr){
printf("shmat error!\n");
exit(1);
}
//初始化數據
memset(shm_addr,'\0',MAXS);
cur=shm_addr;//當前字符起始地址
//創建進程
pid=fork();
if (pid==-1){
printf("fork error!\n");
exit(1);
}
else if(pid==0){//子進程,接受鍵盤輸入,往共享內存中寫字符行 myhaspl
int isend=0;//是否結束輸入
printf("\ndeepfuture.javeye.com#line %d$",line); //自定義鍵盤輸入時使用的SHELL外觀
while((!isend)&&fgets(buf,BUFFERSIZE,stdin)!=NULL){//從shell中讀入一行
line++;
printf("\ndeepfuture.javeye.com#line %d$",line); //自定義鍵盤輸入時使用的SHELL外觀
if (buf[0]=='Q'&&strlen(buf)<=2){//單個字符Q表示退出輸入
isend++;//退出輸入
printf("\n退出....\n");
}
else
{//如果不是退出命令
//寫共享內存 myhaspl
memcpy(cur,buf,strlen(buf));
cur+=strlen(buf);
}
//寫入一行,增加信號
sem_op.sem_num=0;
sem_op.sem_op=1;
sem_op.sem_flg=0;
semop(sem_id,&sem_op,1);//操作信號量,每次+1
}
*cur=-1;
exit(0);
}
else{//父進程,從共享內存中讀字符行 ,並寫入文件 myhaspl
while(1)
{
//讀出一行,減少信號 myhaspl
sem_op.sem_num=0;
sem_op.sem_op=-1;
sem_op.sem_flg=0;
semop(sem_id,&sem_op,1);//操作信號量,每次-1
//myhaspl 讀共享內存中一行
if ((*cur)==-1) break;//輸入結束
int i;
for (i=0;*cur!='\n';cur++,i++){
buf[i]=*cur;
}
cur++;
buf[i]='\n';
buf[++i]=0;
//寫文件
FILE *fp=fopen(FILENAME,"ab");
res=fwrite(buf,strlen(buf),1,fp);//myhaspl 寫入一個行,長度為strlen(buf),個數為1
//size_t fwrite(const void * ptr,size_t size,size_t nmemb,FILE * stream);
//size為要寫入的每個數據的大小(多少字節),nmemb為要寫入數據的個數。myhaspl
if (res==-1){
perror("write error on pipe\n");
exit(1);
}
fclose(fp);
}
wait(&pid);//等待子進程結束,即用戶輸入完畢
//分離共享進程
if (shmdt(shm_addr)==-1){
printf("shmdt error!\n");
}
//撤銷共享內存,任何進程只要有權限,都可以撤銷共享內存,不一定非要創建它的進程
struct shmid_ds shm_desc;
if (shmctl(shm_id,IPC_RMID,&shm_desc)==-1){
printf("shmctl error!\n");
}
exit(0);
}
}