int main() { int shmid=creatshm(); char* buf=(char*)atshm(shmid); int i=0; while(i<(SIZE-1)) { buf[i]='A'; i++; } buf[SIZE-1]='\0'; dtshm(buf); // destroyshm(shmid); return 0; }
void *second_level_recv() { int flag; unsigned char real_time[256]; unsigned char second_data[6*1024]; struct plc_struct *p; struct msg_remote sdata; int semid,shmid; int rc; int count; int fd; int length; char*shmaddr; char*ret; struct shm_local* shared; printf(" second level recv pthread\n"); fd=tcp_connect(); read_plcfile(); length=sizeof(struct msg_remote)-sizeof(long); if((shmid=creatshm(1))==-1) exit(1); if((shmaddr=shmat(shmid,(char*)0,0))==(char*)-1) { perror("attch shared memory error\n"); exit(1); } shared=(struct shm_local *)shmaddr; shared->written=0; if((semid=creatsem(1))==-1) exit(1); while(1) { flag=0; switch(real_time_data) { case 0: // printf("1\n"); length=tcp_receive(fd,second_data); while(1) { // printf("1\n"); sem_p(semid); if(shared->written==0) { memcpy(shared->data,second_data,length); shared->length=length; shared->written=1; printf(" send shm data ok\n"); flag=1; } sem_v(semid); if(flag==1) { sleep(second_rate); break; } else { usleep(100000); continue; } } break; case 1: sdata.mtype=2; length=tcp_receive(fd,second_data); length=real_time_select(second_data,real_time); sdata.length=length; if(rc=msgsnd(remote_msgid,(void*)&sdata,sizeof(struct msg_remote)-sizeof(long),0)==-1) exit(1); printf(" send real time msg ok\n"); break; } } exit(1); }