线程池共享内存+信号量 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$myh
aspl
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);// |