写共享内存程序:
/*
* File: server.cpp
* Author: centos
*说明:从键盘读入数据,存放在共享内存中。
* Created on 2010年3月1日, 下午3:44
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#define BUF_SIZE 1024
#define MYKEY 114
int main(int argc, char** argv)
{
int shmid;
char *shmptr;
struct shmid_ds shmbuf;
system("ipcrm -M114"); //调试程序时用
shmid = shmget(MYKEY, BUF_SIZE, (IPC_CREAT |0777) );
if ( -1 == shmid )
{
printf("server shmget error!\n");
fprintf(stderr, "Error: %d - %s\n", errno, strerror(errno));
exit(1);
}
shmptr = (char *) (shmat(shmid, 0, 0)) ;
if ( -1 == (int) shmptr )
{
printf("server shmat error!\n");
fprintf(stderr, "Error: %d - %s\n", errno, strerror(errno));
if(shmctl(shmid,IPC_RMID,&shmbuf) < 0)
{
perror("shmctl error");
fprintf(stderr, "Error: %d - %s\n", errno, strerror(errno));
}
exit(1);
}
strcpy(shmptr,"this is a test. \n");
while(1)
{
printf("Please input:");
scanf("%s",shmptr) ;
while ('\n' != getchar() );
// fflush(stdin);
// printf("your input: %s\n", shmptr);
if (! strcmp(shmptr,"quit")) break;
}
shmdt(shmptr);
if(shmctl(shmid,IPC_RMID,&shmbuf) < 0) perror("shmctl error");
return (EXIT_SUCCESS);
}
读共享内存的程序:
/*
* File: main.cpp
* Author: centos
*
说明:从共享内存中读取数据,显示到屏幕上。
* Created on 2010年3月2日, 上午10:47
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#define BUF_SIZE 1024
#define M
接触Linux也是很久的事情了,不过自我感觉却从来没有揭开过它的真面纱。从我工作以来到现在,几乎总会用到linux或在linux系统上进行工作,也一直想把linux搞懂。但是,由于总感觉Linux的庞大,英文资料难以阅读,加上自身非常的懒散,因此几次下定决心,最终还是不了了之吧。 ......
说实话,信号是我讨厌的东西,在我些的代码中,我都想用最简单的办法来处理它,现在遇到多线程中信号的处理,APUE中的说法也看得不是很懂,CSDN中一位朋友有如下的总结,先记在这里。
1. 默认情况下,信号将由主进程接收处理,就算信号处理函数是由子线程注册的
2. 每个线程均有自己的信号屏蔽字,可以使用sigprocmask函 ......